@rsc-kit/mcp 0.41.0 → 0.42.1
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/recipes.js +18 -2
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +206 -6
- package/guides/api-routes.md +159 -215
- package/guides/authorization.md +103 -114
- package/guides/backend-answered-pages.md +301 -252
- package/guides/bap-vs-inertia.md +67 -25
- package/guides/blueprints.md +300 -169
- package/guides/bun.md +42 -25
- package/guides/caching.md +46 -49
- package/guides/coming-from-inertia.md +2 -2
- package/guides/commands.md +251 -200
- package/guides/connection.md +50 -54
- package/guides/content-security-policy.md +133 -84
- package/guides/domains.md +190 -149
- package/guides/edge-caching.md +242 -268
- package/guides/emails.md +96 -45
- package/guides/errors.md +116 -137
- package/guides/feature-flags.md +100 -30
- package/guides/file-uploads.md +101 -26
- package/guides/fonts.md +139 -110
- package/guides/forms.md +520 -542
- package/guides/images.md +87 -28
- package/guides/index.json +13 -33
- package/guides/instrumentation.md +134 -95
- package/guides/isr.md +3 -3
- package/guides/live-data-in-production.md +695 -0
- package/guides/live-data.md +170 -839
- package/guides/mcp.md +83 -59
- package/guides/metadata.md +124 -179
- package/guides/navigation.md +118 -147
- package/guides/no-javascript.md +49 -57
- package/guides/offline.md +118 -153
- package/guides/openapi.md +100 -35
- package/guides/pages.md +160 -50
- package/guides/ppr.md +178 -256
- package/guides/pwa.md +270 -216
- package/guides/queries.md +171 -300
- package/guides/react-compiler.md +90 -50
- package/guides/redirects.md +141 -177
- package/guides/response-headers.md +76 -74
- package/guides/route-interception.md +112 -111
- package/guides/routing.md +216 -322
- package/guides/sections.md +63 -66
- package/guides/seo-files.md +127 -65
- package/guides/server-actions.md +285 -357
- package/guides/static-generation.md +131 -199
- package/guides/testing.md +269 -147
- package/guides/third-party-scripts.md +94 -47
- package/guides/typed-routes.md +115 -113
- package/guides/url-validation.md +113 -61
- package/guides/validation.md +53 -62
- package/guides/view-transitions.md +159 -132
- package/guides/where-it-runs.md +226 -162
- package/package.json +2 -2
- package/guides/admin.md +0 -520
- package/guides/deployment.md +0 -598
- package/guides/go.md +0 -539
- package/guides/laravel.md +0 -787
- package/guides/your-own-backend.md +0 -602
package/guides/admin.md
DELETED
|
@@ -1,520 +0,0 @@
|
|
|
1
|
-
# Admin panels
|
|
2
|
-
|
|
3
|
-
> An admin panel over any data - a database, an ORM, a REST API, a Laravel or Go backend - with server or client tables, tenants and plugins.
|
|
4
|
-
|
|
5
|
-
`@rsc-kit/press` builds an admin panel from a list of resources. Each one is a
|
|
6
|
-
table, a form and a data source. The panel decides what each page shows and
|
|
7
|
-
who may see it; a [theme](#themes) draws it - shadcn's sidebar, data table and
|
|
8
|
-
dialogs is the one that ships - and the app keeps everything that is its own:
|
|
9
|
-
where the data lives, who may see it, and whose it is.
|
|
10
|
-
|
|
11
|
-
The package has three entry points:
|
|
12
|
-
|
|
13
|
-
- `@rsc-kit/press` - resources, data sources, tenants, plugins and blocks. Nothing here draws.
|
|
14
|
-
- `@rsc-kit/press/admin` - `definePanel`, and the url schemas its routes export.
|
|
15
|
-
- `@rsc-kit/press/forms` - `defineBlueprint`, for [blueprints](/guides/blueprints/): a form defined once, drawn in any page and as a resource's form.
|
|
16
|
-
- `@rsc-kit/press/pages` - `definePages`, for [pages an editor builds from blocks](/guides/pages/).
|
|
17
|
-
- `@rsc-kit/press/shadcn` - the shadcn theme, which draws both.
|
|
18
|
-
|
|
19
|
-
## Install
|
|
20
|
-
|
|
21
|
-
The shadcn theme is styled by the app's shadcn variables, so the app has
|
|
22
|
-
Tailwind v4 and shadcn set up (`create-rsc-kit --tailwind --shadcn` does both):
|
|
23
|
-
|
|
24
|
-
```sh
|
|
25
|
-
bun add @rsc-kit/press
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
```css title="src/app/styles.css"
|
|
29
|
-
@import "tailwindcss";
|
|
30
|
-
@import "shadcn/tailwind.css";
|
|
31
|
-
@import "@rsc-kit/press/shadcn.css";
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
The last line tells Tailwind to scan the theme's components. Without it the
|
|
35
|
-
panel renders unstyled.
|
|
36
|
-
|
|
37
|
-
The header's theme menu is Light, Dark or System, and System - the default -
|
|
38
|
-
follows the OS. It sets the `dark` class on `<html>` before the first paint,
|
|
39
|
-
so give the root layout's `<html>` `suppressHydrationWarning`, as shadcn's own
|
|
40
|
-
theme setup does. `definePanel({ modeToggle: false })` hides it for an app
|
|
41
|
-
with its own.
|
|
42
|
-
|
|
43
|
-
## A resource
|
|
44
|
-
|
|
45
|
-
```ts title="src/admin/resources/products.ts"
|
|
46
|
-
import * as z from 'zod'
|
|
47
|
-
import { defineResource } from '@rsc-kit/press'
|
|
48
|
-
|
|
49
|
-
export const products = defineResource<Product>()({
|
|
50
|
-
name: 'products',
|
|
51
|
-
key: 'id',
|
|
52
|
-
title: 'name',
|
|
53
|
-
source: productsSource,
|
|
54
|
-
table: (col) => [
|
|
55
|
-
col.text('name').searchable().sortable(),
|
|
56
|
-
col.money('price').sortable(),
|
|
57
|
-
col.badge('status'),
|
|
58
|
-
col.boolean('featured').hidden(),
|
|
59
|
-
],
|
|
60
|
-
filters: ['status'],
|
|
61
|
-
form: z.object({
|
|
62
|
-
name: z.string().min(2),
|
|
63
|
-
price: z.number().positive(),
|
|
64
|
-
status: z.enum(['draft', 'active']),
|
|
65
|
-
featured: z.boolean(),
|
|
66
|
-
}),
|
|
67
|
-
fields: { description: { type: 'textarea' } },
|
|
68
|
-
})
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Columns are `text`, `money`, `badge`, `boolean`, `date` and `relation`. `.hidden()` starts
|
|
72
|
-
a column switched off in the Columns menu, and `.cell((value, row) => ...)`
|
|
73
|
-
draws a cell yourself - a server component, so it can use the app's own shadcn
|
|
74
|
-
components.
|
|
75
|
-
|
|
76
|
-
## The form is any Standard Schema
|
|
77
|
-
|
|
78
|
-
`form` is a Standard Schema of an object: zod, valibot, arktype, Effect Schema
|
|
79
|
-
- whatever the app already uses. The panel never imports a validation library.
|
|
80
|
-
The same schema checks every save on the server, and each refusal shows under
|
|
81
|
-
the field it is about.
|
|
82
|
-
|
|
83
|
-
The fields are read from the schema. A schema that describes itself as JSON
|
|
84
|
-
Schema - **Standard JSON Schema**, which zod 4.2+ implements - gives the form
|
|
85
|
-
its fields, their types, which are required and an enum's options. A schema
|
|
86
|
-
that does not still validates; list its fields in `fields`, and the panel says
|
|
87
|
-
so if you forget:
|
|
88
|
-
|
|
89
|
-
```ts
|
|
90
|
-
form: v.object({ title: v.pipe(v.string(), v.minLength(1)) }),
|
|
91
|
-
fields: { title: { type: 'text' } },
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Checked in the browser first
|
|
95
|
-
|
|
96
|
-
The constraints the browser can check itself come from the schema too, as
|
|
97
|
-
attributes on the controls: `required`, `min` and `max`, `minlength` and
|
|
98
|
-
`maxlength`, an anchored `pattern`, `type="email"`, and `step="1"` for an
|
|
99
|
-
integer. A submit with a wrong field never leaves the page, and the browser's
|
|
100
|
-
message - in the visitor's language - shows under the field in the same
|
|
101
|
-
`FieldError` the server's refusals use. With JavaScript off, the browser shows
|
|
102
|
-
its own bubble instead.
|
|
103
|
-
|
|
104
|
-
The schema itself never reaches the browser, so this is the same for a schema
|
|
105
|
-
in the app and for rules that live in Laravel or Go. It is a courtesy only: a
|
|
106
|
-
custom message (`'Like ABC-123'`) and anything the browser cannot know - a
|
|
107
|
-
unique email - come from the server, which checks every save.
|
|
108
|
-
|
|
109
|
-
A number field arrives as a number, so the schema says `z.number()` - not
|
|
110
|
-
`z.coerce.number()` - and is the same schema an API would use. A switch that is
|
|
111
|
-
off is `false`, and an empty optional field is left out.
|
|
112
|
-
|
|
113
|
-
## Any data source
|
|
114
|
-
|
|
115
|
-
A data source is five functions. The panel calls nothing else, so it is a
|
|
116
|
-
database, an ORM, a REST API or a backend behind `rpc()` alike:
|
|
117
|
-
|
|
118
|
-
```ts title="src/admin/resources/customers.ts"
|
|
119
|
-
import type { DataSource } from '@rsc-kit/press'
|
|
120
|
-
|
|
121
|
-
const customersSource: DataSource<Customer, Values> = {
|
|
122
|
-
async list({ q, sort, dir, page, perPage, filters }, { tenant }) {
|
|
123
|
-
const res = await fetch(`${API}/customers?${new URLSearchParams({ q: q ?? '', page: String(page) })}`)
|
|
124
|
-
const { data, total } = await res.json()
|
|
125
|
-
|
|
126
|
-
return { rows: data, total }
|
|
127
|
-
},
|
|
128
|
-
find: (id) => fetch(`${API}/customers/${id}`).then((r) => (r.ok ? r.json() : null)),
|
|
129
|
-
create: (values) => post(`${API}/customers`, values),
|
|
130
|
-
update: (id, values) => put(`${API}/customers/${id}`, values),
|
|
131
|
-
delete: (id) => del(`${API}/customers/${id}`),
|
|
132
|
-
}
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
### Pages by count, or by cursor
|
|
136
|
-
|
|
137
|
-
A list answers its page one of two ways, and the table pages to match:
|
|
138
|
-
|
|
139
|
-
- `{ rows, total }` - counted. The table says how many, and numbers its pages.
|
|
140
|
-
- `{ rows, next, prev }` - by cursor. The table has Previous and Next, each a
|
|
141
|
-
link to the list with `?cursor=` set to the one the source handed out, and
|
|
142
|
-
says nothing of how many. `null` is no page that way.
|
|
143
|
-
|
|
144
|
-
A cursor suits a table too big to count, or a query that is cheap only by key -
|
|
145
|
-
Laravel's `cursorPaginate`, a keyset query, an API that hands back a cursor.
|
|
146
|
-
The source is told the one it gave as `query.cursor`, absent on the first page,
|
|
147
|
-
and reads that rather than `page`. A search, a filter or a sort starts again from
|
|
148
|
-
the first page; paging keeps them.
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
list: async (query) => {
|
|
152
|
-
const page = await rpc('Orders.list', query)
|
|
153
|
-
|
|
154
|
-
return { rows: page.data as Order[], next: page.next_cursor, prev: page.prev_cursor }
|
|
155
|
-
},
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
```php title="app/Rsc/Orders.php"
|
|
159
|
-
public function list(array $query): CursorPaginator
|
|
160
|
-
{
|
|
161
|
-
return Order::latest('id')->cursorPaginate($query['perPage'], cursor: $query['cursor'] ?? null);
|
|
162
|
-
}
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
A method returning a paginator is typed as Laravel encodes it - `data`,
|
|
166
|
-
`next_cursor` and `prev_cursor` from `cursorPaginate`, `data` and `total` from
|
|
167
|
-
`paginate`, which is `{ rows: page.data as Order[], total: page.total }` - with
|
|
168
|
-
its rows unknown, which the cast names (`rsc-kit/laravel` 0.19).
|
|
169
|
-
|
|
170
|
-
Each resource chooses for itself, so one panel can have both. The dashboard
|
|
171
|
-
shows a dash for a resource it cannot count.
|
|
172
|
-
|
|
173
|
-
A save says "Product saved." on the list it lands on, and a delete says
|
|
174
|
-
"Product deleted." in place, as a toast. Both use
|
|
175
|
-
[`flash()`](/guides/server-actions#a-message-for-the-next-page).
|
|
176
|
-
|
|
177
|
-
`find` answering `null` is a 404, for the page and for a save or delete of that
|
|
178
|
-
id. `memorySource(rows, key, make)` is one for tests and demos.
|
|
179
|
-
|
|
180
|
-
### A Laravel or Go backend
|
|
181
|
-
|
|
182
|
-
On a backend the five functions are `rpc()` calls. The source runs in the
|
|
183
|
-
renderer, in a page and in the panel's save, and both forward the visitor's
|
|
184
|
-
cookie, so `auth()->user()` is them:
|
|
185
|
-
|
|
186
|
-
```ts title="src/admin/resources/products.ts"
|
|
187
|
-
const productsSource: DataSource<Product, Values> = {
|
|
188
|
-
list: (query) => rpc('Products.list', query),
|
|
189
|
-
find: (id) => rpc('Products.find', Number(id)),
|
|
190
|
-
create: (values) => rpc('Products.create', values),
|
|
191
|
-
update: (id, values) => rpc('Products.update', Number(id), values),
|
|
192
|
-
delete: (id) => rpc('Products.delete', Number(id)),
|
|
193
|
-
}
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
```php title="app/Rsc/Products.php"
|
|
197
|
-
public function create(StoreProduct $request): Product
|
|
198
|
-
{
|
|
199
|
-
return Product::create($request->validated());
|
|
200
|
-
}
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
The backend validates again, and its answer is the one that counts. A rule the
|
|
204
|
-
schema cannot know - `unique:products,sku`, a stock check - comes back as a 422,
|
|
205
|
-
and its messages show under the same fields as the schema's, with the form's
|
|
206
|
-
values kept. Nothing to catch: the panel treats the backend's refusal and its
|
|
207
|
-
own the same way.
|
|
208
|
-
|
|
209
|
-
### The backend's actions as the form
|
|
210
|
-
|
|
211
|
-
When the backend already says what a record takes - a FormRequest, a Go struct
|
|
212
|
-
with `validate` tags - the resource needs no schema of its own. Give it the
|
|
213
|
-
backend's generated actions instead of `form`, and the source keeps only
|
|
214
|
-
`list`, `find` and `delete`:
|
|
215
|
-
|
|
216
|
-
```ts title="src/admin/resources/products.ts"
|
|
217
|
-
import { store, update } from '../../server-actions/products'
|
|
218
|
-
|
|
219
|
-
export const products = defineResource<Product>()({
|
|
220
|
-
name: 'products',
|
|
221
|
-
key: 'id',
|
|
222
|
-
title: 'name',
|
|
223
|
-
source: { list: (q) => rpc('Products.list', q), find: (id) => rpc('Products.find', id), delete: (id) => rpc('Products.delete', id) },
|
|
224
|
-
actions: { create: store, update },
|
|
225
|
-
fields: { category_id: { relation: 'categories' } },
|
|
226
|
-
table: (col) => [col.text('name'), col.money('price')],
|
|
227
|
-
})
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
The create form is drawn from what `create` takes, and the edit form from what
|
|
231
|
-
`update` takes, so a password required on one can be optional on the other.
|
|
232
|
-
`fields` names the backend's fields - a misspelt one fails the typecheck - and
|
|
233
|
-
adds what the rules cannot say, such as a relation. `create` is called with the
|
|
234
|
-
values; `update` with the values, then the record's key:
|
|
235
|
-
|
|
236
|
-
```php title="app/Rsc/Actions/Products.php"
|
|
237
|
-
public function update(UpdateProductRequest $request, string $product): Product
|
|
238
|
-
{
|
|
239
|
-
return tap(Product::findOrFail($product))->update($request->validated());
|
|
240
|
-
}
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
```php title="app/Http/Requests/UpdateProductRequest.php"
|
|
244
|
-
'sku' => ['required', Rule::unique('products')->ignore($this->route('product'))],
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
The key reaches the FormRequest as its route parameter, under the method's own
|
|
248
|
-
name for it, so a unique rule ignores the record being edited as it would on
|
|
249
|
-
`PUT /products/{product}` - and `Route::model()` binds it. In Go the update is
|
|
250
|
-
`func(ctx context.Context, in ProductInput, id string)`. An update that takes
|
|
251
|
-
only the values is refused at startup, since it could not be told which
|
|
252
|
-
record, and so is a `create` that takes a key.
|
|
253
|
-
|
|
254
|
-
The backend does every check. The panel still refuses a relation the visitor
|
|
255
|
-
cannot see before the backend is asked.
|
|
256
|
-
|
|
257
|
-
### A blueprint as the form
|
|
258
|
-
|
|
259
|
-
The same form is often wanted outside the panel too: a settings page where
|
|
260
|
-
people edit their own profile, a sign-up that creates what the panel lists.
|
|
261
|
-
Define it once as a [blueprint](/guides/blueprints/), and hand it to the
|
|
262
|
-
resource as its `form`:
|
|
263
|
-
|
|
264
|
-
```ts
|
|
265
|
-
import { defineBlueprint } from '@rsc-kit/press/forms'
|
|
266
|
-
|
|
267
|
-
export const product = defineBlueprint({
|
|
268
|
-
name: 'product',
|
|
269
|
-
theme: shadcn,
|
|
270
|
-
actions: { create: store, update },
|
|
271
|
-
fields: { category_id: { relation: 'categories' } },
|
|
272
|
-
})
|
|
273
|
-
|
|
274
|
-
export const products = defineResource<Product>()({
|
|
275
|
-
name: 'products',
|
|
276
|
-
key: 'id',
|
|
277
|
-
title: 'name',
|
|
278
|
-
source: { list, find, delete: remove },
|
|
279
|
-
table: (col) => [col.text('name'), col.money('price')],
|
|
280
|
-
form: product,
|
|
281
|
-
})
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
The panel's create and edit pages are then the blueprint's fields, saved with
|
|
285
|
-
the blueprint's actions - or a schema blueprint's `handle` and `update` - and a
|
|
286
|
-
page draws the same form with `<product.Form />`, or
|
|
287
|
-
`<product.Form id={p.id} values={p} />` to edit. A blueprint that does not
|
|
288
|
-
save leaves it to a whole source.
|
|
289
|
-
|
|
290
|
-
### Choices that read as words
|
|
291
|
-
|
|
292
|
-
An enum's options are shown humanized - `'in_stock'` reads "In stock" in the
|
|
293
|
-
form and the filter, and `'in_stock'` is still what is saved. Say them
|
|
294
|
-
yourself where that is not the word:
|
|
295
|
-
|
|
296
|
-
```ts
|
|
297
|
-
fields: { status: { options: ['draft', { value: 'active', label: 'Live' }] } },
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
An optional select starts on "None", which saves nothing.
|
|
301
|
-
|
|
302
|
-
## Files and images
|
|
303
|
-
|
|
304
|
-
A field the schema holds as a string - the url of what was uploaded - can be
|
|
305
|
-
an upload instead:
|
|
306
|
-
|
|
307
|
-
```ts title="src/admin/resources/products.ts"
|
|
308
|
-
form: z.object({ name: z.string(), photo: z.url().nullable().optional() }),
|
|
309
|
-
fields: {
|
|
310
|
-
photo: { type: 'image', storage: uploads, maxSize: 2 * 1024 * 1024 },
|
|
311
|
-
},
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
`storage` is where the file goes. It is one function, and press ships none,
|
|
315
|
-
as it ships no database:
|
|
316
|
-
|
|
317
|
-
```ts title="src/storage.ts"
|
|
318
|
-
import type { Storage } from '@rsc-kit/press'
|
|
319
|
-
|
|
320
|
-
export const uploads: Storage = {
|
|
321
|
-
async put(file, { field, tenant }) {
|
|
322
|
-
const key = `uploads/${crypto.randomUUID()}`
|
|
323
|
-
|
|
324
|
-
await Bun.s3.write(key, file, { type: file.type })
|
|
325
|
-
|
|
326
|
-
return `https://cdn.example.com/${key}`
|
|
327
|
-
},
|
|
328
|
-
}
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
What `put` answers is what the record saves. On a Worker the same function
|
|
332
|
-
writes to an R2 binding; on a server, to a disk the app serves. A Laravel or Go
|
|
333
|
-
backend gets the url, not the file - `rpc()` carries JSON - so point `put` at
|
|
334
|
-
the bucket the backend's own disk reads, and the url means the same on both
|
|
335
|
-
sides.
|
|
336
|
-
|
|
337
|
-
The form shows what the field holds, a preview of a new image as it is chosen,
|
|
338
|
-
and, when the schema allows `null`, a **Remove** button that saves `null`.
|
|
339
|
-
|
|
340
|
-
- **Upload last.** The form is checked first with the file still a file; only
|
|
341
|
-
when nothing else is wrong is it stored. A refused save leaves nothing in
|
|
342
|
-
the bucket.
|
|
343
|
-
- **Checked twice.** `accept` and `maxSize` are checked in the browser as the
|
|
344
|
-
file is chosen and again on the server. An image field takes PNG, JPEG,
|
|
345
|
-
WebP, GIF and AVIF unless it says `accept`. Not SVG: an SVG can carry script,
|
|
346
|
-
and served from your own origin it runs as your app.
|
|
347
|
-
- **Kept values are checked.** A save that chooses nothing sends back what
|
|
348
|
-
the field held, and the server keeps it only if the record already had it.
|
|
349
|
-
A posted url is not an upload.
|
|
350
|
-
- **The body limit.** Files arrive in the action's body, 8 MB unless the host's
|
|
351
|
-
`maxActionBody` says more. Set `maxSize` below it, so the visitor reads which
|
|
352
|
-
field is too large rather than a refused request.
|
|
353
|
-
- **Replaced files stay.** Removing or replacing an upload does not delete the
|
|
354
|
-
old file; a bucket lifecycle rule, or a job that sweeps keys no record
|
|
355
|
-
names, does that.
|
|
356
|
-
|
|
357
|
-
The field works the same in a [block](/guides/pages/) and in a
|
|
358
|
-
[blueprint's form](/guides/blueprints/).
|
|
359
|
-
|
|
360
|
-
## Relations
|
|
361
|
-
|
|
362
|
-
A belongsTo is a field that names another resource of the panel:
|
|
363
|
-
|
|
364
|
-
```ts
|
|
365
|
-
table: (col) => [col.text('name'), col.relation('categoryId', 'categories')],
|
|
366
|
-
form: z.object({ name: z.string(), categoryId: z.number().int() }),
|
|
367
|
-
fields: { categoryId: { relation: 'categories' } },
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
The form offers the categories as a select, labelled "Category": each one's
|
|
371
|
-
`key` saved, its `title` shown. The column shows "Lighting", not `3`, and a
|
|
372
|
-
client-mode table searches and sorts it by that word. A `z.number()` relation
|
|
373
|
-
arrives as a number, as a number field does.
|
|
374
|
-
|
|
375
|
-
The choices are the related resource's `list` with `perPage: null`, asked as
|
|
376
|
-
the visitor - scoped to their tenant like any list - so a relation is for a
|
|
377
|
-
resource with a select's worth of rows: categories, teams, plans. A save checks
|
|
378
|
-
the id it was sent with that resource's `find`, as the visitor, so an id posted
|
|
379
|
-
by hand from another tenant is refused under the field. A relation to a
|
|
380
|
-
resource the panel does not have fails when the panel is defined.
|
|
381
|
-
|
|
382
|
-
## Bulk actions
|
|
383
|
-
|
|
384
|
-
Every table that is not `readOnly` can tick rows; ticking shows what can be
|
|
385
|
-
done to them below the table, with the panel's own **Delete**. Add your own:
|
|
386
|
-
|
|
387
|
-
```ts
|
|
388
|
-
bulkActions: [
|
|
389
|
-
{ name: 'publish', label: 'Publish', done: 'published', run: (rows) => rpc('Products.publish', rows.map((row) => row.id)) },
|
|
390
|
-
{ name: 'archive', label: 'Archive', destructive: true, run: archiveAll },
|
|
391
|
-
],
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
`run` is given the rows themselves, each found by the source's `find` as the
|
|
395
|
-
visitor: an id the browser sent that is not theirs, or is already gone, never
|
|
396
|
-
reaches it. It runs after `authorize`, like every save. A `destructive` action
|
|
397
|
-
asks first, as Delete does. When it is done the table refreshes and a toast
|
|
398
|
-
says "3 products published." from `done`, as Delete says "deleted" - or
|
|
399
|
-
whatever `run` returns as a string.
|
|
400
|
-
|
|
401
|
-
## Server or client tables
|
|
402
|
-
|
|
403
|
-
```ts
|
|
404
|
-
mode: 'server' // the default
|
|
405
|
-
mode: 'client'
|
|
406
|
-
```
|
|
407
|
-
|
|
408
|
-
**Server** - each search, filter, sort and page is a request, and `list` is
|
|
409
|
-
asked for one page (`perPage` a number). The url holds the state, so a table is
|
|
410
|
-
a link, the back button works and it works with JavaScript off. For data that
|
|
411
|
-
grows.
|
|
412
|
-
|
|
413
|
-
**Client** - `list` is asked once with `perPage: null` and returns every row;
|
|
414
|
-
search, filters, sorting and paging happen in the browser with no request. For
|
|
415
|
-
data that stays small - hundreds of rows, not thousands.
|
|
416
|
-
|
|
417
|
-
## Who may use it
|
|
418
|
-
|
|
419
|
-
The panel has no login of its own. It asks the app's:
|
|
420
|
-
|
|
421
|
-
```ts title="src/admin/panel.ts"
|
|
422
|
-
import { definePanel } from '@rsc-kit/press/admin'
|
|
423
|
-
import { shadcn } from '@rsc-kit/press/shadcn'
|
|
424
|
-
|
|
425
|
-
export const panel = definePanel({
|
|
426
|
-
path: '/admin',
|
|
427
|
-
theme: shadcn,
|
|
428
|
-
resources: [products, customers],
|
|
429
|
-
authorize: async () => (await currentUser())?.isAdmin === true,
|
|
430
|
-
user: currentUser,
|
|
431
|
-
signOut: () => endSession(),
|
|
432
|
-
})
|
|
433
|
-
```
|
|
434
|
-
|
|
435
|
-
`authorize` runs on every page, and on every save, delete and team switch -
|
|
436
|
-
an action is reachable without the page that rendered it, so a guard on the
|
|
437
|
-
pages alone would not be one. A refusal is a 404. `user` fills the sidebar's
|
|
438
|
-
footer, and `signOut` runs in a server action and lands the visitor on `/`.
|
|
439
|
-
|
|
440
|
-
## Tenants
|
|
441
|
-
|
|
442
|
-
```ts
|
|
443
|
-
tenancy: {
|
|
444
|
-
label: 'Teams',
|
|
445
|
-
tenants: () => teamsOf(currentUser()),
|
|
446
|
-
current: () => teamFromSession(),
|
|
447
|
-
switch: (id) => rememberTeam(id),
|
|
448
|
-
},
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
The sidebar gets a team switcher, and every data source call is told the
|
|
452
|
-
tenant as its last argument - `list(query, { tenant })`. A current tenant that
|
|
453
|
-
is not one of the visitor's own - a cookie naming someone else's team - is
|
|
454
|
-
refused, so `current` reads the session and the panel checks membership.
|
|
455
|
-
`switch` runs in a server action, so it may set a cookie.
|
|
456
|
-
|
|
457
|
-
`perTenant((tenantId) => source)` keeps a source per tenant, for data that is
|
|
458
|
-
already split that way.
|
|
459
|
-
|
|
460
|
-
## Mounting it
|
|
461
|
-
|
|
462
|
-
The engine finds routes, url schemas and sections by their files, so the
|
|
463
|
-
panel is mounted with seven files of one or two lines each:
|
|
464
|
-
|
|
465
|
-
```
|
|
466
|
-
src/app/admin/middleware.ts export default panel.middleware
|
|
467
|
-
src/app/admin/layout.tsx export default panel.Layout
|
|
468
|
-
src/app/admin/page.tsx export default panel.Dashboard
|
|
469
|
-
src/app/admin/[resource]/page.tsx export const params = resourceParams
|
|
470
|
-
export const searchParams = listSearch
|
|
471
|
-
export default panel.Index
|
|
472
|
-
src/app/admin/[resource]/admin-table.section.tsx export default panel.Table
|
|
473
|
-
src/app/admin/[resource]/new/page.tsx export const params = resourceParams
|
|
474
|
-
export default panel.Create
|
|
475
|
-
src/app/admin/[resource]/[id]/page.tsx export const params = recordParams
|
|
476
|
-
export default panel.Edit
|
|
477
|
-
```
|
|
478
|
-
|
|
479
|
-
`resourceParams`, `recordParams`, `listSearch` and `pluginsSearch` come from
|
|
480
|
-
`@rsc-kit/press/admin`. The section must be named `admin-table.section.tsx`: a
|
|
481
|
-
delete refreshes the table by that name.
|
|
482
|
-
|
|
483
|
-
## Themes
|
|
484
|
-
|
|
485
|
-
`theme` is what draws the panel. The panel works out everything a page needs -
|
|
486
|
-
whether the visitor may see it, which tenant's rows, the save already bound to
|
|
487
|
-
its record - and hands the theme the result as props. A theme is an object of
|
|
488
|
-
components, typed `AdminTheme` from `@rsc-kit/press/admin`:
|
|
489
|
-
|
|
490
|
-
| Component | Draws |
|
|
491
|
-
| --- | --- |
|
|
492
|
-
| `Shell` | The frame: `brand`, `nav`, `user` and `crumbs` arrive drawn, and the page as `children` |
|
|
493
|
-
| `Brand`, `TenantSwitcher`, `Nav`, `Breadcrumbs`, `UserMenu` | What goes in the frame |
|
|
494
|
-
| `Dashboard`, `Index`, `Form`, `Plugins` | Each page |
|
|
495
|
-
| `Table` | A resource's rows, in server or client mode |
|
|
496
|
-
| `Cell` | A `money`, `badge`, `boolean` or `date` value |
|
|
497
|
-
| `Loading` | What shows while a page waits for the request |
|
|
498
|
-
|
|
499
|
-
Every prop is data, a server action already bound, or an element already
|
|
500
|
-
drawn - never a component - so any of them may be a client component. The
|
|
501
|
-
panel keeps the Suspense boundaries, because it knows which parts read the
|
|
502
|
-
request; a theme only draws what shows meanwhile.
|
|
503
|
-
|
|
504
|
-
To change one page, spread the shipped theme and replace it:
|
|
505
|
-
|
|
506
|
-
```tsx title="src/admin/panel.ts"
|
|
507
|
-
import { shadcn } from '@rsc-kit/press/shadcn'
|
|
508
|
-
import { BrandedDashboard } from './branded-dashboard'
|
|
509
|
-
|
|
510
|
-
export const panel = definePanel({
|
|
511
|
-
theme: { ...shadcn, Dashboard: BrandedDashboard },
|
|
512
|
-
// ...
|
|
513
|
-
})
|
|
514
|
-
```
|
|
515
|
-
|
|
516
|
-
## Plugins
|
|
517
|
-
|
|
518
|
-
A plugin adds resources, dashboard widgets and a reaction to every save and
|
|
519
|
-
delete, and is installed with `plugins: [auditLog()]`. Writing one to publish
|
|
520
|
-
or sell is [its own guide](/guides/admin-plugins/).
|