@stacksjs/defaults 0.75.571 → 0.75.572

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 (61) hide show
  1. package/ai/skills/stacks-actions/SKILL.md +1 -1
  2. package/ai/skills/stacks-cms/SKILL.md +1 -1
  3. package/ai/skills/stacks-dashboard/SKILL.md +283 -12
  4. package/ai/skills/stacks-database/SKILL.md +3 -1
  5. package/ai/skills/stacks-native/CATALOG.md +2 -2
  6. package/ai/skills/stacks-orm/SKILL.md +5 -1
  7. package/app/Actions/Dashboard/Content/CommentUpdateAction.ts +1 -1
  8. package/app/Actions/Dashboard/Content/PostStoreAction.ts +1 -1
  9. package/app/Actions/Dashboard/Content/PostUpdateAction.ts +1 -1
  10. package/app/Actions/Dashboard/Marketing/marketing-list-records.test.ts +2 -2
  11. package/app/Actions/Dashboard/Marketing/marketing-list-records.ts +4 -4
  12. package/app/Actions/Dashboard/Models/ModelShowAction.ts +42 -43
  13. package/app/Actions/Dashboard/Models/ModelUpdateAction.ts +17 -3
  14. package/app/Actions/Dashboard/Models/model-filters.ts +429 -0
  15. package/app/Actions/Dashboard/Models/model-switches.ts +159 -0
  16. package/app/Actions/Dashboard/Models/model-write.ts +6 -0
  17. package/app/Models/Comment.ts +3 -3
  18. package/app/Models/Content/Post.ts +3 -3
  19. package/app/Models/EmailList.ts +8 -6
  20. package/app/Models/Subscriber.ts +2 -0
  21. package/app/Models/Team.ts +2 -0
  22. package/app/Models/User.ts +3 -0
  23. package/functions/dashboard-model-filters.ts +219 -0
  24. package/functions/dashboard-model-record.ts +42 -8
  25. package/ide/vscode/package.json +1 -1
  26. package/package.json +3 -3
  27. package/resources/components/Dashboard/Commerce/CommerceCategoryDialog.stx +3 -1
  28. package/resources/components/Dashboard/Commerce/CommerceCouponDialog.stx +3 -1
  29. package/resources/components/Dashboard/Commerce/CommerceGiftCardDialog.stx +9 -3
  30. package/resources/components/Dashboard/Commerce/CommerceManufacturerDialog.stx +3 -4
  31. package/resources/components/Dashboard/Commerce/CommerceProductDialog.stx +3 -1
  32. package/resources/components/Dashboard/Commerce/CommerceReviewEditDialog.stx +6 -8
  33. package/resources/components/Dashboard/Commerce/CommerceTaxDialog.stx +6 -14
  34. package/resources/components/Dashboard/Commerce/CommerceUnitDialog.stx +3 -7
  35. package/resources/components/Dashboard/Commerce/Delivery/DigitalDeliveryDialog.stx +6 -2
  36. package/resources/components/Dashboard/Content/BlogPostEditorDialog.stx +2 -8
  37. package/resources/components/Dashboard/Content/PageEditorDialog.stx +3 -7
  38. package/resources/components/Dashboard/Content/PostEditorDialog.stx +3 -4
  39. package/resources/components/Dashboard/Data/DataModelRecords.stx +12 -75
  40. package/resources/components/Dashboard/Data/DataStatCards.stx +110 -0
  41. package/resources/components/Dashboard/Email/InboxSettingsDashboard.stx +20 -36
  42. package/resources/components/Dashboard/Marketing/MarketingListDialog.stx +7 -14
  43. package/resources/components/Dashboard/Marketing/MarketingListsDashboard.stx +3 -2
  44. package/resources/components/Dashboard/Models/ModelFieldInputs.stx +10 -13
  45. package/resources/components/Dashboard/Models/ModelFilterPanel.stx +166 -0
  46. package/resources/components/Dashboard/Models/ModelRecordDetail.stx +18 -0
  47. package/resources/components/Dashboard/Models/ModelRecordEdit.stx +3 -2
  48. package/resources/components/Dashboard/Models/ModelRecordsDashboard.stx +96 -29
  49. package/resources/components/Dashboard/Models/ModelRegion.stx +25 -0
  50. package/resources/components/Dashboard/Models/ModelRegionFor.stx +31 -0
  51. package/resources/components/Dashboard/Operations/RecoveryDestinationDialog.stx +2 -2
  52. package/resources/components/Dashboard/Operations/RecoveryPolicyDialog.stx +3 -1
  53. package/resources/components/Dashboard/Operations/RecoveryRestoreDialog.stx +3 -1
  54. package/resources/components/Dashboard/Settings/AppearanceSettingsDashboard.stx +11 -14
  55. package/resources/components/Dashboard/Settings/SettingsDashboard.stx +6 -9
  56. package/resources/components/Dashboard/UI/Toggle.stx +16 -12
  57. package/resources/functions/dashboard/regions.ts +113 -0
  58. package/resources/functions/dashboard/sidebar.ts +2 -0
  59. package/views/dashboard/composables/useModelList.ts +52 -0
  60. package/views/dashboard/composables/useModelRecord.ts +46 -0
  61. package/views/dashboard/stores/model-page.ts +105 -0
@@ -54,8 +54,8 @@ function isVisible(id: string) {
54
54
  return !hiddenSections().includes(id)
55
55
  }
56
56
 
57
- function toggleSection(id: string, event: Event) {
58
- appearance.toggleSection(id, (event.target as HTMLInputElement).checked)
57
+ function toggleSection(id: string, visible: boolean) {
58
+ appearance.toggleSection(id, visible)
59
59
  }
60
60
 
61
61
  function resetAll() {
@@ -302,19 +302,16 @@ const selected = (a: unknown, b: unknown) => String(a === b)
302
302
  </div>
303
303
 
304
304
  <div class="divide-neutral-200/60 divide-y dark:divide-white/5">
305
- <label :for="section in sections" class="flex gap-4 items-center justify-between px-6 py-4 hover:bg-stone-50/60 dark:hover:bg-white/[0.02] cursor-pointer">
306
- <span class="min-w-0">
307
- <span class="block font-medium text-neutral-900 text-sm dark:text-white">{{ section.label }}</span>
308
- <span class="block mt-0.5 text-neutral-500 text-xs dark:text-neutral-400">{{ section.count }} items</span>
309
- </span>
310
- <input
311
- type="checkbox"
305
+ <div :for="section in sections" class="px-6 py-4 hover:bg-stone-50/60 dark:hover:bg-white/[0.02]">
306
+ <Toggle
312
307
  :checked="isVisible(section.id)"
313
- @change="toggleSection(section.id, $event)"
314
- class="flex-none h-4 w-4 text-blue-600 border-neutral-300 rounded focus:ring-blue-500"
315
- :aria-label="'Show ' + section.label + ' section'"
316
- >
317
- </label>
308
+ :label="section.label"
309
+ :description="section.count + ' items'"
310
+ :ariaLabel="'Show ' + section.label + ' section'"
311
+ labelPosition="left"
312
+ @update:checked="toggleSection(section.id, $event)"
313
+ />
314
+ </div>
318
315
  </div>
319
316
  </Card>
320
317
 
@@ -277,15 +277,12 @@ void loadFiles()
277
277
 
278
278
  <div>
279
279
  @if(field.type === 'boolean' && field.editable)
280
- <label class="inline-flex gap-3 items-center cursor-pointer">
281
- <input
282
- :id="'config-' + field.key"
283
- type="checkbox"
284
- x-model="drafts[field.key]"
285
- class="h-4 w-4 text-blue-600 border-neutral-300 rounded focus:ring-blue-500"
286
- >
287
- <span class="text-neutral-700 text-sm dark:text-neutral-300">{{ drafts[field.key] ? 'Enabled' : 'Disabled' }}</span>
288
- </label>
280
+ <Toggle
281
+ :id="'config-' + field.key"
282
+ v-model:checked="drafts[field.key]"
283
+ :label="drafts[field.key] ? 'Enabled' : 'Disabled'"
284
+ :ariaLabel="field.key"
285
+ />
289
286
  @elseif(field.editable && field.type === 'number')
290
287
  <input
291
288
  :id="'config-' + field.key"
@@ -11,6 +11,8 @@ const liveLabelPosition = useReactiveProp('labelPosition', 'right' as ToggleLabe
11
11
  const liveName = useReactiveProp('name', '')
12
12
  const liveId = useReactiveProp('id', '')
13
13
  const liveAriaLabel = useReactiveProp('ariaLabel', '')
14
+ // What a refused save said about this switch. Shown in place of the description.
15
+ const liveError = useReactiveProp('error', '')
14
16
  const emit = defineEmits(['update:checked', 'change'])
15
17
  const generatedId = useId('toggle')
16
18
 
@@ -31,31 +33,31 @@ const checkedTranslations: Record<ToggleSize, string> = {
31
33
  }
32
34
 
33
35
  const toggleId = derived(() => liveId() || generatedId)
34
- const hasCopy = derived(() => Boolean(liveLabel() || liveDescription()))
36
+ const hasCopy = derived(() => Boolean(liveLabel() || liveDescription() || liveError()))
35
37
  const wrapperClass = derived(() => [
36
38
  'flex items-center gap-3',
37
39
  liveLabelPosition() === 'left' ? 'flex-row-reverse justify-end' : '',
38
- liveDisabled() ? 'cursor-not-allowed opacity-60' : 'cursor-pointer',
40
+ // Only the track dims: what a switch that cannot be turned says under it still has to be read.
41
+ liveDisabled() ? 'cursor-not-allowed' : 'cursor-pointer',
39
42
  ].join(' '))
40
43
  const trackClass = derived(() => [
41
44
  trackSizes[liveSize()] || trackSizes.md,
42
45
  'relative inline-flex shrink-0 rounded-full transition-colors duration-150',
43
- 'focus-within:ring-2 focus-within:ring-blue-500/40',
46
+ 'peer-focus-visible:ring-2 peer-focus-visible:ring-blue-500/40',
44
47
  liveChecked() ? 'bg-green-500' : 'bg-black/15 dark:bg-white/20',
45
48
  liveDisabled() ? 'opacity-40' : '',
46
49
  ].join(' '))
47
50
  const thumbClass = derived(() => [
48
51
  thumbSizes[liveSize()] || thumbSizes.md,
49
- 'pointer-events-none inline-block translate-x-0.5 rounded-full bg-white shadow-sm transition-transform duration-150 ease-in-out',
50
- liveChecked() ? checkedTranslations[liveSize()] || checkedTranslations.md : '',
52
+ 'pointer-events-none inline-block rounded-full bg-white shadow-sm transition-transform duration-150 ease-in-out',
53
+ // One position or the other, never both: with both on the thumb, which won
54
+ // was the order the page's stylesheet happened to list them in, and on
55
+ // some pages a switch that was on kept its thumb on the left.
56
+ liveChecked() ? checkedTranslations[liveSize()] || checkedTranslations.md : 'translate-x-0.5',
51
57
  ].join(' '))
52
58
  const labelClass = derived(() => [
53
59
  'text-[13px] font-medium tracking-tight',
54
- liveDisabled() ? 'text-neutral-500 dark:text-neutral-600' : 'text-neutral-700 dark:text-neutral-200',
55
- ].join(' '))
56
- const descriptionClass = derived(() => [
57
- 'mt-0.5 text-[12px]',
58
- liveDisabled() ? 'text-neutral-300 dark:text-neutral-600' : 'text-neutral-500 dark:text-neutral-400',
60
+ liveDisabled() ? 'text-neutral-500 dark:text-neutral-400' : 'text-neutral-700 dark:text-neutral-200',
59
61
  ].join(' '))
60
62
 
61
63
  function handleChange(event: Event): void {
@@ -72,7 +74,8 @@ function handleChange(event: Event): void {
72
74
  role="switch"
73
75
  :name="liveName() || null"
74
76
  :aria-label="liveAriaLabel() || liveLabel() || 'Toggle'"
75
- class="sr-only"
77
+ :aria-invalid="liveError() ? 'true' : null"
78
+ class="sr-only peer"
76
79
  x-model="liveChecked"
77
80
  :disabled="liveDisabled()"
78
81
  @change="handleChange"
@@ -84,6 +87,7 @@ function handleChange(event: Event): void {
84
87
 
85
88
  <span :if="hasCopy()" class="flex-1 min-w-0">
86
89
  <span :if="liveLabel()" class="block" :class="labelClass()" :text="liveLabel()"></span>
87
- <span :if="liveDescription()" class="block" :class="descriptionClass()" :text="liveDescription()"></span>
90
+ <span :if="liveDescription() && !liveError()" class="block mt-0.5 text-[12px] text-neutral-500 dark:text-neutral-400" :text="liveDescription()"></span>
91
+ <span :if="liveError()" role="alert" class="block mt-0.5 text-[12px] text-red-600 dark:text-red-400" :text="liveError()"></span>
88
92
  </span>
89
93
  </label>
@@ -0,0 +1,113 @@
1
+ /**
2
+ * The app components a model places on its dashboard pages
3
+ * (`dashboard.regions`).
4
+ *
5
+ * There were two ways to change one of the dashboard's pages: say something
6
+ * on the model's `dashboard` block that the dashboard already knows how to
7
+ * draw, or replace the page with a file of the same name and own it from
8
+ * then on. To add one panel to a record's page an app copied the page.
9
+ *
10
+ * A region is the step between. The model names a component of the app's
11
+ * own for a place on the page, as it names an action for a button, and the
12
+ * framework's page draws it there:
13
+ *
14
+ * ```ts
15
+ * dashboard: { regions: { recordTop: 'OrderTimeline' } }
16
+ * ```
17
+ *
18
+ * A page reads this on the server, before it is drawn, because that is when
19
+ * a component can be chosen by name: the address says which model the page
20
+ * is for, and the manifest the dashboard is started with carries what each
21
+ * model declared. Read synchronously, as the sidebar is.
22
+ */
23
+ import type { DiscoveredModel } from './sidebar'
24
+ import { loadDiscoveredModels } from './sidebar'
25
+
26
+ /** The places a component can be put. `DashboardModelRegionName` in `@stacksjs/types`. */
27
+ export const DASHBOARD_MODEL_REGIONS = ['listTop', 'listBottom', 'recordTop', 'recordBottom'] as const
28
+ export type DashboardRegionName = typeof DASHBOARD_MODEL_REGIONS[number]
29
+
30
+ /** The component at each place, or an empty string where the model names none. */
31
+ export type DashboardRegions = Record<DashboardRegionName, string>
32
+
33
+ /**
34
+ * A component's name, as its file is called: `OrderTimeline`. Nothing else,
35
+ * since the name is what the file is looked up by.
36
+ */
37
+ const COMPONENT_NAME = /^[A-Z][A-Za-z0-9]*$/
38
+
39
+ const NONE: DashboardRegions = { listTop: '', listBottom: '', recordTop: '', recordBottom: '' }
40
+
41
+ /** Read a declaration: the places it fills, and what in it could not be used. */
42
+ export function readDashboardRegions(declared: unknown): { regions: DashboardRegions, problems: string[] } {
43
+ const regions: DashboardRegions = { ...NONE }
44
+ const problems: string[] = []
45
+ if (declared === undefined || declared === null)
46
+ return { regions, problems }
47
+ if (typeof declared !== 'object' || Array.isArray(declared)) {
48
+ problems.push('it is not an object of places and component names')
49
+ return { regions, problems }
50
+ }
51
+ for (const [place, name] of Object.entries(declared as Record<string, unknown>)) {
52
+ if (!(DASHBOARD_MODEL_REGIONS as readonly string[]).includes(place)) {
53
+ problems.push(`'${place}' is not a place a component can be put: ${DASHBOARD_MODEL_REGIONS.join(', ')}`)
54
+ continue
55
+ }
56
+ if (typeof name !== 'string' || !COMPONENT_NAME.test(name)) {
57
+ problems.push(`'${place}' must name a component as its file is called, such as 'OrderTimeline'`)
58
+ continue
59
+ }
60
+ regions[place as DashboardRegionName] = name
61
+ }
62
+ return { regions, problems }
63
+ }
64
+
65
+ const warned = new Set<string>()
66
+
67
+ /**
68
+ * The components the model at this address places on its pages.
69
+ *
70
+ * `slug` is the model as an address has it (`social-post`). A model the
71
+ * dashboard does not know, or one that names nothing, places nothing. What
72
+ * could not be used is logged once per model, by name.
73
+ */
74
+ export function dashboardModelRegions(slug: unknown, models: readonly DiscoveredModel[] = loadDiscoveredModels()): DashboardRegions {
75
+ const model = typeof slug === 'string' ? models.find(candidate => candidate.id === slug) : undefined
76
+ const { regions, problems } = readDashboardRegions(model?.dashboard?.regions)
77
+ if (model && problems.length > 0 && !warned.has(model.name)) {
78
+ warned.add(model.name)
79
+ for (const problem of problems)
80
+ console.warn(`[dashboard] ${model.name} dashboard.regions: ${problem}. It is left out.`)
81
+ }
82
+ return regions
83
+ }
84
+
85
+ /** One component placed at a place, and the model that placed it. */
86
+ export interface DashboardRegionEntry {
87
+ /** The model, as an address has it: `social-post`. */
88
+ model: string
89
+ component: string
90
+ }
91
+
92
+ /**
93
+ * Every component placed at one place, by the model that placed it.
94
+ *
95
+ * A dashboard page is the same document whatever address it is opened at:
96
+ * it is drawn once, and reads its address in the browser. So the server
97
+ * cannot draw "this model's component". It draws the component of every
98
+ * model that places one here, and the page mounts the one for the model in
99
+ * front of the viewer (`Models/ModelRegion.stx`).
100
+ *
101
+ * A place the dashboard does not have draws nothing.
102
+ */
103
+ export function dashboardRegionEntries(place: unknown, models: readonly DiscoveredModel[] = loadDiscoveredModels()): DashboardRegionEntry[] {
104
+ if (typeof place !== 'string' || !(DASHBOARD_MODEL_REGIONS as readonly string[]).includes(place))
105
+ return []
106
+ const entries: DashboardRegionEntry[] = []
107
+ for (const model of models) {
108
+ const component = dashboardModelRegions(model.id, models)[place as DashboardRegionName]
109
+ if (component)
110
+ entries.push({ model: model.id, component })
111
+ }
112
+ return entries.sort((a, b) => a.model.localeCompare(b.model))
113
+ }
@@ -27,6 +27,8 @@ interface DashboardModelOptionsLite {
27
27
  section?: string
28
28
  roles?: string[]
29
29
  description?: string
30
+ /** The app components the model places on its pages; read by `./regions`. */
31
+ regions?: unknown
30
32
  }
31
33
 
32
34
  export interface DiscoveredModel {
@@ -0,0 +1,52 @@
1
+ /**
2
+ * What a model's list is showing, for a component an app places on that
3
+ * page (`dashboard.regions.listTop` / `listBottom`).
4
+ *
5
+ * ```html
6
+ * <script client>
7
+ * import { useModelList } from '@stacksjs/defaults/views/dashboard/composables/useModelList'
8
+ *
9
+ * const { rows, total, filters } = useModelList()
10
+ * const overdue = derived(() => rows().filter(row => row.status === 'overdue').length)
11
+ * </script>
12
+ * ```
13
+ *
14
+ * Every value is read as a signal, so a component follows the list as it is
15
+ * searched, narrowed and paged.
16
+ */
17
+ import type { ModelListFilterValues } from '../../../functions/dashboard-model-filters'
18
+ import { modelPageStore } from '../stores/model-page'
19
+
20
+ export interface ModelListSnapshot {
21
+ /** The rows on the page in view, each its values by column. */
22
+ rows: () => Array<Record<string, unknown>>
23
+ /** How many records match, across every page. */
24
+ total: () => number
25
+ /** What the list is searched for. */
26
+ search: () => string
27
+ /** What the list is narrowed by, by column. */
28
+ filters: () => ModelListFilterValues
29
+ /** The model, as its address has it: `social-post`. Empty off a list. */
30
+ model: () => string
31
+ /** Ask the list to read again, after changing something it shows. */
32
+ reload: () => void
33
+ /**
34
+ * How many times the list has changed a record since the dashboard was
35
+ * opened: a delete, a bulk run, one of the model's actions. Read it in an
36
+ * `effect` to read numbers of your own again. Searching, narrowing and
37
+ * paging do not move it.
38
+ */
39
+ changes: () => number
40
+ }
41
+
42
+ export function useModelList(): ModelListSnapshot {
43
+ return {
44
+ rows: () => modelPageStore.list()?.rows ?? [],
45
+ total: () => modelPageStore.list()?.total ?? 0,
46
+ search: () => modelPageStore.list()?.search ?? '',
47
+ filters: () => modelPageStore.list()?.filters ?? {},
48
+ model: () => modelPageStore.list()?.slug ?? '',
49
+ reload: () => modelPageStore.reloadList(),
50
+ changes: () => modelPageStore.listChanges(),
51
+ }
52
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The record a record's page is showing, for a component an app places on
3
+ * that page (`dashboard.regions.recordTop` / `recordBottom`).
4
+ *
5
+ * ```html
6
+ * <script client>
7
+ * import { useModelRecord } from '@stacksjs/defaults/views/dashboard/composables/useModelRecord'
8
+ *
9
+ * const { record, reload } = useModelRecord()
10
+ * const shipped = derived(() => record()?.status === 'shipped')
11
+ * </script>
12
+ * ```
13
+ *
14
+ * Every value is read as a signal, so a component follows the page: null
15
+ * while the record loads, then the record, and the new one when the page
16
+ * reads it again after an action.
17
+ */
18
+ import type { ModelRecord } from '../../../functions/dashboard-model-record'
19
+ import { modelPageStore } from '../stores/model-page'
20
+
21
+ export interface ModelRecordSnapshot {
22
+ /** The record's values by column (`{ id: 7, name: 'Northwind', team_id: 3 }`), or null until it has loaded. */
23
+ record: () => Record<string, unknown> | null
24
+ /**
25
+ * Everything the page was told about the record: its values, what its
26
+ * columns are called, the records it points at by name, and what may be
27
+ * done to it. Null until it has loaded.
28
+ */
29
+ details: () => ModelRecord | null
30
+ /** The model, as its address has it: `social-post`. Empty off a record's page. */
31
+ model: () => string
32
+ /** The record's id, from the address. Known before the record has loaded. */
33
+ id: () => string
34
+ /** Ask the page to read the record again, after changing something it shows. */
35
+ reload: () => void
36
+ }
37
+
38
+ export function useModelRecord(): ModelRecordSnapshot {
39
+ return {
40
+ record: () => modelPageStore.record()?.record?.row ?? null,
41
+ details: () => modelPageStore.record()?.record ?? null,
42
+ model: () => modelPageStore.record()?.slug ?? '',
43
+ id: () => modelPageStore.record()?.id ?? '',
44
+ reload: () => modelPageStore.reloadRecord(),
45
+ }
46
+ }
@@ -0,0 +1,105 @@
1
+ import { defineStore, state } from '@stacksjs/stx'
2
+ import type { ModelListFilterValues } from '../../../functions/dashboard-model-filters'
3
+ import type { ModelRecord } from '../../../functions/dashboard-model-record'
4
+
5
+ /** The record a record's page is showing. `record` is null until it has loaded. */
6
+ export interface ModelPageRecord {
7
+ /** The model, as its address has it: `social-post`. */
8
+ slug: string
9
+ id: string
10
+ record: ModelRecord | null
11
+ }
12
+
13
+ /** What a model's list is showing now. */
14
+ export interface ModelPageList {
15
+ slug: string
16
+ rows: Array<Record<string, unknown>>
17
+ /** How many records match, across every page. */
18
+ total: number
19
+ search: string
20
+ filters: ModelListFilterValues
21
+ }
22
+
23
+ /**
24
+ * What the model page in front of the viewer is showing, for a component the
25
+ * page did not write.
26
+ *
27
+ * A component an app places on a framework page (`dashboard.regions`) is
28
+ * drawn on the server, where a page is the same document at every address.
29
+ * It can be handed its model and nothing that differs by address or changes
30
+ * afterwards. What the page loads, it publishes here, and the component
31
+ * reads it through `useModelRecord()` or `useModelList()`.
32
+ *
33
+ * It is also how the page says which model it is showing, which decides
34
+ * whose components are mounted (`Models/ModelRegionFor.stx`).
35
+ *
36
+ * One page at a time: a page says which record or list it is before it has
37
+ * loaded anything, so a component never shows the page before's.
38
+ */
39
+ export const modelPageStore = defineStore('dashboard-model-page', () => {
40
+ const record = state<ModelPageRecord | null>(null)
41
+ const list = state<ModelPageList | null>(null)
42
+ // Goes up each time the list changes a record: a delete, a bulk run that
43
+ // did something, one of the model's actions. What a component counted from
44
+ // the table is out of date after that, whichever rows are in view.
45
+ const listChanges = state(0)
46
+ // How to ask the page to read again. Set by the page, called by a component
47
+ // that changed something the page shows.
48
+ let reloadRecord: (() => void) | null = null
49
+ let reloadList: (() => void) | null = null
50
+
51
+ return {
52
+ record,
53
+ list,
54
+ listChanges,
55
+
56
+ /** A record's page has opened. Nothing is known of the record yet. */
57
+ openRecord(slug: string, id: string, reload: () => void): void {
58
+ const shown = record()
59
+ if (!shown || shown.slug !== slug || shown.id !== id)
60
+ record.set({ slug, id, record: null })
61
+ reloadRecord = reload
62
+ },
63
+ /** The record has loaded, or loaded again. */
64
+ showRecord(slug: string, id: string, loaded: ModelRecord | null): void {
65
+ record.set({ slug, id, record: loaded })
66
+ },
67
+ /** The page is gone. Only its own record is cleared, never the next page's. */
68
+ closeRecord(slug: string, id: string): void {
69
+ const shown = record()
70
+ if (shown && shown.slug === slug && shown.id === id) {
71
+ record.set(null)
72
+ reloadRecord = null
73
+ }
74
+ },
75
+ reloadRecord(): void {
76
+ reloadRecord?.()
77
+ },
78
+
79
+ /** A model's list has opened. */
80
+ openList(slug: string, reload: () => void): void {
81
+ const shown = list()
82
+ if (!shown || shown.slug !== slug)
83
+ list.set({ slug, rows: [], total: 0, search: '', filters: {} })
84
+ reloadList = reload
85
+ },
86
+ /** The list has loaded, or loaded again. */
87
+ showList(shown: ModelPageList): void {
88
+ list.set(shown)
89
+ },
90
+ closeList(slug: string): void {
91
+ if (list()?.slug === slug) {
92
+ list.set(null)
93
+ reloadList = null
94
+ }
95
+ },
96
+ reloadList(): void {
97
+ reloadList?.()
98
+ },
99
+ /** The list changed a record. Only the list in view says so. */
100
+ listChanged(slug: string): void {
101
+ if (list()?.slug === slug)
102
+ listChanges.set(listChanges() + 1)
103
+ },
104
+ }
105
+ })