@flaredev/cli 0.2.0 → 0.3.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/bin/flare.js +9 -1
- package/dist/index.js +751 -280
- package/package.json +2 -2
- package/templates/app/app/admin/page.tsx +9 -61
- package/templates/app/app/dashboard/account/page.tsx +6 -4
- package/templates/app/app/{admin → dashboard}/actions.ts +109 -11
- package/templates/app/app/dashboard/import-actions.ts +91 -0
- package/templates/app/app/dashboard/layout.tsx +42 -39
- package/templates/app/app/dashboard/observability/page.tsx +174 -0
- package/templates/app/app/dashboard/page.tsx +106 -25
- package/templates/app/app/verify-email/page.tsx +33 -0
- package/templates/app/components/account/security-settings.tsx +6 -9
- package/templates/app/components/auth/otp-input.tsx +95 -0
- package/templates/app/components/auth/password-field.tsx +116 -0
- package/templates/app/components/auth/password-forms.tsx +76 -79
- package/templates/app/components/auth/sign-in-flow.tsx +306 -315
- package/templates/app/components/auth/sign-up-form.tsx +93 -101
- package/templates/app/components/auth/two-factor-form.tsx +139 -124
- package/templates/app/components/auth/use-resend.ts +44 -0
- package/templates/app/components/auth/verify-email-form.tsx +135 -0
- package/templates/app/components/{admin/admin-header.tsx → dashboard/dashboard-header.tsx} +78 -78
- package/templates/app/components/{admin/resource-nav.tsx → dashboard/dashboard-sidebar.tsx} +166 -120
- package/templates/app/components/dashboard/export-button.tsx +41 -0
- package/templates/app/components/{admin → dashboard}/fields/file-field.tsx +302 -302
- package/templates/app/components/dashboard/import-dialog.tsx +487 -0
- package/templates/app/components/dashboard/page-header.tsx +57 -0
- package/templates/app/components/dashboard/resource-form-dialog.tsx +80 -0
- package/templates/app/components/{admin → dashboard}/resource-form-page.tsx +61 -55
- package/templates/app/components/{admin → dashboard}/resource-form.tsx +165 -139
- package/templates/app/components/dashboard/resource-stats.tsx +53 -0
- package/templates/app/components/{admin → dashboard}/resource-table.tsx +331 -281
- package/templates/app/components/{admin → dashboard}/row-actions.tsx +144 -111
- package/templates/app/components/dashboard/stat-card.tsx +69 -0
- package/templates/app/components/dashboard/table-selection.tsx +185 -0
- package/templates/app/components/dashboard/traffic-chart.tsx +32 -0
- package/templates/app/db/flare-schema.ts +33 -1
- package/templates/app/lib/analytics.ts +121 -0
- package/templates/app/lib/audit.ts +80 -0
- package/templates/app/lib/auth-config.ts +38 -33
- package/templates/app/lib/auth.ts +34 -0
- package/templates/app/lib/cache.ts +83 -83
- package/templates/app/lib/csv.ts +24 -0
- package/templates/app/lib/dashboard-nav.ts +15 -0
- package/templates/app/lib/{admin.ts → dashboard.ts} +138 -103
- package/templates/app/lib/password-rules.ts +159 -0
- package/templates/app/lib/site.ts +56 -47
- package/templates/app/migrations/0000_init.sql +15 -3
- package/templates/app/migrations/meta/0000_snapshot.json +97 -1
- package/templates/app/migrations/meta/_journal.json +1 -1
- package/templates/app/worker-configuration.d.ts +15764 -0
- package/templates/locks/package-lock.json +9189 -0
- package/templates/locks/pnpm-lock.yaml +6347 -0
- package/templates/app/app/admin/layout.tsx +0 -32
- package/templates/app/components/dashboard/dashboard-nav.tsx +0 -35
- package/templates/app/lib/admin-nav.ts +0 -15
- /package/templates/app/components/{admin → dashboard}/fields/country-field.tsx +0 -0
- /package/templates/app/components/{admin → dashboard}/fields/date-field.tsx +0 -0
- /package/templates/app/components/{admin → dashboard}/fields/field-widget.tsx +0 -0
- /package/templates/app/components/{admin → dashboard}/fields/multi-select-field.tsx +0 -0
- /package/templates/app/components/{admin → dashboard}/fields/phone-field.tsx +0 -0
- /package/templates/app/components/{admin → dashboard}/fields/relation-field.tsx +0 -0
- /package/templates/app/components/{admin → dashboard}/query.ts +0 -0
- /package/templates/app/components/{admin → dashboard}/resource-icon.tsx +0 -0
- /package/templates/app/components/{admin → dashboard}/resource-table-toolbar.tsx +0 -0
|
@@ -1,55 +1,61 @@
|
|
|
1
|
-
import { notFound } from "next/navigation";
|
|
2
|
-
import { formatValue, storedFields, type Resource } from "@flaredev/core";
|
|
3
|
-
import {
|
|
4
|
-
import type { RelationMeta } from "./fields/field-widget";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
1
|
+
import { notFound } from "next/navigation";
|
|
2
|
+
import { formatValue, storedFields, type Resource } from "@flaredev/core";
|
|
3
|
+
import { resourcePath, allResources, dashboardStore, requireAccess } from "@/lib/dashboard";
|
|
4
|
+
import type { RelationMeta } from "./fields/field-widget";
|
|
5
|
+
import { PageHeader } from "./page-header";
|
|
6
|
+
import { ResourceForm } from "./resource-form";
|
|
7
|
+
|
|
8
|
+
/** Server wrapper for create/edit pages: loads the record and relation titles, renders the heading and form. */
|
|
9
|
+
export async function ResourceFormPage({ resource, id }: { resource: Resource; id?: string }) {
|
|
10
|
+
await requireAccess(resource, id ? "update" : "create");
|
|
11
|
+
let record: Record<string, unknown> | null = null;
|
|
12
|
+
if (id) {
|
|
13
|
+
const result = await dashboardStore(resource.name).get(id);
|
|
14
|
+
if (!result.ok) notFound();
|
|
15
|
+
record = result.data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const byName = new Map(allResources().map((r) => [r.name, r]));
|
|
19
|
+
const relations: Record<string, RelationMeta & { initialTitle?: string }> = {};
|
|
20
|
+
for (const [key, def] of storedFields(resource)) {
|
|
21
|
+
if (def.kind !== "belongsTo") continue;
|
|
22
|
+
const target = byName.get(def.target);
|
|
23
|
+
if (!target) continue;
|
|
24
|
+
const value = record?.[key];
|
|
25
|
+
const titles = typeof value === "string" ? await dashboardStore(target.name).titles([value]) : {};
|
|
26
|
+
relations[key] = {
|
|
27
|
+
name: target.name,
|
|
28
|
+
label: target.label,
|
|
29
|
+
pluralLabel: target.pluralLabel,
|
|
30
|
+
slug: target.slug,
|
|
31
|
+
titleField: target.titleField,
|
|
32
|
+
initialTitle: typeof value === "string" ? titles[value] : undefined,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const titleDef = resource.fields[resource.titleField];
|
|
37
|
+
const title =
|
|
38
|
+
record && titleDef && titleDef.kind !== "hasMany" ? formatValue(titleDef, record[resource.titleField]) : `New ${resource.label.toLowerCase()}`;
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<div className="flex flex-col gap-6">
|
|
42
|
+
<PageHeader
|
|
43
|
+
title={title}
|
|
44
|
+
description={record ? `Edit this ${resource.label.toLowerCase()}.` : `Add a ${resource.label.toLowerCase()} to ${resource.pluralLabel.toLowerCase()}.`}
|
|
45
|
+
crumbs={[
|
|
46
|
+
{ label: "Dashboard", href: "/dashboard" },
|
|
47
|
+
{ label: resource.pluralLabel, href: resourcePath(resource) },
|
|
48
|
+
{ label: record ? "Edit" : "New" },
|
|
49
|
+
]}
|
|
50
|
+
/>
|
|
51
|
+
<ResourceForm
|
|
52
|
+
resource={resource}
|
|
53
|
+
mode={record ? "edit" : "create"}
|
|
54
|
+
id={id}
|
|
55
|
+
record={record}
|
|
56
|
+
relations={relations}
|
|
57
|
+
listHref={resourcePath(resource)}
|
|
58
|
+
/>
|
|
59
|
+
</div>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
@@ -1,139 +1,165 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import Link from "next/link";
|
|
4
|
-
import { useRouter } from "next/navigation";
|
|
5
|
-
import { useMemo, useState, useTransition, type FormEvent } from "react";
|
|
6
|
-
import { createValidators, formValuesToInput, initialFormValues, issuesByField, storedFields, type Resource } from "@flaredev/core";
|
|
7
|
-
import { toast } from "sonner";
|
|
8
|
-
import { createRecordAction, updateRecordAction } from "@/app/
|
|
9
|
-
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
10
|
-
import { Button } from "@/components/ui/button";
|
|
11
|
-
import { Card, CardContent, CardFooter } from "@/components/ui/card";
|
|
12
|
-
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
|
|
13
|
-
import { Spinner } from "@/components/ui/spinner";
|
|
14
|
-
import { FieldWidget, type RelationMeta } from "./fields/field-widget";
|
|
15
|
-
|
|
16
|
-
export interface ResourceFormProps {
|
|
17
|
-
resource: Resource;
|
|
18
|
-
mode: "create" | "edit";
|
|
19
|
-
/** Record id (edit mode). */
|
|
20
|
-
id?: string;
|
|
21
|
-
record?: Record<string, unknown> | null;
|
|
22
|
-
/** belongsTo field key → related resource metadata and the current value's title. */
|
|
23
|
-
relations?: Record<string, RelationMeta & { initialTitle?: string }>;
|
|
24
|
-
/** Where Cancel and a successful save go. */
|
|
25
|
-
listHref: string;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const [
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import Link from "next/link";
|
|
4
|
+
import { useRouter } from "next/navigation";
|
|
5
|
+
import { useMemo, useState, useTransition, type FormEvent } from "react";
|
|
6
|
+
import { createValidators, formValuesToInput, initialFormValues, issuesByField, storedFields, type Resource } from "@flaredev/core";
|
|
7
|
+
import { toast } from "sonner";
|
|
8
|
+
import { createRecordAction, updateRecordAction } from "@/app/dashboard/actions";
|
|
9
|
+
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
10
|
+
import { Button } from "@/components/ui/button";
|
|
11
|
+
import { Card, CardContent, CardFooter } from "@/components/ui/card";
|
|
12
|
+
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
|
|
13
|
+
import { Spinner } from "@/components/ui/spinner";
|
|
14
|
+
import { FieldWidget, type RelationMeta } from "./fields/field-widget";
|
|
15
|
+
|
|
16
|
+
export interface ResourceFormProps {
|
|
17
|
+
resource: Resource;
|
|
18
|
+
mode: "create" | "edit";
|
|
19
|
+
/** Record id (edit mode). */
|
|
20
|
+
id?: string;
|
|
21
|
+
record?: Record<string, unknown> | null;
|
|
22
|
+
/** belongsTo field key → related resource metadata and the current value's title. */
|
|
23
|
+
relations?: Record<string, RelationMeta & { initialTitle?: string }>;
|
|
24
|
+
/** Where Cancel and a successful save go, when the form owns the page. */
|
|
25
|
+
listHref: string;
|
|
26
|
+
/** In a dialog: close it instead of navigating, and drop the card around the fields. */
|
|
27
|
+
onDone?: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const WIDE = new Set(["text", "file"]);
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create/edit form rendered from field metadata. Values are validated in the browser
|
|
34
|
+
* with the descriptor's zod schemas, then again on the server by the same store the
|
|
35
|
+
* REST API uses; server errors (e.g. unique conflicts) land on their fields.
|
|
36
|
+
*/
|
|
37
|
+
export function ResourceForm({ resource, mode, id, record, relations = {}, listHref, onDone }: ResourceFormProps) {
|
|
38
|
+
const router = useRouter();
|
|
39
|
+
const validators = useMemo(() => createValidators(resource), [resource]);
|
|
40
|
+
const fields = useMemo(() => storedFields(resource), [resource]);
|
|
41
|
+
const [values, setValues] = useState(() => initialFormValues(resource, record));
|
|
42
|
+
const [errors, setErrors] = useState<Record<string, string>>({});
|
|
43
|
+
const [pending, startTransition] = useTransition();
|
|
44
|
+
|
|
45
|
+
const setValue = (key: string, value: string | boolean) => {
|
|
46
|
+
setValues((current) => ({ ...current, [key]: value }));
|
|
47
|
+
if (errors[key]) setErrors(({ [key]: _removed, ...rest }) => rest);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function focusFirstError(fieldErrors: Record<string, string>) {
|
|
51
|
+
const first = fields.find(([key]) => fieldErrors[key]);
|
|
52
|
+
if (first) document.getElementById(`field-${first[0]}`)?.focus();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function onSubmit(event: FormEvent) {
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
const input = formValuesToInput(resource, values, mode);
|
|
58
|
+
const parsed = (mode === "create" ? validators.create : validators.update).safeParse(input);
|
|
59
|
+
if (!parsed.success) {
|
|
60
|
+
const fieldErrors = issuesByField(parsed.error.issues);
|
|
61
|
+
setErrors(fieldErrors);
|
|
62
|
+
focusFirstError(fieldErrors);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
startTransition(async () => {
|
|
67
|
+
const result = mode === "create" ? await createRecordAction(resource.name, input) : await updateRecordAction(resource.name, id!, input);
|
|
68
|
+
if (result.ok) {
|
|
69
|
+
toast.success(`${resource.label} ${mode === "create" ? "created" : "saved"}.`);
|
|
70
|
+
if (onDone) onDone();
|
|
71
|
+
else router.push(listHref);
|
|
72
|
+
router.refresh();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const fieldErrors = result.issues ? issuesByField(result.issues) : result.field ? { [result.field]: result.error } : { _form: result.error };
|
|
76
|
+
setErrors(fieldErrors);
|
|
77
|
+
focusFirstError(fieldErrors);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const Frame = onDone ? DialogFrame : Card;
|
|
82
|
+
const Body = onDone ? DialogBody : CardContent;
|
|
83
|
+
const Footer = onDone ? DialogFooterRow : CardFooter;
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<form onSubmit={onSubmit} noValidate>
|
|
87
|
+
<Frame>
|
|
88
|
+
<Body>
|
|
89
|
+
{errors._form && (
|
|
90
|
+
<Alert variant="destructive" className="mb-6">
|
|
91
|
+
<AlertDescription>{errors._form}</AlertDescription>
|
|
92
|
+
</Alert>
|
|
93
|
+
)}
|
|
94
|
+
<FieldGroup className="grid gap-6 md:grid-cols-2">
|
|
95
|
+
{fields.map(([key, def]) => {
|
|
96
|
+
const error = errors[key];
|
|
97
|
+
const widgetId = `field-${key}`;
|
|
98
|
+
const horizontal = def.kind === "boolean";
|
|
99
|
+
return (
|
|
100
|
+
<Field
|
|
101
|
+
key={key}
|
|
102
|
+
data-invalid={error ? true : undefined}
|
|
103
|
+
className={WIDE.has(def.kind) ? "md:col-span-2" : undefined}
|
|
104
|
+
>
|
|
105
|
+
<FieldLabel htmlFor={widgetId}>
|
|
106
|
+
{def.label}
|
|
107
|
+
{def.required && def.kind !== "boolean" && (
|
|
108
|
+
<span className="ml-0.5 text-muted-foreground" aria-hidden>
|
|
109
|
+
*
|
|
110
|
+
</span>
|
|
111
|
+
)}
|
|
112
|
+
</FieldLabel>
|
|
113
|
+
<div className={horizontal ? "flex h-9 items-center" : undefined}>
|
|
114
|
+
<FieldWidget
|
|
115
|
+
id={widgetId}
|
|
116
|
+
name={key}
|
|
117
|
+
resourceName={resource.name}
|
|
118
|
+
fieldKey={key}
|
|
119
|
+
field={def}
|
|
120
|
+
value={values[key] ?? ""}
|
|
121
|
+
onChange={(value) => setValue(key, value)}
|
|
122
|
+
invalid={Boolean(error)}
|
|
123
|
+
disabled={pending}
|
|
124
|
+
relation={relations[key]}
|
|
125
|
+
/>
|
|
126
|
+
</div>
|
|
127
|
+
{def.helpText && <FieldDescription>{def.helpText}</FieldDescription>}
|
|
128
|
+
{error && <FieldError>{error}</FieldError>}
|
|
129
|
+
</Field>
|
|
130
|
+
);
|
|
131
|
+
})}
|
|
132
|
+
</FieldGroup>
|
|
133
|
+
</Body>
|
|
134
|
+
<Footer className="flex justify-end gap-2 border-t pt-4">
|
|
135
|
+
{onDone ? (
|
|
136
|
+
<Button type="button" variant="outline" onClick={onDone} disabled={pending}>
|
|
137
|
+
Cancel
|
|
138
|
+
</Button>
|
|
139
|
+
) : (
|
|
140
|
+
<Button variant="outline" asChild>
|
|
141
|
+
<Link href={listHref}>Cancel</Link>
|
|
142
|
+
</Button>
|
|
143
|
+
)}
|
|
144
|
+
<Button type="submit" disabled={pending}>
|
|
145
|
+
{pending && <Spinner data-icon="inline-start" />}
|
|
146
|
+
{mode === "create" ? `Create ${resource.label.toLowerCase()}` : "Save changes"}
|
|
147
|
+
</Button>
|
|
148
|
+
</Footer>
|
|
149
|
+
</Frame>
|
|
150
|
+
</form>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Plain wrappers so the same form works in a page (a card) and in a dialog (no card). */
|
|
155
|
+
function DialogFrame({ children }: { children: React.ReactNode }) {
|
|
156
|
+
return <div className="flex flex-col gap-4">{children}</div>;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function DialogBody({ children }: { children: React.ReactNode }) {
|
|
160
|
+
return <div className="max-h-[60vh] overflow-y-auto px-1">{children}</div>;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function DialogFooterRow({ children, className }: { children: React.ReactNode; className?: string }) {
|
|
164
|
+
return <div className={className}>{children}</div>;
|
|
165
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { storedFields, optionLabel, type Resource } from "@flaredev/core";
|
|
2
|
+
import { dashboardStore, recentCounts, recordCount, trend } from "@/lib/dashboard";
|
|
3
|
+
import { resourceIcon } from "./resource-icon";
|
|
4
|
+
import { StatCards, type Stat } from "./stat-card";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The numbers above a resource's table: how many there are, how many are new, and how
|
|
8
|
+
* they split across the resource's own status field.
|
|
9
|
+
*
|
|
10
|
+
* The split comes from the first enum field the descriptor marks as filterable, which
|
|
11
|
+
* in practice is the status of the thing — a deal's stage, an invoice's state.
|
|
12
|
+
*/
|
|
13
|
+
export async function ResourceStats({ resource }: { resource: Resource }) {
|
|
14
|
+
const [total, recent] = await Promise.all([recordCount(resource.name), recentCounts(resource.name)]);
|
|
15
|
+
|
|
16
|
+
const stats: Stat[] = [
|
|
17
|
+
{
|
|
18
|
+
label: `Total ${resource.pluralLabel.toLowerCase()}`,
|
|
19
|
+
value: total,
|
|
20
|
+
icon: resourceIcon(resource.icon),
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
label: "New this week",
|
|
24
|
+
value: recent.current,
|
|
25
|
+
change: trend(recent.current, recent.previous),
|
|
26
|
+
hint: `${recent.previous.toLocaleString()} the week before`,
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const statusField = storedFields(resource).find(([, def]) => def.kind === "enum" && def.filterable !== false);
|
|
31
|
+
if (statusField && total > 0) {
|
|
32
|
+
const [key, def] = statusField;
|
|
33
|
+
if (def.kind === "enum") {
|
|
34
|
+
const store = dashboardStore(resource.name);
|
|
35
|
+
// Two options at most: a strip of eight cards is a wall, not a summary.
|
|
36
|
+
const counts = await Promise.all(
|
|
37
|
+
def.options.slice(0, 2).map(async (option) => {
|
|
38
|
+
const result = await store.list(new URLSearchParams({ perPage: "1", [`filter[${key}]`]: option }));
|
|
39
|
+
return { option, total: result.ok ? result.data.meta.total : 0 };
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
for (const { option, total: count } of counts) {
|
|
43
|
+
stats.push({
|
|
44
|
+
label: optionLabel(def, option),
|
|
45
|
+
value: count,
|
|
46
|
+
hint: total > 0 ? `${Math.round((count / total) * 100)}% of all` : undefined,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return <StatCards stats={stats.slice(0, 4)} />;
|
|
53
|
+
}
|