opacacms 0.1.1 → 0.1.2

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.
Files changed (212) hide show
  1. package/package.json +36 -1
  2. package/bun.lock +0 -34
  3. package/global.d.ts +0 -11
  4. package/src/admin/api-client.ts +0 -63
  5. package/src/admin/auth-client.ts +0 -40
  6. package/src/admin/custom-field.ts +0 -179
  7. package/src/admin/index.ts +0 -15
  8. package/src/admin/react.tsx +0 -72
  9. package/src/admin/router.ts +0 -9
  10. package/src/admin/stores/admin-queries.ts +0 -121
  11. package/src/admin/stores/auth.ts +0 -61
  12. package/src/admin/stores/column-visibility.ts +0 -67
  13. package/src/admin/stores/config.ts +0 -15
  14. package/src/admin/stores/media.ts +0 -95
  15. package/src/admin/stores/query.ts +0 -13
  16. package/src/admin/stores/ui.ts +0 -29
  17. package/src/admin/ui/admin-client.tsx +0 -283
  18. package/src/admin/ui/admin-layout.tsx +0 -276
  19. package/src/admin/ui/components/ColumnVisibilityToggle.tsx +0 -141
  20. package/src/admin/ui/components/DataDetailSheet.tsx +0 -141
  21. package/src/admin/ui/components/DataDetailView.tsx +0 -175
  22. package/src/admin/ui/components/Table.tsx +0 -67
  23. package/src/admin/ui/components/fields/ArrayField.tsx +0 -166
  24. package/src/admin/ui/components/fields/BlocksField.tsx +0 -202
  25. package/src/admin/ui/components/fields/BooleanField.tsx +0 -50
  26. package/src/admin/ui/components/fields/CollapsibleField.tsx +0 -75
  27. package/src/admin/ui/components/fields/DateField.tsx +0 -45
  28. package/src/admin/ui/components/fields/FileField.tsx +0 -322
  29. package/src/admin/ui/components/fields/GroupField.tsx +0 -50
  30. package/src/admin/ui/components/fields/JoinField.tsx +0 -23
  31. package/src/admin/ui/components/fields/NumberField.tsx +0 -46
  32. package/src/admin/ui/components/fields/RadioField.tsx +0 -62
  33. package/src/admin/ui/components/fields/RelationshipField.tsx +0 -278
  34. package/src/admin/ui/components/fields/RowField.tsx +0 -40
  35. package/src/admin/ui/components/fields/SelectField.tsx +0 -59
  36. package/src/admin/ui/components/fields/TabsField.tsx +0 -101
  37. package/src/admin/ui/components/fields/TextAreaField.tsx +0 -54
  38. package/src/admin/ui/components/fields/TextField.tsx +0 -49
  39. package/src/admin/ui/components/fields/VirtualField.tsx +0 -53
  40. package/src/admin/ui/components/fields/index.tsx +0 -371
  41. package/src/admin/ui/components/fields/richtext-editor/index.tsx +0 -211
  42. package/src/admin/ui/components/fields/richtext-editor/nodes/ImageComponent.tsx +0 -142
  43. package/src/admin/ui/components/fields/richtext-editor/nodes/ImageNode.tsx +0 -95
  44. package/src/admin/ui/components/fields/richtext-editor/plugins/ComponentPickerPlugin.tsx +0 -226
  45. package/src/admin/ui/components/fields/richtext-editor/plugins/EditableSyncPlugin.tsx +0 -16
  46. package/src/admin/ui/components/fields/richtext-editor/plugins/NotionToolbarPlugin.tsx +0 -184
  47. package/src/admin/ui/components/fields/richtext-editor/plugins/SimpleToolbarPlugin.tsx +0 -240
  48. package/src/admin/ui/components/fields/richtext-editor/plugins/ValueSyncPlugin.tsx +0 -40
  49. package/src/admin/ui/components/fields/utils.ts +0 -1
  50. package/src/admin/ui/components/link.tsx +0 -41
  51. package/src/admin/ui/components/media/AssetManagerModal.tsx +0 -334
  52. package/src/admin/ui/components/toast.tsx +0 -72
  53. package/src/admin/ui/components/ui/accordion.tsx +0 -51
  54. package/src/admin/ui/components/ui/alert-dialog.tsx +0 -98
  55. package/src/admin/ui/components/ui/blocks.tsx +0 -32
  56. package/src/admin/ui/components/ui/breadcrumbs.tsx +0 -59
  57. package/src/admin/ui/components/ui/button.tsx +0 -26
  58. package/src/admin/ui/components/ui/collapsible.tsx +0 -124
  59. package/src/admin/ui/components/ui/dialog.tsx +0 -79
  60. package/src/admin/ui/components/ui/group.tsx +0 -20
  61. package/src/admin/ui/components/ui/index.ts +0 -17
  62. package/src/admin/ui/components/ui/input.tsx +0 -12
  63. package/src/admin/ui/components/ui/join.tsx +0 -53
  64. package/src/admin/ui/components/ui/label.tsx +0 -11
  65. package/src/admin/ui/components/ui/radio-group.tsx +0 -75
  66. package/src/admin/ui/components/ui/relationship-detail-sheet.tsx +0 -122
  67. package/src/admin/ui/components/ui/relationship.tsx +0 -58
  68. package/src/admin/ui/components/ui/scroll-area.tsx +0 -19
  69. package/src/admin/ui/components/ui/select.tsx +0 -187
  70. package/src/admin/ui/components/ui/separator.tsx +0 -21
  71. package/src/admin/ui/components/ui/sheet.tsx +0 -106
  72. package/src/admin/ui/components/ui/tabs.tsx +0 -116
  73. package/src/admin/ui/components/ui/utils.ts +0 -3
  74. package/src/admin/ui/hooks/use-debounce.ts +0 -15
  75. package/src/admin/ui/styles/_locale-switcher.scss +0 -33
  76. package/src/admin/ui/styles/accordion.scss +0 -60
  77. package/src/admin/ui/styles/animations.scss +0 -41
  78. package/src/admin/ui/styles/asset-manager.scss +0 -547
  79. package/src/admin/ui/styles/badge.scss +0 -13
  80. package/src/admin/ui/styles/base.scss +0 -22
  81. package/src/admin/ui/styles/button.scss +0 -161
  82. package/src/admin/ui/styles/card.scss +0 -13
  83. package/src/admin/ui/styles/collapsible.scss +0 -75
  84. package/src/admin/ui/styles/data-detail.scss +0 -92
  85. package/src/admin/ui/styles/dialog.scss +0 -102
  86. package/src/admin/ui/styles/empty-state.scss +0 -22
  87. package/src/admin/ui/styles/group.scss +0 -19
  88. package/src/admin/ui/styles/index.scss +0 -33
  89. package/src/admin/ui/styles/input.scss +0 -80
  90. package/src/admin/ui/styles/label.scss +0 -12
  91. package/src/admin/ui/styles/layout.scss +0 -56
  92. package/src/admin/ui/styles/lexical.scss +0 -469
  93. package/src/admin/ui/styles/loading.scss +0 -102
  94. package/src/admin/ui/styles/media-registry.scss +0 -597
  95. package/src/admin/ui/styles/pagination.scss +0 -20
  96. package/src/admin/ui/styles/radio-group.scss +0 -66
  97. package/src/admin/ui/styles/row.scss +0 -17
  98. package/src/admin/ui/styles/scrollbar.scss +0 -36
  99. package/src/admin/ui/styles/select.scss +0 -121
  100. package/src/admin/ui/styles/separator.scss +0 -14
  101. package/src/admin/ui/styles/sheet.scss +0 -152
  102. package/src/admin/ui/styles/sidebar.scss +0 -148
  103. package/src/admin/ui/styles/switch.scss +0 -59
  104. package/src/admin/ui/styles/table.scss +0 -207
  105. package/src/admin/ui/styles/tabs.scss +0 -62
  106. package/src/admin/ui/styles/toast.scss +0 -45
  107. package/src/admin/ui/styles/variables.scss +0 -24
  108. package/src/admin/ui/views/collection-list-view.tsx +0 -720
  109. package/src/admin/ui/views/dashboard-view.tsx +0 -263
  110. package/src/admin/ui/views/document-edit-view.tsx +0 -384
  111. package/src/admin/ui/views/global-edit-view.tsx +0 -226
  112. package/src/admin/ui/views/init-view.tsx +0 -182
  113. package/src/admin/ui/views/login-view.tsx +0 -123
  114. package/src/admin/ui/views/media-registry-view.tsx +0 -1104
  115. package/src/admin/ui/views/settings-view.tsx +0 -729
  116. package/src/admin/webcomponent.tsx +0 -15
  117. package/src/auth/index.ts +0 -194
  118. package/src/auth/migrations.ts +0 -87
  119. package/src/auth/premissions.ts +0 -46
  120. package/src/cli/commands/generate-types.ts +0 -116
  121. package/src/cli/commands/init.ts +0 -95
  122. package/src/cli/commands/migrate-commands.ts +0 -160
  123. package/src/cli/commands/seed-command.ts +0 -11
  124. package/src/cli/d1-mock.ts +0 -101
  125. package/src/cli/index.test.ts +0 -84
  126. package/src/cli/index.ts +0 -183
  127. package/src/cli/r2-mock.ts +0 -217
  128. package/src/cli/seeding.ts +0 -409
  129. package/src/client.ts +0 -181
  130. package/src/config-utils.ts +0 -102
  131. package/src/config.ts +0 -49
  132. package/src/db/adapter.ts +0 -53
  133. package/src/db/better-sqlite.ts +0 -630
  134. package/src/db/bun-sqlite.ts +0 -646
  135. package/src/db/d1.ts +0 -711
  136. package/src/db/index.ts +0 -2
  137. package/src/db/kysely/data-mapper.ts +0 -142
  138. package/src/db/kysely/field-mapper.ts +0 -148
  139. package/src/db/kysely/migration-generator.ts +0 -223
  140. package/src/db/kysely/query-builder.ts +0 -92
  141. package/src/db/kysely/schema-builder.ts +0 -439
  142. package/src/db/kysely/sql-utils.ts +0 -13
  143. package/src/db/migration.ts +0 -40
  144. package/src/db/postgres.ts +0 -621
  145. package/src/db/sqlite.ts +0 -658
  146. package/src/db/system-schema.ts +0 -121
  147. package/src/index.ts +0 -11
  148. package/src/runtimes/README.md +0 -59
  149. package/src/runtimes/bun.ts +0 -49
  150. package/src/runtimes/cloudflare-workers.ts +0 -38
  151. package/src/runtimes/next.ts +0 -26
  152. package/src/runtimes/node.ts +0 -52
  153. package/src/schema/collection.ts +0 -184
  154. package/src/schema/fields/base.ts +0 -164
  155. package/src/schema/fields/index.ts +0 -427
  156. package/src/schema/global.ts +0 -145
  157. package/src/schema/index.ts +0 -4
  158. package/src/schema/infer.ts +0 -72
  159. package/src/server/admin-router.ts +0 -20
  160. package/src/server/admin.ts +0 -142
  161. package/src/server/assets.ts +0 -306
  162. package/src/server/collection-router.ts +0 -55
  163. package/src/server/handlers.ts +0 -722
  164. package/src/server/middlewares/admin.ts +0 -27
  165. package/src/server/middlewares/auth.ts +0 -89
  166. package/src/server/middlewares/context.ts +0 -17
  167. package/src/server/middlewares/cors.ts +0 -24
  168. package/src/server/middlewares/database-init.ts +0 -74
  169. package/src/server/middlewares/rate-limit.ts +0 -71
  170. package/src/server/router.ts +0 -47
  171. package/src/server/setup-middlewares.ts +0 -58
  172. package/src/server/system-router.ts +0 -35
  173. package/src/server.ts +0 -9
  174. package/src/storage/adapters/cloudflare-r2.ts +0 -136
  175. package/src/storage/adapters/local.ts +0 -146
  176. package/src/storage/adapters/s3.ts +0 -186
  177. package/src/storage/errors.ts +0 -46
  178. package/src/storage/index.ts +0 -6
  179. package/src/storage/types.ts +0 -39
  180. package/src/types.ts +0 -605
  181. package/src/utils/lexical.ts +0 -37
  182. package/src/utils/logger.ts +0 -73
  183. package/src/validation.ts +0 -429
  184. package/src/validator.ts +0 -179
  185. package/test/admin-custom-field.test.ts +0 -162
  186. package/test/admin-react-field.test.tsx +0 -134
  187. package/test/api-features.test.ts +0 -78
  188. package/test/api.test.ts +0 -178
  189. package/test/auth.test.ts +0 -62
  190. package/test/cli-integration.test.ts +0 -148
  191. package/test/cli.test.ts +0 -25
  192. package/test/db/postgres.test.ts +0 -95
  193. package/test/db/sqlite-filter.test.ts +0 -53
  194. package/test/db/sqlite.test.ts +0 -82
  195. package/test/engine-features.test.ts +0 -79
  196. package/test/globals.test.ts +0 -74
  197. package/test/integration-tmp/db-app/opacacms.config.ts +0 -15
  198. package/test/integration-tmp/my-sqlite-app/opacacms.config.ts +0 -25
  199. package/test/integration-tmp/my-test-app/index.ts +0 -8
  200. package/test/integration-tmp/my-test-app/opacacms.config.ts +0 -16
  201. package/test/integration-tmp/my-test-app/package.json +0 -12
  202. package/test/populate.test.ts +0 -79
  203. package/test/runtimes.test.ts +0 -43
  204. package/test/schema-builder.test.ts +0 -107
  205. package/test/schema-features.test.ts +0 -63
  206. package/test/seeding.test.ts +0 -68
  207. package/test/storage/local.test.ts +0 -72
  208. package/test/storage/s3.test.ts +0 -60
  209. package/test/structural-data.test.ts +0 -100
  210. package/test/test-setup.ts +0 -11
  211. package/test/validation.test.ts +0 -162
  212. package/tsconfig.json +0 -42
@@ -1,263 +0,0 @@
1
- import { Activity, ArrowUpRight, Database } from "lucide-react";
2
- import type { SerializableConfig } from "../../../types";
3
- import { Link } from "../components/link";
4
-
5
- export interface DashboardViewProps {
6
- config: SerializableConfig;
7
- user?: {
8
- name: string;
9
- email: string;
10
- image?: string | null;
11
- } | null;
12
- }
13
-
14
- export function DashboardView({ config, user }: DashboardViewProps) {
15
- const userCollections = config.collections.filter((c) => !c.slug.startsWith("_"));
16
-
17
- return (
18
- <div>
19
- <div
20
- className="opaca-header"
21
- style={{
22
- display: "flex",
23
- justifyContent: "space-between",
24
- alignItems: "center",
25
- marginBottom: "2rem",
26
- }}
27
- >
28
- <div>
29
- <h1 className="opaca-title" style={{ marginBottom: "0.25rem" }}>
30
- {config.appName || "OpacaCMS"}
31
- </h1>
32
- <p className="opaca-subtitle">Overview of your content management system</p>
33
- </div>
34
-
35
- {user && (
36
- <div
37
- style={{
38
- display: "flex",
39
- alignItems: "center",
40
- gap: "0.75rem",
41
- padding: "0.625rem 1rem",
42
- background: "rgba(255, 255, 255, 0.03)",
43
- borderRadius: "12px",
44
- border: "1px solid rgba(255, 255, 255, 0.08)",
45
- backdropFilter: "blur(10px)",
46
- boxShadow: "0 4px 12px rgba(0, 0, 0, 0.1)",
47
- }}
48
- >
49
- <div
50
- style={{
51
- width: "36px",
52
- height: "36px",
53
- borderRadius: "10px",
54
- background: "linear-gradient(135deg, var(--opaca-accent) 0%, #a78bfa 100%)",
55
- color: "white",
56
- display: "flex",
57
- alignItems: "center",
58
- justifyContent: "center",
59
- fontSize: "0.875rem",
60
- fontWeight: "600",
61
- boxShadow: "0 2px 8px rgba(124, 58, 237, 0.3)",
62
- }}
63
- >
64
- {user.name.charAt(0).toUpperCase()}
65
- </div>
66
- <div style={{ lineHeight: "1.3" }}>
67
- <div
68
- style={{
69
- fontSize: "0.875rem",
70
- fontWeight: "600",
71
- color: "var(--opaca-text)",
72
- }}
73
- >
74
- {user.name}
75
- </div>
76
- <div style={{ fontSize: "0.75rem", color: "var(--opaca-text-dim)" }}>
77
- {user.email}
78
- </div>
79
- </div>
80
- </div>
81
- )}
82
- </div>
83
-
84
- <div className="opaca-grid" style={{ marginBottom: "2.5rem" }}>
85
- <div className="opaca-card">
86
- <div
87
- style={{
88
- display: "flex",
89
- alignItems: "center",
90
- gap: "0.75rem",
91
- marginBottom: "0.75rem",
92
- }}
93
- >
94
- <div
95
- style={{
96
- padding: "0.5rem",
97
- background: "var(--opaca-primary-glow)",
98
- color: "var(--opaca-accent)",
99
- borderRadius: "var(--opaca-radius)",
100
- }}
101
- >
102
- <Database size={18} />
103
- </div>
104
- <div>
105
- <div
106
- style={{
107
- fontSize: "1.25rem",
108
- fontWeight: "600",
109
- lineHeight: "1.2",
110
- }}
111
- >
112
- {userCollections.length}
113
- </div>
114
- <div
115
- style={{
116
- fontSize: "0.6875rem",
117
- color: "var(--opaca-text-dim)",
118
- textTransform: "uppercase",
119
- letterSpacing: "0.05em",
120
- }}
121
- >
122
- Collections
123
- </div>
124
- </div>
125
- </div>
126
- <p style={{ fontSize: "0.75rem", color: "var(--opaca-text-muted)" }}>
127
- Schema defined by Drizzle and synced automatically.
128
- </p>
129
- </div>
130
-
131
- <div className="opaca-card">
132
- <div
133
- style={{
134
- display: "flex",
135
- alignItems: "center",
136
- gap: "0.75rem",
137
- marginBottom: "0.75rem",
138
- }}
139
- >
140
- <div
141
- style={{
142
- padding: "0.5rem",
143
- background: "rgba(52, 211, 153, 0.1)",
144
- color: "var(--opaca-success)",
145
- borderRadius: "var(--opaca-radius)",
146
- }}
147
- >
148
- <Activity size={18} />
149
- </div>
150
- <div>
151
- <div
152
- style={{
153
- fontSize: "1.25rem",
154
- fontWeight: "600",
155
- lineHeight: "1.2",
156
- }}
157
- >
158
- Online
159
- </div>
160
- <div
161
- style={{
162
- fontSize: "0.6875rem",
163
- color: "var(--opaca-text-dim)",
164
- textTransform: "uppercase",
165
- letterSpacing: "0.05em",
166
- }}
167
- >
168
- API Status
169
- </div>
170
- </div>
171
- </div>
172
- <p style={{ fontSize: "0.75rem", color: "var(--opaca-text-muted)" }}>
173
- Hono-powered API on {config.serverURL || "localhost"}.
174
- </p>
175
- </div>
176
- </div>
177
-
178
- <div
179
- style={{
180
- fontSize: "0.6875rem",
181
- fontWeight: "500",
182
- color: "var(--opaca-text-dim)",
183
- textTransform: "uppercase",
184
- letterSpacing: "0.05em",
185
- marginBottom: "0.75rem",
186
- }}
187
- >
188
- Collections
189
- </div>
190
- <div className="opaca-grid">
191
- {userCollections
192
- .filter((c) => !c.hidden)
193
- .sort((a, b) => (a.admin === b.admin ? 0 : a.admin ? -1 : 1))
194
- .map((col: any) => {
195
- // Format label: prioritize col.label, otherwise strip leading underscores and capitalize each word
196
- const colLabel =
197
- col.label ||
198
- col.slug
199
- .replace(/^_+/, "")
200
- .replace(/_/g, " ")
201
- .replace(/\b\w/g, (c: string) => c.toUpperCase());
202
- return (
203
- <Link
204
- key={col.slug}
205
- href={`/admin/collections/${col.slug}`}
206
- className="opaca-card"
207
- style={{
208
- textDecoration: "none",
209
- color: "inherit",
210
- cursor: "pointer",
211
- }}
212
- >
213
- <div
214
- style={{
215
- display: "flex",
216
- alignItems: "center",
217
- justifyContent: "space-between",
218
- marginBottom: "0.25rem",
219
- }}
220
- >
221
- <div
222
- style={{
223
- display: "flex",
224
- alignItems: "center",
225
- gap: "0.5rem",
226
- }}
227
- >
228
- <span style={{ fontWeight: "500", fontSize: "0.875rem" }}>{colLabel}</span>
229
- {col.admin && (
230
- <div
231
- style={{
232
- width: "5px",
233
- height: "5px",
234
- borderRadius: "50%",
235
- backgroundColor: "var(--opaca-accent)",
236
- opacity: 0.8,
237
- boxShadow: "0 0 5px var(--opaca-accent)",
238
- }}
239
- title={
240
- col.slug === "_opaca_assets"
241
- ? "System Assets Registry"
242
- : "Universal Collection"
243
- }
244
- />
245
- )}
246
- </div>
247
- <ArrowUpRight size={14} color="var(--opaca-text-dim)" />
248
- </div>
249
- <div
250
- style={{
251
- fontSize: "0.75rem",
252
- color: "var(--opaca-text-muted)",
253
- }}
254
- >
255
- {col.fields.length} fields
256
- </div>
257
- </Link>
258
- );
259
- })}
260
- </div>
261
- </div>
262
- );
263
- }
@@ -1,384 +0,0 @@
1
- import { useStore } from "@nanostores/react";
2
- import { useForm } from "@tanstack/react-form";
3
- import { AlertCircle, ArrowLeft, Globe, Loader2, Save, Trash2 } from "lucide-react";
4
- import { useEffect, useMemo, useState } from "react";
5
- import type { SerializableCollection } from "../../../types";
6
- import { api } from "../../api-client";
7
- import { $config } from "../../stores/config";
8
- import { createFetcherStore, createMutatorStore } from "../../stores/query";
9
- import { notify } from "../../stores/ui";
10
- import { FieldRenderer } from "../components/fields";
11
- import { AlertDialog } from "../components/ui/alert-dialog";
12
-
13
- export interface DocumentEditViewProps {
14
- collection: SerializableCollection;
15
- id: string;
16
- onBack: () => void;
17
- }
18
-
19
- // Data Fetcher
20
- const fetchDocument = async (path: string, id: string, locale?: string) => {
21
- if (id === "create") return {};
22
- const headers: Record<string, string> = {};
23
- if (locale) headers["x-opaca-locale"] = locale;
24
-
25
- const data = await api.get(`api/${path}/${id}`, { headers }).json<any>();
26
- return data;
27
- };
28
-
29
- // Data Mutator
30
- const saveDocument = async (path: string, id: string, payload: any, locale?: string) => {
31
- const payloadToSave = { ...payload };
32
- for (const key in payloadToSave) {
33
- if (payloadToSave[key] === "") payloadToSave[key] = null;
34
- }
35
-
36
- const headers: Record<string, string> = {};
37
- if (locale) headers["x-opaca-locale"] = locale;
38
-
39
- if (id && id !== "create") {
40
- return api.patch(`api/${path}/${id}`, { json: payloadToSave, headers }).json();
41
- }
42
- return api.post(`api/${path}`, { json: payloadToSave, headers }).json();
43
- };
44
-
45
- export function DocumentEditView({ collection, id, onBack }: DocumentEditViewProps) {
46
- const path = collection.apiPath || collection.slug;
47
- const docPath = path;
48
-
49
- const config = useStore($config);
50
- const [currentLocale, setCurrentLocale] = useState(config?.i18n?.defaultLocale || "en");
51
-
52
- const documentFetcher = useMemo(
53
- () =>
54
- createFetcherStore<any>([path, id, currentLocale], {
55
- fetcher: () => fetchDocument(path, id, currentLocale),
56
- }),
57
- [path, id, currentLocale],
58
- );
59
-
60
- const documentMutator = useMemo(
61
- () =>
62
- createMutatorStore<any, any>(async ({ data }) => {
63
- const payload = data;
64
- return saveDocument(path, id, payload, currentLocale);
65
- }),
66
- [path, id, currentLocale],
67
- );
68
-
69
- const docState = useStore(documentFetcher);
70
- const mutateState = useStore(documentMutator);
71
- const loading = docState.loading;
72
- const saving = mutateState.loading;
73
- const error = docState.error || mutateState.error;
74
-
75
- const hasDrafts = !!collection.versions?.drafts;
76
-
77
- const form = useForm({
78
- defaultValues: docState.data || {},
79
- onSubmit: async ({ value }) => {
80
- try {
81
- await documentMutator.mutate(value);
82
- notify(`Document ${id === "create" ? "created" : "updated"} successfully`, "success");
83
- onBack();
84
- } catch (err: any) {
85
- let errorMsg = "Failed to save document";
86
- if (err.response) {
87
- try {
88
- const errorData = await err.response.json();
89
- errorMsg = errorData.message || errorMsg;
90
-
91
- if (errorData.errors) {
92
- Object.keys(errorData.errors).forEach((field) => {
93
- if (field === "_errors") return;
94
- form.setErrorMap({
95
- onChange: errorData.errors[field]._errors.join(", "),
96
- } as any);
97
- });
98
- }
99
- } catch (e) {}
100
- }
101
- notify(errorMsg, "error");
102
- }
103
- },
104
- });
105
-
106
- useEffect(() => {
107
- if (docState.data && !docState.loading) {
108
- form.reset(docState.data);
109
- }
110
- }, [docState.data, docState.loading, form, currentLocale]);
111
-
112
- const handleSubmit = async (e?: React.FormEvent, status: "published" | "draft" = "published") => {
113
- if (e) e.preventDefault();
114
- e?.stopPropagation();
115
-
116
- // Set status in form values before submitting
117
- if (hasDrafts) {
118
- form.setFieldValue("_status" as never, status as never);
119
- }
120
-
121
- await form.handleSubmit();
122
- };
123
-
124
- const handleBack = () => {
125
- if (!form.state.isDirty) {
126
- onBack();
127
- }
128
- };
129
-
130
- if (loading) {
131
- return (
132
- <div
133
- style={{
134
- display: "flex",
135
- alignItems: "center",
136
- gap: "0.75rem",
137
- marginTop: "2rem",
138
- padding: "0 2.5rem",
139
- }}
140
- >
141
- <Loader2 size={18} className="opaca-spin" />
142
- <span className="opaca-subtitle">Loading document...</span>
143
- </div>
144
- );
145
- }
146
-
147
- return (
148
- <div className="opaca-admin-form">
149
- <div className="opaca-header">
150
- <div style={{ display: "flex", alignItems: "center", gap: "1rem" }}>
151
- {form.state.isDirty ? (
152
- <AlertDialog
153
- title="Unsaved Changes"
154
- description="You have unsaved changes. Are you sure you want to discard them and leave?"
155
- onConfirm={onBack}
156
- variant="destructive"
157
- >
158
- <button
159
- type="button"
160
- className="opaca-btn opaca-btn-outline"
161
- style={{ padding: "0.5rem" }}
162
- title="Go Back"
163
- >
164
- <ArrowLeft size={18} />
165
- </button>
166
- </AlertDialog>
167
- ) : (
168
- <button
169
- type="button"
170
- onClick={handleBack}
171
- className="opaca-btn opaca-btn-outline"
172
- style={{ padding: "0.5rem" }}
173
- title="Go Back"
174
- >
175
- <ArrowLeft size={18} />
176
- </button>
177
- )}
178
- <div>
179
- <h1 className="opaca-title">
180
- {id === "create"
181
- ? `Create ${collection.label || collection.slug}`
182
- : `Edit ${collection.label || collection.slug}`}
183
- </h1>
184
- <p className="opaca-subtitle">
185
- {id === "create" ? "Add a new entry to the collection." : `ID: ${id}`}
186
- </p>
187
- </div>
188
- </div>
189
-
190
- <div style={{ display: "flex", gap: "0.75rem" }}>
191
- {/* Locale Switcher */}
192
- {config?.i18n && config.i18n.locales.length > 1 && (
193
- <div className="opaca-locale-switcher">
194
- {config.i18n.locales.map((loc) => (
195
- <button
196
- key={loc}
197
- type="button"
198
- className={`opaca-locale-btn ${currentLocale === loc ? "active" : ""}`}
199
- onClick={() => {
200
- if (form.state.isDirty) {
201
- if (confirm("Unsaved changes will be lost. Continue?")) {
202
- setCurrentLocale(loc);
203
- }
204
- } else {
205
- setCurrentLocale(loc);
206
- }
207
- }}
208
- >
209
- {loc.toUpperCase()}
210
- </button>
211
- ))}
212
- </div>
213
- )}
214
-
215
- {id !== "create" && (
216
- <AlertDialog
217
- title="Delete Document"
218
- description="Are you sure you want to delete this document? This action cannot be undone."
219
- onConfirm={async () => {
220
- try {
221
- await api.delete(`api/${docPath}/${id}`);
222
- notify("Document deleted successfully", "success");
223
- onBack();
224
- } catch (err: unknown) {
225
- console.error(err);
226
- notify("Failed to delete document", "error");
227
- }
228
- }}
229
- variant="destructive"
230
- confirmText="Delete"
231
- >
232
- <button
233
- type="button"
234
- className="opaca-btn opaca-btn-outline"
235
- style={{
236
- color: "var(--opaca-error)",
237
- borderColor: "var(--opaca-error)",
238
- }}
239
- title="Delete Document"
240
- >
241
- <Trash2 size={18} />
242
- Delete
243
- </button>
244
- </AlertDialog>
245
- )}
246
- <form.Subscribe selector={(state) => [state.isDirty]}>
247
- {([isDirty]) => (
248
- <div style={{ display: "flex", gap: "0.75rem" }}>
249
- {hasDrafts && (
250
- <button
251
- type="button"
252
- onClick={(e) => handleSubmit(e, "draft")}
253
- disabled={saving}
254
- className="opaca-btn opaca-btn-outline"
255
- title="Save as Draft"
256
- >
257
- {saving ? <Loader2 size={18} className="opaca-spin" /> : <Save size={18} />}
258
- Save Draft
259
- </button>
260
- )}
261
- <button
262
- type="button"
263
- onClick={(e) => handleSubmit(e, "published")}
264
- disabled={saving}
265
- className="opaca-btn opaca-btn-primary"
266
- style={{ position: "relative" }}
267
- title={hasDrafts ? "Publish" : "Save Changes"}
268
- >
269
- {saving ? <Loader2 size={18} className="opaca-spin" /> : <Save size={18} />}
270
- {id === "create"
271
- ? hasDrafts
272
- ? "Publish"
273
- : "Create"
274
- : hasDrafts
275
- ? "Publish"
276
- : "Save"}
277
- {isDirty && !saving && (
278
- <span
279
- style={{
280
- position: "absolute",
281
- top: "-4px",
282
- right: "-4px",
283
- width: "10px",
284
- height: "10px",
285
- borderRadius: "50%",
286
- background: "var(--opaca-warning)",
287
- border: "2px solid var(--opaca-card-bg)",
288
- boxShadow: "0 1px 2px rgba(0,0,0,0.2)",
289
- }}
290
- title="Unsaved changes"
291
- />
292
- )}
293
- </button>
294
- </div>
295
- )}
296
- </form.Subscribe>
297
- </div>
298
- </div>
299
-
300
- {error && (
301
- <div
302
- style={{
303
- padding: "1rem",
304
- backgroundColor: "rgba(239, 68, 68, 0.1)",
305
- border: "1px solid var(--opaca-error)",
306
- color: "var(--opaca-error)",
307
- borderRadius: "var(--opaca-radius)",
308
- marginBottom: "2rem",
309
- display: "flex",
310
- alignItems: "center",
311
- gap: "0.75rem",
312
- fontSize: "0.875rem",
313
- }}
314
- >
315
- <AlertCircle size={18} />
316
- {error}
317
- </div>
318
- )}
319
-
320
- <div className="opaca-card" style={{ padding: "2.5rem" }}>
321
- <form id="opaca-doc-form" onSubmit={(e) => handleSubmit(e)}>
322
- <div style={{ display: "grid", gap: "2rem" }}>
323
- {collection.fields.map((field, index) => {
324
- const fieldName = field.name;
325
- const access = fieldName
326
- ? docState.data?._access?.[fieldName]
327
- : {
328
- hidden: false,
329
- readOnly: false,
330
- disabled: false,
331
- };
332
-
333
- if (access?.hidden) return null;
334
-
335
- const isReadOnly = access?.readOnly;
336
- const isDisabled = access?.disabled || isReadOnly;
337
-
338
- // Layout fields without a name
339
- if (!fieldName) {
340
- return (
341
- <form.Subscribe
342
- // biome-ignore lint/suspicious/noArrayIndexKey: ignore, layout field sometimes don't have names
343
- key={index}
344
- selector={(state) => state.values}
345
- >
346
- {(values) => (
347
- <FieldRenderer
348
- field={field}
349
- value={undefined}
350
- disabled={isDisabled}
351
- readOnly={isReadOnly}
352
- onChange={(_name, val) => {
353
- if (typeof _name === "string") {
354
- form.setFieldValue(_name as never, val as never);
355
- }
356
- }}
357
- parentData={values}
358
- />
359
- )}
360
- </form.Subscribe>
361
- );
362
- }
363
-
364
- return (
365
- <form.Field key={fieldName} name={fieldName}>
366
- {(fieldApi) => (
367
- <FieldRenderer
368
- field={field}
369
- value={fieldApi.state.value}
370
- disabled={isDisabled}
371
- readOnly={isReadOnly}
372
- onChange={(val) => fieldApi.handleChange(val)}
373
- parentData={form.state.values}
374
- />
375
- )}
376
- </form.Field>
377
- );
378
- })}
379
- </div>
380
- </form>
381
- </div>
382
- </div>
383
- );
384
- }