wcz-layout 9.7.0 → 10.0.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/AggregatedCell-BtKC5-m6.mjs +497 -0
- package/dist/AggregatedCell-BtKC5-m6.mjs.map +1 -0
- package/dist/Approval-C-hF4jbT.mjs.map +1 -1
- package/dist/DialogsContext-DLqA8RJ_.mjs.map +1 -1
- package/dist/Email-BzQQkbe9.mjs.map +1 -1
- package/dist/FileMeta-DDqUju1Y.mjs.map +1 -1
- package/dist/LayoutTable-4DLs-NI-.d.mts +752 -0
- package/dist/{QueryClientParam-Cbb0MIKg.d.mts → QueryClientParam-JcH0mJWl.d.mts} +2 -2
- package/dist/RouterListItemButton-ULMlaQh6.mjs.map +1 -1
- package/dist/{User-1gKoYJvW.d.mts → User-D4__w1I1.d.mts} +4 -2
- package/dist/approval-DFExxyAJ.mjs +84 -0
- package/dist/approval-DFExxyAJ.mjs.map +1 -0
- package/dist/auth.d.mts +21 -11
- package/dist/auth.mjs +43 -26
- package/dist/auth.mjs.map +1 -1
- package/dist/components.d.mts +22 -30
- package/dist/components.mjs +786 -139
- package/dist/components.mjs.map +1 -1
- package/dist/createApi-CLYWCzR2.mjs +27 -0
- package/dist/createApi-CLYWCzR2.mjs.map +1 -0
- package/dist/data/client.d.mts +702 -1152
- package/dist/data/client.mjs +4 -3
- package/dist/data/client.mjs.map +1 -1
- package/dist/data/server.d.mts +3 -3
- package/dist/data/server.mjs +4 -2
- package/dist/data/server.mjs.map +1 -1
- package/dist/{file-BWcNzq55.mjs → file-CZwQfvTU.mjs} +5 -24
- package/dist/file-CZwQfvTU.mjs.map +1 -0
- package/dist/{file-DimNsRly.mjs → file-aSQto2rw.mjs} +6 -7
- package/dist/file-aSQto2rw.mjs.map +1 -0
- package/dist/hooks.d.mts +7 -46
- package/dist/hooks.mjs +2578 -239
- package/dist/hooks.mjs.map +1 -1
- package/dist/index.d.mts +28 -9
- package/dist/index.mjs +1248 -500
- package/dist/index.mjs.map +1 -1
- package/dist/middleware.d.mts +20 -19
- package/dist/middleware.mjs +47 -2
- package/dist/middleware.mjs.map +1 -1
- package/dist/models.d.mts +17 -10
- package/dist/models.mjs.map +1 -1
- package/dist/{peoplesoft-CskGsqd6.d.mts → peoplesoft-BY_75WTS.d.mts} +2 -2
- package/dist/{peoplesoft-DTPGpN2h.mjs → peoplesoft-CQnc01Hc.mjs} +24 -81
- package/dist/peoplesoft-CQnc01Hc.mjs.map +1 -0
- package/dist/providers-Dbl95AxR.mjs +74 -0
- package/dist/providers-Dbl95AxR.mjs.map +1 -0
- package/dist/tokens-D8CVIFTb.mjs +143 -0
- package/dist/tokens-D8CVIFTb.mjs.map +1 -0
- package/dist/{utils-5x4uNxsX.d.mts → utils-Bhi0KiBx.d.mts} +18 -6
- package/dist/{utils-DjdexspV.mjs → utils-KaM5VMTJ.mjs} +64 -42
- package/dist/utils-KaM5VMTJ.mjs.map +1 -0
- package/dist/utils.d.mts +2 -2
- package/dist/utils.mjs +2 -2
- package/dist/vite.d.mts +1 -2
- package/dist/vite.mjs +3 -3
- package/dist/vite.mjs.map +1 -1
- package/package.json +23 -23
- package/skills/client-db/SKILL.md +104 -96
- package/skills/db-schema/SKILL.md +46 -33
- package/skills/dialogs/SKILL.md +21 -27
- package/skills/forms/SKILL.md +60 -39
- package/skills/general/SKILL.md +56 -25
- package/skills/general/references/exports.md +106 -0
- package/skills/migrations/SKILL.md +53 -2
- package/skills/migrations/versions/migrate-v9-to-v10.md +581 -0
- package/skills/routing/SKILL.md +50 -60
- package/skills/server-functions/SKILL.md +36 -56
- package/skills/services/SKILL.md +7 -4
- package/skills/services/docs/approval.md +2 -2
- package/skills/services/docs/peoplesoft.md +1 -1
- package/skills/start/SKILL.md +9 -5
- package/skills/start/steps/05-database-setup.md +52 -52
- package/skills/start/steps/06-entra-setup.md +1 -1
- package/skills/start/steps/07-vault-setup.md +5 -12
- package/skills/table/SKILL.md +275 -0
- package/skills/unslop/SKILL.md +80 -0
- package/dist/NotificationContext-CgwUOeW0.mjs +0 -7
- package/dist/NotificationContext-CgwUOeW0.mjs.map +0 -1
- package/dist/authMiddleware-DPly0b_T.mjs +0 -48
- package/dist/authMiddleware-DPly0b_T.mjs.map +0 -1
- package/dist/entra-CJSyeQsx.mjs +0 -109
- package/dist/entra-CJSyeQsx.mjs.map +0 -1
- package/dist/file-BWcNzq55.mjs.map +0 -1
- package/dist/file-DimNsRly.mjs.map +0 -1
- package/dist/msalServer-DLwA7_ta.mjs +0 -30
- package/dist/msalServer-DLwA7_ta.mjs.map +0 -1
- package/dist/peoplesoft-DTPGpN2h.mjs.map +0 -1
- package/dist/useDialogs-BUzVrTSP.mjs +0 -248
- package/dist/useDialogs-BUzVrTSP.mjs.map +0 -1
- package/dist/user-Dzv0N9fj.d.mts +0 -16
- package/dist/utils-DjdexspV.mjs.map +0 -1
- package/skills/data-grid/SKILL.md +0 -142
- package/skills/migrations/versions/migrate-v9-to-v10.tsx +0 -9
|
@@ -1,124 +1,132 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: client-db
|
|
3
|
-
description: "Use when
|
|
3
|
+
description: "Use when reading or writing client data with TanStack DB: collections, live queries, route preloading, optimistic writes, eager/on-demand sync, or browser SQLite persistence."
|
|
4
|
+
metadata:
|
|
5
|
+
type: convention
|
|
6
|
+
library: wcz-layout
|
|
4
7
|
---
|
|
5
8
|
|
|
6
|
-
> Mechanics (
|
|
9
|
+
> Mechanics (the live-query builder, `createOptimisticAction`, collection adapters,
|
|
10
|
+
> browser persistence) are covered by TanStack's own skills. List the skills shipped by
|
|
11
|
+
> `@tanstack/db` and `@tanstack/react-db` and load whichever cover the task at hand.
|
|
12
|
+
> This skill adds the wcz-layout conventions on top, and overrides upstream where noted.
|
|
7
13
|
|
|
8
14
|
## Rules
|
|
9
15
|
|
|
10
|
-
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
- One `DbClient` per request, built in `src/router.tsx` and put on the router context
|
|
17
|
+
next to `queryClient`. Components reach it with `useDbClient()`, loaders with
|
|
18
|
+
`context.dbClient`.
|
|
19
|
+
- Declare collections as descriptors: `collectionOptions(id, (client) => ...)`. Take the
|
|
20
|
+
query client from the factory with `client.requireDependency<QueryClient>("queryClient")`,
|
|
21
|
+
never a module-scope one — it is built per request and a singleton leaks across SSR.
|
|
22
|
+
- Export a `use<Table>Collection` hook next to each collection. It is the only way components
|
|
23
|
+
get a live collection to write to.
|
|
24
|
+
- Define every live query once in `src/db/queries/<table>.ts` as
|
|
25
|
+
`{ query: (q: InitialQueryBuilder) => ... }`. Reuse that definition in loaders and
|
|
26
|
+
components.
|
|
27
|
+
- Preload in the route loader and return the promise:
|
|
28
|
+
`loader: ({ context }) => context.dbClient.preloadLiveQuery(<x>QueryOptions)`.
|
|
29
|
+
This transports the query result. Explicit collection preload transports source rows
|
|
30
|
+
and is a no-op for on-demand collections.
|
|
31
|
+
- `useLiveQuery` for list pages, `useLiveSuspenseQuery` for detail components under
|
|
32
|
+
suspense. Suspense data is initialized, but `.findOne()` still returns `T | undefined`;
|
|
33
|
+
handle not-found explicitly.
|
|
34
|
+
- Shape data in the query: filter, order, group and aggregate only the fields the
|
|
35
|
+
consumer needs. Do not load broad results and transform them in component code.
|
|
36
|
+
- Mutations go through `createOptimisticAction`: mirror the server write in `onMutate`,
|
|
37
|
+
call the server function in `mutationFn`, then
|
|
38
|
+
`await utils.refetch({ throwOnError: true })` on every collection that function writes.
|
|
39
|
+
Await `transaction.isPersisted.promise` at the call site and surface failures with
|
|
40
|
+
`alert()`.
|
|
41
|
+
- `syncMode: "eager"` for top-level collections, `"on-demand"` for child data and anything expected past ~10k rows.
|
|
42
|
+
- Increment persisted `schemaVersion` whenever the row shape changes.
|
|
26
43
|
|
|
27
44
|
## Examples
|
|
28
45
|
|
|
29
46
|
```ts
|
|
30
|
-
// src/db
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
47
|
+
// src/db/collections/<table>.ts
|
|
48
|
+
import { persistedCollectionOptions } from "@tanstack/browser-db-sqlite-persistence";
|
|
49
|
+
import { queryCollectionOptions } from "@tanstack/query-db-collection";
|
|
50
|
+
import { collectionOptions, useDbClient } from "@tanstack/react-db";
|
|
51
|
+
import type { QueryClient } from "@tanstack/react-query";
|
|
52
|
+
import { browserPersistence } from "../persistence";
|
|
53
|
+
import { TodoSchema } from "~/lib/schemas/todo";
|
|
54
|
+
import { selectTodos } from "~/server/actions/todo";
|
|
55
|
+
|
|
56
|
+
const TODOS_ID = "todos";
|
|
57
|
+
|
|
58
|
+
export const todoCollection = collectionOptions(TODOS_ID, (client) => {
|
|
59
|
+
const options = queryCollectionOptions({
|
|
60
|
+
id: TODOS_ID,
|
|
61
|
+
queryKey: [TODOS_ID],
|
|
62
|
+
queryFn: () => selectTodos(),
|
|
35
63
|
getKey: ({ id }) => id,
|
|
36
|
-
schema:
|
|
37
|
-
queryClient: queryClient,
|
|
64
|
+
schema: TodoSchema,
|
|
65
|
+
queryClient: client.requireDependency<QueryClient>("queryClient"),
|
|
38
66
|
syncMode: "eager",
|
|
39
|
-
})
|
|
40
|
-
);
|
|
67
|
+
});
|
|
41
68
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
import { fromTanDb } from "agnostic-query/tanstack-db";
|
|
69
|
+
const persistence = browserPersistence();
|
|
70
|
+
if (!persistence) return options;
|
|
45
71
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
schema: BookSchema,
|
|
52
|
-
queryClient: queryClient,
|
|
53
|
-
syncMode: "on-demand",
|
|
54
|
-
autoIndex: "eager",
|
|
55
|
-
defaultIndexType: BasicIndex,
|
|
56
|
-
}),
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
// Preload
|
|
60
|
-
export const Route = createFileRoute("/")({
|
|
61
|
-
loader: () => {
|
|
62
|
-
void librariesCollection.preload();
|
|
63
|
-
},
|
|
72
|
+
return persistedCollectionOptions({
|
|
73
|
+
...options,
|
|
74
|
+
persistence,
|
|
75
|
+
schemaVersion: 1, // Increment when the persisted row shape changes.
|
|
76
|
+
}) as typeof options;
|
|
64
77
|
});
|
|
65
78
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
export const useTodoCollection = () => useDbClient().collection(todoCollection);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
// src/db/queries/<table>.ts
|
|
84
|
+
import type { InitialQueryBuilder } from "@tanstack/react-db";
|
|
85
|
+
import { eq } from "@tanstack/react-db";
|
|
86
|
+
import { todoCollection } from "../collections/todo";
|
|
87
|
+
|
|
88
|
+
export const todosQueryOptions = {
|
|
89
|
+
query: (q: InitialQueryBuilder) =>
|
|
90
|
+
q.from({ todo: todoCollection }).orderBy(({ todo }) => todo.id, "desc"),
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const todoByIdQueryOptions = (id: string) => ({
|
|
94
|
+
query: (q: InitialQueryBuilder) =>
|
|
95
|
+
q
|
|
96
|
+
.from({ todo: todoCollection })
|
|
97
|
+
.where(({ todo }) => eq(todo.id, id))
|
|
98
|
+
.findOne(),
|
|
78
99
|
});
|
|
100
|
+
```
|
|
79
101
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
.from({ book: bookCollection })
|
|
96
|
-
.where(({ book }) => eq(book.libraryId, library.id))
|
|
97
|
-
.orderBy(({ book }) => book.title, "asc")
|
|
98
|
-
.select(({ book }) => ({
|
|
99
|
-
id: book.id,
|
|
100
|
-
title: book.title,
|
|
101
|
-
})),
|
|
102
|
-
),
|
|
103
|
-
})),
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
// form submission handler
|
|
107
|
-
const handleOnSubmit = createOptimisticAction<Library>({
|
|
108
|
-
onMutate: (formValues) => {
|
|
109
|
-
libraryCollection.update(id, (prev) => Object.assign(prev, formValues));
|
|
102
|
+
```tsx
|
|
103
|
+
// route + component
|
|
104
|
+
export const Route = createFileRoute("/todos/$id")({
|
|
105
|
+
beforeLoad: requireAuth("admin"),
|
|
106
|
+
loader: ({ context, params }) =>
|
|
107
|
+
context.dbClient.preloadLiveQuery(todoByIdQueryOptions(params.id)),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const { id } = Route.useParams();
|
|
111
|
+
const todos = useTodoCollection();
|
|
112
|
+
const { data } = useLiveSuspenseQuery(todoByIdQueryOptions(id));
|
|
113
|
+
|
|
114
|
+
const handleOnDelete = createOptimisticAction<string>({
|
|
115
|
+
onMutate: (id) => {
|
|
116
|
+
todos.delete(id);
|
|
110
117
|
},
|
|
111
|
-
mutationFn: async (
|
|
112
|
-
await
|
|
113
|
-
await
|
|
118
|
+
mutationFn: async (id) => {
|
|
119
|
+
await deleteTodo({ data: { id } });
|
|
120
|
+
await todos.utils.refetch({ throwOnError: true });
|
|
114
121
|
},
|
|
115
122
|
});
|
|
116
123
|
|
|
117
|
-
// calling the handler
|
|
118
124
|
try {
|
|
119
|
-
const transaction =
|
|
125
|
+
const transaction = handleOnDelete(id);
|
|
120
126
|
await transaction.isPersisted.promise;
|
|
121
127
|
} catch (error) {
|
|
122
|
-
if (error instanceof Error) alert(error.message);
|
|
128
|
+
if (error instanceof Error) await alert(error.message);
|
|
123
129
|
}
|
|
130
|
+
|
|
131
|
+
if (!data) return null;
|
|
124
132
|
```
|
|
@@ -1,55 +1,64 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: db-schema
|
|
3
|
-
description: "Use when
|
|
3
|
+
description: "Use when changing PostgreSQL tables, columns, constraints, indexes, enums, foreign keys, relations, migrations, or Drizzle-derived Zod schemas in a wcz-layout app."
|
|
4
|
+
metadata:
|
|
5
|
+
type: convention
|
|
6
|
+
library: wcz-layout
|
|
4
7
|
---
|
|
5
8
|
|
|
6
|
-
# Database
|
|
9
|
+
# Database schema patterns
|
|
7
10
|
|
|
8
11
|
## Rules
|
|
9
12
|
|
|
10
13
|
- One table per file, named after the table (e.g. `bookTable` lives in `book.ts`). Never put multiple tables in one file, even when they are related — a parent and its children each get their own file.
|
|
11
|
-
-
|
|
12
|
-
- For `uuid` columns, do not generate random values in the schema.
|
|
14
|
+
- Use `uuid` for application-table primary keys and `snakeCase` for generating tables.
|
|
15
|
+
- For `uuid` columns, do not generate random values in the schema. The client generates the id with `uuidv7()` from `wcz-layout/utils` when creating the record.
|
|
13
16
|
- Always add `withTimezone: true` to timestamp columns.
|
|
14
|
-
- Always
|
|
17
|
+
- Always declare `onDelete` explicitly on every foreign key — never rely on the default. Pick the behavior from the business rule: `"cascade"` when the child is meaningless without its parent, `"restrict"` when the parent must not be deleted while children exist, `"set null"` for optional references.
|
|
18
|
+
- Index every foreign key column. Postgres does not create one for you.
|
|
15
19
|
- Define relations in a central `relations.ts` file.
|
|
16
|
-
-
|
|
17
|
-
-
|
|
20
|
+
- Declare enums with `pgEnum` in the table's own file, export them, and reference `enumValues` everywhere else — never re-declare the values.
|
|
21
|
+
- Generate the full client record schema with `createSelectSchema` from `drizzle-orm/zod`. Use `createInsertSchema`; override fields with stricter rules (trim, min/max) where needed.
|
|
22
|
+
- The second argument of `createSelectSchema` refines **existing columns only**. Adding a key
|
|
23
|
+
that is not a column is a type error. Attach related data with `.extend()` after it.
|
|
18
24
|
- Do not auto-generate migrations unless the user explicitly asks. When asked, run `npm run db:generate`; migrations apply automatically at app startup while `DATABASE_AUTO_MIGRATE=true`.
|
|
19
|
-
- Export enums from the schema file and reference enumValues everywhere else — never re-declare the values.
|
|
20
|
-
|
|
21
|
-
## File Placement
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
drizzle-orm - defineRelations utility
|
|
25
|
-
drizzle-orm/pg-core - snakeCase table builder and column types
|
|
26
|
-
drizzle-orm/zod - createSelectSchema
|
|
27
|
-
zod - Zod
|
|
28
|
-
src/server/db/schemas/ - Drizzle table schemas, one table per file + relations.ts
|
|
29
|
-
src/server/db/migrations/ - Drizzle migration files
|
|
30
|
-
src/lib/schemas/ - Zod schemas (shared between client and server)
|
|
31
|
-
wcz-layout/utils - `t` translation function
|
|
32
|
-
```
|
|
33
25
|
|
|
34
26
|
## Examples
|
|
35
27
|
|
|
36
28
|
```ts
|
|
37
29
|
// src/server/db/schemas/library.ts
|
|
30
|
+
import { snakeCase, text, uuid } from "drizzle-orm/pg-core";
|
|
31
|
+
|
|
38
32
|
export const libraryTable = snakeCase.table("libraries", {
|
|
39
33
|
id: uuid().primaryKey(),
|
|
40
34
|
name: text().notNull(),
|
|
41
35
|
});
|
|
42
36
|
|
|
43
37
|
// src/server/db/schemas/book.ts
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
38
|
+
import { index, pgEnum, snakeCase, text, timestamp, uuid } from "drizzle-orm/pg-core";
|
|
39
|
+
import { libraryTable } from "./library";
|
|
40
|
+
|
|
41
|
+
export const bookStatusEnum = pgEnum("book_status", ["draft", "published"]);
|
|
42
|
+
|
|
43
|
+
export const bookTable = snakeCase.table(
|
|
44
|
+
"books",
|
|
45
|
+
{
|
|
46
|
+
id: uuid().primaryKey(),
|
|
47
|
+
title: text().notNull(),
|
|
48
|
+
status: bookStatusEnum().notNull().default("draft"),
|
|
49
|
+
libraryId: uuid()
|
|
50
|
+
.notNull()
|
|
51
|
+
.references(() => libraryTable.id, { onDelete: "cascade" }),
|
|
52
|
+
createdAt: timestamp({ withTimezone: true }).defaultNow().notNull(),
|
|
53
|
+
},
|
|
54
|
+
(table) => [index().on(table.libraryId)],
|
|
55
|
+
);
|
|
51
56
|
|
|
52
57
|
// src/server/db/schemas/relations.ts
|
|
58
|
+
import { defineRelations } from "drizzle-orm";
|
|
59
|
+
import { bookTable } from "./book";
|
|
60
|
+
import { libraryTable } from "./library";
|
|
61
|
+
|
|
53
62
|
export const relations = defineRelations(
|
|
54
63
|
{ libraryTable, bookTable },
|
|
55
64
|
({ one, many, libraryTable: library, bookTable: book }) => ({
|
|
@@ -62,18 +71,22 @@ export const relations = defineRelations(
|
|
|
62
71
|
}),
|
|
63
72
|
);
|
|
64
73
|
|
|
65
|
-
// src/lib/schemas/<
|
|
74
|
+
// src/lib/schemas/<table>.ts
|
|
75
|
+
import { createSelectSchema } from "drizzle-orm/zod";
|
|
76
|
+
import { t } from "wcz-layout/utils";
|
|
77
|
+
import type { z } from "zod";
|
|
78
|
+
import { bookTable } from "~/server/db/schemas/book";
|
|
79
|
+
import { PageSchema } from "./page";
|
|
80
|
+
|
|
66
81
|
export const BookSchema = createSelectSchema(bookTable, {
|
|
67
82
|
title: (schema) =>
|
|
68
83
|
schema
|
|
69
84
|
.trim()
|
|
70
85
|
.min(1, t("Validation.Required"))
|
|
71
86
|
.max(255, t("Validation.MaxLength", { length: 255 })),
|
|
87
|
+
}).extend({
|
|
72
88
|
pages: PageSchema.array().min(1, t("Validation.Required")),
|
|
73
|
-
})
|
|
74
|
-
...data,
|
|
75
|
-
pages: data.pages.map((page) => ({ ...page, bookId: data.id })),
|
|
76
|
-
}));
|
|
89
|
+
});
|
|
77
90
|
|
|
78
91
|
export type Book = z.infer<typeof BookSchema>;
|
|
79
92
|
```
|
package/skills/dialogs/SKILL.md
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: dialogs
|
|
3
|
-
description: "Use
|
|
3
|
+
description: "Use for acknowledgement alerts, confirmation prompts, or custom MUI Dialogs opened through useDialogs."
|
|
4
|
+
metadata:
|
|
5
|
+
type: convention
|
|
6
|
+
library: wcz-layout
|
|
4
7
|
---
|
|
5
8
|
|
|
6
9
|
## Rules
|
|
7
10
|
|
|
8
|
-
-
|
|
11
|
+
- Use `useDialogs()` instead of mounting another provider. It returns `{ alert, confirm, open, close }`.
|
|
9
12
|
- Use `confirm()` for destructive or user-confirmed actions.
|
|
10
13
|
- Use `alert()` for informational or error messages that require acknowledgement.
|
|
11
14
|
- Use `open()` for feature-specific custom dialogs with typed payloads.
|
|
12
|
-
- Custom dialogs
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
src/components/dialogs/ — shared dialogs used across features
|
|
20
|
-
src/routes/<feature>s/-components/ — feature-scoped dialogs
|
|
21
|
-
wcz-layout/hooks — useDialogs hook, DialogProps type, useTranslation
|
|
22
|
-
src/lib/locales/ — translation keys
|
|
23
|
-
@mui/material — MUI Dialog components
|
|
24
|
-
```
|
|
15
|
+
- Custom dialogs receive `DialogProps<TPayload, TResult>`. `open()` resolves with
|
|
16
|
+
whatever the dialog passes to `onClose(result)`, so a dialog that answers a
|
|
17
|
+
question declares its result type and the caller awaits it. Leave `TResult` off
|
|
18
|
+
only for dialogs that return nothing.
|
|
19
|
+
- Use `close(dialog, result)` only to close outside the dialog component, passing the
|
|
20
|
+
promise returned by `open()`. Custom dialogs normally call their injected `onClose(result)`.
|
|
21
|
+
- Colocate feature-specific dialogs in `routes/<feature>s/-components`; put dialogs used by more than one route in `src/components`.
|
|
25
22
|
|
|
26
23
|
## Examples
|
|
27
24
|
|
|
@@ -31,7 +28,7 @@ const { confirm } = useDialogs();
|
|
|
31
28
|
const { t } = useTranslation();
|
|
32
29
|
|
|
33
30
|
const confirmed = await confirm(t("DeleteConfirmation", { count: selectedIds.length }));
|
|
34
|
-
if (confirmed) await
|
|
31
|
+
if (confirmed) await deleteFeatures({ data: selectedIds });
|
|
35
32
|
|
|
36
33
|
// alert dialog
|
|
37
34
|
const { alert } = useDialogs();
|
|
@@ -39,15 +36,15 @@ const { t } = useTranslation();
|
|
|
39
36
|
|
|
40
37
|
try {
|
|
41
38
|
await saveFeature(values);
|
|
42
|
-
await alert(t("FeatureSavedSuccessfully"));
|
|
43
39
|
} catch (error) {
|
|
44
40
|
await alert(error instanceof Error ? error.message : t("UnknownError"));
|
|
45
41
|
}
|
|
46
42
|
|
|
47
|
-
// custom dialog
|
|
43
|
+
// custom dialog that returns a result
|
|
48
44
|
const { open } = useDialogs();
|
|
49
45
|
|
|
50
|
-
await open(EditFeatureDialog, { id });
|
|
46
|
+
const saved = await open(EditFeatureDialog, { id });
|
|
47
|
+
if (saved) await features.utils.refetch({ throwOnError: true });
|
|
51
48
|
|
|
52
49
|
// custom dialog component
|
|
53
50
|
interface Payload {
|
|
@@ -58,19 +55,16 @@ export const EditFeatureDialog = ({
|
|
|
58
55
|
payload,
|
|
59
56
|
open,
|
|
60
57
|
onClose,
|
|
61
|
-
}: DialogProps<Payload>) => {
|
|
58
|
+
}: DialogProps<Payload, boolean>) => {
|
|
62
59
|
const { t } = useTranslation();
|
|
63
60
|
|
|
64
61
|
return (
|
|
65
|
-
<Dialog fullWidth open={open} onClose={() => onClose()}>
|
|
62
|
+
<Dialog fullWidth open={open} onClose={() => onClose(false)}>
|
|
66
63
|
<DialogTitle>{t("EditFeature")}</DialogTitle>
|
|
67
|
-
<DialogContent>
|
|
68
|
-
{payload.id}
|
|
69
|
-
</DialogContent>
|
|
64
|
+
<DialogContent>{payload.id}</DialogContent>
|
|
70
65
|
<DialogActions>
|
|
71
|
-
<Button onClick={() => onClose()}>
|
|
72
|
-
|
|
73
|
-
</Button>
|
|
66
|
+
<Button onClick={() => onClose(false)}>{t("Cancel")}</Button>
|
|
67
|
+
<Button onClick={() => onClose(true)}>{t("Save")}</Button>
|
|
74
68
|
</DialogActions>
|
|
75
69
|
</Dialog>
|
|
76
70
|
);
|
package/skills/forms/SKILL.md
CHANGED
|
@@ -1,32 +1,47 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: forms
|
|
3
|
-
description: "Use when
|
|
3
|
+
description: "Use when building or changing forms, including fields, validation, submission, reset, date values, async initial values, or reusable subforms."
|
|
4
|
+
metadata:
|
|
5
|
+
type: convention
|
|
6
|
+
library: wcz-layout
|
|
4
7
|
---
|
|
5
8
|
|
|
6
|
-
# Form
|
|
9
|
+
# Form patterns
|
|
10
|
+
|
|
11
|
+
> Mechanics (`createFormHook`, `withForm`, field API, validation lifecycle) belong to
|
|
12
|
+
> TanStack Form v1. `useLayoutForm` is its `useAppForm` and `withLayoutForm` its `withForm`,
|
|
13
|
+
> pre-bound to the MUI field set below.
|
|
7
14
|
|
|
8
15
|
## Rules
|
|
9
16
|
|
|
10
|
-
- Always use `useLayoutForm` with pre-defined components.
|
|
17
|
+
- Always use `useLayoutForm` with the pre-defined components.
|
|
18
|
+
- Fields are not importable; reach them through render props. `form.AppField` yields
|
|
19
|
+
`field.Autocomplete`, `field.Checkbox`, `field.DatePicker`, `field.DateTimePicker`,
|
|
20
|
+
`field.NumberField`, `field.RadioGroup`, `field.Slider`, `field.Switch`, `field.TextField`
|
|
21
|
+
and `field.TimePicker`. `form.AppForm` yields `form.SubmitButton`, and nothing else. Pick
|
|
22
|
+
the field that matches the value.
|
|
11
23
|
- Define `width` for all form fields based on the expected content length.
|
|
12
|
-
- Reuse Zod schemas from `src/lib/schemas
|
|
24
|
+
- Reuse Zod schemas from `src/lib/schemas/`, derived from Drizzle tables with `createSelectSchema`.
|
|
25
|
+
- `field.DatePicker`, `field.TimePicker` and `field.DateTimePicker` hold `Dayjs`, not `Date`.
|
|
26
|
+
A Drizzle `timestamp` column gives you a `Date`, so convert at both boundaries:
|
|
27
|
+
`dayjs(row.dueAt)` into `defaultValues`, `.toDate()` on the way out in `onSubmit`.
|
|
13
28
|
- Use translation for labels, helper text, validation messages and submit.
|
|
14
|
-
- Reset the form after successful create
|
|
15
|
-
|
|
29
|
+
- Reset the form after a successful create, and let the submit handler reject on failure so
|
|
30
|
+
a failed save keeps what the user typed.
|
|
31
|
+
- `SubmitButton` calls `form.handleSubmit()` and owns its loading and disabled state.
|
|
32
|
+
- `reset()` restores the `defaultValues` captured when the form was constructed. If those
|
|
33
|
+
contain a generated `uuidv7()` id, pass a fresh one to `reset({ ...defaults, id: uuidv7() })`
|
|
34
|
+
or the second create collides on the primary key.
|
|
35
|
+
- `defaultValues` only re-syncs into a mounted form while it is still untouched. On edit
|
|
36
|
+
routes, preload the query in the loader so the row is there before the form mounts.
|
|
16
37
|
- When fetching data for a form, always use a suspense query.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
src/routes/<feature>s/-components/ - route-scoped forms
|
|
22
|
-
src/lib/schemas/ — Zod schemas (shared between client and server)
|
|
23
|
-
wcz-layout/hooks - useLayoutForm hook, useTranslation
|
|
24
|
-
```
|
|
38
|
+
- Split a long form with `withLayoutForm`, which binds a sub-form to the same
|
|
39
|
+
`defaultValues` shape as the parent. Custom field components read `useFieldContext<T>()`.
|
|
25
40
|
|
|
26
41
|
## Examples
|
|
27
42
|
|
|
28
|
-
```
|
|
29
|
-
// Form
|
|
43
|
+
```tsx
|
|
44
|
+
// Form component
|
|
30
45
|
interface FormProps {
|
|
31
46
|
defaultValues: Feature;
|
|
32
47
|
onSubmit: (value: Feature) => Promise<void>;
|
|
@@ -46,38 +61,44 @@ export const Form: FC<FormProps> = ({ defaultValues, onSubmit }) => {
|
|
|
46
61
|
|
|
47
62
|
return (
|
|
48
63
|
<form
|
|
49
|
-
onSubmit={(event) => {
|
|
64
|
+
onSubmit={async (event) => {
|
|
50
65
|
event.preventDefault();
|
|
51
66
|
event.stopPropagation();
|
|
52
|
-
form.handleSubmit();
|
|
67
|
+
await form.handleSubmit();
|
|
53
68
|
}}
|
|
54
69
|
>
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
70
|
+
<form.AppField name="property1">
|
|
71
|
+
{(field) => <field.TextField label={t("Feature.Property1")} required sx={{ width: 420 }} />}
|
|
72
|
+
</form.AppField>
|
|
73
|
+
|
|
74
|
+
<form.AppField name="property2">
|
|
75
|
+
{(field) => (
|
|
76
|
+
<field.Autocomplete
|
|
77
|
+
options={options}
|
|
78
|
+
autoHighlight
|
|
79
|
+
autoSelect
|
|
80
|
+
autoComplete
|
|
81
|
+
loading={isLoading}
|
|
82
|
+
sx={{ width: 250 }}
|
|
83
|
+
textFieldProps={{ label: t("Feature.Property2"), required: true }}
|
|
84
|
+
/>
|
|
85
|
+
)}
|
|
58
86
|
</form.AppField>
|
|
59
|
-
|
|
87
|
+
|
|
88
|
+
<form.AppField name="property3">
|
|
89
|
+
{(field) => (
|
|
90
|
+
<field.NumberField label={t("Feature.Property3")} required sx={{ width: 120 }} />
|
|
91
|
+
)}
|
|
92
|
+
</form.AppField>
|
|
93
|
+
|
|
94
|
+
<form.AppField name="property4">
|
|
95
|
+
{(field) => <field.Switch label={t("Feature.Property4")} />}
|
|
96
|
+
</form.AppField>
|
|
97
|
+
|
|
60
98
|
<form.AppForm>
|
|
61
99
|
<form.SubmitButton variant="contained">{t("Submit")}</form.SubmitButton>
|
|
62
100
|
</form.AppForm>
|
|
63
101
|
</form>
|
|
64
102
|
);
|
|
65
103
|
};
|
|
66
|
-
|
|
67
|
-
// Autocomplete
|
|
68
|
-
<field.Autocomplete
|
|
69
|
-
options={options}
|
|
70
|
-
sx={{ width: 250 }}
|
|
71
|
-
autoHighlight
|
|
72
|
-
autoSelect
|
|
73
|
-
autoComplete
|
|
74
|
-
loading={isLoading}
|
|
75
|
-
textFieldProps={{
|
|
76
|
-
label: t("Customer"),
|
|
77
|
-
required: true,
|
|
78
|
-
}}
|
|
79
|
-
/>
|
|
80
|
-
|
|
81
|
-
// Checkbox
|
|
82
|
-
<field.Checkbox label={t("IsThisTrue")} />
|
|
83
104
|
```
|