@modern-admin/react 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/dist/action-guard.d.ts +13 -0
- package/dist/action-guard.d.ts.map +1 -0
- package/dist/action-guard.js +15 -0
- package/dist/action-guard.js.map +1 -0
- package/dist/action-menu.d.ts +17 -0
- package/dist/action-menu.d.ts.map +1 -0
- package/dist/action-menu.jsx +80 -0
- package/dist/action-menu.jsx.map +1 -0
- package/dist/admin-app.d.ts +23 -0
- package/dist/admin-app.d.ts.map +1 -0
- package/dist/admin-app.jsx +407 -0
- package/dist/admin-app.jsx.map +1 -0
- package/dist/admin-router.d.ts +29 -0
- package/dist/admin-router.d.ts.map +1 -0
- package/dist/admin-router.jsx +215 -0
- package/dist/admin-router.jsx.map +1 -0
- package/dist/breadcrumbs.d.ts +17 -0
- package/dist/breadcrumbs.d.ts.map +1 -0
- package/dist/breadcrumbs.jsx +40 -0
- package/dist/breadcrumbs.jsx.map +1 -0
- package/dist/client.d.ts +526 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +582 -0
- package/dist/client.js.map +1 -0
- package/dist/component-loader.d.ts +10 -0
- package/dist/component-loader.d.ts.map +1 -0
- package/dist/component-loader.js +23 -0
- package/dist/component-loader.js.map +1 -0
- package/dist/components/ai-assistant-widget.d.ts +3 -0
- package/dist/components/ai-assistant-widget.d.ts.map +1 -0
- package/dist/components/ai-assistant-widget.jsx +390 -0
- package/dist/components/ai-assistant-widget.jsx.map +1 -0
- package/dist/components/ai-fill-dialog.d.ts +9 -0
- package/dist/components/ai-fill-dialog.d.ts.map +1 -0
- package/dist/components/ai-fill-dialog.jsx +105 -0
- package/dist/components/ai-fill-dialog.jsx.map +1 -0
- package/dist/components/chart-builder-dialog.d.ts +10 -0
- package/dist/components/chart-builder-dialog.d.ts.map +1 -0
- package/dist/components/chart-builder-dialog.jsx +433 -0
- package/dist/components/chart-builder-dialog.jsx.map +1 -0
- package/dist/components/chart-widget.d.ts +12 -0
- package/dist/components/chart-widget.d.ts.map +1 -0
- package/dist/components/chart-widget.jsx +365 -0
- package/dist/components/chart-widget.jsx.map +1 -0
- package/dist/components/global-search-dialog.d.ts +7 -0
- package/dist/components/global-search-dialog.d.ts.map +1 -0
- package/dist/components/global-search-dialog.jsx +187 -0
- package/dist/components/global-search-dialog.jsx.map +1 -0
- package/dist/components/group-settings-dialog.d.ts +13 -0
- package/dist/components/group-settings-dialog.d.ts.map +1 -0
- package/dist/components/group-settings-dialog.jsx +53 -0
- package/dist/components/group-settings-dialog.jsx.map +1 -0
- package/dist/components/move-chart-dialog.d.ts +18 -0
- package/dist/components/move-chart-dialog.d.ts.map +1 -0
- package/dist/components/move-chart-dialog.jsx +68 -0
- package/dist/components/move-chart-dialog.jsx.map +1 -0
- package/dist/components/reference-multi-table-dialog.d.ts +12 -0
- package/dist/components/reference-multi-table-dialog.d.ts.map +1 -0
- package/dist/components/reference-multi-table-dialog.jsx +126 -0
- package/dist/components/reference-multi-table-dialog.jsx.map +1 -0
- package/dist/components/related-records-tabs.d.ts +8 -0
- package/dist/components/related-records-tabs.d.ts.map +1 -0
- package/dist/components/related-records-tabs.jsx +75 -0
- package/dist/components/related-records-tabs.jsx.map +1 -0
- package/dist/components/revisions-button.d.ts +7 -0
- package/dist/components/revisions-button.d.ts.map +1 -0
- package/dist/components/revisions-button.jsx +152 -0
- package/dist/components/revisions-button.jsx.map +1 -0
- package/dist/components/wizard-form.d.ts +43 -0
- package/dist/components/wizard-form.d.ts.map +1 -0
- package/dist/components/wizard-form.jsx +136 -0
- package/dist/components/wizard-form.jsx.map +1 -0
- package/dist/dashboard/time-series.d.ts +20 -0
- package/dist/dashboard/time-series.d.ts.map +1 -0
- package/dist/dashboard/time-series.js +108 -0
- package/dist/dashboard/time-series.js.map +1 -0
- package/dist/dialogs.d.ts +35 -0
- package/dist/dialogs.d.ts.map +1 -0
- package/dist/dialogs.jsx +152 -0
- package/dist/dialogs.jsx.map +1 -0
- package/dist/export.d.ts +39 -0
- package/dist/export.d.ts.map +1 -0
- package/dist/export.js +114 -0
- package/dist/export.js.map +1 -0
- package/dist/extension-registry.d.ts +122 -0
- package/dist/extension-registry.d.ts.map +1 -0
- package/dist/extension-registry.js +93 -0
- package/dist/extension-registry.js.map +1 -0
- package/dist/header-controls.d.ts +4 -0
- package/dist/header-controls.d.ts.map +1 -0
- package/dist/header-controls.jsx +70 -0
- package/dist/header-controls.jsx.map +1 -0
- package/dist/hooks.d.ts +104 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +374 -0
- package/dist/hooks.js.map +1 -0
- package/dist/hotkey-help.d.ts +3 -0
- package/dist/hotkey-help.d.ts.map +1 -0
- package/dist/hotkey-help.jsx +32 -0
- package/dist/hotkey-help.jsx.map +1 -0
- package/dist/hotkey-registry.d.ts +18 -0
- package/dist/hotkey-registry.d.ts.map +1 -0
- package/dist/hotkey-registry.jsx +34 -0
- package/dist/hotkey-registry.jsx.map +1 -0
- package/dist/i18n.d.ts +74 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.jsx +127 -0
- package/dist/i18n.jsx.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/notify.d.ts +41 -0
- package/dist/notify.d.ts.map +1 -0
- package/dist/notify.jsx +58 -0
- package/dist/notify.jsx.map +1 -0
- package/dist/pages/ai-assistant-settings-section.d.ts +3 -0
- package/dist/pages/ai-assistant-settings-section.d.ts.map +1 -0
- package/dist/pages/ai-assistant-settings-section.jsx +126 -0
- package/dist/pages/ai-assistant-settings-section.jsx.map +1 -0
- package/dist/pages/audit-log-page.d.ts +3 -0
- package/dist/pages/audit-log-page.d.ts.map +1 -0
- package/dist/pages/audit-log-page.jsx +354 -0
- package/dist/pages/audit-log-page.jsx.map +1 -0
- package/dist/pages/edit-page.d.ts +7 -0
- package/dist/pages/edit-page.d.ts.map +1 -0
- package/dist/pages/edit-page.jsx +614 -0
- package/dist/pages/edit-page.jsx.map +1 -0
- package/dist/pages/export-dialog.d.ts +11 -0
- package/dist/pages/export-dialog.d.ts.map +1 -0
- package/dist/pages/export-dialog.jsx +102 -0
- package/dist/pages/export-dialog.jsx.map +1 -0
- package/dist/pages/home-page.d.ts +3 -0
- package/dist/pages/home-page.d.ts.map +1 -0
- package/dist/pages/home-page.jsx +211 -0
- package/dist/pages/home-page.jsx.map +1 -0
- package/dist/pages/list-page.d.ts +42 -0
- package/dist/pages/list-page.d.ts.map +1 -0
- package/dist/pages/list-page.jsx +1596 -0
- package/dist/pages/list-page.jsx.map +1 -0
- package/dist/pages/login-page.d.ts +11 -0
- package/dist/pages/login-page.d.ts.map +1 -0
- package/dist/pages/login-page.jsx +157 -0
- package/dist/pages/login-page.jsx.map +1 -0
- package/dist/pages/settings-page.d.ts +5 -0
- package/dist/pages/settings-page.d.ts.map +1 -0
- package/dist/pages/settings-page.jsx +787 -0
- package/dist/pages/settings-page.jsx.map +1 -0
- package/dist/pages/settings-shared.d.ts +51 -0
- package/dist/pages/settings-shared.d.ts.map +1 -0
- package/dist/pages/settings-shared.jsx +66 -0
- package/dist/pages/settings-shared.jsx.map +1 -0
- package/dist/pages/show-page.d.ts +7 -0
- package/dist/pages/show-page.d.ts.map +1 -0
- package/dist/pages/show-page.jsx +147 -0
- package/dist/pages/show-page.jsx.map +1 -0
- package/dist/pages/wizard-create-page.d.ts +14 -0
- package/dist/pages/wizard-create-page.d.ts.map +1 -0
- package/dist/pages/wizard-create-page.jsx +106 -0
- package/dist/pages/wizard-create-page.jsx.map +1 -0
- package/dist/property-renderer.d.ts +8 -0
- package/dist/property-renderer.d.ts.map +1 -0
- package/dist/property-renderer.jsx +690 -0
- package/dist/property-renderer.jsx.map +1 -0
- package/dist/provider.d.ts +20 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.jsx +32 -0
- package/dist/provider.jsx.map +1 -0
- package/dist/realtime.d.ts +22 -0
- package/dist/realtime.d.ts.map +1 -0
- package/dist/realtime.js +38 -0
- package/dist/realtime.js.map +1 -0
- package/dist/reference.d.ts +52 -0
- package/dist/reference.d.ts.map +1 -0
- package/dist/reference.jsx +224 -0
- package/dist/reference.jsx.map +1 -0
- package/dist/relations.d.ts +11 -0
- package/dist/relations.d.ts.map +1 -0
- package/dist/relations.js +36 -0
- package/dist/relations.js.map +1 -0
- package/dist/router.d.ts +82 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.jsx +187 -0
- package/dist/router.jsx.map +1 -0
- package/dist/show-when.d.ts +7 -0
- package/dist/show-when.d.ts.map +1 -0
- package/dist/show-when.js +77 -0
- package/dist/show-when.js.map +1 -0
- package/dist/types.d.ts +194 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +18 -0
- package/dist/types.js.map +1 -0
- package/dist/use-dashboard-charts.d.ts +93 -0
- package/dist/use-dashboard-charts.d.ts.map +1 -0
- package/dist/use-dashboard-charts.js +263 -0
- package/dist/use-dashboard-charts.js.map +1 -0
- package/dist/use-hotkey.d.ts +17 -0
- package/dist/use-hotkey.d.ts.map +1 -0
- package/dist/use-hotkey.js +103 -0
- package/dist/use-hotkey.js.map +1 -0
- package/dist/user-directory.d.ts +18 -0
- package/dist/user-directory.d.ts.map +1 -0
- package/dist/user-directory.js +51 -0
- package/dist/user-directory.js.map +1 -0
- package/dist/validation.d.ts +22 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +338 -0
- package/dist/validation.js.map +1 -0
- package/package.json +59 -0
- package/src/action-guard.ts +20 -0
- package/src/action-menu.tsx +161 -0
- package/src/admin-app.tsx +630 -0
- package/src/admin-router.tsx +273 -0
- package/src/breadcrumbs.tsx +75 -0
- package/src/client.ts +1093 -0
- package/src/component-loader.ts +33 -0
- package/src/components/ai-assistant-widget.tsx +565 -0
- package/src/components/ai-fill-dialog.tsx +143 -0
- package/src/components/chart-builder-dialog.tsx +618 -0
- package/src/components/chart-widget.tsx +654 -0
- package/src/components/global-search-dialog.tsx +272 -0
- package/src/components/group-settings-dialog.tsx +93 -0
- package/src/components/move-chart-dialog.tsx +130 -0
- package/src/components/reference-multi-table-dialog.tsx +196 -0
- package/src/components/related-records-tabs.tsx +130 -0
- package/src/components/revisions-button.tsx +237 -0
- package/src/components/wizard-form.tsx +302 -0
- package/src/dashboard/time-series.ts +125 -0
- package/src/dialogs.tsx +265 -0
- package/src/export.ts +140 -0
- package/src/extension-registry.ts +195 -0
- package/src/header-controls.tsx +125 -0
- package/src/hooks.ts +509 -0
- package/src/hotkey-help.tsx +56 -0
- package/src/hotkey-registry.tsx +60 -0
- package/src/i18n.tsx +267 -0
- package/src/index.ts +192 -0
- package/src/notify.tsx +94 -0
- package/src/pages/ai-assistant-settings-section.tsx +167 -0
- package/src/pages/audit-log-page.tsx +580 -0
- package/src/pages/edit-page.tsx +743 -0
- package/src/pages/export-dialog.tsx +154 -0
- package/src/pages/home-page.tsx +318 -0
- package/src/pages/list-page.tsx +2645 -0
- package/src/pages/login-page.tsx +242 -0
- package/src/pages/settings-page.tsx +1143 -0
- package/src/pages/settings-shared.tsx +134 -0
- package/src/pages/show-page.tsx +223 -0
- package/src/pages/wizard-create-page.tsx +164 -0
- package/src/property-renderer.tsx +1143 -0
- package/src/provider.tsx +70 -0
- package/src/realtime.ts +55 -0
- package/src/reference.tsx +386 -0
- package/src/relations.ts +55 -0
- package/src/router.tsx +211 -0
- package/src/show-when.ts +76 -0
- package/src/types.ts +198 -0
- package/src/use-dashboard-charts.ts +362 -0
- package/src/use-hotkey.ts +128 -0
- package/src/user-directory.ts +56 -0
- package/src/validation.ts +361 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
// Edit / new page driven by react-hook-form + zodResolver. The Zod schema
|
|
2
|
+
// is derived dynamically from the resource's editable PropertyJSON list via
|
|
3
|
+
// `buildValidationSchema()` — every property type maps to the right runtime
|
|
4
|
+
// check, with localized error messages. Field-level server errors from
|
|
5
|
+
// `record.errors` are projected back onto the form via setError, and global
|
|
6
|
+
// success/failure messages surface as toasts.
|
|
7
|
+
|
|
8
|
+
import * as React from 'react'
|
|
9
|
+
import { useForm, useWatch, type Control, type SubmitHandler } from 'react-hook-form'
|
|
10
|
+
import { zodResolver } from '@hookform/resolvers/zod'
|
|
11
|
+
import {
|
|
12
|
+
Button,
|
|
13
|
+
Card,
|
|
14
|
+
CardContent,
|
|
15
|
+
CardHeader,
|
|
16
|
+
CardTitle,
|
|
17
|
+
Field,
|
|
18
|
+
FieldError,
|
|
19
|
+
FieldLabel,
|
|
20
|
+
Form,
|
|
21
|
+
FormField,
|
|
22
|
+
InfoTooltip,
|
|
23
|
+
Kbd,
|
|
24
|
+
Tooltip,
|
|
25
|
+
TooltipContent,
|
|
26
|
+
TooltipTrigger,
|
|
27
|
+
getModKeyLabel,
|
|
28
|
+
} from '@modern-admin/ui'
|
|
29
|
+
import { AlertCircle, Eye, Plus, Save, Sparkles, Trash2, X } from 'lucide-react'
|
|
30
|
+
import { useCreateRecord, useDeleteRecord, useFeatures, useRecord, useResource, useUpdateRecord } from '../hooks.js'
|
|
31
|
+
import { parseApiError } from '../client.js'
|
|
32
|
+
import { PropertyEditor } from '../property-renderer.js'
|
|
33
|
+
import { Link, useNavigate } from '../router.js'
|
|
34
|
+
import { useI18n } from '../i18n.js'
|
|
35
|
+
import { useNotify } from '../notify.js'
|
|
36
|
+
import { useHotkey } from '../use-hotkey.js'
|
|
37
|
+
import { PageBreadcrumbs, homeCrumb } from '../breadcrumbs.js'
|
|
38
|
+
import type { BreadcrumbItemSpec } from '../breadcrumbs.js'
|
|
39
|
+
import { buildValidationSchema, defaultValueFor } from '../validation.js'
|
|
40
|
+
import { evaluateShowWhen } from '../show-when.js'
|
|
41
|
+
import type { PropertyJSON } from '../types.js'
|
|
42
|
+
import { useDialogs } from '../dialogs.js'
|
|
43
|
+
import { RevisionsButton } from '../components/revisions-button.js'
|
|
44
|
+
import { AiFillDialog } from '../components/ai-fill-dialog.js'
|
|
45
|
+
import { visibleRecordProperties } from '../relations.js'
|
|
46
|
+
|
|
47
|
+
export interface ResourceEditPageProps {
|
|
48
|
+
resourceId: string
|
|
49
|
+
recordId?: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Defensive coercion applied to values returned by `aiFillFromImage` before
|
|
54
|
+
* they reach `form.setValue`. The backend already normalises numbers/dates/
|
|
55
|
+
* enums/references; this is a last-line check so an unexpected stringy
|
|
56
|
+
* number does not poison a numeric field's state and so out-of-domain enum
|
|
57
|
+
* values are dropped rather than silently committed. Returns `undefined` to
|
|
58
|
+
* mean "drop this value, keep the existing form state".
|
|
59
|
+
*/
|
|
60
|
+
function coerceAiFillValue(property: PropertyJSON, value: unknown): unknown {
|
|
61
|
+
if (value === null || value === undefined) return undefined
|
|
62
|
+
// Enum-style: snap by exact / case-insensitive value or label.
|
|
63
|
+
if (property.availableValues?.length) {
|
|
64
|
+
if (typeof value !== 'string') return undefined
|
|
65
|
+
const trimmed = value.trim()
|
|
66
|
+
if (trimmed === '') return undefined
|
|
67
|
+
const lower = trimmed.toLowerCase()
|
|
68
|
+
const match = property.availableValues.find((v) => v.value === trimmed)
|
|
69
|
+
?? property.availableValues.find((v) => v.value.toLowerCase() === lower)
|
|
70
|
+
?? property.availableValues.find((v) => v.label.toLowerCase() === lower)
|
|
71
|
+
return match?.value
|
|
72
|
+
}
|
|
73
|
+
// References: keep id-shaped scalars only (string or number).
|
|
74
|
+
if (property.reference) {
|
|
75
|
+
if (typeof value === 'string' || typeof value === 'number') return value
|
|
76
|
+
return undefined
|
|
77
|
+
}
|
|
78
|
+
switch (property.type) {
|
|
79
|
+
case 'bigint':
|
|
80
|
+
case 'biginteger': {
|
|
81
|
+
// BigInt values are transported as digit strings end-to-end (matches
|
|
82
|
+
// BaseRecord.toJSON + the Prisma adapter's accept-string write path).
|
|
83
|
+
// Never round-trip via Number() — values >2^53 would lose precision.
|
|
84
|
+
if (typeof value === 'bigint') return value.toString()
|
|
85
|
+
if (typeof value === 'number') {
|
|
86
|
+
if (!Number.isFinite(value) || !Number.isInteger(value)) return undefined
|
|
87
|
+
return String(value)
|
|
88
|
+
}
|
|
89
|
+
if (typeof value !== 'string') return undefined
|
|
90
|
+
const trimmed = value.trim().replace(/[\s_]/g, '')
|
|
91
|
+
return /^-?\d+$/.test(trimmed) ? trimmed : undefined
|
|
92
|
+
}
|
|
93
|
+
case 'number':
|
|
94
|
+
case 'float':
|
|
95
|
+
case 'currency':
|
|
96
|
+
case 'money':
|
|
97
|
+
case 'decimal':
|
|
98
|
+
case 'integer': {
|
|
99
|
+
if (typeof value === 'number') return Number.isFinite(value) ? value : undefined
|
|
100
|
+
if (typeof value !== 'string') return undefined
|
|
101
|
+
const trimmed = value.trim().replace(/[^\d.,\-+eE]/g, '')
|
|
102
|
+
if (trimmed === '' || trimmed === '-' || trimmed === '+') return undefined
|
|
103
|
+
const lastDot = trimmed.lastIndexOf('.')
|
|
104
|
+
const lastComma = trimmed.lastIndexOf(',')
|
|
105
|
+
const normalised = lastDot >= lastComma
|
|
106
|
+
? trimmed.replace(/,/g, '')
|
|
107
|
+
: trimmed.replace(/\./g, '').replace(',', '.')
|
|
108
|
+
const n = Number(normalised)
|
|
109
|
+
if (!Number.isFinite(n)) return undefined
|
|
110
|
+
return property.type === 'integer' ? Math.trunc(n) : n
|
|
111
|
+
}
|
|
112
|
+
case 'boolean':
|
|
113
|
+
if (typeof value === 'boolean') return value
|
|
114
|
+
if (typeof value === 'number') return value !== 0
|
|
115
|
+
if (typeof value === 'string') {
|
|
116
|
+
const t = value.trim().toLowerCase()
|
|
117
|
+
if (['true', 'yes', 'y', '1', 'on'].includes(t)) return true
|
|
118
|
+
if (['false', 'no', 'n', '0', 'off'].includes(t)) return false
|
|
119
|
+
}
|
|
120
|
+
return undefined
|
|
121
|
+
case 'date':
|
|
122
|
+
case 'datetime':
|
|
123
|
+
case 'datetime-local':
|
|
124
|
+
// Keep ISO-ish strings; date pickers cope with both YYYY-MM-DD and full
|
|
125
|
+
// ISO. Reject anything that doesn't at least look year-prefixed so we
|
|
126
|
+
// don't push "12 March 2026" into a <DatePicker>.
|
|
127
|
+
if (typeof value !== 'string') return undefined
|
|
128
|
+
return /^\d{4}-\d{2}-\d{2}/.test(value.trim()) ? value.trim() : undefined
|
|
129
|
+
default:
|
|
130
|
+
return value
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
type FormValues = Record<string, unknown>
|
|
135
|
+
|
|
136
|
+
export function ResourceEditPage({
|
|
137
|
+
resourceId,
|
|
138
|
+
recordId,
|
|
139
|
+
}: ResourceEditPageProps): React.ReactElement {
|
|
140
|
+
const resource = useResource(resourceId)
|
|
141
|
+
const existing = useRecord(resourceId, recordId)
|
|
142
|
+
const create = useCreateRecord(resourceId)
|
|
143
|
+
const update = useUpdateRecord(resourceId)
|
|
144
|
+
const remove = useDeleteRecord(resourceId)
|
|
145
|
+
const features = useFeatures()
|
|
146
|
+
const navigate = useNavigate()
|
|
147
|
+
const { t, locale } = useI18n()
|
|
148
|
+
const notify = useNotify()
|
|
149
|
+
const dialogs = useDialogs()
|
|
150
|
+
|
|
151
|
+
const editable = React.useMemo<PropertyJSON[]>(
|
|
152
|
+
() =>
|
|
153
|
+
resource
|
|
154
|
+
? visibleRecordProperties(resource.properties, 'edit').filter((p) => !p.isDisabled)
|
|
155
|
+
: [],
|
|
156
|
+
[resource],
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
// The validation schema needs to consult the live form values so it can
|
|
160
|
+
// skip required/format checks for fields hidden by `showWhen`. We can't
|
|
161
|
+
// call `form.getValues` here (the form isn't built yet), so we route
|
|
162
|
+
// through a ref filled in below — the schema closure reads from the ref
|
|
163
|
+
// at validation time, not at build time.
|
|
164
|
+
const getValuesRef = React.useRef<() => Record<string, unknown>>(() => ({}))
|
|
165
|
+
|
|
166
|
+
// Re-build the schema when locale changes so error messages re-translate.
|
|
167
|
+
const schema = React.useMemo(
|
|
168
|
+
() => buildValidationSchema(editable, t, () => getValuesRef.current()),
|
|
169
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
170
|
+
[editable, locale],
|
|
171
|
+
)
|
|
172
|
+
const defaults = React.useMemo<FormValues>(() => {
|
|
173
|
+
const out: FormValues = {}
|
|
174
|
+
for (const p of editable) out[p.path] = defaultValueFor(p)
|
|
175
|
+
return out
|
|
176
|
+
}, [editable])
|
|
177
|
+
|
|
178
|
+
const form = useForm<FormValues>({
|
|
179
|
+
resolver: zodResolver(schema),
|
|
180
|
+
defaultValues: defaults,
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
// Keep the ref pointing at the latest getValues so the schema closure
|
|
184
|
+
// always sees the current form snapshot.
|
|
185
|
+
getValuesRef.current = form.getValues
|
|
186
|
+
|
|
187
|
+
// Track which recordId has already been hydrated so background refetches
|
|
188
|
+
// don't overwrite user edits after the initial load.
|
|
189
|
+
const hydratedRecordIdRef = React.useRef<string | undefined>(undefined)
|
|
190
|
+
|
|
191
|
+
const isNew = !recordId
|
|
192
|
+
|
|
193
|
+
// localStorage key for the per-resource new-record draft. We persist the
|
|
194
|
+
// form snapshot here whenever the user has typed anything but not yet
|
|
195
|
+
// submitted, so that closing the tab / navigating away doesn't lose work.
|
|
196
|
+
const draftKey = isNew ? `modern-admin:draft:${resourceId}` : null
|
|
197
|
+
|
|
198
|
+
// Once-per-resource init flag for the new-record form. Gates the whole
|
|
199
|
+
// initialisation block — without this, every dep change in the hydration
|
|
200
|
+
// effect (e.g. a background `useResource` refetch bumping the `defaults`
|
|
201
|
+
// reference) would re-run `form.reset(defaults)` and wipe the user's
|
|
202
|
+
// in-progress input (and any draft we just restored).
|
|
203
|
+
const newFormInitForRef = React.useRef<string | undefined>(undefined)
|
|
204
|
+
|
|
205
|
+
// When we programmatically reset the form (draft restore / undo), we don't
|
|
206
|
+
// want that synthetic change to trigger the persistence watcher and re-save
|
|
207
|
+
// a draft that we just decided not to keep.
|
|
208
|
+
const skipNextPersistRef = React.useRef(false)
|
|
209
|
+
|
|
210
|
+
// Hydrate when the existing record arrives (edit mode) or after the resource
|
|
211
|
+
// schema settles (new mode).
|
|
212
|
+
React.useEffect(() => {
|
|
213
|
+
if (!recordId) {
|
|
214
|
+
// New-record form: initialise exactly once per resource. Second and
|
|
215
|
+
// later runs (triggered by background refetches changing `editable` /
|
|
216
|
+
// `defaults` references) must NOT touch the form — `form.reset(...)`
|
|
217
|
+
// would wipe the user's in-progress input.
|
|
218
|
+
hydratedRecordIdRef.current = undefined
|
|
219
|
+
if (newFormInitForRef.current === resourceId) return
|
|
220
|
+
// Defer until the schema has loaded — otherwise we'd "initialise"
|
|
221
|
+
// against an empty defaults map and then refuse to ever re-init.
|
|
222
|
+
if (editable.length === 0) return
|
|
223
|
+
newFormInitForRef.current = resourceId
|
|
224
|
+
|
|
225
|
+
// Attempt to restore a saved draft. If we find one, reset to the
|
|
226
|
+
// merged snapshot and surface a bottom-center toast with an Undo
|
|
227
|
+
// action. If not, fall back to a single reset to defaults.
|
|
228
|
+
let draft: FormValues | null = null
|
|
229
|
+
if (draftKey && typeof window !== 'undefined') {
|
|
230
|
+
try {
|
|
231
|
+
const stored = window.localStorage.getItem(draftKey)
|
|
232
|
+
if (stored) draft = JSON.parse(stored) as FormValues
|
|
233
|
+
} catch {
|
|
234
|
+
/* corrupted JSON — ignore */
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (draft && typeof draft === 'object') {
|
|
239
|
+
const merged: FormValues = { ...defaults }
|
|
240
|
+
for (const p of editable) {
|
|
241
|
+
if (Object.prototype.hasOwnProperty.call(draft, p.path)) {
|
|
242
|
+
merged[p.path] = (draft as FormValues)[p.path]
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
skipNextPersistRef.current = true
|
|
246
|
+
form.reset(merged)
|
|
247
|
+
notify.raw(t('common:draftRestored'), {
|
|
248
|
+
position: 'bottom-center',
|
|
249
|
+
duration: 8000,
|
|
250
|
+
action: {
|
|
251
|
+
label: t('common:undoDraftRestore'),
|
|
252
|
+
onClick: () => {
|
|
253
|
+
skipNextPersistRef.current = true
|
|
254
|
+
form.reset(defaults)
|
|
255
|
+
try {
|
|
256
|
+
if (draftKey) window.localStorage.removeItem(draftKey)
|
|
257
|
+
} catch {
|
|
258
|
+
/* ignore */
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
})
|
|
263
|
+
} else {
|
|
264
|
+
form.reset(defaults)
|
|
265
|
+
}
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
// Wait until both the resource schema and the record data are ready.
|
|
269
|
+
// Without this guard an early fire with editable=[] would wipe the form.
|
|
270
|
+
if (editable.length === 0 || !existing.data) return
|
|
271
|
+
// Hydrate only once per record — prevents background refetches from
|
|
272
|
+
// overwriting in-progress user edits.
|
|
273
|
+
if (hydratedRecordIdRef.current === recordId) return
|
|
274
|
+
hydratedRecordIdRef.current = recordId
|
|
275
|
+
// Switching into edit mode invalidates any prior new-form init — so the
|
|
276
|
+
// next visit to `/new` (potentially in a reused component instance)
|
|
277
|
+
// restores the draft / resets to defaults instead of keeping the edit
|
|
278
|
+
// record's values.
|
|
279
|
+
newFormInitForRef.current = undefined
|
|
280
|
+
|
|
281
|
+
const params = existing.data.record.params
|
|
282
|
+
const merged: FormValues = { ...defaults }
|
|
283
|
+
for (const p of editable) {
|
|
284
|
+
const v = params[p.path]
|
|
285
|
+
if (p.type === 'boolean') merged[p.path] = Boolean(v)
|
|
286
|
+
else if (v == null) merged[p.path] = defaultValueFor(p)
|
|
287
|
+
else merged[p.path] = v
|
|
288
|
+
}
|
|
289
|
+
form.reset(merged)
|
|
290
|
+
}, [recordId, existing.data, editable, defaults, form, draftKey, resourceId, notify, t])
|
|
291
|
+
|
|
292
|
+
// Persist the form snapshot to localStorage on every change in new mode.
|
|
293
|
+
// We only write when at least one field deviates from defaults; when the
|
|
294
|
+
// form is back to pristine state we delete the key so the user isn't
|
|
295
|
+
// greeted with an "empty draft" toast on reopen.
|
|
296
|
+
React.useEffect(() => {
|
|
297
|
+
if (!isNew || !draftKey || editable.length === 0 || typeof window === 'undefined') return
|
|
298
|
+
const subscription = form.watch((values) => {
|
|
299
|
+
if (skipNextPersistRef.current) {
|
|
300
|
+
skipNextPersistRef.current = false
|
|
301
|
+
return
|
|
302
|
+
}
|
|
303
|
+
let isDirty = false
|
|
304
|
+
const sanitized: FormValues = {}
|
|
305
|
+
for (const p of editable) {
|
|
306
|
+
const v = (values as FormValues)[p.path]
|
|
307
|
+
// Skip non-serializable values (File objects from file inputs).
|
|
308
|
+
if (v instanceof File) continue
|
|
309
|
+
if (Array.isArray(v) && v.some((x) => x instanceof File)) continue
|
|
310
|
+
sanitized[p.path] = v
|
|
311
|
+
if (!valuesEqual(v, defaults[p.path])) isDirty = true
|
|
312
|
+
}
|
|
313
|
+
try {
|
|
314
|
+
if (isDirty) {
|
|
315
|
+
window.localStorage.setItem(draftKey, JSON.stringify(sanitized))
|
|
316
|
+
} else {
|
|
317
|
+
window.localStorage.removeItem(draftKey)
|
|
318
|
+
}
|
|
319
|
+
} catch {
|
|
320
|
+
/* quota / disabled storage — ignore */
|
|
321
|
+
}
|
|
322
|
+
})
|
|
323
|
+
return () => subscription.unsubscribe()
|
|
324
|
+
}, [isNew, draftKey, editable, defaults, form])
|
|
325
|
+
|
|
326
|
+
const clearDraft = React.useCallback((): void => {
|
|
327
|
+
// After a successful submit we navigate to the show page, but the
|
|
328
|
+
// component might be reused if the user clicks "back" or follows a
|
|
329
|
+
// Create link again. Clearing the init flag forces the next /new visit
|
|
330
|
+
// to fall back to defaults instead of retaining the just-submitted
|
|
331
|
+
// values.
|
|
332
|
+
newFormInitForRef.current = undefined
|
|
333
|
+
if (!draftKey || typeof window === 'undefined') return
|
|
334
|
+
try {
|
|
335
|
+
window.localStorage.removeItem(draftKey)
|
|
336
|
+
} catch {
|
|
337
|
+
/* ignore */
|
|
338
|
+
}
|
|
339
|
+
}, [draftKey])
|
|
340
|
+
|
|
341
|
+
const [submitError, setSubmitError] = React.useState<string | null>(null)
|
|
342
|
+
const [aiFillOpen, setAiFillOpen] = React.useState(false)
|
|
343
|
+
|
|
344
|
+
// The `aiFill` feature plugin (packages/feature-ai-fill) registers a
|
|
345
|
+
// resource-scoped action whose `custom.aiFill === true` flag tells us
|
|
346
|
+
// the resource opts in. Absent the action, the button is hidden.
|
|
347
|
+
// Detect the aiFill plugin by the `custom.aiFill === true` marker, not by
|
|
348
|
+
// action name, so renaming the action in the future can't silently break this.
|
|
349
|
+
const aiFillEnabled = React.useMemo(
|
|
350
|
+
() =>
|
|
351
|
+
Boolean(
|
|
352
|
+
resource?.actions.find(
|
|
353
|
+
(a) => (a.custom as { aiFill?: boolean } | undefined)?.aiFill === true,
|
|
354
|
+
),
|
|
355
|
+
),
|
|
356
|
+
[resource],
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
const applyAiFillValues = React.useCallback(
|
|
360
|
+
(values: Record<string, unknown>): void => {
|
|
361
|
+
// Snapshot the current values of the fields that will be overwritten so
|
|
362
|
+
// the user can undo with a single toast action. Only fields we actually
|
|
363
|
+
// applied are snapshotted — fields skipped due to unknown path or
|
|
364
|
+
// failed coercion must not be "undone" to undefined.
|
|
365
|
+
const known = new Map(editable.map((p) => [p.path, p]))
|
|
366
|
+
const snapshot: Record<string, unknown> = {}
|
|
367
|
+
const current = form.getValues()
|
|
368
|
+
const applied: Array<[string, unknown]> = []
|
|
369
|
+
for (const [path, value] of Object.entries(values)) {
|
|
370
|
+
const property = known.get(path)
|
|
371
|
+
if (!property) continue
|
|
372
|
+
const coerced = coerceAiFillValue(property, value)
|
|
373
|
+
if (coerced === undefined) continue
|
|
374
|
+
applied.push([path, coerced])
|
|
375
|
+
snapshot[path] = current[path]
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
for (const [path, value] of applied) {
|
|
379
|
+
// shouldValidate: true so any value the model returned that does not
|
|
380
|
+
// pass the form schema is highlighted to the user immediately rather
|
|
381
|
+
// than discovered on submit.
|
|
382
|
+
form.setValue(path, value as never, { shouldDirty: true, shouldValidate: true })
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (applied.length === 0) {
|
|
386
|
+
notify.error({ key: 'aiFill:noValues' })
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Offer a quick undo via a bottom-center toast — same pattern as draft restore.
|
|
391
|
+
notify.raw(t('aiFill:applied', { count: applied.length }), {
|
|
392
|
+
position: 'bottom-center',
|
|
393
|
+
duration: 8000,
|
|
394
|
+
action: {
|
|
395
|
+
label: t('common:undoDraftRestore'),
|
|
396
|
+
onClick: () => {
|
|
397
|
+
for (const [path, prev] of Object.entries(snapshot)) {
|
|
398
|
+
form.setValue(path, prev as never, { shouldDirty: true, shouldValidate: true })
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
})
|
|
403
|
+
},
|
|
404
|
+
[editable, form, notify, t],
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
const onSubmit: SubmitHandler<FormValues> = async (values) => {
|
|
408
|
+
setSubmitError(null)
|
|
409
|
+
try {
|
|
410
|
+
const result = isNew
|
|
411
|
+
? await create.mutateAsync(values)
|
|
412
|
+
: await update.mutateAsync({ id: recordId!, payload: values })
|
|
413
|
+
// Field-level errors come back as 200 with `record.errors`.
|
|
414
|
+
const errors = result.record.errors as Record<string, { message?: string } | string>
|
|
415
|
+
if (errors && Object.keys(errors).length > 0) {
|
|
416
|
+
for (const [path, err] of Object.entries(errors)) {
|
|
417
|
+
const message = typeof err === 'string' ? err : (err?.message ?? 'Invalid value')
|
|
418
|
+
form.setError(path, { type: 'server', message })
|
|
419
|
+
}
|
|
420
|
+
if (result.record.baseError) {
|
|
421
|
+
setSubmitError(String(result.record.baseError))
|
|
422
|
+
}
|
|
423
|
+
notify.error({ key: 'toast:validationFailed' })
|
|
424
|
+
return
|
|
425
|
+
}
|
|
426
|
+
// Successful submit — drop the saved draft so we don't restore it on
|
|
427
|
+
// the next visit to the new-form route.
|
|
428
|
+
if (isNew) clearDraft()
|
|
429
|
+
notify.success({ key: isNew ? 'toast:created' : 'toast:saved' })
|
|
430
|
+
navigate({ name: 'show', resourceId, recordId: String(result.record.id) })
|
|
431
|
+
} catch (err) {
|
|
432
|
+
const message = err instanceof Error ? err.message : String(err)
|
|
433
|
+
setSubmitError(message)
|
|
434
|
+
notify.error({ key: isNew ? 'toast:createFailed' : 'toast:saveFailed' }, { description: message })
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const onInvalid = (): void => {
|
|
439
|
+
notify.error({ key: 'toast:validationFailed' })
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const handleDelete = async (): Promise<void> => {
|
|
443
|
+
if (!recordId) return
|
|
444
|
+
const ok = await dialogs.confirm({
|
|
445
|
+
title: t('common:confirmDelete'),
|
|
446
|
+
confirmLabel: t('common:delete'),
|
|
447
|
+
destructive: true,
|
|
448
|
+
})
|
|
449
|
+
if (!ok) return
|
|
450
|
+
await remove.mutateAsync(recordId)
|
|
451
|
+
navigate({ name: 'list', resourceId })
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// While editing an existing record, block submit/delete until the record
|
|
455
|
+
// has been hydrated. Otherwise the form submits with empty defaults and
|
|
456
|
+
// wipes server-side fields (e.g. enums coerce '' → null).
|
|
457
|
+
const isHydrating = !isNew && (existing.isLoading || hydratedRecordIdRef.current !== recordId)
|
|
458
|
+
|
|
459
|
+
// ── Keyboard shortcuts ──
|
|
460
|
+
// Ctrl/Cmd+S submits the form. Submit-on-Ctrl+S works even when focus is
|
|
461
|
+
// in an input — that's the standard "save" gesture across editors.
|
|
462
|
+
useHotkey(
|
|
463
|
+
'mod+s',
|
|
464
|
+
() => {
|
|
465
|
+
if (form.formState.isSubmitting || isHydrating) return
|
|
466
|
+
void form.handleSubmit(onSubmit, onInvalid)()
|
|
467
|
+
},
|
|
468
|
+
{ description: isNew ? t('common:create') : t('common:save') },
|
|
469
|
+
)
|
|
470
|
+
|
|
471
|
+
if (!resource) return <div className="p-6">{t('common:loading')}</div>
|
|
472
|
+
|
|
473
|
+
// When editing an existing record that failed to load (e.g. 404), bail out
|
|
474
|
+
// before rendering the form — there's nothing to edit.
|
|
475
|
+
if (!isNew && existing.isError) {
|
|
476
|
+
const { status, message } = parseApiError(existing.error)
|
|
477
|
+
const title =
|
|
478
|
+
status === 404
|
|
479
|
+
? t('errors:notFound')
|
|
480
|
+
: status === 403
|
|
481
|
+
? t('errors:forbidden')
|
|
482
|
+
: t('errors:server')
|
|
483
|
+
return (
|
|
484
|
+
<div className="flex min-h-full flex-col gap-4">
|
|
485
|
+
<PageBreadcrumbs
|
|
486
|
+
items={[
|
|
487
|
+
homeCrumb(t('common:home')),
|
|
488
|
+
{ label: resource.name, to: { name: 'list', resourceId } },
|
|
489
|
+
{ label: recordId ?? '' },
|
|
490
|
+
]}
|
|
491
|
+
/>
|
|
492
|
+
<Card>
|
|
493
|
+
<CardHeader className="flex flex-row items-center justify-between gap-3">
|
|
494
|
+
<CardTitle className="truncate">
|
|
495
|
+
{resource.name} #{recordId}
|
|
496
|
+
</CardTitle>
|
|
497
|
+
<Link to={{ name: 'list', resourceId }}>
|
|
498
|
+
<Button variant="ghost" size="sm">
|
|
499
|
+
{t('common:back')}
|
|
500
|
+
</Button>
|
|
501
|
+
</Link>
|
|
502
|
+
</CardHeader>
|
|
503
|
+
<CardContent>
|
|
504
|
+
<div className="flex items-start gap-3 rounded-lg border border-destructive/50 bg-destructive/10 p-4 dark:bg-destructive/15">
|
|
505
|
+
<AlertCircle className="mt-0.5 size-5 shrink-0 text-destructive" />
|
|
506
|
+
<div className="space-y-1 text-sm">
|
|
507
|
+
<p className="font-semibold text-destructive">{title}</p>
|
|
508
|
+
<p className="text-destructive/90">{message}</p>
|
|
509
|
+
</div>
|
|
510
|
+
</div>
|
|
511
|
+
</CardContent>
|
|
512
|
+
</Card>
|
|
513
|
+
</div>
|
|
514
|
+
)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const modLabel = getModKeyLabel()
|
|
518
|
+
|
|
519
|
+
const recordLabel = existing.data?.record.title || recordId
|
|
520
|
+
const crumbs: BreadcrumbItemSpec[] = [
|
|
521
|
+
homeCrumb(t('common:home')),
|
|
522
|
+
{ label: resource.name, to: { name: 'list', resourceId } },
|
|
523
|
+
...(isNew
|
|
524
|
+
? [{ label: t('common:new') }]
|
|
525
|
+
: [
|
|
526
|
+
{
|
|
527
|
+
label: recordLabel ?? '',
|
|
528
|
+
to: { name: 'show' as const, resourceId, recordId: recordId! },
|
|
529
|
+
},
|
|
530
|
+
{ label: t('common:edit') },
|
|
531
|
+
]),
|
|
532
|
+
]
|
|
533
|
+
|
|
534
|
+
return (
|
|
535
|
+
<div className="flex min-h-full flex-col gap-4">
|
|
536
|
+
<PageBreadcrumbs items={crumbs} />
|
|
537
|
+
<Card className="flex-1">
|
|
538
|
+
<CardHeader className="flex flex-row items-center justify-between gap-3">
|
|
539
|
+
<CardTitle className="truncate">
|
|
540
|
+
{isNew
|
|
541
|
+
? t('common:newRecord', { name: resource.name })
|
|
542
|
+
: t('common:editRecord', { name: resource.name, id: recordId ?? '' })}
|
|
543
|
+
</CardTitle>
|
|
544
|
+
<div className="flex shrink-0 gap-2">
|
|
545
|
+
{aiFillEnabled && (
|
|
546
|
+
<Button
|
|
547
|
+
type="button"
|
|
548
|
+
variant="outline"
|
|
549
|
+
size="sm"
|
|
550
|
+
disabled={form.formState.isSubmitting || isHydrating}
|
|
551
|
+
onClick={() => setAiFillOpen(true)}
|
|
552
|
+
>
|
|
553
|
+
<Sparkles className="size-4" />
|
|
554
|
+
<span className="hidden sm:inline">{t('aiFill:button')}</span>
|
|
555
|
+
</Button>
|
|
556
|
+
)}
|
|
557
|
+
{!isNew && (
|
|
558
|
+
<>
|
|
559
|
+
{features.history && (
|
|
560
|
+
<RevisionsButton resourceId={resourceId} recordId={recordId!} />
|
|
561
|
+
)}
|
|
562
|
+
<Link to={{ name: 'show', resourceId, recordId: recordId! }}>
|
|
563
|
+
<Button variant="outline" size="sm" aria-label={t('common:show')}>
|
|
564
|
+
<Eye className="size-4" />
|
|
565
|
+
<span className="hidden sm:inline">{t('common:show')}</span>
|
|
566
|
+
</Button>
|
|
567
|
+
</Link>
|
|
568
|
+
<Button
|
|
569
|
+
variant="destructive"
|
|
570
|
+
size="sm"
|
|
571
|
+
disabled={remove.isPending || form.formState.isSubmitting || isHydrating}
|
|
572
|
+
onClick={() => void handleDelete()}
|
|
573
|
+
aria-label={t('common:delete')}
|
|
574
|
+
>
|
|
575
|
+
<Trash2 className="size-4" />
|
|
576
|
+
<span className="hidden sm:inline">{t('common:delete')}</span>
|
|
577
|
+
</Button>
|
|
578
|
+
</>
|
|
579
|
+
)}
|
|
580
|
+
</div>
|
|
581
|
+
</CardHeader>
|
|
582
|
+
<Form {...form}>
|
|
583
|
+
<form id="edit-record-form" onSubmit={form.handleSubmit(onSubmit, onInvalid)}>
|
|
584
|
+
<CardContent className="gap-4 pb-6 [column-fill:_balance] md:columns-2">
|
|
585
|
+
{editable.map((property) => (
|
|
586
|
+
<ConditionalField
|
|
587
|
+
key={property.path}
|
|
588
|
+
control={form.control}
|
|
589
|
+
property={property}
|
|
590
|
+
>
|
|
591
|
+
<FormField
|
|
592
|
+
control={form.control}
|
|
593
|
+
name={property.path}
|
|
594
|
+
render={({ field, fieldState }) => (
|
|
595
|
+
<Field
|
|
596
|
+
data-invalid={fieldState.error ? true : undefined}
|
|
597
|
+
className="mb-8 break-inside-avoid"
|
|
598
|
+
>
|
|
599
|
+
<FieldLabel htmlFor={field.name}>
|
|
600
|
+
{property.label}
|
|
601
|
+
{property.description ? (
|
|
602
|
+
<InfoTooltip
|
|
603
|
+
content={property.description}
|
|
604
|
+
ariaLabel={property.description}
|
|
605
|
+
/>
|
|
606
|
+
) : null}
|
|
607
|
+
{property.isRequired && (
|
|
608
|
+
<span className="ml-1 text-destructive">*</span>
|
|
609
|
+
)}
|
|
610
|
+
</FieldLabel>
|
|
611
|
+
<PropertyEditor
|
|
612
|
+
property={property}
|
|
613
|
+
value={field.value}
|
|
614
|
+
onChange={field.onChange}
|
|
615
|
+
disabled={form.formState.isSubmitting}
|
|
616
|
+
resourceId={resourceId}
|
|
617
|
+
/>
|
|
618
|
+
{fieldState.error?.message && (
|
|
619
|
+
<FieldError>{fieldState.error.message}</FieldError>
|
|
620
|
+
)}
|
|
621
|
+
</Field>
|
|
622
|
+
)}
|
|
623
|
+
/>
|
|
624
|
+
</ConditionalField>
|
|
625
|
+
))}
|
|
626
|
+
</CardContent>
|
|
627
|
+
</form>
|
|
628
|
+
</Form>
|
|
629
|
+
</Card>
|
|
630
|
+
{/* Sticky action bar — sibling of Card, same pattern as the list-page
|
|
631
|
+
paginator. Pinned to the viewport bottom while the user scrolls.
|
|
632
|
+
`form="edit-record-form"` ties the submit button to the <form> above
|
|
633
|
+
even though it's not a descendant of that element. */}
|
|
634
|
+
{aiFillOpen && (
|
|
635
|
+
<AiFillDialog
|
|
636
|
+
resourceId={resourceId}
|
|
637
|
+
onClose={() => setAiFillOpen(false)}
|
|
638
|
+
onFilled={applyAiFillValues}
|
|
639
|
+
/>
|
|
640
|
+
)}
|
|
641
|
+
<div className="sticky bottom-0 -mb-px z-20 -mx-2 border-t border-border bg-card px-2 py-3 pr-14 sm:-mx-6 sm:px-6 sm:pr-16">
|
|
642
|
+
<div className="flex items-center justify-between">
|
|
643
|
+
<div>
|
|
644
|
+
{submitError && (
|
|
645
|
+
<span className="text-sm text-destructive">{submitError}</span>
|
|
646
|
+
)}
|
|
647
|
+
</div>
|
|
648
|
+
<div className="flex gap-2">
|
|
649
|
+
<Button
|
|
650
|
+
type="button"
|
|
651
|
+
variant="ghost"
|
|
652
|
+
onClick={() =>
|
|
653
|
+
navigate(
|
|
654
|
+
isNew
|
|
655
|
+
? { name: 'list', resourceId }
|
|
656
|
+
: { name: 'show', resourceId, recordId: recordId! },
|
|
657
|
+
)
|
|
658
|
+
}
|
|
659
|
+
aria-label={t('common:cancel')}
|
|
660
|
+
>
|
|
661
|
+
<X className="size-4" />
|
|
662
|
+
<span className="hidden sm:inline">{t('common:cancel')}</span>
|
|
663
|
+
</Button>
|
|
664
|
+
<Tooltip>
|
|
665
|
+
<TooltipTrigger asChild>
|
|
666
|
+
<Button
|
|
667
|
+
type="submit"
|
|
668
|
+
form="edit-record-form"
|
|
669
|
+
disabled={form.formState.isSubmitting || isHydrating}
|
|
670
|
+
aria-label={isNew ? t('common:create') : t('common:save')}
|
|
671
|
+
>
|
|
672
|
+
{isNew ? <Plus className="size-4" /> : <Save className="size-4" />}
|
|
673
|
+
<span className="hidden sm:inline">
|
|
674
|
+
{isNew ? t('common:create') : t('common:save')}
|
|
675
|
+
</span>
|
|
676
|
+
</Button>
|
|
677
|
+
</TooltipTrigger>
|
|
678
|
+
<TooltipContent className="flex items-center gap-1.5">
|
|
679
|
+
<span>{isNew ? t('common:create') : t('common:save')}</span>
|
|
680
|
+
<span className="inline-flex items-center gap-0.5">
|
|
681
|
+
<Kbd>{modLabel}</Kbd>
|
|
682
|
+
<span className="text-muted-foreground">+</span>
|
|
683
|
+
<Kbd>S</Kbd>
|
|
684
|
+
</span>
|
|
685
|
+
</TooltipContent>
|
|
686
|
+
</Tooltip>
|
|
687
|
+
</div>
|
|
688
|
+
</div>
|
|
689
|
+
</div>
|
|
690
|
+
</div>
|
|
691
|
+
)
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// ── ConditionalField ──────────────────────────────────────────────────────────
|
|
695
|
+
// Wraps a FormField with `showWhen` evaluation. Subscribes only to the named
|
|
696
|
+
// control field via `useWatch` so unrelated form changes do not re-render the
|
|
697
|
+
// branch. When the rule does not match, the entire FormField subtree (label,
|
|
698
|
+
// editor, description, error) is unmounted — and crucially, the schema's
|
|
699
|
+
// matching `superRefine` skips required/format checks for the same field, so
|
|
700
|
+
// hidden branches cannot block submission.
|
|
701
|
+
|
|
702
|
+
interface ConditionalFieldProps {
|
|
703
|
+
control: Control<FormValues>
|
|
704
|
+
property: PropertyJSON
|
|
705
|
+
children: React.ReactNode
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
function ConditionalField({
|
|
709
|
+
control,
|
|
710
|
+
property,
|
|
711
|
+
children,
|
|
712
|
+
}: ConditionalFieldProps): React.ReactElement | null {
|
|
713
|
+
const rule = property.showWhen
|
|
714
|
+
// useWatch with `name: undefined` would subscribe to every field, defeating
|
|
715
|
+
// the purpose. Always pass a string when there's no rule we still want a
|
|
716
|
+
// stable hook order, so we subscribe to the property's own path.
|
|
717
|
+
const watched = useWatch({ control, name: rule?.field ?? property.path })
|
|
718
|
+
if (!rule) return <>{children}</>
|
|
719
|
+
const visible = evaluateShowWhen(rule, { [rule.field]: watched })
|
|
720
|
+
if (!visible) return null
|
|
721
|
+
return <>{children}</>
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// Cheap structural equality for form values. Empty-ish primitives are treated
|
|
725
|
+
// as equal (`''` ≈ `null` ≈ `undefined`) so the persistence watcher does not
|
|
726
|
+
// store a draft for an untouched form whose defaults happen to be `''` vs
|
|
727
|
+
// `undefined`. Falls back to JSON stringify for objects/arrays.
|
|
728
|
+
function valuesEqual(a: unknown, b: unknown): boolean {
|
|
729
|
+
if (a === b) return true
|
|
730
|
+
const aEmpty = a == null || a === ''
|
|
731
|
+
const bEmpty = b == null || b === ''
|
|
732
|
+
if (aEmpty && bEmpty) return true
|
|
733
|
+
if (aEmpty || bEmpty) return false
|
|
734
|
+
if (typeof a !== typeof b) return false
|
|
735
|
+
if (typeof a === 'object') {
|
|
736
|
+
try {
|
|
737
|
+
return JSON.stringify(a) === JSON.stringify(b)
|
|
738
|
+
} catch {
|
|
739
|
+
return false
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
return false
|
|
743
|
+
}
|