@opensaas/stack-ui 0.22.0 → 0.23.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.
Files changed (42) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/CHANGELOG.md +62 -0
  3. package/CLAUDE.md +46 -9
  4. package/README.md +41 -10
  5. package/dist/components/AdminUI.d.ts +1 -1
  6. package/dist/components/AdminUI.d.ts.map +1 -1
  7. package/dist/components/AdminUI.js +17 -2
  8. package/dist/components/Dashboard.d.ts.map +1 -1
  9. package/dist/components/Dashboard.js +13 -4
  10. package/dist/components/ItemForm.d.ts.map +1 -1
  11. package/dist/components/ItemForm.js +6 -65
  12. package/dist/components/ItemFormClient.d.ts +8 -1
  13. package/dist/components/ItemFormClient.d.ts.map +1 -1
  14. package/dist/components/ItemFormClient.js +2 -2
  15. package/dist/components/Navigation.d.ts.map +1 -1
  16. package/dist/components/Navigation.js +12 -1
  17. package/dist/components/SingletonView.d.ts +37 -0
  18. package/dist/components/SingletonView.d.ts.map +1 -0
  19. package/dist/components/SingletonView.js +82 -0
  20. package/dist/index.d.ts +2 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -0
  23. package/dist/lib/operationAccess.d.ts +34 -0
  24. package/dist/lib/operationAccess.d.ts.map +1 -0
  25. package/dist/lib/operationAccess.js +43 -0
  26. package/dist/lib/prepareItemForm.d.ts +35 -0
  27. package/dist/lib/prepareItemForm.d.ts.map +1 -0
  28. package/dist/lib/prepareItemForm.js +85 -0
  29. package/dist/styles/globals.css +12 -0
  30. package/package.json +2 -2
  31. package/src/components/AdminUI.tsx +28 -2
  32. package/src/components/Dashboard.tsx +108 -5
  33. package/src/components/ItemForm.tsx +11 -77
  34. package/src/components/ItemFormClient.tsx +10 -2
  35. package/src/components/Navigation.tsx +58 -1
  36. package/src/components/SingletonView.tsx +228 -0
  37. package/src/index.ts +2 -0
  38. package/src/lib/operationAccess.ts +53 -0
  39. package/src/lib/prepareItemForm.ts +121 -0
  40. package/tests/components/AdminUISingleton.test.tsx +296 -0
  41. package/tests/components/AdminUISingletonSuppress.test.tsx +259 -0
  42. package/tests/components/SingletonNavDashboard.test.tsx +141 -0
@@ -0,0 +1,82 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import Link from 'next/link.js';
3
+ import { ItemFormClient } from './ItemFormClient.js';
4
+ import { formatListName } from '../lib/utils.js';
5
+ import { getDbKey, getUrlKey } from '@opensaas/stack-core';
6
+ import { prepareItemForm } from '../lib/prepareItemForm.js';
7
+ import { isOperationPotentiallyAllowed } from '../lib/operationAccess.js';
8
+ /**
9
+ * Singleton editor — renders a single-record edit form for a list configured
10
+ * with `isSingleton: true`.
11
+ *
12
+ * Resolves the record via the singleton `get()` operation (which auto-creates
13
+ * the row with field defaults when absent, unless `autoCreate: false`), then
14
+ * reuses the same `ItemFormClient` + serialization path as `ItemForm` so the
15
+ * existing field rendering, validation, and `serverAction` save flow apply.
16
+ *
17
+ * A `null` from `get()` is ambiguous at the boundary — it means EITHER an
18
+ * `autoCreate: false` singleton with no row yet, OR that `query` access is
19
+ * denied (access-controlled reads return null/[] silently). We disambiguate
20
+ * using the list's operation-level access:
21
+ *
22
+ * - `query` denied → friendly "no access" message (never an editable form).
23
+ * - `query` allowed + `create` allowed → a create-on-first-save form
24
+ * (`ItemFormClient` in `mode="create"`); core assigns the singleton `id` and
25
+ * enforces the single-record constraint on create.
26
+ * - `query` allowed + `create` denied → friendly "no record yet" message.
27
+ *
28
+ * An update-denied singleton still renders the edit form (the happy path), but
29
+ * the save fails gracefully: the server action's `update` access check returns
30
+ * a denied envelope, which `ItemFormClient` surfaces as an error.
31
+ *
32
+ * Server Component that fetches data and sets up actions.
33
+ */
34
+ export async function SingletonView({ context, config, listKey, basePath = '/admin', serverAction, }) {
35
+ const listConfig = config.lists[listKey];
36
+ const urlKey = getUrlKey(listKey);
37
+ if (!listConfig) {
38
+ return (_jsx("div", { className: "p-8", children: _jsxs("div", { className: "bg-destructive/10 border border-destructive text-destructive rounded-lg p-6", children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "List not found" }), _jsxs("p", { children: ["The list \"", listKey, "\" does not exist in your configuration."] })] }) }));
39
+ }
40
+ // Resolve the singleton record. `get()` auto-creates with field defaults when
41
+ // absent (the default), so a record is the common case. It returns null when
42
+ // either `autoCreate: false` with no row yet, OR `query` access is denied —
43
+ // these are indistinguishable here, so we disambiguate via access below.
44
+ let record = null;
45
+ try {
46
+ const delegate = context.db[getDbKey(listKey)];
47
+ if (delegate?.get) {
48
+ record = await delegate.get();
49
+ }
50
+ }
51
+ catch (error) {
52
+ console.error(`Failed to resolve singleton ${listKey}:`, error);
53
+ }
54
+ if (!record) {
55
+ // A null `get()` is ambiguous (autoCreate:false-empty vs query-denied).
56
+ // Evaluate operation access to choose the safe affordance.
57
+ const accessArgs = { session: context.session, context };
58
+ const canQuery = await isOperationPotentiallyAllowed(listConfig.access?.operation, 'query', accessArgs);
59
+ // Query denied → the session cannot read this singleton at all. Show a
60
+ // friendly message; never an editable/create form.
61
+ if (!canQuery) {
62
+ return (_jsxs("div", { className: "p-8 max-w-4xl", children: [_jsx("div", { className: "mb-8", children: _jsx("h1", { className: "text-3xl font-bold", children: formatListName(listKey) }) }), _jsx("div", { className: "bg-muted/50 border border-border rounded-lg p-6", children: _jsxs("p", { className: "text-muted-foreground", children: ["You don't have access to ", formatListName(listKey), "."] }) })] }));
63
+ }
64
+ // Query allowed but no row → an `autoCreate: false` singleton. Offer a
65
+ // create-on-first-save form only when `create` is actually permitted.
66
+ const canCreate = await isOperationPotentiallyAllowed(listConfig.access?.operation, 'create', accessArgs);
67
+ if (!canCreate) {
68
+ return (_jsxs("div", { className: "p-8 max-w-4xl", children: [_jsx("div", { className: "mb-8", children: _jsx("h1", { className: "text-3xl font-bold", children: formatListName(listKey) }) }), _jsx("div", { className: "bg-muted/50 border border-border rounded-lg p-6", children: _jsxs("p", { className: "text-muted-foreground", children: ["There is no ", formatListName(listKey), " record yet."] }) })] }));
69
+ }
70
+ // Create-on-first-save: render the form in create mode with an empty record.
71
+ // The save goes through the existing `serverAction` create path; core
72
+ // assigns the singleton `id` (always `1`) and enforces the single-record
73
+ // constraint, so the form sends only the user-entered field data.
74
+ const { serializableFields: createFields, initialData: createInitialData, relationshipData: createRelationshipData, } = await prepareItemForm(context, config, listConfig, {});
75
+ return (_jsxs("div", { className: "p-8 max-w-4xl", children: [_jsxs("div", { className: "mb-8", children: [_jsxs(Link, { href: basePath, className: "inline-flex items-center text-sm text-muted-foreground hover:text-foreground mb-4", children: [_jsx("svg", { className: "w-4 h-4 mr-1", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M15 19l-7-7 7-7" }) }), "Back to dashboard"] }), _jsxs("h1", { className: "text-3xl font-bold", children: ["Create ", formatListName(listKey)] })] }), _jsx("div", { className: "bg-card border border-border rounded-lg p-6", children: _jsx(ItemFormClient, { listKey: listKey, urlKey: urlKey, mode: "create", fields: createFields, initialData: createInitialData, basePath: basePath, serverAction: serverAction, relationshipData: createRelationshipData, canDelete: false }) })] }));
76
+ }
77
+ // Reuse the shared field-serialization + relationship-data logic so the
78
+ // singleton editor stays in lockstep with the regular item form.
79
+ const { serializableFields, initialData, relationshipData } = await prepareItemForm(context, config, listConfig, record);
80
+ const itemId = record.id;
81
+ return (_jsxs("div", { className: "p-8 max-w-4xl", children: [_jsxs("div", { className: "mb-8", children: [_jsxs(Link, { href: basePath, className: "inline-flex items-center text-sm text-muted-foreground hover:text-foreground mb-4", children: [_jsx("svg", { className: "w-4 h-4 mr-1", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M15 19l-7-7 7-7" }) }), "Back to dashboard"] }), _jsxs("h1", { className: "text-3xl font-bold", children: ["Edit ", formatListName(listKey)] })] }), _jsx("div", { className: "bg-card border border-border rounded-lg p-6", children: _jsx(ItemFormClient, { listKey: listKey, urlKey: urlKey, mode: "edit", fields: serializableFields, initialData: initialData, itemId: itemId, basePath: basePath, serverAction: serverAction, relationshipData: relationshipData, canDelete: false }) })] }));
82
+ }
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export { ListView } from './components/ListView.js';
6
6
  export { ListViewClient } from './components/ListViewClient.js';
7
7
  export { ItemForm } from './components/ItemForm.js';
8
8
  export { ItemFormClient } from './components/ItemFormClient.js';
9
+ export { SingletonView } from './components/SingletonView.js';
9
10
  export { ConfirmDialog } from './components/ConfirmDialog.js';
10
11
  export { LoadingSpinner } from './components/LoadingSpinner.js';
11
12
  export { SkeletonLoader, TableSkeleton, FormSkeleton } from './components/SkeletonLoader.js';
@@ -18,6 +19,7 @@ export type { ListViewProps } from './components/ListView.js';
18
19
  export type { ListViewClientProps } from './components/ListViewClient.js';
19
20
  export type { ItemFormProps } from './components/ItemForm.js';
20
21
  export type { ItemFormClientProps } from './components/ItemFormClient.js';
22
+ export type { SingletonViewProps } from './components/SingletonView.js';
21
23
  export type { ConfirmDialogProps } from './components/ConfirmDialog.js';
22
24
  export type { LoadingSpinnerProps } from './components/LoadingSpinner.js';
23
25
  export type { SkeletonLoaderProps } from './components/SkeletonLoader.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EAAE,QAAQ,E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yBAAyB,CAAA;AAC3D,YAAY,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC/D,YAAY,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AACjE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAC7D,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAC7D,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAA;AACzE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAC7D,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAA;AACzE,YAAY,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAA;AACvE,YAAY,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAA;AACvE,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAA;AACzE,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAA;AAEzE,YAAY,EACV,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACtB,kBAAkB,EAClB,cAAc,EACd,mBAAmB,GACpB,MAAM,8BAA8B,CAAA;AAGrC,OAAO,EACL,cAAc,EACd,YAAY,EACZ,SAAS,EACT,SAAS,EACT,YAAY,GACb,MAAM,kCAAkC,CAAA;AAEzC,YAAY,EACV,mBAAmB,EACnB,iBAAiB,EACjB,cAAc,EACd,cAAc,EACd,iBAAiB,GAClB,MAAM,kCAAkC,CAAA;AAGzC,OAAO,EAAE,EAAE,EAAE,cAAc,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAG1F,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA"}
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ export { ListView } from './components/ListView.js';
7
7
  export { ListViewClient } from './components/ListViewClient.js';
8
8
  export { ItemForm } from './components/ItemForm.js';
9
9
  export { ItemFormClient } from './components/ItemFormClient.js';
10
+ export { SingletonView } from './components/SingletonView.js';
10
11
  export { ConfirmDialog } from './components/ConfirmDialog.js';
11
12
  export { LoadingSpinner } from './components/LoadingSpinner.js';
12
13
  export { SkeletonLoader, TableSkeleton, FormSkeleton } from './components/SkeletonLoader.js';
@@ -0,0 +1,34 @@
1
+ import type { AccessContext, OperationAccess, Session } from '@opensaas/stack-core';
2
+ /**
3
+ * The names of the operation-level access checks we evaluate in the UI.
4
+ */
5
+ export type OperationAccessName = 'query' | 'create' | 'update' | 'delete';
6
+ /**
7
+ * Evaluate a list's operation-level access control for the current session,
8
+ * coercing the result to a single "is this operation potentially permitted?"
9
+ * boolean.
10
+ *
11
+ * This mirrors how the core access engine treats a result:
12
+ * - `false` → denied
13
+ * - `true` → permitted
14
+ * - a filter object → permitted, but scoped to matching rows
15
+ *
16
+ * Because the UI cannot know whether a returned filter would match the
17
+ * (possibly not-yet-created) singleton row, a filter is treated as "potentially
18
+ * permitted" — the actual operation still runs through the access engine, which
19
+ * re-applies the filter. This helper only decides which affordance to render.
20
+ *
21
+ * Access functions are user-defined and may throw (e.g. they assume a session
22
+ * shape that anonymous requests don't have). A throw is treated as **denied** —
23
+ * the safest outcome, so a misbehaving access function never exposes an
24
+ * editable/create form to a session that might not be allowed.
25
+ *
26
+ * No access function configured for the operation is also treated as denied,
27
+ * matching the core engine's deny-by-default (`checkAccess` returns `false`
28
+ * when `accessControl` is undefined).
29
+ */
30
+ export declare function isOperationPotentiallyAllowed(access: OperationAccess | undefined, operation: OperationAccessName, args: {
31
+ session: Session | null;
32
+ context: AccessContext<unknown>;
33
+ }): Promise<boolean>;
34
+ //# sourceMappingURL=operationAccess.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operationAccess.d.ts","sourceRoot":"","sources":["../../src/lib/operationAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAEnF;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAE1E;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,6BAA6B,CACjD,MAAM,EAAE,eAAe,GAAG,SAAS,EACnC,SAAS,EAAE,mBAAmB,EAC9B,IAAI,EAAE;IAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAAC,OAAO,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;CAAE,GACjE,OAAO,CAAC,OAAO,CAAC,CAiBlB"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Evaluate a list's operation-level access control for the current session,
3
+ * coercing the result to a single "is this operation potentially permitted?"
4
+ * boolean.
5
+ *
6
+ * This mirrors how the core access engine treats a result:
7
+ * - `false` → denied
8
+ * - `true` → permitted
9
+ * - a filter object → permitted, but scoped to matching rows
10
+ *
11
+ * Because the UI cannot know whether a returned filter would match the
12
+ * (possibly not-yet-created) singleton row, a filter is treated as "potentially
13
+ * permitted" — the actual operation still runs through the access engine, which
14
+ * re-applies the filter. This helper only decides which affordance to render.
15
+ *
16
+ * Access functions are user-defined and may throw (e.g. they assume a session
17
+ * shape that anonymous requests don't have). A throw is treated as **denied** —
18
+ * the safest outcome, so a misbehaving access function never exposes an
19
+ * editable/create form to a session that might not be allowed.
20
+ *
21
+ * No access function configured for the operation is also treated as denied,
22
+ * matching the core engine's deny-by-default (`checkAccess` returns `false`
23
+ * when `accessControl` is undefined).
24
+ */
25
+ export async function isOperationPotentiallyAllowed(access, operation, args) {
26
+ const accessControl = access?.[operation];
27
+ // Deny by default — no rule means no access (matches the core engine).
28
+ if (!accessControl)
29
+ return false;
30
+ try {
31
+ const result = await accessControl({
32
+ session: args.session,
33
+ // The access engine passes the same context through to the function.
34
+ context: args.context,
35
+ });
36
+ // `false` denies; `true` or a filter object both mean "potentially allowed".
37
+ return result !== false;
38
+ }
39
+ catch {
40
+ // A throwing access function is treated as denied — never widen access on error.
41
+ return false;
42
+ }
43
+ }
@@ -0,0 +1,35 @@
1
+ import { type AccessContext, OpenSaasConfig } from '@opensaas/stack-core';
2
+ import type { ListConfig } from '@opensaas/stack-core';
3
+ import { type SerializableFieldConfig } from './serializeFieldConfig.js';
4
+ /**
5
+ * Data prepared on the server for the client item form.
6
+ *
7
+ * Everything here is JSON-serializable and contains only what `ItemFormClient`
8
+ * needs to render — see the repo rule on minimal, serializable client props.
9
+ */
10
+ export interface PreparedItemForm {
11
+ /** Field configs stripped of functions/non-serializable props. */
12
+ serializableFields: Record<string, SerializableFieldConfig>;
13
+ /** Initial form values (relationships reduced to ids, client transforms applied). */
14
+ initialData: Record<string, unknown>;
15
+ /** Relationship options keyed by field name. */
16
+ relationshipData: Record<string, Array<{
17
+ id: string;
18
+ label: string;
19
+ }>>;
20
+ }
21
+ /**
22
+ * Build the `include` object needed to hydrate relationship fields when
23
+ * fetching an item for editing.
24
+ */
25
+ export declare function buildRelationshipInclude(listConfig: ListConfig<any>): Record<string, boolean>;
26
+ /**
27
+ * Prepare the serializable props for `ItemFormClient` from an already-fetched
28
+ * record (or an empty object for create).
29
+ *
30
+ * This is shared by `ItemForm` (which fetches via `findUnique`) and
31
+ * `SingletonView` (which resolves via the singleton `get()`), so the
32
+ * relationship/serialization logic lives in exactly one place.
33
+ */
34
+ export declare function prepareItemForm(context: AccessContext<unknown>, config: OpenSaasConfig, listConfig: ListConfig<any>, itemData: Record<string, unknown>): Promise<PreparedItemForm>;
35
+ //# sourceMappingURL=prepareItemForm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prepareItemForm.d.ts","sourceRoot":"","sources":["../../src/lib/prepareItemForm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAY,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACnF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAyB,KAAK,uBAAuB,EAAE,MAAM,2BAA2B,CAAA;AAE/F;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B,kEAAkE;IAClE,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,uBAAuB,CAAC,CAAA;IAC3D,qFAAqF;IACrF,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACpC,gDAAgD;IAChD,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC,CAAA;CACvE;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAEtC,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,GAC1B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAQzB;AAED;;;;;;;GAOG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,aAAa,CAAC,OAAO,CAAC,EAC/B,MAAM,EAAE,cAAc,EAEtB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAC3B,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,OAAO,CAAC,gBAAgB,CAAC,CAsE3B"}
@@ -0,0 +1,85 @@
1
+ import { getDbKey } from '@opensaas/stack-core';
2
+ import { serializeFieldConfigs } from './serializeFieldConfig.js';
3
+ /**
4
+ * Build the `include` object needed to hydrate relationship fields when
5
+ * fetching an item for editing.
6
+ */
7
+ export function buildRelationshipInclude(
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig is generic over TypeInfo
9
+ listConfig) {
10
+ const includeRelationships = {};
11
+ for (const [fieldName, fieldConfig] of Object.entries(listConfig.fields)) {
12
+ if (fieldConfig.type === 'relationship') {
13
+ includeRelationships[fieldName] = true;
14
+ }
15
+ }
16
+ return includeRelationships;
17
+ }
18
+ /**
19
+ * Prepare the serializable props for `ItemFormClient` from an already-fetched
20
+ * record (or an empty object for create).
21
+ *
22
+ * This is shared by `ItemForm` (which fetches via `findUnique`) and
23
+ * `SingletonView` (which resolves via the singleton `get()`), so the
24
+ * relationship/serialization logic lives in exactly one place.
25
+ */
26
+ export async function prepareItemForm(context, config,
27
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig is generic over TypeInfo
28
+ listConfig, itemData) {
29
+ // Fetch relationship options for all relationship fields
30
+ const relationshipData = {};
31
+ for (const [fieldName, fieldConfig] of Object.entries(listConfig.fields)) {
32
+ const fieldConfigAny = fieldConfig;
33
+ if (fieldConfigAny.type === 'relationship') {
34
+ const ref = fieldConfigAny.ref;
35
+ if (ref) {
36
+ // Parse ref format: "ListName.fieldName"
37
+ const relatedListName = ref.split('.')[0];
38
+ const relatedListConfig = config.lists[relatedListName];
39
+ if (relatedListConfig) {
40
+ try {
41
+ const delegate = context.db[getDbKey(relatedListName)];
42
+ const relatedItems = delegate?.findMany ? await delegate.findMany({}) : [];
43
+ // Use 'name' field as label if it exists, otherwise use 'id'
44
+ relationshipData[fieldName] = relatedItems.map((item) => ({
45
+ id: item.id,
46
+ label: (item.name || item.title || item.id) || '',
47
+ }));
48
+ }
49
+ catch (error) {
50
+ console.error(`Failed to fetch relationship items for ${fieldName}:`, error);
51
+ relationshipData[fieldName] = [];
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+ // Serialize field configs to remove non-serializable properties
58
+ const serializableFields = serializeFieldConfigs(listConfig.fields);
59
+ // Transform relationship data in itemData from objects to IDs for form
60
+ // Also apply valueForClientSerialization transformation
61
+ const formData = { ...itemData };
62
+ for (const [fieldName, fieldConfig] of Object.entries(listConfig.fields)) {
63
+ const fieldConfigAny = fieldConfig;
64
+ if (fieldConfigAny.type === 'relationship' && formData[fieldName]) {
65
+ const value = formData[fieldName];
66
+ if (fieldConfigAny.many && Array.isArray(value)) {
67
+ // Many relationship: extract IDs from array of objects
68
+ formData[fieldName] = value.map((item) => item.id);
69
+ }
70
+ else if (value && typeof value === 'object' && 'id' in value) {
71
+ // Single relationship: extract ID from object
72
+ formData[fieldName] = value.id;
73
+ }
74
+ }
75
+ // Apply valueForClientSerialization if defined
76
+ if (fieldConfigAny.ui?.valueForClientSerialization &&
77
+ typeof fieldConfigAny.ui.valueForClientSerialization === 'function') {
78
+ const transformer = fieldConfigAny.ui.valueForClientSerialization;
79
+ formData[fieldName] = transformer({ value: formData[fieldName] });
80
+ }
81
+ }
82
+ // JSON round-trip ensures only serializable data crosses the client boundary
83
+ const initialData = JSON.parse(JSON.stringify(formData));
84
+ return { serializableFields, initialData, relationshipData };
85
+ }
@@ -384,6 +384,9 @@
384
384
  .grid {
385
385
  display: grid;
386
386
  }
387
+ .hidden {
388
+ display: none;
389
+ }
387
390
  .inline-block {
388
391
  display: inline-block;
389
392
  }
@@ -402,6 +405,9 @@
402
405
  .h-4 {
403
406
  height: calc(var(--spacing) * 4);
404
407
  }
408
+ .h-5 {
409
+ height: calc(var(--spacing) * 5);
410
+ }
405
411
  .h-7 {
406
412
  height: calc(var(--spacing) * 7);
407
413
  }
@@ -459,6 +465,12 @@
459
465
  .w-4 {
460
466
  width: calc(var(--spacing) * 4);
461
467
  }
468
+ .w-5 {
469
+ width: calc(var(--spacing) * 5);
470
+ }
471
+ .w-7 {
472
+ width: calc(var(--spacing) * 7);
473
+ }
462
474
  .w-8 {
463
475
  width: calc(var(--spacing) * 8);
464
476
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opensaas/stack-ui",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "description": "Composable React UI components for OpenSaas Stack",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -94,7 +94,7 @@
94
94
  "tailwindcss": "^4.2.1",
95
95
  "typescript": "^5.9.3",
96
96
  "vitest": "^4.1.0",
97
- "@opensaas/stack-core": "0.22.0"
97
+ "@opensaas/stack-core": "0.23.0"
98
98
  },
99
99
  "scripts": {
100
100
  "build": "tsc && npm run build:css",
@@ -1,10 +1,17 @@
1
1
  import * as React from 'react'
2
+ import { redirect } from 'next/navigation.js'
2
3
  import { Navigation } from './Navigation.js'
3
4
  import { Dashboard } from './Dashboard.js'
4
5
  import { ListView } from './ListView.js'
5
6
  import { ItemForm } from './ItemForm.js'
7
+ import { SingletonView } from './SingletonView.js'
6
8
  import type { ServerActionInput } from '../server/types.js'
7
- import { type AccessContext, getListKeyFromUrl, OpenSaasConfig } from '@opensaas/stack-core'
9
+ import {
10
+ type AccessContext,
11
+ getListKeyFromUrl,
12
+ getUrlKey,
13
+ OpenSaasConfig,
14
+ } from '@opensaas/stack-core'
8
15
  import { generateThemeCSS } from '../lib/theme.js'
9
16
 
10
17
  export interface AdminUIProps {
@@ -24,7 +31,7 @@ export interface AdminUIProps {
24
31
  *
25
32
  * Handles routing based on params array:
26
33
  * - [] → Dashboard
27
- * - [list] → ListView
34
+ * - [list] → ListView (or SingletonView when the list is `isSingleton`)
28
35
  * - [list, 'create'] → ItemForm (create)
29
36
  * - [list, id] → ItemForm (edit)
30
37
  */
@@ -52,6 +59,13 @@ export function AdminUI({
52
59
  if (!listKey) {
53
60
  // Dashboard
54
61
  content = <Dashboard context={context} config={config} basePath={basePath} />
62
+ } else if (config.lists[listKey]?.isSingleton && action) {
63
+ // A singleton has a single record edited at its bare [list] route, so the
64
+ // create/id sub-routes (`[list, 'create']` / `[list, id]`) don't apply.
65
+ // Redirect them to the bare editor so old links keep working. This runs
66
+ // before the create/edit ItemForm branches; non-singleton routing below is
67
+ // unchanged.
68
+ redirect(`${basePath}/${getUrlKey(listKey)}`)
55
69
  } else if (action === 'create') {
56
70
  // Create form
57
71
  content = (
@@ -77,6 +91,18 @@ export function AdminUI({
77
91
  serverAction={serverAction}
78
92
  />
79
93
  )
94
+ } else if (config.lists[listKey]?.isSingleton) {
95
+ // Singleton editor: a singleton has a single record, so its bare [list]
96
+ // route renders a single-record editor instead of a list table.
97
+ content = (
98
+ <SingletonView
99
+ context={context}
100
+ config={config}
101
+ listKey={listKey}
102
+ basePath={basePath}
103
+ serverAction={serverAction}
104
+ />
105
+ )
80
106
  } else {
81
107
  // List view
82
108
  const search = typeof searchParams.search === 'string' ? searchParams.search : undefined
@@ -16,9 +16,16 @@ export interface DashboardProps {
16
16
  export async function Dashboard({ context, config, basePath = '/admin' }: DashboardProps) {
17
17
  const lists = Object.keys(config.lists || {})
18
18
 
19
- // Get counts for each list
19
+ // Split lists into standard lists (shown in the counted grid) and singletons
20
+ // (shown in their own "Settings" section). A singleton's count is always 0/1,
21
+ // so the "N items" label is misleading — show a "Configure" affordance instead.
22
+ const standardLists = lists.filter((listKey) => !config.lists[listKey]?.isSingleton)
23
+ const singletonLists = lists.filter((listKey) => config.lists[listKey]?.isSingleton)
24
+
25
+ // Get counts for the standard lists only. Singletons don't show a count, so
26
+ // there's no need to call count() for them here.
20
27
  const listCounts = await Promise.all(
21
- lists.map(async (listKey) => {
28
+ standardLists.map(async (listKey) => {
22
29
  try {
23
30
  const delegate = context.db[getDbKey(listKey)]
24
31
  const count = delegate?.count ? await delegate.count() : 0
@@ -51,7 +58,7 @@ export async function Dashboard({ context, config, basePath = '/admin' }: Dashbo
51
58
  Add lists to your opensaas.config.ts to get started.
52
59
  </p>
53
60
  </Card>
54
- ) : (
61
+ ) : standardLists.length > 0 ? (
55
62
  <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
56
63
  {listCounts.map(({ listKey, count }) => {
57
64
  const urlKey = getUrlKey(listKey)
@@ -97,9 +104,103 @@ export async function Dashboard({ context, config, basePath = '/admin' }: Dashbo
97
104
  )
98
105
  })}
99
106
  </div>
107
+ ) : null}
108
+
109
+ {/* Settings section — singletons present a "Configure" affordance instead
110
+ of a misleading "N items" count. */}
111
+ {singletonLists.length > 0 && (
112
+ <div className={standardLists.length > 0 ? 'mt-12' : ''}>
113
+ <div className="mb-4 flex items-center gap-2">
114
+ <svg
115
+ className="w-5 h-5 text-muted-foreground"
116
+ fill="none"
117
+ stroke="currentColor"
118
+ viewBox="0 0 24 24"
119
+ aria-hidden="true"
120
+ >
121
+ <path
122
+ strokeLinecap="round"
123
+ strokeLinejoin="round"
124
+ strokeWidth={2}
125
+ d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"
126
+ />
127
+ <path
128
+ strokeLinecap="round"
129
+ strokeLinejoin="round"
130
+ strokeWidth={2}
131
+ d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
132
+ />
133
+ </svg>
134
+ <h2 className="text-xl font-semibold text-foreground">Settings</h2>
135
+ </div>
136
+ <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
137
+ {singletonLists.map((listKey) => {
138
+ const urlKey = getUrlKey(listKey)
139
+ return (
140
+ <Link key={listKey} href={`${basePath}/${urlKey}`}>
141
+ <Card className="group hover:border-primary hover:shadow-lg hover:shadow-primary/20 transition-all duration-200 cursor-pointer h-full relative overflow-hidden">
142
+ <div className="absolute inset-0 bg-gradient-to-br from-primary/5 to-accent/5 opacity-0 group-hover:opacity-100 transition-opacity" />
143
+ <CardHeader className="relative">
144
+ <div className="flex items-start justify-between">
145
+ <div>
146
+ <CardTitle className="text-xl group-hover:text-primary transition-colors">
147
+ {formatListName(listKey)}
148
+ </CardTitle>
149
+ </div>
150
+ <div className="text-muted-foreground opacity-60 group-hover:opacity-100 transition-opacity">
151
+ <svg
152
+ className="w-7 h-7"
153
+ fill="none"
154
+ stroke="currentColor"
155
+ viewBox="0 0 24 24"
156
+ aria-hidden="true"
157
+ >
158
+ <path
159
+ strokeLinecap="round"
160
+ strokeLinejoin="round"
161
+ strokeWidth={2}
162
+ d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"
163
+ />
164
+ <path
165
+ strokeLinecap="round"
166
+ strokeLinejoin="round"
167
+ strokeWidth={2}
168
+ d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
169
+ />
170
+ </svg>
171
+ </div>
172
+ </div>
173
+ </CardHeader>
174
+ <CardContent className="relative">
175
+ <div className="flex items-center text-sm font-medium text-primary">
176
+ <span>Configure</span>
177
+ <svg
178
+ className="ml-1 w-4 h-4 group-hover:translate-x-1 transition-transform"
179
+ fill="none"
180
+ stroke="currentColor"
181
+ viewBox="0 0 24 24"
182
+ >
183
+ <path
184
+ strokeLinecap="round"
185
+ strokeLinejoin="round"
186
+ strokeWidth={2}
187
+ d="M9 5l7 7-7 7"
188
+ />
189
+ </svg>
190
+ </div>
191
+ </CardContent>
192
+ </Card>
193
+ </Link>
194
+ )
195
+ })}
196
+ </div>
197
+ </div>
100
198
  )}
101
199
 
102
- {lists.length > 0 && (
200
+ {/* Quick Actions only contains "Create {list}" links for standard lists,
201
+ so hide the whole card when there are no standard lists (e.g. a
202
+ singleton-only admin). */}
203
+ {standardLists.length > 0 && (
103
204
  <Card className="mt-12 bg-gradient-to-br from-accent/10 to-primary/10 border-accent/20">
104
205
  <CardHeader>
105
206
  <CardTitle className="text-lg flex items-center gap-2">
@@ -109,7 +210,9 @@ export async function Dashboard({ context, config, basePath = '/admin' }: Dashbo
109
210
  </CardHeader>
110
211
  <CardContent>
111
212
  <div className="flex flex-wrap gap-3">
112
- {lists.map((listKey) => {
213
+ {/* Singletons have a single record (no create), so they're
214
+ excluded here — only standard lists get a "Create" quick-action. */}
215
+ {standardLists.map((listKey) => {
113
216
  const urlKey = getUrlKey(listKey)
114
217
  return (
115
218
  <Link