@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,27 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Admin UI language (independent from content localization).
|
|
4
|
+
*
|
|
5
|
+
* English is the default; the user's choice is persisted in localStorage.
|
|
6
|
+
* Collection/field labels come from the schema and are intentionally NOT
|
|
7
|
+
* translated here.
|
|
8
|
+
*/
|
|
9
|
+
export type AdminLocale = "en" | "pt";
|
|
10
|
+
export declare const ADMIN_LOCALES: {
|
|
11
|
+
code: AdminLocale;
|
|
12
|
+
label: string;
|
|
13
|
+
}[];
|
|
14
|
+
export declare const DEFAULT_ADMIN_LOCALE: AdminLocale;
|
|
15
|
+
export declare const ADMIN_LOCALE_STORAGE_KEY = "serenojs:admin-locale";
|
|
16
|
+
type Vars = Record<string, string | number>;
|
|
17
|
+
export declare function translate(locale: AdminLocale, key: string, vars?: Vars): string;
|
|
18
|
+
type I18nValue = {
|
|
19
|
+
locale: AdminLocale;
|
|
20
|
+
setLocale: (locale: AdminLocale) => void;
|
|
21
|
+
t: (key: string, vars?: Vars) => string;
|
|
22
|
+
};
|
|
23
|
+
export declare function I18nProvider({ children }: {
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
}): import("react").JSX.Element;
|
|
26
|
+
export declare function useI18n(): I18nValue;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useCallback, useContext, useMemo, useState, } from "react";
|
|
4
|
+
export const ADMIN_LOCALES = [
|
|
5
|
+
{ code: "en", label: "English" },
|
|
6
|
+
{ code: "pt", label: "Português (BR)" },
|
|
7
|
+
];
|
|
8
|
+
export const DEFAULT_ADMIN_LOCALE = "en";
|
|
9
|
+
export const ADMIN_LOCALE_STORAGE_KEY = "serenojs:admin-locale";
|
|
10
|
+
const en = {
|
|
11
|
+
"common.loading": "Loading…",
|
|
12
|
+
"common.saveChanges": "Save changes",
|
|
13
|
+
"common.saving": "Saving…",
|
|
14
|
+
"common.cancel": "Cancel",
|
|
15
|
+
"common.remove": "Remove",
|
|
16
|
+
"common.removing": "Removing…",
|
|
17
|
+
"common.edit": "Edit",
|
|
18
|
+
"common.createdAt": "Created at",
|
|
19
|
+
"common.updatedAt": "Updated at",
|
|
20
|
+
"common.email": "Email",
|
|
21
|
+
"common.password": "Password",
|
|
22
|
+
"common.deleteWarning": "This action cannot be undone.",
|
|
23
|
+
"common.search": "Search",
|
|
24
|
+
"common.clear": "Clear",
|
|
25
|
+
"common.closeNotification": "Close notification",
|
|
26
|
+
"common.yes": "Yes",
|
|
27
|
+
"common.no": "No",
|
|
28
|
+
"common.language": "Language",
|
|
29
|
+
"sidebar.openMenu": "Open menu",
|
|
30
|
+
"app.unexpected": "Unexpected error.",
|
|
31
|
+
"app.errorTitle": "Something went wrong",
|
|
32
|
+
"app.verifyingSession": "Checking session…",
|
|
33
|
+
"app.collectionNotFound": 'There is no collection "{slug}".',
|
|
34
|
+
"app.missingRuntime": "SerenoJS configuration is missing. The middleware did not inject the runtime.",
|
|
35
|
+
"login.title": "Sign in",
|
|
36
|
+
"login.subtitle": "{title} · admin panel",
|
|
37
|
+
"login.submitting": "Signing in…",
|
|
38
|
+
"login.submit": "Sign in",
|
|
39
|
+
"login.error": "Could not sign in.",
|
|
40
|
+
"login.network": "Network error while signing in.",
|
|
41
|
+
"sidebar.collections": "Collections",
|
|
42
|
+
"sidebar.overview": "Overview",
|
|
43
|
+
"sidebar.singleRecord": "Single record",
|
|
44
|
+
"sidebar.administration": "Administration",
|
|
45
|
+
"sidebar.users": "Users",
|
|
46
|
+
"sidebar.backup": "Backup",
|
|
47
|
+
"sidebar.sessionActive": "Active session",
|
|
48
|
+
"sidebar.signOut": "Sign out",
|
|
49
|
+
"dashboard.subtitle": "Panel generated from the collection descriptors.",
|
|
50
|
+
"dashboard.single": "Single",
|
|
51
|
+
"dashboard.fields": "{count} field(s) defined in the schema.",
|
|
52
|
+
"dashboard.manage": "Manage →",
|
|
53
|
+
"collection.newItem": "+ New item",
|
|
54
|
+
"collection.searchPlaceholder": "Search…",
|
|
55
|
+
"collection.empty": "No items registered yet.",
|
|
56
|
+
"collection.editTitle": "Edit item #{id}",
|
|
57
|
+
"collection.newTitle": "New item in {label}",
|
|
58
|
+
"collection.removeTitle": "Remove item",
|
|
59
|
+
"collection.removeConfirm": "Are you sure you want to remove {label}?",
|
|
60
|
+
"collection.loadError": "Failed to load items.",
|
|
61
|
+
"collection.saveError": "Could not save.",
|
|
62
|
+
"collection.created": "Item created successfully.",
|
|
63
|
+
"collection.updated": "Item updated successfully.",
|
|
64
|
+
"collection.removeError": "Could not remove the item.",
|
|
65
|
+
"collection.removed": "Item removed successfully.",
|
|
66
|
+
"collection.networkSave": "Network error while saving the item.",
|
|
67
|
+
"form.general": "General",
|
|
68
|
+
"form.create": "Create item",
|
|
69
|
+
"standalone.badge": "Single record",
|
|
70
|
+
"standalone.saved": "Record updated successfully.",
|
|
71
|
+
"standalone.saveError": "Could not save.",
|
|
72
|
+
"standalone.networkSave": "Network error while saving the record.",
|
|
73
|
+
"field.select": "— select —",
|
|
74
|
+
"field.none": "— none —",
|
|
75
|
+
"media.upload": "Upload file",
|
|
76
|
+
"media.replace": "Replace file",
|
|
77
|
+
"media.uploading": "Uploading…",
|
|
78
|
+
"media.uploadFailed": "Upload failed.",
|
|
79
|
+
"media.networkUpload": "Network error during upload.",
|
|
80
|
+
"media.addImages": "Add images",
|
|
81
|
+
"media.moveBack": "Move backward",
|
|
82
|
+
"media.setPrimary": "Set as primary",
|
|
83
|
+
"media.moveForward": "Move forward",
|
|
84
|
+
"media.count": "{count} media",
|
|
85
|
+
"media.primary": "Primary",
|
|
86
|
+
"media.noFile": "No file selected.",
|
|
87
|
+
"media.noMedia": "No media uploaded.",
|
|
88
|
+
"relation.empty": "No items available to relate.",
|
|
89
|
+
"list.empty": "No items.",
|
|
90
|
+
"list.item": "Item {index}",
|
|
91
|
+
"list.moveUp": "Move up",
|
|
92
|
+
"list.moveDown": "Move down",
|
|
93
|
+
"list.remove": "Remove",
|
|
94
|
+
"list.add": "Add item",
|
|
95
|
+
"pagination.range": "{from}–{to} of {total} items",
|
|
96
|
+
"pagination.previous": "Previous",
|
|
97
|
+
"pagination.next": "Next",
|
|
98
|
+
"table.loading": "Loading…",
|
|
99
|
+
"table.empty": "No items found.",
|
|
100
|
+
"users.title": "Users",
|
|
101
|
+
"users.subtitle": "Who can access the admin panel.",
|
|
102
|
+
"users.you": "you",
|
|
103
|
+
"users.new": "+ New user",
|
|
104
|
+
"users.createTitle": "New user",
|
|
105
|
+
"users.empty": "No users registered yet.",
|
|
106
|
+
"users.editTitle": "Edit user #{id}",
|
|
107
|
+
"users.removeTitle": "Remove user",
|
|
108
|
+
"users.removeConfirm": "Remove the user {email}? Their active sessions will be revoked.",
|
|
109
|
+
"users.newPassword": "New password",
|
|
110
|
+
"users.passwordHintKeep": "Leave blank to keep the current password.",
|
|
111
|
+
"users.passwordHintMin": "At least 8 characters.",
|
|
112
|
+
"users.create": "Create user",
|
|
113
|
+
"users.created": "User created.",
|
|
114
|
+
"users.updated": "User updated.",
|
|
115
|
+
"users.removed": "User removed.",
|
|
116
|
+
"users.saveError": "Could not save the user.",
|
|
117
|
+
"users.networkSave": "Network error while saving the user.",
|
|
118
|
+
"users.removeError": "Could not remove the user.",
|
|
119
|
+
"users.networkRemove": "Network error while removing the user.",
|
|
120
|
+
"backup.title": "Backup",
|
|
121
|
+
"backup.subtitle": "Download or restore the database and system images.",
|
|
122
|
+
"backup.refresh": "Refresh",
|
|
123
|
+
"backup.downloadCurrent": "Download current",
|
|
124
|
+
"backup.restoreAction": "Restore",
|
|
125
|
+
"backup.create": "Create backup",
|
|
126
|
+
"backup.description": "The backup bundles a consistent SQLite snapshot and every file under public/images and public/files. Restoring replaces the current database and rewrites the images.",
|
|
127
|
+
"backup.file": "File",
|
|
128
|
+
"backup.size": "Size",
|
|
129
|
+
"backup.download": "Download",
|
|
130
|
+
"backup.empty": "No stored backups yet.",
|
|
131
|
+
"backup.restoreTitle": "Restore backup",
|
|
132
|
+
"backup.deleteTitle": "Remove backup",
|
|
133
|
+
"backup.restoreWarning": "The current database will be replaced.",
|
|
134
|
+
"backup.restoreConfirm": "Restore the file {name}? The current database will be replaced and images from the backup rewritten. The session may be terminated.",
|
|
135
|
+
"backup.deleteConfirm": "Remove the backup {name}?",
|
|
136
|
+
"backup.restoring": "Restoring…",
|
|
137
|
+
"backup.restored": "Backup restored. Reloading…",
|
|
138
|
+
"backup.created": "Backup created.",
|
|
139
|
+
"backup.createError": "Could not create the backup.",
|
|
140
|
+
"backup.networkCreate": "Network error while creating the backup.",
|
|
141
|
+
"backup.restoreError": "Could not restore the backup.",
|
|
142
|
+
"backup.networkRestore": "Network error while restoring the backup.",
|
|
143
|
+
"backup.removed": "Backup removed.",
|
|
144
|
+
"backup.removeError": "Could not remove the backup.",
|
|
145
|
+
"backup.networkRemove": "Network error while removing the backup.",
|
|
146
|
+
};
|
|
147
|
+
const pt = {
|
|
148
|
+
"common.loading": "Carregando…",
|
|
149
|
+
"common.saveChanges": "Salvar alterações",
|
|
150
|
+
"common.saving": "Salvando…",
|
|
151
|
+
"common.cancel": "Cancelar",
|
|
152
|
+
"common.remove": "Remover",
|
|
153
|
+
"common.removing": "Removendo…",
|
|
154
|
+
"common.edit": "Editar",
|
|
155
|
+
"common.createdAt": "Criado em",
|
|
156
|
+
"common.updatedAt": "Atualizado em",
|
|
157
|
+
"common.email": "E-mail",
|
|
158
|
+
"common.password": "Senha",
|
|
159
|
+
"common.deleteWarning": "Esta ação não pode ser desfeita.",
|
|
160
|
+
"common.search": "Buscar",
|
|
161
|
+
"common.clear": "Limpar",
|
|
162
|
+
"common.closeNotification": "Fechar notificação",
|
|
163
|
+
"common.yes": "Sim",
|
|
164
|
+
"common.no": "Não",
|
|
165
|
+
"common.language": "Idioma",
|
|
166
|
+
"sidebar.openMenu": "Abrir menu",
|
|
167
|
+
"app.unexpected": "Erro inesperado.",
|
|
168
|
+
"app.errorTitle": "Algo deu errado",
|
|
169
|
+
"app.verifyingSession": "Verificando sessão…",
|
|
170
|
+
"app.collectionNotFound": 'Não há coleção "{slug}".',
|
|
171
|
+
"app.missingRuntime": "Configuração do SerenoJS ausente. O middleware não injetou o runtime.",
|
|
172
|
+
"login.title": "Entrar",
|
|
173
|
+
"login.subtitle": "{title} · painel administrativo",
|
|
174
|
+
"login.submitting": "Entrando…",
|
|
175
|
+
"login.submit": "Entrar",
|
|
176
|
+
"login.error": "Não foi possível entrar.",
|
|
177
|
+
"login.network": "Erro de rede ao entrar.",
|
|
178
|
+
"sidebar.collections": "Coleções",
|
|
179
|
+
"sidebar.overview": "Visão geral",
|
|
180
|
+
"sidebar.singleRecord": "Registro único",
|
|
181
|
+
"sidebar.administration": "Administração",
|
|
182
|
+
"sidebar.users": "Usuários",
|
|
183
|
+
"sidebar.backup": "Backup",
|
|
184
|
+
"sidebar.sessionActive": "Sessão ativa",
|
|
185
|
+
"sidebar.signOut": "Sair",
|
|
186
|
+
"dashboard.subtitle": "Painel gerado automaticamente a partir dos descritores de coleção.",
|
|
187
|
+
"dashboard.single": "Único",
|
|
188
|
+
"dashboard.fields": "{count} campo(s) definidos no schema.",
|
|
189
|
+
"dashboard.manage": "Gerenciar →",
|
|
190
|
+
"collection.newItem": "+ Novo item",
|
|
191
|
+
"collection.searchPlaceholder": "Buscar…",
|
|
192
|
+
"collection.empty": "Nenhum item cadastrado ainda.",
|
|
193
|
+
"collection.editTitle": "Editar item #{id}",
|
|
194
|
+
"collection.newTitle": "Novo item em {label}",
|
|
195
|
+
"collection.removeTitle": "Remover item",
|
|
196
|
+
"collection.removeConfirm": "Tem certeza que deseja remover {label}?",
|
|
197
|
+
"collection.loadError": "Falha ao carregar os itens.",
|
|
198
|
+
"collection.saveError": "Não foi possível salvar.",
|
|
199
|
+
"collection.created": "Item criado com sucesso.",
|
|
200
|
+
"collection.updated": "Item atualizado com sucesso.",
|
|
201
|
+
"collection.removeError": "Não foi possível remover o item.",
|
|
202
|
+
"collection.removed": "Item removido com sucesso.",
|
|
203
|
+
"collection.networkSave": "Erro de rede ao salvar o item.",
|
|
204
|
+
"form.general": "Geral",
|
|
205
|
+
"form.create": "Criar item",
|
|
206
|
+
"standalone.badge": "Registro único",
|
|
207
|
+
"standalone.saved": "Registro atualizado com sucesso.",
|
|
208
|
+
"standalone.saveError": "Não foi possível salvar.",
|
|
209
|
+
"standalone.networkSave": "Erro de rede ao salvar o registro.",
|
|
210
|
+
"field.select": "— selecione —",
|
|
211
|
+
"field.none": "— nenhum —",
|
|
212
|
+
"media.upload": "Enviar arquivo",
|
|
213
|
+
"media.replace": "Substituir arquivo",
|
|
214
|
+
"media.uploading": "Enviando…",
|
|
215
|
+
"media.uploadFailed": "Falha no upload.",
|
|
216
|
+
"media.networkUpload": "Erro de rede no upload.",
|
|
217
|
+
"media.addImages": "Adicionar imagens",
|
|
218
|
+
"media.moveBack": "Mover para trás",
|
|
219
|
+
"media.setPrimary": "Definir como principal",
|
|
220
|
+
"media.moveForward": "Mover para frente",
|
|
221
|
+
"media.count": "{count} mídia(s)",
|
|
222
|
+
"media.primary": "Principal",
|
|
223
|
+
"media.noFile": "Nenhum arquivo enviado.",
|
|
224
|
+
"media.noMedia": "Nenhuma mídia enviada.",
|
|
225
|
+
"relation.empty": "Nenhum item disponível para relacionar.",
|
|
226
|
+
"list.empty": "Nenhum item.",
|
|
227
|
+
"list.item": "Item {index}",
|
|
228
|
+
"list.moveUp": "Mover para cima",
|
|
229
|
+
"list.moveDown": "Mover para baixo",
|
|
230
|
+
"list.remove": "Remover",
|
|
231
|
+
"list.add": "Adicionar item",
|
|
232
|
+
"pagination.range": "{from}–{to} de {total} itens",
|
|
233
|
+
"pagination.previous": "Anterior",
|
|
234
|
+
"pagination.next": "Próxima",
|
|
235
|
+
"table.loading": "Carregando…",
|
|
236
|
+
"table.empty": "Nenhum item encontrado.",
|
|
237
|
+
"users.title": "Usuários",
|
|
238
|
+
"users.subtitle": "Quem pode acessar o painel administrativo.",
|
|
239
|
+
"users.you": "você",
|
|
240
|
+
"users.new": "+ Novo usuário",
|
|
241
|
+
"users.createTitle": "Novo usuário",
|
|
242
|
+
"users.empty": "Nenhum usuário cadastrado.",
|
|
243
|
+
"users.editTitle": "Editar usuário #{id}",
|
|
244
|
+
"users.removeTitle": "Remover usuário",
|
|
245
|
+
"users.removeConfirm": "Remover o usuário {email}? As sessões ativas dele serão encerradas.",
|
|
246
|
+
"users.newPassword": "Nova senha",
|
|
247
|
+
"users.passwordHintKeep": "Deixe em branco para manter a senha atual.",
|
|
248
|
+
"users.passwordHintMin": "Mínimo de 8 caracteres.",
|
|
249
|
+
"users.create": "Criar usuário",
|
|
250
|
+
"users.created": "Usuário criado.",
|
|
251
|
+
"users.updated": "Usuário atualizado.",
|
|
252
|
+
"users.removed": "Usuário removido.",
|
|
253
|
+
"users.saveError": "Não foi possível salvar o usuário.",
|
|
254
|
+
"users.networkSave": "Erro de rede ao salvar o usuário.",
|
|
255
|
+
"users.removeError": "Não foi possível remover o usuário.",
|
|
256
|
+
"users.networkRemove": "Erro de rede ao remover o usuário.",
|
|
257
|
+
"backup.title": "Backup",
|
|
258
|
+
"backup.subtitle": "Baixe ou restaure o banco de dados e as imagens do sistema.",
|
|
259
|
+
"backup.refresh": "Atualizar",
|
|
260
|
+
"backup.downloadCurrent": "Baixar atual",
|
|
261
|
+
"backup.restoreAction": "Restaurar",
|
|
262
|
+
"backup.create": "Criar backup",
|
|
263
|
+
"backup.description": "O backup reúne um snapshot consistente do banco SQLite e todos os arquivos de public/images e public/files. Restaurar substitui o banco atual e regrava as imagens.",
|
|
264
|
+
"backup.file": "Arquivo",
|
|
265
|
+
"backup.size": "Tamanho",
|
|
266
|
+
"backup.download": "Baixar",
|
|
267
|
+
"backup.empty": "Nenhum backup armazenado ainda.",
|
|
268
|
+
"backup.restoreTitle": "Restaurar backup",
|
|
269
|
+
"backup.deleteTitle": "Remover backup",
|
|
270
|
+
"backup.restoreWarning": "O banco atual será substituído.",
|
|
271
|
+
"backup.restoreConfirm": "Restaurar o arquivo {name}? O banco de dados atual será substituído e as imagens do backup regravadas. A sessão pode ser encerrada.",
|
|
272
|
+
"backup.deleteConfirm": "Remover o backup {name}?",
|
|
273
|
+
"backup.restoring": "Restaurando…",
|
|
274
|
+
"backup.restored": "Backup restaurado. Recarregando…",
|
|
275
|
+
"backup.created": "Backup criado.",
|
|
276
|
+
"backup.createError": "Não foi possível criar o backup.",
|
|
277
|
+
"backup.networkCreate": "Erro de rede ao criar o backup.",
|
|
278
|
+
"backup.restoreError": "Não foi possível restaurar o backup.",
|
|
279
|
+
"backup.networkRestore": "Erro de rede ao restaurar o backup.",
|
|
280
|
+
"backup.removed": "Backup removido.",
|
|
281
|
+
"backup.removeError": "Não foi possível remover o backup.",
|
|
282
|
+
"backup.networkRemove": "Erro de rede ao remover o backup.",
|
|
283
|
+
};
|
|
284
|
+
const messages = { en, pt };
|
|
285
|
+
export function translate(locale, key, vars) {
|
|
286
|
+
const template = messages[locale][key] ?? en[key] ?? key;
|
|
287
|
+
if (!vars)
|
|
288
|
+
return template;
|
|
289
|
+
return template.replace(/\{(\w+)\}/g, (match, name) => name in vars ? String(vars[name]) : match);
|
|
290
|
+
}
|
|
291
|
+
function readStoredLocale() {
|
|
292
|
+
try {
|
|
293
|
+
const stored = window.localStorage.getItem(ADMIN_LOCALE_STORAGE_KEY);
|
|
294
|
+
if (stored === "en" || stored === "pt")
|
|
295
|
+
return stored;
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
// localStorage can be unavailable (private mode/SSR).
|
|
299
|
+
}
|
|
300
|
+
return DEFAULT_ADMIN_LOCALE;
|
|
301
|
+
}
|
|
302
|
+
const I18nContext = createContext(null);
|
|
303
|
+
export function I18nProvider({ children }) {
|
|
304
|
+
const [locale, setLocaleState] = useState(readStoredLocale);
|
|
305
|
+
const setLocale = useCallback((next) => {
|
|
306
|
+
setLocaleState(next);
|
|
307
|
+
try {
|
|
308
|
+
window.localStorage.setItem(ADMIN_LOCALE_STORAGE_KEY, next);
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
// Ignore persistence failures.
|
|
312
|
+
}
|
|
313
|
+
}, []);
|
|
314
|
+
const t = useCallback((key, vars) => translate(locale, key, vars), [locale]);
|
|
315
|
+
const value = useMemo(() => ({ locale, setLocale, t }), [locale, setLocale, t]);
|
|
316
|
+
return _jsx(I18nContext.Provider, { value: value, children: children });
|
|
317
|
+
}
|
|
318
|
+
export function useI18n() {
|
|
319
|
+
const value = useContext(I18nContext);
|
|
320
|
+
if (!value) {
|
|
321
|
+
throw new Error("[serenojs] I18nProvider is missing.");
|
|
322
|
+
}
|
|
323
|
+
return value;
|
|
324
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CollectionDef, FieldValue, Item, RelationOptionsMap } from "../../core/types.js";
|
|
2
|
+
export type ItemFormProps = {
|
|
3
|
+
collection: CollectionDef;
|
|
4
|
+
item: Item | null;
|
|
5
|
+
submitting: boolean;
|
|
6
|
+
errors: Record<string, string>;
|
|
7
|
+
formError: string | null;
|
|
8
|
+
relationOptions: RelationOptionsMap;
|
|
9
|
+
onSubmit: (values: Record<string, FieldValue>) => void;
|
|
10
|
+
onCancel?: () => void;
|
|
11
|
+
/** Hide the cancel button (e.g. standalone records, where there is no list to return to). */
|
|
12
|
+
hideCancel?: boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Create/edit form rendered inside the drawer.
|
|
16
|
+
*
|
|
17
|
+
* Localized fields are shown in one tab per locale; language-independent fields
|
|
18
|
+
* live in a "Geral" section below the tabs.
|
|
19
|
+
*/
|
|
20
|
+
export declare const ItemForm: ({ collection, item, submitting, errors, formError, relationOptions, onSubmit, onCancel, hideCancel, }: ItemFormProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { collectionLocales, emptyValues, isLocalizedField } from "../../core/schema.js";
|
|
5
|
+
import { localeLabel } from "./locales.js";
|
|
6
|
+
import { Box } from "../box.js";
|
|
7
|
+
import { Button } from "../button.js";
|
|
8
|
+
import { Text } from "../text.js";
|
|
9
|
+
import { FieldControl } from "./field-control.js";
|
|
10
|
+
import { useI18n } from "./i18n.js";
|
|
11
|
+
function initialValues(collection, item) {
|
|
12
|
+
const base = emptyValues(collection);
|
|
13
|
+
if (!item)
|
|
14
|
+
return base;
|
|
15
|
+
for (const field of collection.fields) {
|
|
16
|
+
base[field.name] = item[field.name] ?? base[field.name] ?? null;
|
|
17
|
+
}
|
|
18
|
+
return base;
|
|
19
|
+
}
|
|
20
|
+
function asLocalized(value) {
|
|
21
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
22
|
+
? value
|
|
23
|
+
: {};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create/edit form rendered inside the drawer.
|
|
27
|
+
*
|
|
28
|
+
* Localized fields are shown in one tab per locale; language-independent fields
|
|
29
|
+
* live in a "Geral" section below the tabs.
|
|
30
|
+
*/
|
|
31
|
+
export const ItemForm = ({ collection, item, submitting, errors, formError, relationOptions, onSubmit, onCancel, hideCancel, }) => {
|
|
32
|
+
const { t } = useI18n();
|
|
33
|
+
const locales = collectionLocales(collection);
|
|
34
|
+
const localizedFields = collection.fields.filter((field) => isLocalizedField(collection, field));
|
|
35
|
+
const sharedFields = collection.fields.filter((field) => !isLocalizedField(collection, field));
|
|
36
|
+
const [values, setValues] = useState(() => initialValues(collection, item));
|
|
37
|
+
const [locale, setLocale] = useState(locales[0] ?? "");
|
|
38
|
+
const update = (name, value) => {
|
|
39
|
+
setValues((previous) => ({ ...previous, [name]: value }));
|
|
40
|
+
};
|
|
41
|
+
const updateLocalized = (field, loc, value) => {
|
|
42
|
+
setValues((previous) => ({
|
|
43
|
+
...previous,
|
|
44
|
+
[field.name]: {
|
|
45
|
+
...asLocalized(previous[field.name]),
|
|
46
|
+
[loc]: value,
|
|
47
|
+
},
|
|
48
|
+
}));
|
|
49
|
+
};
|
|
50
|
+
const renderField = (field, value, error, onChange) => (_jsx(FieldControl, { field: field, collectionSlug: collection.slug, value: value ?? null, error: error, disabled: submitting, relationOptions: relationOptions[field.name] ?? [], onChange: onChange ?? ((next) => update(field.name, next)) }, field.name));
|
|
51
|
+
return (_jsxs(Box, { as: "form", className: "flex-col gap-5", onSubmit: (event) => {
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
onSubmit(values);
|
|
54
|
+
}, children: [formError ? (_jsx(Box, { className: "rounded-md border border-red-200 bg-red-50 px-3 py-2", children: _jsx(Text, { size: "caption", className: "text-red-700", children: formError }) })) : null, localizedFields.length > 0 ? (_jsxs(Box, { className: "flex-col gap-5", children: [_jsx(Box, { className: "flex-wrap items-center gap-1 rounded-lg bg-gray-100 p-1", children: locales.map((code) => {
|
|
55
|
+
const active = code === locale;
|
|
56
|
+
return (_jsx("button", { type: "button", onClick: () => setLocale(code), className: `rounded-md px-3 py-1.5 text-sm font-medium transition-colors ${active
|
|
57
|
+
? "bg-white text-gray-900 shadow-sm"
|
|
58
|
+
: "text-gray-500 hover:text-gray-900"}`, children: localeLabel(code) }, code));
|
|
59
|
+
}) }), localizedFields.map((field) => renderField(field, asLocalized(values[field.name])[locale] ?? null, errors[`${field.name}_${locale}`], (value) => updateLocalized(field, locale, value)))] })) : null, sharedFields.length > 0 ? (_jsxs(Box, { className: "flex-col gap-5 border-t border-gray-200 pt-5", children: [localizedFields.length > 0 ? (_jsx(Text, { size: "caption", className: "font-semibold uppercase tracking-wide text-gray-400", children: t("form.general") })) : null, sharedFields.map((field) => renderField(field, values[field.name] ?? null, errors[field.name]))] })) : null, _jsxs(Box, { className: "items-center justify-end gap-2 border-t border-gray-200 pt-4", children: [hideCancel ? null : (_jsx(Button, { type: "button", variant: "secondary", onClick: onCancel, disabled: submitting, children: t("common.cancel") })), _jsx(Button, { type: "submit", variant: "primary", disabled: submitting, children: submitting ? t("common.saving") : item ? t("common.saveChanges") : t("form.create") })] })] }));
|
|
60
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FieldDef } from "../../core/types.js";
|
|
2
|
+
export type ListControlProps = {
|
|
3
|
+
field: FieldDef;
|
|
4
|
+
value: string[];
|
|
5
|
+
error?: string | undefined;
|
|
6
|
+
disabled?: boolean | undefined;
|
|
7
|
+
onChange: (value: string[]) => void;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Native `list` widget: an ordered list of short text values (add, edit,
|
|
11
|
+
* reorder, remove).
|
|
12
|
+
*/
|
|
13
|
+
export declare const ListControl: ({ field, value, error, disabled, onChange, }: ListControlProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { FiArrowDown, FiArrowUp, FiPlus, FiTrash2, } from "react-icons/fi";
|
|
4
|
+
import { Box } from "../box.js";
|
|
5
|
+
import { Button, IconButton } from "../button.js";
|
|
6
|
+
import { FieldWrapper, inputClass } from "../field-wrapper.js";
|
|
7
|
+
import { Text } from "../text.js";
|
|
8
|
+
import { useI18n } from "./i18n.js";
|
|
9
|
+
/**
|
|
10
|
+
* Native `list` widget: an ordered list of short text values (add, edit,
|
|
11
|
+
* reorder, remove).
|
|
12
|
+
*/
|
|
13
|
+
export const ListControl = ({ field, value, error, disabled, onChange, }) => {
|
|
14
|
+
const { t } = useI18n();
|
|
15
|
+
const update = (index, next) => onChange(value.map((item, i) => (i === index ? next : item)));
|
|
16
|
+
const remove = (index) => onChange(value.filter((_, i) => i !== index));
|
|
17
|
+
const move = (index, direction) => {
|
|
18
|
+
const target = index + direction;
|
|
19
|
+
if (target < 0 || target >= value.length)
|
|
20
|
+
return;
|
|
21
|
+
const next = [...value];
|
|
22
|
+
const current = next[index];
|
|
23
|
+
const swap = next[target];
|
|
24
|
+
if (current === undefined || swap === undefined)
|
|
25
|
+
return;
|
|
26
|
+
next[index] = swap;
|
|
27
|
+
next[target] = current;
|
|
28
|
+
onChange(next);
|
|
29
|
+
};
|
|
30
|
+
return (_jsx(FieldWrapper, { label: field.label, required: field.required, help: field.help, error: error, children: _jsxs(Box, { className: "flex-col gap-2", children: [value.length === 0 ? (_jsx(Text, { size: "caption", className: "text-gray-400", children: t("list.empty") })) : null, value.map((item, index) => (_jsxs(Box, { className: "items-center gap-1.5", children: [_jsx(Box, { className: "min-w-0 flex-1", children: _jsx("input", { value: item, disabled: disabled, placeholder: t("list.item", { index: index + 1 }), onChange: (event) => update(index, event.target.value), className: `${inputClass} border-gray-300` }) }), _jsx(IconButton, { type: "button", label: t("list.moveUp"), disabled: disabled || index === 0, onClick: () => move(index, -1), children: _jsx(FiArrowUp, { "aria-hidden": true }) }), _jsx(IconButton, { type: "button", label: t("list.moveDown"), disabled: disabled || index === value.length - 1, onClick: () => move(index, 1), children: _jsx(FiArrowDown, { "aria-hidden": true }) }), _jsx(IconButton, { type: "button", label: t("list.remove"), className: "text-red-600 hover:bg-red-50 hover:text-red-700", disabled: disabled, onClick: () => remove(index), children: _jsx(FiTrash2, { "aria-hidden": true }) })] }, index))), _jsxs(Button, { type: "button", variant: "secondary", size: "sm", disabled: disabled, onClick: () => onChange([...value, ""]), children: [_jsx(FiPlus, { "aria-hidden": true, className: "mr-1.5" }), t("list.add")] })] }) }));
|
|
31
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
let labels = new Map();
|
|
2
|
+
/** Injected once by the admin SPA bootstrap. */
|
|
3
|
+
export function setLocaleLabels(locales) {
|
|
4
|
+
labels = new Map(locales.map((locale) => [locale.code, locale.label]));
|
|
5
|
+
}
|
|
6
|
+
export function localeLabel(code) {
|
|
7
|
+
return labels.get(code) ?? code.toUpperCase();
|
|
8
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { Box } from "../box.js";
|
|
5
|
+
import { Button } from "../button.js";
|
|
6
|
+
import { FieldWrapper, inputClass } from "../field-wrapper.js";
|
|
7
|
+
import { Text } from "../text.js";
|
|
8
|
+
import { useCmsUrls } from "./runtime.js";
|
|
9
|
+
import { useI18n } from "./i18n.js";
|
|
10
|
+
function nextTarget() {
|
|
11
|
+
if (typeof window === "undefined")
|
|
12
|
+
return "/admin";
|
|
13
|
+
const next = new URLSearchParams(window.location.search).get("next");
|
|
14
|
+
// Only allow internal paths.
|
|
15
|
+
return next && next.startsWith("/") && !next.startsWith("//") ? next : "/admin";
|
|
16
|
+
}
|
|
17
|
+
export const LoginForm = ({ onSuccess }) => {
|
|
18
|
+
const { auth } = useCmsUrls();
|
|
19
|
+
const { t } = useI18n();
|
|
20
|
+
const [email, setEmail] = useState("");
|
|
21
|
+
const [password, setPassword] = useState("");
|
|
22
|
+
const [error, setError] = useState(null);
|
|
23
|
+
const [submitting, setSubmitting] = useState(false);
|
|
24
|
+
const submit = async (event) => {
|
|
25
|
+
event.preventDefault();
|
|
26
|
+
setSubmitting(true);
|
|
27
|
+
setError(null);
|
|
28
|
+
try {
|
|
29
|
+
const response = await fetch(auth("login"), {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: { "Content-Type": "application/json" },
|
|
32
|
+
body: JSON.stringify({ email, password }),
|
|
33
|
+
});
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
const json = (await response.json().catch(() => null));
|
|
36
|
+
setError(json?.message ?? t("login.error"));
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (onSuccess)
|
|
40
|
+
onSuccess();
|
|
41
|
+
else
|
|
42
|
+
window.location.assign(nextTarget());
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
setError(t("login.network"));
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
setSubmitting(false);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
return (_jsxs(Box, { as: "form", className: "flex-col gap-4", onSubmit: (event) => void submit(event), children: [error ? (_jsx(Box, { className: "rounded-md border border-red-200 bg-red-50 px-3 py-2", children: _jsx(Text, { size: "caption", className: "text-red-700", children: error }) })) : null, _jsx(FieldWrapper, { label: t("common.email"), htmlFor: "login-email", required: true, children: _jsx("input", { id: "login-email", type: "email", autoComplete: "username", required: true, value: email, disabled: submitting, onChange: (event) => setEmail(event.target.value), className: `${inputClass} border-gray-300` }) }), _jsx(FieldWrapper, { label: t("common.password"), htmlFor: "login-password", required: true, children: _jsx("input", { id: "login-password", type: "password", autoComplete: "current-password", required: true, value: password, disabled: submitting, onChange: (event) => setPassword(event.target.value), className: `${inputClass} border-gray-300` }) }), _jsx(Button, { type: "submit", variant: "primary", disabled: submitting, children: submitting ? t("login.submitting") : t("login.submit") })] }));
|
|
52
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { FieldDef } from "../../core/types.js";
|
|
2
|
+
export type MediaControlProps = {
|
|
3
|
+
field: FieldDef;
|
|
4
|
+
collectionSlug: string;
|
|
5
|
+
value: string;
|
|
6
|
+
error?: string | undefined;
|
|
7
|
+
disabled?: boolean | undefined;
|
|
8
|
+
onChange: (value: string) => void;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Upload control for `media` fields. Uploads to `/media?collection=&field=`,
|
|
12
|
+
* stores the returned URL, and can delete the underlying file on removal.
|
|
13
|
+
*/
|
|
14
|
+
export declare const MediaControl: ({ field, collectionSlug, value, error, disabled, onChange, }: MediaControlProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useRef, useState } from "react";
|
|
4
|
+
import { acceptAttribute, isImageUrl } from "../../core/media.js";
|
|
5
|
+
import { Box } from "../box.js";
|
|
6
|
+
import { Button } from "../button.js";
|
|
7
|
+
import { FieldWrapper } from "../field-wrapper.js";
|
|
8
|
+
import { useCmsUrls } from "./runtime.js";
|
|
9
|
+
import { Text } from "../text.js";
|
|
10
|
+
import { useI18n } from "./i18n.js";
|
|
11
|
+
function fileName(url) {
|
|
12
|
+
return url.split("/").pop() ?? url;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Upload control for `media` fields. Uploads to `/media?collection=&field=`,
|
|
16
|
+
* stores the returned URL, and can delete the underlying file on removal.
|
|
17
|
+
*/
|
|
18
|
+
export const MediaControl = ({ field, collectionSlug, value, error, disabled, onChange, }) => {
|
|
19
|
+
const { media } = useCmsUrls();
|
|
20
|
+
const { t } = useI18n();
|
|
21
|
+
const inputRef = useRef(null);
|
|
22
|
+
const [uploading, setUploading] = useState(false);
|
|
23
|
+
const [localError, setLocalError] = useState(null);
|
|
24
|
+
const upload = async (file) => {
|
|
25
|
+
setUploading(true);
|
|
26
|
+
setLocalError(null);
|
|
27
|
+
try {
|
|
28
|
+
const params = new URLSearchParams({
|
|
29
|
+
collection: collectionSlug,
|
|
30
|
+
field: field.name,
|
|
31
|
+
});
|
|
32
|
+
const form = new FormData();
|
|
33
|
+
form.append("file", file);
|
|
34
|
+
const response = await fetch(media(params.toString()), {
|
|
35
|
+
method: "POST",
|
|
36
|
+
body: form,
|
|
37
|
+
});
|
|
38
|
+
const json = (await response.json().catch(() => null));
|
|
39
|
+
if (!response.ok || !json?.media?.url) {
|
|
40
|
+
setLocalError(json?.message ?? t("media.uploadFailed"));
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
onChange(json.media.url);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
setLocalError(t("media.networkUpload"));
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
setUploading(false);
|
|
50
|
+
if (inputRef.current)
|
|
51
|
+
inputRef.current.value = "";
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const remove = async () => {
|
|
55
|
+
setLocalError(null);
|
|
56
|
+
if (value.startsWith("/files/")) {
|
|
57
|
+
try {
|
|
58
|
+
await fetch(media(`url=${encodeURIComponent(value)}`), {
|
|
59
|
+
method: "DELETE",
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Ignore: the folder can be cleaned manually.
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
onChange("");
|
|
67
|
+
};
|
|
68
|
+
const busy = disabled || uploading;
|
|
69
|
+
return (_jsx(FieldWrapper, { label: field.label, required: field.required, help: field.help, error: error ?? localError ?? undefined, children: _jsxs(Box, { className: "flex-col gap-2", children: [value ? (_jsxs(Box, { className: "flex-col gap-2 rounded-md border border-gray-200 bg-gray-50 p-3", children: [isImageUrl(value) ? (_jsx("img", { src: value, alt: "", className: "h-32 w-full rounded-md border border-gray-200 bg-white object-contain" })) : null, _jsxs(Box, { className: "items-center justify-between gap-2", children: [_jsx("a", { href: value, target: "_blank", rel: "noreferrer", className: "truncate text-sm text-gray-600 underline hover:text-gray-900", children: fileName(value) }), _jsx(Button, { type: "button", variant: "ghost", size: "sm", className: "text-red-600 hover:bg-red-50 hover:text-red-700", disabled: busy, onClick: () => void remove(), children: "Remover" })] })] })) : (_jsx(Text, { size: "caption", className: "text-gray-400", children: t("media.noFile") })), _jsxs(Box, { className: "items-center gap-2", children: [_jsx(Button, { type: "button", variant: "secondary", size: "sm", disabled: busy, onClick: () => inputRef.current?.click(), children: uploading ? t("media.uploading") : value ? t("media.replace") : t("media.upload") }), _jsx("input", { ref: inputRef, type: "file", accept: acceptAttribute(field), disabled: busy, className: "hidden", onChange: (event) => {
|
|
70
|
+
const file = event.target.files?.[0];
|
|
71
|
+
if (file)
|
|
72
|
+
void upload(file);
|
|
73
|
+
} })] })] }) }));
|
|
74
|
+
};
|