@eutobias/serenojs 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +642 -2
- package/admin-dist/assets/index-B3EgqtT6.js +9 -0
- package/admin-dist/assets/index-D2m0Eb4k.css +2 -0
- package/admin-dist/index.html +17 -0
- package/dist/auth/auth.server.d.ts +31 -0
- package/dist/auth/auth.server.js +141 -0
- package/dist/auth/config.d.ts +32 -0
- package/dist/auth/config.js +25 -0
- package/dist/auth/cookies.server.d.ts +7 -0
- package/dist/auth/cookies.server.js +65 -0
- package/dist/auth/http.server.d.ts +8 -0
- package/dist/auth/http.server.js +30 -0
- package/dist/auth/password.server.d.ts +2 -0
- package/dist/auth/password.server.js +21 -0
- package/dist/auth/session.server.d.ts +2 -0
- package/dist/auth/session.server.js +14 -0
- package/dist/auth/users.server.d.ts +16 -0
- package/dist/auth/users.server.js +123 -0
- package/dist/config.d.ts +91 -0
- package/dist/config.js +39 -0
- package/dist/core/backup.server.d.ts +52 -0
- package/dist/core/backup.server.js +349 -0
- package/dist/core/db.server.d.ts +24 -0
- package/dist/core/db.server.js +526 -0
- package/dist/core/media.d.ts +22 -0
- package/dist/core/media.js +76 -0
- package/dist/core/media.server.d.ts +18 -0
- package/dist/core/media.server.js +102 -0
- package/dist/core/schema.d.ts +26 -0
- package/dist/core/schema.js +99 -0
- package/dist/core/types.d.ts +143 -0
- package/dist/core/types.js +1 -0
- package/dist/core/validation.d.ts +16 -0
- package/dist/core/validation.js +192 -0
- package/dist/locale.d.ts +2 -0
- package/dist/locale.js +14 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +37 -0
- package/dist/server/app.d.ts +20 -0
- package/dist/server/app.js +661 -0
- package/dist/ui/admin/admin-shell.d.ts +16 -0
- package/dist/ui/admin/admin-shell.js +38 -0
- package/dist/ui/admin/backup-manager.d.ts +5 -0
- package/dist/ui/admin/backup-manager.js +165 -0
- package/dist/ui/admin/collection-icon.d.ts +22 -0
- package/dist/ui/admin/collection-icon.js +34 -0
- package/dist/ui/admin/collection-manager.d.ts +7 -0
- package/dist/ui/admin/collection-manager.js +212 -0
- package/dist/ui/admin/display.d.ts +7 -0
- package/dist/ui/admin/display.js +65 -0
- package/dist/ui/admin/field-control.d.ts +14 -0
- package/dist/ui/admin/field-control.js +58 -0
- package/dist/ui/admin/i18n.d.ts +27 -0
- package/dist/ui/admin/i18n.js +324 -0
- package/dist/ui/admin/item-form.d.ts +20 -0
- package/dist/ui/admin/item-form.js +60 -0
- package/dist/ui/admin/list-control.d.ts +13 -0
- package/dist/ui/admin/list-control.js +31 -0
- package/dist/ui/admin/locales.d.ts +4 -0
- package/dist/ui/admin/locales.js +8 -0
- package/dist/ui/admin/login-form.d.ts +3 -0
- package/dist/ui/admin/login-form.js +52 -0
- package/dist/ui/admin/media-control.d.ts +14 -0
- package/dist/ui/admin/media-control.js +74 -0
- package/dist/ui/admin/media-list-control.d.ts +14 -0
- package/dist/ui/admin/media-list-control.js +91 -0
- package/dist/ui/admin/pagination.d.ts +11 -0
- package/dist/ui/admin/pagination.js +14 -0
- package/dist/ui/admin/relation-control.d.ts +14 -0
- package/dist/ui/admin/relation-control.js +33 -0
- package/dist/ui/admin/router.d.ts +17 -0
- package/dist/ui/admin/router.js +58 -0
- package/dist/ui/admin/runtime.d.ts +28 -0
- package/dist/ui/admin/runtime.js +29 -0
- package/dist/ui/admin/sidebar.d.ts +13 -0
- package/dist/ui/admin/sidebar.js +29 -0
- package/dist/ui/admin/standalone-manager.d.ts +11 -0
- package/dist/ui/admin/standalone-manager.js +51 -0
- package/dist/ui/admin/table.d.ts +21 -0
- package/dist/ui/admin/table.js +23 -0
- package/dist/ui/admin/toast.d.ts +16 -0
- package/dist/ui/admin/toast.js +59 -0
- package/dist/ui/admin/users-manager.d.ts +6 -0
- package/dist/ui/admin/users-manager.js +157 -0
- package/dist/ui/badge.d.ts +9 -0
- package/dist/ui/badge.js +14 -0
- package/dist/ui/box.d.ts +17 -0
- package/dist/ui/box.js +14 -0
- package/dist/ui/button.d.ts +25 -0
- package/dist/ui/button.js +28 -0
- package/dist/ui/card.d.ts +45 -0
- package/dist/ui/card.js +49 -0
- package/dist/ui/container.d.ts +13 -0
- package/dist/ui/container.js +13 -0
- package/dist/ui/drawer.d.ts +15 -0
- package/dist/ui/drawer.js +31 -0
- package/dist/ui/field-wrapper.d.ts +15 -0
- package/dist/ui/field-wrapper.js +11 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.js +8 -0
- package/dist/ui/text.d.ts +19 -0
- package/dist/ui/text.js +40 -0
- package/package.json +81 -3
- package/src/admin/app.tsx +371 -0
- package/src/admin/index.html +16 -0
- package/src/admin/main.tsx +42 -0
- package/src/admin/styles.css +9 -0
- package/src/auth/auth.server.ts +203 -0
- package/src/auth/config.ts +46 -0
- package/src/auth/cookies.server.ts +84 -0
- package/src/auth/http.server.ts +32 -0
- package/src/auth/password.server.ts +25 -0
- package/src/auth/session.server.ts +16 -0
- package/src/auth/users.server.ts +159 -0
- package/src/config.ts +120 -0
- package/src/core/backup.server.ts +446 -0
- package/src/core/db.server.ts +685 -0
- package/src/core/media.server.ts +136 -0
- package/src/core/media.ts +91 -0
- package/src/core/schema.ts +126 -0
- package/src/core/types.ts +194 -0
- package/src/core/validation.ts +215 -0
- package/src/global.d.ts +1 -0
- package/src/locale.ts +16 -0
- package/src/runtime.ts +54 -0
- package/src/server/app.ts +737 -0
- package/src/ui/admin/admin-shell.tsx +106 -0
- package/src/ui/admin/backup-manager.tsx +327 -0
- package/src/ui/admin/collection-icon.tsx +69 -0
- package/src/ui/admin/collection-manager.tsx +408 -0
- package/src/ui/admin/display.tsx +138 -0
- package/src/ui/admin/field-control.tsx +214 -0
- package/src/ui/admin/i18n.tsx +401 -0
- package/src/ui/admin/item-form.tsx +200 -0
- package/src/ui/admin/list-control.tsx +121 -0
- package/src/ui/admin/locales.ts +12 -0
- package/src/ui/admin/login-form.tsx +98 -0
- package/src/ui/admin/media-control.tsx +161 -0
- package/src/ui/admin/media-list-control.tsx +242 -0
- package/src/ui/admin/pagination.tsx +58 -0
- package/src/ui/admin/relation-control.tsx +142 -0
- package/src/ui/admin/router.tsx +105 -0
- package/src/ui/admin/runtime.tsx +58 -0
- package/src/ui/admin/sidebar.tsx +178 -0
- package/src/ui/admin/standalone-manager.tsx +103 -0
- package/src/ui/admin/table.tsx +129 -0
- package/src/ui/admin/toast.tsx +126 -0
- package/src/ui/admin/users-manager.tsx +344 -0
- package/src/ui/badge.tsx +27 -0
- package/src/ui/box.tsx +33 -0
- package/src/ui/button.tsx +77 -0
- package/src/ui/card.tsx +92 -0
- package/src/ui/container.tsx +28 -0
- package/src/ui/drawer.tsx +104 -0
- package/src/ui/field-wrapper.tsx +54 -0
- package/src/ui/index.ts +8 -0
- package/src/ui/text.tsx +93 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Link, useRouter } from "./router.js";
|
|
4
|
+
|
|
5
|
+
import type { CollectionDef } from "../../core/types.js";
|
|
6
|
+
import type { AuthUser } from "../../auth/config.js";
|
|
7
|
+
import { Box } from "../box.js";
|
|
8
|
+
import { Button } from "../button.js";
|
|
9
|
+
import { Text } from "../text.js";
|
|
10
|
+
import { FiDatabase, FiUsers } from "react-icons/fi";
|
|
11
|
+
import { CollectionIcon } from "./collection-icon.js";
|
|
12
|
+
import { ADMIN_LOCALES, useI18n } from "./i18n.js";
|
|
13
|
+
|
|
14
|
+
export type SidebarProps = {
|
|
15
|
+
title: string;
|
|
16
|
+
collections: CollectionDef[];
|
|
17
|
+
user?: AuthUser;
|
|
18
|
+
onLogout?: () => void;
|
|
19
|
+
onNavigate?: () => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Left navigation derived from the schema descriptor.
|
|
24
|
+
*/
|
|
25
|
+
export const Sidebar = ({
|
|
26
|
+
title,
|
|
27
|
+
collections,
|
|
28
|
+
user,
|
|
29
|
+
onLogout,
|
|
30
|
+
onNavigate,
|
|
31
|
+
}: SidebarProps) => {
|
|
32
|
+
const { path } = useRouter();
|
|
33
|
+
const { t, locale, setLocale } = useI18n();
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<Box
|
|
37
|
+
as="aside"
|
|
38
|
+
className="sticky top-0 h-screen w-64 shrink-0 flex-col border-r border-gray-200 bg-white"
|
|
39
|
+
>
|
|
40
|
+
<Box className="flex-col gap-1 border-b border-gray-200 px-5 py-5">
|
|
41
|
+
<Text size="caption" className="font-semibold uppercase tracking-widest text-gray-400">
|
|
42
|
+
{title}
|
|
43
|
+
</Text>
|
|
44
|
+
<Text size="caption" className="text-gray-400">
|
|
45
|
+
Admin
|
|
46
|
+
</Text>
|
|
47
|
+
</Box>
|
|
48
|
+
|
|
49
|
+
<Box className="flex-1 flex-col gap-1 p-3">
|
|
50
|
+
<Text
|
|
51
|
+
size="caption"
|
|
52
|
+
className="px-2 pb-1 pt-2 font-semibold uppercase tracking-wide text-gray-400"
|
|
53
|
+
>
|
|
54
|
+
{t("sidebar.collections")}
|
|
55
|
+
</Text>
|
|
56
|
+
<Link
|
|
57
|
+
to="/"
|
|
58
|
+
onClick={onNavigate}
|
|
59
|
+
className={`flex items-center gap-2 rounded-md px-3 py-2 text-sm transition-colors ${
|
|
60
|
+
path === "/"
|
|
61
|
+
? "bg-gray-900 font-medium text-white"
|
|
62
|
+
: "text-gray-700 hover:bg-gray-100"
|
|
63
|
+
}`}
|
|
64
|
+
>
|
|
65
|
+
<span aria-hidden>▦</span>
|
|
66
|
+
<span>{t("sidebar.overview")}</span>
|
|
67
|
+
</Link>
|
|
68
|
+
{collections.map((collection) => {
|
|
69
|
+
const href = `/${collection.slug}`;
|
|
70
|
+
const active = path === href || path.startsWith(`${href}/`);
|
|
71
|
+
return (
|
|
72
|
+
<Link
|
|
73
|
+
key={collection.slug}
|
|
74
|
+
to={href}
|
|
75
|
+
onClick={onNavigate}
|
|
76
|
+
className={`flex items-center gap-2 rounded-md px-3 py-2 text-sm transition-colors ${
|
|
77
|
+
active
|
|
78
|
+
? "bg-gray-900 font-medium text-white"
|
|
79
|
+
: "text-gray-700 hover:bg-gray-100"
|
|
80
|
+
}`}
|
|
81
|
+
>
|
|
82
|
+
<CollectionIcon
|
|
83
|
+
name={collection.icon}
|
|
84
|
+
className="h-4 w-4 shrink-0"
|
|
85
|
+
/>
|
|
86
|
+
<span>{collection.label}</span>
|
|
87
|
+
{collection.standalone ? (
|
|
88
|
+
<span
|
|
89
|
+
aria-hidden
|
|
90
|
+
title={t("sidebar.singleRecord")}
|
|
91
|
+
className={`ml-auto h-1.5 w-1.5 rounded-full ${
|
|
92
|
+
active ? "bg-white/70" : "bg-gray-300"
|
|
93
|
+
}`}
|
|
94
|
+
/>
|
|
95
|
+
) : null}
|
|
96
|
+
</Link>
|
|
97
|
+
);
|
|
98
|
+
})}
|
|
99
|
+
|
|
100
|
+
<Text
|
|
101
|
+
size="caption"
|
|
102
|
+
className="px-2 pb-1 pt-4 font-semibold uppercase tracking-wide text-gray-400"
|
|
103
|
+
>
|
|
104
|
+
{t("sidebar.administration")}
|
|
105
|
+
</Text>
|
|
106
|
+
<Link
|
|
107
|
+
to="/users"
|
|
108
|
+
onClick={onNavigate}
|
|
109
|
+
className={`flex items-center gap-2 rounded-md px-3 py-2 text-sm transition-colors ${
|
|
110
|
+
path === "/users"
|
|
111
|
+
? "bg-gray-900 font-medium text-white"
|
|
112
|
+
: "text-gray-700 hover:bg-gray-100"
|
|
113
|
+
}`}
|
|
114
|
+
>
|
|
115
|
+
<FiUsers aria-hidden className="h-4 w-4 shrink-0" />
|
|
116
|
+
<span>{t("sidebar.users")}</span>
|
|
117
|
+
</Link>
|
|
118
|
+
<Link
|
|
119
|
+
to="/backup"
|
|
120
|
+
onClick={onNavigate}
|
|
121
|
+
className={`flex items-center gap-2 rounded-md px-3 py-2 text-sm transition-colors ${
|
|
122
|
+
path === "/backup"
|
|
123
|
+
? "bg-gray-900 font-medium text-white"
|
|
124
|
+
: "text-gray-700 hover:bg-gray-100"
|
|
125
|
+
}`}
|
|
126
|
+
>
|
|
127
|
+
<FiDatabase aria-hidden className="h-4 w-4 shrink-0" />
|
|
128
|
+
<span>{t("sidebar.backup")}</span>
|
|
129
|
+
</Link>
|
|
130
|
+
</Box>
|
|
131
|
+
|
|
132
|
+
<Box className="flex-col gap-1 border-t border-gray-200 p-3">
|
|
133
|
+
<Text size="caption" className="px-2 font-semibold uppercase tracking-wide text-gray-400">
|
|
134
|
+
{t("common.language")}
|
|
135
|
+
</Text>
|
|
136
|
+
<select
|
|
137
|
+
value={locale}
|
|
138
|
+
onChange={(event) => setLocale(event.target.value === "pt" ? "pt" : "en")}
|
|
139
|
+
className="rounded-md border border-gray-300 bg-white px-2 py-1.5 text-sm text-gray-700 outline-none focus:border-gray-900 focus:ring-1 focus:ring-gray-900"
|
|
140
|
+
aria-label={t("common.language")}
|
|
141
|
+
>
|
|
142
|
+
{ADMIN_LOCALES.map((option) => (
|
|
143
|
+
<option key={option.code} value={option.code}>
|
|
144
|
+
{option.label}
|
|
145
|
+
</option>
|
|
146
|
+
))}
|
|
147
|
+
</select>
|
|
148
|
+
</Box>
|
|
149
|
+
|
|
150
|
+
{user ? (
|
|
151
|
+
<Box className="flex-col gap-2 border-t border-gray-200 p-3">
|
|
152
|
+
<Box className="flex-col px-2">
|
|
153
|
+
<Text
|
|
154
|
+
size="caption"
|
|
155
|
+
className="truncate font-medium text-gray-700"
|
|
156
|
+
title={user.email}
|
|
157
|
+
>
|
|
158
|
+
{user.email}
|
|
159
|
+
</Text>
|
|
160
|
+
<Text size="caption" className="text-gray-400">
|
|
161
|
+
{t("sidebar.sessionActive")}
|
|
162
|
+
</Text>
|
|
163
|
+
</Box>
|
|
164
|
+
{onLogout ? (
|
|
165
|
+
<Button
|
|
166
|
+
type="button"
|
|
167
|
+
variant="secondary"
|
|
168
|
+
size="sm"
|
|
169
|
+
onClick={onLogout}
|
|
170
|
+
>
|
|
171
|
+
{t("sidebar.signOut")}
|
|
172
|
+
</Button>
|
|
173
|
+
) : null}
|
|
174
|
+
</Box>
|
|
175
|
+
) : null}
|
|
176
|
+
</Box>
|
|
177
|
+
);
|
|
178
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
CollectionDef,
|
|
7
|
+
FieldValue,
|
|
8
|
+
Item,
|
|
9
|
+
RelationOptionsMap,
|
|
10
|
+
} from "../../core/types.js";
|
|
11
|
+
import { Badge } from "../badge.js";
|
|
12
|
+
import { Box } from "../box.js";
|
|
13
|
+
import { Text } from "../text.js";
|
|
14
|
+
|
|
15
|
+
import { ItemForm } from "./item-form.js";
|
|
16
|
+
import { useToast } from "./toast.js";
|
|
17
|
+
import { useI18n } from "./i18n.js";
|
|
18
|
+
import { useCmsUrls } from "./runtime.js";
|
|
19
|
+
|
|
20
|
+
export type StandaloneManagerProps = {
|
|
21
|
+
collection: CollectionDef;
|
|
22
|
+
initial: Item;
|
|
23
|
+
relationOptions: RelationOptionsMap;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Editor for a standalone collection: no table, no create/delete — just the
|
|
28
|
+
* single record rendered as a form.
|
|
29
|
+
*/
|
|
30
|
+
export const StandaloneManager = ({
|
|
31
|
+
collection,
|
|
32
|
+
initial,
|
|
33
|
+
relationOptions,
|
|
34
|
+
}: StandaloneManagerProps) => {
|
|
35
|
+
const toast = useToast();
|
|
36
|
+
const { api } = useCmsUrls();
|
|
37
|
+
const { t } = useI18n();
|
|
38
|
+
const [item, setItem] = useState<Item>(initial);
|
|
39
|
+
const [submitting, setSubmitting] = useState(false);
|
|
40
|
+
const [errors, setErrors] = useState<Record<string, string>>({});
|
|
41
|
+
const [formError, setFormError] = useState<string | null>(null);
|
|
42
|
+
|
|
43
|
+
const submit = async (values: Record<string, FieldValue>) => {
|
|
44
|
+
setSubmitting(true);
|
|
45
|
+
setErrors({});
|
|
46
|
+
setFormError(null);
|
|
47
|
+
try {
|
|
48
|
+
const response = await fetch(api(collection.slug), {
|
|
49
|
+
method: "PATCH",
|
|
50
|
+
headers: { "Content-Type": "application/json" },
|
|
51
|
+
body: JSON.stringify(values),
|
|
52
|
+
});
|
|
53
|
+
const json = (await response.json()) as {
|
|
54
|
+
item?: Item;
|
|
55
|
+
message?: string;
|
|
56
|
+
errors?: Record<string, string>;
|
|
57
|
+
};
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
setErrors(json.errors ?? {});
|
|
60
|
+
setFormError(json.message ?? t("standalone.saveError"));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (json.item) setItem(json.item);
|
|
64
|
+
toast.success(t("standalone.saved"));
|
|
65
|
+
} catch {
|
|
66
|
+
setFormError(t("standalone.networkSave"));
|
|
67
|
+
} finally {
|
|
68
|
+
setSubmitting(false);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<Box className="flex-col gap-6 p-4 sm:p-6 lg:p-8">
|
|
74
|
+
<Box className="flex-col gap-1">
|
|
75
|
+
<Box className="items-center gap-3">
|
|
76
|
+
<Text size="title" className="font-semibold text-gray-900">
|
|
77
|
+
{collection.label}
|
|
78
|
+
</Text>
|
|
79
|
+
<Badge tone="purple">{t("standalone.badge")}</Badge>
|
|
80
|
+
</Box>
|
|
81
|
+
{collection.description ? (
|
|
82
|
+
<Text size="caption" className="text-gray-500">
|
|
83
|
+
{collection.description}
|
|
84
|
+
</Text>
|
|
85
|
+
) : null}
|
|
86
|
+
</Box>
|
|
87
|
+
|
|
88
|
+
<Box className="max-w-2xl flex-col rounded-lg border border-gray-200 bg-white p-5 sm:p-6">
|
|
89
|
+
<ItemForm
|
|
90
|
+
key={collection.slug}
|
|
91
|
+
collection={collection}
|
|
92
|
+
item={item}
|
|
93
|
+
submitting={submitting}
|
|
94
|
+
errors={errors}
|
|
95
|
+
formError={formError}
|
|
96
|
+
relationOptions={relationOptions}
|
|
97
|
+
onSubmit={submit}
|
|
98
|
+
hideCancel
|
|
99
|
+
/>
|
|
100
|
+
</Box>
|
|
101
|
+
</Box>
|
|
102
|
+
);
|
|
103
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import { Box } from "../box.js";
|
|
4
|
+
import { Text } from "../text.js";
|
|
5
|
+
import { useI18n } from "./i18n.js";
|
|
6
|
+
|
|
7
|
+
export type Column<T> = {
|
|
8
|
+
key: string;
|
|
9
|
+
header: string;
|
|
10
|
+
width?: string;
|
|
11
|
+
align?: "left" | "center" | "right";
|
|
12
|
+
render?: (row: T) => ReactNode;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type DataTableProps<T> = {
|
|
16
|
+
columns: Column<T>[];
|
|
17
|
+
rows: T[];
|
|
18
|
+
rowKey: (row: T) => string | number;
|
|
19
|
+
empty?: string;
|
|
20
|
+
loading?: boolean;
|
|
21
|
+
caption?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const alignClass = {
|
|
25
|
+
left: "text-left",
|
|
26
|
+
center: "text-center",
|
|
27
|
+
right: "text-right",
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
const cellAlign = {
|
|
31
|
+
left: "justify-start",
|
|
32
|
+
center: "justify-center",
|
|
33
|
+
right: "justify-end",
|
|
34
|
+
} as const;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Real semantic `<table>` rendered inside a horizontally scrollable wrapper so
|
|
38
|
+
* it stays usable on small screens.
|
|
39
|
+
*/
|
|
40
|
+
export const DataTable = <T,>({
|
|
41
|
+
columns,
|
|
42
|
+
rows,
|
|
43
|
+
rowKey,
|
|
44
|
+
empty,
|
|
45
|
+
loading = false,
|
|
46
|
+
caption,
|
|
47
|
+
}: DataTableProps<T>) => {
|
|
48
|
+
const { t } = useI18n();
|
|
49
|
+
const columnCount = columns.length;
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<Box className="w-full overflow-x-auto rounded-lg border border-gray-200 bg-white">
|
|
53
|
+
<table className="w-full min-w-full border-collapse">
|
|
54
|
+
{caption ? (
|
|
55
|
+
<caption className="px-4 py-2 text-left text-xs text-gray-400">
|
|
56
|
+
{caption}
|
|
57
|
+
</caption>
|
|
58
|
+
) : null}
|
|
59
|
+
<thead className="bg-gray-50">
|
|
60
|
+
<tr className="border-b border-gray-200">
|
|
61
|
+
{columns.map((column) => (
|
|
62
|
+
<th
|
|
63
|
+
key={column.key}
|
|
64
|
+
scope="col"
|
|
65
|
+
style={column.width ? { width: column.width } : undefined}
|
|
66
|
+
className={`max-w-[240px] whitespace-nowrap px-4 py-2.5 text-xs font-semibold uppercase tracking-wide text-gray-500 ${
|
|
67
|
+
alignClass[column.align ?? "left"]
|
|
68
|
+
}`}
|
|
69
|
+
>
|
|
70
|
+
{column.header}
|
|
71
|
+
</th>
|
|
72
|
+
))}
|
|
73
|
+
</tr>
|
|
74
|
+
</thead>
|
|
75
|
+
<tbody>
|
|
76
|
+
{loading ? (
|
|
77
|
+
<tr>
|
|
78
|
+
<td colSpan={columnCount} className="px-4 py-10 text-center">
|
|
79
|
+
<Text size="caption" className="text-gray-400">
|
|
80
|
+
{t("table.loading")}
|
|
81
|
+
</Text>
|
|
82
|
+
</td>
|
|
83
|
+
</tr>
|
|
84
|
+
) : rows.length === 0 ? (
|
|
85
|
+
<tr>
|
|
86
|
+
<td colSpan={columnCount} className="px-4 py-10 text-center">
|
|
87
|
+
<Text size="caption" className="text-gray-400">
|
|
88
|
+
{empty ?? t("table.empty")}
|
|
89
|
+
</Text>
|
|
90
|
+
</td>
|
|
91
|
+
</tr>
|
|
92
|
+
) : (
|
|
93
|
+
rows.map((row) => (
|
|
94
|
+
<tr
|
|
95
|
+
key={rowKey(row)}
|
|
96
|
+
className="border-b border-gray-100 transition-colors last:border-0 hover:bg-gray-50"
|
|
97
|
+
>
|
|
98
|
+
{columns.map((column) => (
|
|
99
|
+
<td
|
|
100
|
+
key={column.key}
|
|
101
|
+
className={`max-w-[240px] px-4 py-3 align-middle ${alignClass[column.align ?? "left"]}`}
|
|
102
|
+
>
|
|
103
|
+
<Box
|
|
104
|
+
className={`min-w-0 max-w-full overflow-hidden ${cellAlign[column.align ?? "left"]}`}
|
|
105
|
+
>
|
|
106
|
+
{column.render ? (
|
|
107
|
+
column.render(row)
|
|
108
|
+
) : (
|
|
109
|
+
<Text
|
|
110
|
+
as="span"
|
|
111
|
+
size="caption"
|
|
112
|
+
className="block truncate text-gray-700"
|
|
113
|
+
>
|
|
114
|
+
{String(
|
|
115
|
+
(row as Record<string, unknown>)[column.key] ?? "",
|
|
116
|
+
)}
|
|
117
|
+
</Text>
|
|
118
|
+
)}
|
|
119
|
+
</Box>
|
|
120
|
+
</td>
|
|
121
|
+
))}
|
|
122
|
+
</tr>
|
|
123
|
+
))
|
|
124
|
+
)}
|
|
125
|
+
</tbody>
|
|
126
|
+
</table>
|
|
127
|
+
</Box>
|
|
128
|
+
);
|
|
129
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
} from "react";
|
|
12
|
+
|
|
13
|
+
import { Box } from "../box.js";
|
|
14
|
+
import { Text } from "../text.js";
|
|
15
|
+
import { useI18n } from "./i18n.js";
|
|
16
|
+
|
|
17
|
+
export type ToastTone = "success" | "error" | "info";
|
|
18
|
+
|
|
19
|
+
type Toast = {
|
|
20
|
+
id: number;
|
|
21
|
+
message: string;
|
|
22
|
+
tone: ToastTone;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type ToastContextValue = {
|
|
26
|
+
show: (message: string, tone?: ToastTone) => void;
|
|
27
|
+
success: (message: string) => void;
|
|
28
|
+
error: (message: string) => void;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const ToastContext = createContext<ToastContextValue | null>(null);
|
|
32
|
+
|
|
33
|
+
export const useToast = (): ToastContextValue => {
|
|
34
|
+
const context = useContext(ToastContext);
|
|
35
|
+
if (!context) {
|
|
36
|
+
throw new Error("useToast precisa estar dentro de <ToastProvider>.");
|
|
37
|
+
}
|
|
38
|
+
return context;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const toneStyles: Record<ToastTone, { container: string; icon: string; glyph: string }> = {
|
|
42
|
+
success: {
|
|
43
|
+
container: "border-emerald-200 bg-white",
|
|
44
|
+
icon: "bg-emerald-100 text-emerald-700",
|
|
45
|
+
glyph: "✓",
|
|
46
|
+
},
|
|
47
|
+
error: {
|
|
48
|
+
container: "border-red-200 bg-white",
|
|
49
|
+
icon: "bg-red-100 text-red-700",
|
|
50
|
+
glyph: "!",
|
|
51
|
+
},
|
|
52
|
+
info: {
|
|
53
|
+
container: "border-gray-200 bg-white",
|
|
54
|
+
icon: "bg-gray-100 text-gray-700",
|
|
55
|
+
glyph: "i",
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Toast notifications provider. Renders a fixed viewport (top-right on desktop,
|
|
61
|
+
* full-width on mobile) and exposes `useToast()`.
|
|
62
|
+
*/
|
|
63
|
+
export const ToastProvider = ({ children }: { children: ReactNode }) => {
|
|
64
|
+
const { t } = useI18n();
|
|
65
|
+
const [toasts, setToasts] = useState<Toast[]>([]);
|
|
66
|
+
const nextId = useRef(0);
|
|
67
|
+
|
|
68
|
+
const dismiss = useCallback((id: number) => {
|
|
69
|
+
setToasts((current) => current.filter((toast) => toast.id !== id));
|
|
70
|
+
}, []);
|
|
71
|
+
|
|
72
|
+
const show = useCallback((message: string, tone: ToastTone = "success") => {
|
|
73
|
+
const id = (nextId.current += 1);
|
|
74
|
+
setToasts((current) => [...current, { id, message, tone }]);
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
setToasts((current) => current.filter((toast) => toast.id !== id));
|
|
77
|
+
}, 4000);
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
const value = useMemo<ToastContextValue>(
|
|
81
|
+
() => ({
|
|
82
|
+
show,
|
|
83
|
+
success: (message: string) => show(message, "success"),
|
|
84
|
+
error: (message: string) => show(message, "error"),
|
|
85
|
+
}),
|
|
86
|
+
[show],
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<ToastContext.Provider value={value}>
|
|
91
|
+
{children}
|
|
92
|
+
|
|
93
|
+
<Box className="pointer-events-none fixed inset-x-0 top-0 z-[60] flex-col items-center gap-2 p-4 sm:inset-x-auto sm:right-0 sm:items-end">
|
|
94
|
+
{toasts.map((toast) => {
|
|
95
|
+
const tone = toneStyles[toast.tone];
|
|
96
|
+
return (
|
|
97
|
+
<Box
|
|
98
|
+
key={toast.id}
|
|
99
|
+
role="status"
|
|
100
|
+
aria-live="polite"
|
|
101
|
+
className={`pointer-events-auto w-full max-w-sm items-start gap-3 rounded-lg border px-4 py-3 shadow-lg ${tone.container}`}
|
|
102
|
+
>
|
|
103
|
+
<Box
|
|
104
|
+
className={`mt-0.5 h-6 w-6 shrink-0 items-center justify-center rounded-full text-xs font-bold ${tone.icon}`}
|
|
105
|
+
>
|
|
106
|
+
<span aria-hidden>{tone.glyph}</span>
|
|
107
|
+
</Box>
|
|
108
|
+
<Text size="caption" className="flex-1 pt-1 text-gray-800">
|
|
109
|
+
{toast.message}
|
|
110
|
+
</Text>
|
|
111
|
+
<Box
|
|
112
|
+
as="button"
|
|
113
|
+
type="button"
|
|
114
|
+
aria-label={t("common.closeNotification")}
|
|
115
|
+
onClick={() => dismiss(toast.id)}
|
|
116
|
+
className="shrink-0 rounded-md px-1.5 py-0.5 text-gray-400 transition-colors hover:bg-gray-100 hover:text-gray-700"
|
|
117
|
+
>
|
|
118
|
+
×
|
|
119
|
+
</Box>
|
|
120
|
+
</Box>
|
|
121
|
+
);
|
|
122
|
+
})}
|
|
123
|
+
</Box>
|
|
124
|
+
</ToastContext.Provider>
|
|
125
|
+
);
|
|
126
|
+
};
|