@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,200 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
|
|
5
|
+
import { collectionLocales, emptyValues, isLocalizedField } from "../../core/schema.js";
|
|
6
|
+
import { localeLabel } from "./locales.js";
|
|
7
|
+
import type {
|
|
8
|
+
CollectionDef,
|
|
9
|
+
FieldDef,
|
|
10
|
+
FieldValue,
|
|
11
|
+
Item,
|
|
12
|
+
LocalizedValue,
|
|
13
|
+
RelationOptionsMap,
|
|
14
|
+
} from "../../core/types.js";
|
|
15
|
+
import { Box } from "../box.js";
|
|
16
|
+
import { Button } from "../button.js";
|
|
17
|
+
import { Text } from "../text.js";
|
|
18
|
+
import { FieldControl } from "./field-control.js";
|
|
19
|
+
import { useI18n } from "./i18n.js";
|
|
20
|
+
|
|
21
|
+
export type ItemFormProps = {
|
|
22
|
+
collection: CollectionDef;
|
|
23
|
+
item: Item | null;
|
|
24
|
+
submitting: boolean;
|
|
25
|
+
errors: Record<string, string>;
|
|
26
|
+
formError: string | null;
|
|
27
|
+
relationOptions: RelationOptionsMap;
|
|
28
|
+
onSubmit: (values: Record<string, FieldValue>) => void;
|
|
29
|
+
onCancel?: () => void;
|
|
30
|
+
/** Hide the cancel button (e.g. standalone records, where there is no list to return to). */
|
|
31
|
+
hideCancel?: boolean;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function initialValues(
|
|
35
|
+
collection: CollectionDef,
|
|
36
|
+
item: Item | null,
|
|
37
|
+
): Record<string, FieldValue> {
|
|
38
|
+
const base = emptyValues(collection);
|
|
39
|
+
if (!item) return base;
|
|
40
|
+
for (const field of collection.fields) {
|
|
41
|
+
base[field.name] = item[field.name] ?? base[field.name] ?? null;
|
|
42
|
+
}
|
|
43
|
+
return base;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function asLocalized(value: FieldValue | undefined): LocalizedValue {
|
|
47
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
48
|
+
? (value as LocalizedValue)
|
|
49
|
+
: {};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Create/edit form rendered inside the drawer.
|
|
54
|
+
*
|
|
55
|
+
* Localized fields are shown in one tab per locale; language-independent fields
|
|
56
|
+
* live in a "Geral" section below the tabs.
|
|
57
|
+
*/
|
|
58
|
+
export const ItemForm = ({
|
|
59
|
+
collection,
|
|
60
|
+
item,
|
|
61
|
+
submitting,
|
|
62
|
+
errors,
|
|
63
|
+
formError,
|
|
64
|
+
relationOptions,
|
|
65
|
+
onSubmit,
|
|
66
|
+
onCancel,
|
|
67
|
+
hideCancel,
|
|
68
|
+
}: ItemFormProps) => {
|
|
69
|
+
const { t } = useI18n();
|
|
70
|
+
const locales = collectionLocales(collection);
|
|
71
|
+
const localizedFields = collection.fields.filter((field) =>
|
|
72
|
+
isLocalizedField(collection, field),
|
|
73
|
+
);
|
|
74
|
+
const sharedFields = collection.fields.filter(
|
|
75
|
+
(field) => !isLocalizedField(collection, field),
|
|
76
|
+
);
|
|
77
|
+
const [values, setValues] = useState<Record<string, FieldValue>>(() =>
|
|
78
|
+
initialValues(collection, item),
|
|
79
|
+
);
|
|
80
|
+
const [locale, setLocale] = useState(locales[0] ?? "");
|
|
81
|
+
|
|
82
|
+
const update = (name: string, value: FieldValue) => {
|
|
83
|
+
setValues((previous) => ({ ...previous, [name]: value }));
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const updateLocalized = (
|
|
87
|
+
field: FieldDef,
|
|
88
|
+
loc: string,
|
|
89
|
+
value: FieldValue,
|
|
90
|
+
) => {
|
|
91
|
+
setValues((previous) => ({
|
|
92
|
+
...previous,
|
|
93
|
+
[field.name]: {
|
|
94
|
+
...asLocalized(previous[field.name]),
|
|
95
|
+
[loc]: value,
|
|
96
|
+
},
|
|
97
|
+
}));
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const renderField = (field: FieldDef, value: FieldValue, error?: string, onChange?: (v: FieldValue) => void) => (
|
|
101
|
+
<FieldControl
|
|
102
|
+
key={field.name}
|
|
103
|
+
field={field}
|
|
104
|
+
collectionSlug={collection.slug}
|
|
105
|
+
value={value ?? null}
|
|
106
|
+
error={error}
|
|
107
|
+
disabled={submitting}
|
|
108
|
+
relationOptions={relationOptions[field.name] ?? []}
|
|
109
|
+
onChange={onChange ?? ((next) => update(field.name, next))}
|
|
110
|
+
/>
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<Box
|
|
115
|
+
as="form"
|
|
116
|
+
className="flex-col gap-5"
|
|
117
|
+
onSubmit={(event) => {
|
|
118
|
+
event.preventDefault();
|
|
119
|
+
onSubmit(values);
|
|
120
|
+
}}
|
|
121
|
+
>
|
|
122
|
+
{formError ? (
|
|
123
|
+
<Box className="rounded-md border border-red-200 bg-red-50 px-3 py-2">
|
|
124
|
+
<Text size="caption" className="text-red-700">
|
|
125
|
+
{formError}
|
|
126
|
+
</Text>
|
|
127
|
+
</Box>
|
|
128
|
+
) : null}
|
|
129
|
+
|
|
130
|
+
{localizedFields.length > 0 ? (
|
|
131
|
+
<Box className="flex-col gap-5">
|
|
132
|
+
<Box className="flex-wrap items-center gap-1 rounded-lg bg-gray-100 p-1">
|
|
133
|
+
{locales.map((code) => {
|
|
134
|
+
const active = code === locale;
|
|
135
|
+
return (
|
|
136
|
+
<button
|
|
137
|
+
key={code}
|
|
138
|
+
type="button"
|
|
139
|
+
onClick={() => setLocale(code)}
|
|
140
|
+
className={`rounded-md px-3 py-1.5 text-sm font-medium transition-colors ${
|
|
141
|
+
active
|
|
142
|
+
? "bg-white text-gray-900 shadow-sm"
|
|
143
|
+
: "text-gray-500 hover:text-gray-900"
|
|
144
|
+
}`}
|
|
145
|
+
>
|
|
146
|
+
{localeLabel(code)}
|
|
147
|
+
</button>
|
|
148
|
+
);
|
|
149
|
+
})}
|
|
150
|
+
</Box>
|
|
151
|
+
|
|
152
|
+
{localizedFields.map((field) =>
|
|
153
|
+
renderField(
|
|
154
|
+
field,
|
|
155
|
+
asLocalized(values[field.name])[locale] ?? null,
|
|
156
|
+
errors[`${field.name}_${locale}`],
|
|
157
|
+
(value) => updateLocalized(field, locale, value),
|
|
158
|
+
),
|
|
159
|
+
)}
|
|
160
|
+
</Box>
|
|
161
|
+
) : null}
|
|
162
|
+
|
|
163
|
+
{sharedFields.length > 0 ? (
|
|
164
|
+
<Box className="flex-col gap-5 border-t border-gray-200 pt-5">
|
|
165
|
+
{localizedFields.length > 0 ? (
|
|
166
|
+
<Text
|
|
167
|
+
size="caption"
|
|
168
|
+
className="font-semibold uppercase tracking-wide text-gray-400"
|
|
169
|
+
>
|
|
170
|
+
{t("form.general")}
|
|
171
|
+
</Text>
|
|
172
|
+
) : null}
|
|
173
|
+
{sharedFields.map((field) =>
|
|
174
|
+
renderField(
|
|
175
|
+
field,
|
|
176
|
+
values[field.name] ?? null,
|
|
177
|
+
errors[field.name],
|
|
178
|
+
),
|
|
179
|
+
)}
|
|
180
|
+
</Box>
|
|
181
|
+
) : null}
|
|
182
|
+
|
|
183
|
+
<Box className="items-center justify-end gap-2 border-t border-gray-200 pt-4">
|
|
184
|
+
{hideCancel ? null : (
|
|
185
|
+
<Button
|
|
186
|
+
type="button"
|
|
187
|
+
variant="secondary"
|
|
188
|
+
onClick={onCancel}
|
|
189
|
+
disabled={submitting}
|
|
190
|
+
>
|
|
191
|
+
{t("common.cancel")}
|
|
192
|
+
</Button>
|
|
193
|
+
)}
|
|
194
|
+
<Button type="submit" variant="primary" disabled={submitting}>
|
|
195
|
+
{submitting ? t("common.saving") : item ? t("common.saveChanges") : t("form.create")}
|
|
196
|
+
</Button>
|
|
197
|
+
</Box>
|
|
198
|
+
</Box>
|
|
199
|
+
);
|
|
200
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
FiArrowDown,
|
|
5
|
+
FiArrowUp,
|
|
6
|
+
FiPlus,
|
|
7
|
+
FiTrash2,
|
|
8
|
+
} from "react-icons/fi";
|
|
9
|
+
|
|
10
|
+
import type { FieldDef } from "../../core/types.js";
|
|
11
|
+
import { Box } from "../box.js";
|
|
12
|
+
import { Button, IconButton } from "../button.js";
|
|
13
|
+
import { FieldWrapper, inputClass } from "../field-wrapper.js";
|
|
14
|
+
import { Text } from "../text.js";
|
|
15
|
+
import { useI18n } from "./i18n.js";
|
|
16
|
+
|
|
17
|
+
export type ListControlProps = {
|
|
18
|
+
field: FieldDef;
|
|
19
|
+
value: string[];
|
|
20
|
+
error?: string | undefined;
|
|
21
|
+
disabled?: boolean | undefined;
|
|
22
|
+
onChange: (value: string[]) => void;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Native `list` widget: an ordered list of short text values (add, edit,
|
|
27
|
+
* reorder, remove).
|
|
28
|
+
*/
|
|
29
|
+
export const ListControl = ({
|
|
30
|
+
field,
|
|
31
|
+
value,
|
|
32
|
+
error,
|
|
33
|
+
disabled,
|
|
34
|
+
onChange,
|
|
35
|
+
}: ListControlProps) => {
|
|
36
|
+
const { t } = useI18n();
|
|
37
|
+
const update = (index: number, next: string) =>
|
|
38
|
+
onChange(value.map((item, i) => (i === index ? next : item)));
|
|
39
|
+
|
|
40
|
+
const remove = (index: number) =>
|
|
41
|
+
onChange(value.filter((_, i) => i !== index));
|
|
42
|
+
|
|
43
|
+
const move = (index: number, direction: -1 | 1) => {
|
|
44
|
+
const target = index + direction;
|
|
45
|
+
if (target < 0 || target >= value.length) return;
|
|
46
|
+
const next = [...value];
|
|
47
|
+
const current = next[index];
|
|
48
|
+
const swap = next[target];
|
|
49
|
+
if (current === undefined || swap === undefined) return;
|
|
50
|
+
next[index] = swap;
|
|
51
|
+
next[target] = current;
|
|
52
|
+
onChange(next);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<FieldWrapper
|
|
57
|
+
label={field.label}
|
|
58
|
+
required={field.required}
|
|
59
|
+
help={field.help}
|
|
60
|
+
error={error}
|
|
61
|
+
>
|
|
62
|
+
<Box className="flex-col gap-2">
|
|
63
|
+
{value.length === 0 ? (
|
|
64
|
+
<Text size="caption" className="text-gray-400">
|
|
65
|
+
{t("list.empty")}
|
|
66
|
+
</Text>
|
|
67
|
+
) : null}
|
|
68
|
+
|
|
69
|
+
{value.map((item, index) => (
|
|
70
|
+
<Box key={index} className="items-center gap-1.5">
|
|
71
|
+
<Box className="min-w-0 flex-1">
|
|
72
|
+
<input
|
|
73
|
+
value={item}
|
|
74
|
+
disabled={disabled}
|
|
75
|
+
placeholder={t("list.item", { index: index + 1 })}
|
|
76
|
+
onChange={(event) => update(index, event.target.value)}
|
|
77
|
+
className={`${inputClass} border-gray-300`}
|
|
78
|
+
/>
|
|
79
|
+
</Box>
|
|
80
|
+
<IconButton
|
|
81
|
+
type="button"
|
|
82
|
+
label={t("list.moveUp")}
|
|
83
|
+
disabled={disabled || index === 0}
|
|
84
|
+
onClick={() => move(index, -1)}
|
|
85
|
+
>
|
|
86
|
+
<FiArrowUp aria-hidden />
|
|
87
|
+
</IconButton>
|
|
88
|
+
<IconButton
|
|
89
|
+
type="button"
|
|
90
|
+
label={t("list.moveDown")}
|
|
91
|
+
disabled={disabled || index === value.length - 1}
|
|
92
|
+
onClick={() => move(index, 1)}
|
|
93
|
+
>
|
|
94
|
+
<FiArrowDown aria-hidden />
|
|
95
|
+
</IconButton>
|
|
96
|
+
<IconButton
|
|
97
|
+
type="button"
|
|
98
|
+
label={t("list.remove")}
|
|
99
|
+
className="text-red-600 hover:bg-red-50 hover:text-red-700"
|
|
100
|
+
disabled={disabled}
|
|
101
|
+
onClick={() => remove(index)}
|
|
102
|
+
>
|
|
103
|
+
<FiTrash2 aria-hidden />
|
|
104
|
+
</IconButton>
|
|
105
|
+
</Box>
|
|
106
|
+
))}
|
|
107
|
+
|
|
108
|
+
<Button
|
|
109
|
+
type="button"
|
|
110
|
+
variant="secondary"
|
|
111
|
+
size="sm"
|
|
112
|
+
disabled={disabled}
|
|
113
|
+
onClick={() => onChange([...value, ""])}
|
|
114
|
+
>
|
|
115
|
+
<FiPlus aria-hidden className="mr-1.5" />
|
|
116
|
+
{t("list.add")}
|
|
117
|
+
</Button>
|
|
118
|
+
</Box>
|
|
119
|
+
</FieldWrapper>
|
|
120
|
+
);
|
|
121
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { LocaleDef } from "../../config.js";
|
|
2
|
+
|
|
3
|
+
let labels = new Map<string, string>();
|
|
4
|
+
|
|
5
|
+
/** Injected once by the admin SPA bootstrap. */
|
|
6
|
+
export function setLocaleLabels(locales: LocaleDef[]): void {
|
|
7
|
+
labels = new Map(locales.map((locale) => [locale.code, locale.label]));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function localeLabel(code: string): string {
|
|
11
|
+
return labels.get(code) ?? code.toUpperCase();
|
|
12
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
|
|
5
|
+
import { Box } from "../box.js";
|
|
6
|
+
import { Button } from "../button.js";
|
|
7
|
+
import { FieldWrapper, inputClass } from "../field-wrapper.js";
|
|
8
|
+
import { Text } from "../text.js";
|
|
9
|
+
import { useCmsUrls } from "./runtime.js";
|
|
10
|
+
import { useI18n } from "./i18n.js";
|
|
11
|
+
|
|
12
|
+
function nextTarget(): string {
|
|
13
|
+
if (typeof window === "undefined") return "/admin";
|
|
14
|
+
const next = new URLSearchParams(window.location.search).get("next");
|
|
15
|
+
// Only allow internal paths.
|
|
16
|
+
return next && next.startsWith("/") && !next.startsWith("//") ? next : "/admin";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const LoginForm = ({ onSuccess }: { onSuccess?: () => void }) => {
|
|
20
|
+
const { auth } = useCmsUrls();
|
|
21
|
+
const { t } = useI18n();
|
|
22
|
+
const [email, setEmail] = useState("");
|
|
23
|
+
const [password, setPassword] = useState("");
|
|
24
|
+
const [error, setError] = useState<string | null>(null);
|
|
25
|
+
const [submitting, setSubmitting] = useState(false);
|
|
26
|
+
|
|
27
|
+
const submit = async (event: React.FormEvent) => {
|
|
28
|
+
event.preventDefault();
|
|
29
|
+
setSubmitting(true);
|
|
30
|
+
setError(null);
|
|
31
|
+
try {
|
|
32
|
+
const response = await fetch(auth("login"), {
|
|
33
|
+
method: "POST",
|
|
34
|
+
headers: { "Content-Type": "application/json" },
|
|
35
|
+
body: JSON.stringify({ email, password }),
|
|
36
|
+
});
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
const json = (await response.json().catch(() => null)) as
|
|
39
|
+
| { message?: string }
|
|
40
|
+
| null;
|
|
41
|
+
setError(json?.message ?? t("login.error"));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (onSuccess) onSuccess();
|
|
45
|
+
else window.location.assign(nextTarget());
|
|
46
|
+
} catch {
|
|
47
|
+
setError(t("login.network"));
|
|
48
|
+
} finally {
|
|
49
|
+
setSubmitting(false);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<Box
|
|
55
|
+
as="form"
|
|
56
|
+
className="flex-col gap-4"
|
|
57
|
+
onSubmit={(event) => void submit(event)}
|
|
58
|
+
>
|
|
59
|
+
{error ? (
|
|
60
|
+
<Box className="rounded-md border border-red-200 bg-red-50 px-3 py-2">
|
|
61
|
+
<Text size="caption" className="text-red-700">
|
|
62
|
+
{error}
|
|
63
|
+
</Text>
|
|
64
|
+
</Box>
|
|
65
|
+
) : null}
|
|
66
|
+
|
|
67
|
+
<FieldWrapper label={t("common.email")} htmlFor="login-email" required>
|
|
68
|
+
<input
|
|
69
|
+
id="login-email"
|
|
70
|
+
type="email"
|
|
71
|
+
autoComplete="username"
|
|
72
|
+
required
|
|
73
|
+
value={email}
|
|
74
|
+
disabled={submitting}
|
|
75
|
+
onChange={(event) => setEmail(event.target.value)}
|
|
76
|
+
className={`${inputClass} border-gray-300`}
|
|
77
|
+
/>
|
|
78
|
+
</FieldWrapper>
|
|
79
|
+
|
|
80
|
+
<FieldWrapper label={t("common.password")} htmlFor="login-password" required>
|
|
81
|
+
<input
|
|
82
|
+
id="login-password"
|
|
83
|
+
type="password"
|
|
84
|
+
autoComplete="current-password"
|
|
85
|
+
required
|
|
86
|
+
value={password}
|
|
87
|
+
disabled={submitting}
|
|
88
|
+
onChange={(event) => setPassword(event.target.value)}
|
|
89
|
+
className={`${inputClass} border-gray-300`}
|
|
90
|
+
/>
|
|
91
|
+
</FieldWrapper>
|
|
92
|
+
|
|
93
|
+
<Button type="submit" variant="primary" disabled={submitting}>
|
|
94
|
+
{submitting ? t("login.submitting") : t("login.submit")}
|
|
95
|
+
</Button>
|
|
96
|
+
</Box>
|
|
97
|
+
);
|
|
98
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useRef, useState } from "react";
|
|
4
|
+
|
|
5
|
+
import { acceptAttribute, isImageUrl } from "../../core/media.js";
|
|
6
|
+
import type { FieldDef } from "../../core/types.js";
|
|
7
|
+
import { Box } from "../box.js";
|
|
8
|
+
import { Button } from "../button.js";
|
|
9
|
+
import { FieldWrapper } from "../field-wrapper.js";
|
|
10
|
+
import { useCmsUrls } from "./runtime.js";
|
|
11
|
+
import { Text } from "../text.js";
|
|
12
|
+
import { useI18n } from "./i18n.js";
|
|
13
|
+
|
|
14
|
+
export type MediaControlProps = {
|
|
15
|
+
field: FieldDef;
|
|
16
|
+
collectionSlug: string;
|
|
17
|
+
value: string;
|
|
18
|
+
error?: string | undefined;
|
|
19
|
+
disabled?: boolean | undefined;
|
|
20
|
+
onChange: (value: string) => void;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function fileName(url: string): string {
|
|
24
|
+
return url.split("/").pop() ?? url;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Upload control for `media` fields. Uploads to `/media?collection=&field=`,
|
|
29
|
+
* stores the returned URL, and can delete the underlying file on removal.
|
|
30
|
+
*/
|
|
31
|
+
export const MediaControl = ({
|
|
32
|
+
field,
|
|
33
|
+
collectionSlug,
|
|
34
|
+
value,
|
|
35
|
+
error,
|
|
36
|
+
disabled,
|
|
37
|
+
onChange,
|
|
38
|
+
}: MediaControlProps) => {
|
|
39
|
+
const { media } = useCmsUrls();
|
|
40
|
+
const { t } = useI18n();
|
|
41
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
42
|
+
const [uploading, setUploading] = useState(false);
|
|
43
|
+
const [localError, setLocalError] = useState<string | null>(null);
|
|
44
|
+
|
|
45
|
+
const upload = async (file: File) => {
|
|
46
|
+
setUploading(true);
|
|
47
|
+
setLocalError(null);
|
|
48
|
+
try {
|
|
49
|
+
const params = new URLSearchParams({
|
|
50
|
+
collection: collectionSlug,
|
|
51
|
+
field: field.name,
|
|
52
|
+
});
|
|
53
|
+
const form = new FormData();
|
|
54
|
+
form.append("file", file);
|
|
55
|
+
const response = await fetch(media(params.toString()), {
|
|
56
|
+
method: "POST",
|
|
57
|
+
body: form,
|
|
58
|
+
});
|
|
59
|
+
const json = (await response.json().catch(() => null)) as
|
|
60
|
+
| { media?: { url?: string }; message?: string }
|
|
61
|
+
| null;
|
|
62
|
+
if (!response.ok || !json?.media?.url) {
|
|
63
|
+
setLocalError(json?.message ?? t("media.uploadFailed"));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
onChange(json.media.url);
|
|
67
|
+
} catch {
|
|
68
|
+
setLocalError(t("media.networkUpload"));
|
|
69
|
+
} finally {
|
|
70
|
+
setUploading(false);
|
|
71
|
+
if (inputRef.current) inputRef.current.value = "";
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const remove = async () => {
|
|
76
|
+
setLocalError(null);
|
|
77
|
+
if (value.startsWith("/files/")) {
|
|
78
|
+
try {
|
|
79
|
+
await fetch(media(`url=${encodeURIComponent(value)}`), {
|
|
80
|
+
method: "DELETE",
|
|
81
|
+
});
|
|
82
|
+
} catch {
|
|
83
|
+
// Ignore: the folder can be cleaned manually.
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
onChange("");
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const busy = disabled || uploading;
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<FieldWrapper
|
|
93
|
+
label={field.label}
|
|
94
|
+
required={field.required}
|
|
95
|
+
help={field.help}
|
|
96
|
+
error={error ?? localError ?? undefined}
|
|
97
|
+
>
|
|
98
|
+
<Box className="flex-col gap-2">
|
|
99
|
+
{value ? (
|
|
100
|
+
<Box className="flex-col gap-2 rounded-md border border-gray-200 bg-gray-50 p-3">
|
|
101
|
+
{isImageUrl(value) ? (
|
|
102
|
+
// eslint-disable-next-line @next/next/no-img-element
|
|
103
|
+
<img
|
|
104
|
+
src={value}
|
|
105
|
+
alt=""
|
|
106
|
+
className="h-32 w-full rounded-md border border-gray-200 bg-white object-contain"
|
|
107
|
+
/>
|
|
108
|
+
) : null}
|
|
109
|
+
<Box className="items-center justify-between gap-2">
|
|
110
|
+
<a
|
|
111
|
+
href={value}
|
|
112
|
+
target="_blank"
|
|
113
|
+
rel="noreferrer"
|
|
114
|
+
className="truncate text-sm text-gray-600 underline hover:text-gray-900"
|
|
115
|
+
>
|
|
116
|
+
{fileName(value)}
|
|
117
|
+
</a>
|
|
118
|
+
<Button
|
|
119
|
+
type="button"
|
|
120
|
+
variant="ghost"
|
|
121
|
+
size="sm"
|
|
122
|
+
className="text-red-600 hover:bg-red-50 hover:text-red-700"
|
|
123
|
+
disabled={busy}
|
|
124
|
+
onClick={() => void remove()}
|
|
125
|
+
>
|
|
126
|
+
Remover
|
|
127
|
+
</Button>
|
|
128
|
+
</Box>
|
|
129
|
+
</Box>
|
|
130
|
+
) : (
|
|
131
|
+
<Text size="caption" className="text-gray-400">
|
|
132
|
+
{t("media.noFile")}
|
|
133
|
+
</Text>
|
|
134
|
+
)}
|
|
135
|
+
|
|
136
|
+
<Box className="items-center gap-2">
|
|
137
|
+
<Button
|
|
138
|
+
type="button"
|
|
139
|
+
variant="secondary"
|
|
140
|
+
size="sm"
|
|
141
|
+
disabled={busy}
|
|
142
|
+
onClick={() => inputRef.current?.click()}
|
|
143
|
+
>
|
|
144
|
+
{uploading ? t("media.uploading") : value ? t("media.replace") : t("media.upload")}
|
|
145
|
+
</Button>
|
|
146
|
+
<input
|
|
147
|
+
ref={inputRef}
|
|
148
|
+
type="file"
|
|
149
|
+
accept={acceptAttribute(field)}
|
|
150
|
+
disabled={busy}
|
|
151
|
+
className="hidden"
|
|
152
|
+
onChange={(event) => {
|
|
153
|
+
const file = event.target.files?.[0];
|
|
154
|
+
if (file) void upload(file);
|
|
155
|
+
}}
|
|
156
|
+
/>
|
|
157
|
+
</Box>
|
|
158
|
+
</Box>
|
|
159
|
+
</FieldWrapper>
|
|
160
|
+
);
|
|
161
|
+
};
|