@connextar/house 0.23.0 → 0.23.3
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/README.md +27 -3
- package/dist/analytics/browser.d.ts +1 -0
- package/dist/analytics/browser.d.ts.map +1 -1
- package/dist/analytics/browser.js +20 -3
- package/dist/analytics/browser.js.map +1 -1
- package/dist/analytics/config.d.ts +5 -0
- package/dist/analytics/config.d.ts.map +1 -1
- package/dist/analytics/config.js +4 -1
- package/dist/analytics/config.js.map +1 -1
- package/dist/analytics/react.d.ts.map +1 -1
- package/dist/analytics/react.js +4 -2
- package/dist/analytics/react.js.map +1 -1
- package/dist/analytics/redact.d.ts.map +1 -1
- package/dist/analytics/redact.js +6 -1
- package/dist/analytics/redact.js.map +1 -1
- package/dist/rate-limit/prisma-index.d.ts +1 -1
- package/dist/rate-limit/prisma-index.d.ts.map +1 -1
- package/dist/rate-limit/prisma-index.js.map +1 -1
- package/dist/rate-limit/prisma.d.ts +10 -1
- package/dist/rate-limit/prisma.d.ts.map +1 -1
- package/dist/rate-limit/prisma.js +2 -1
- package/dist/rate-limit/prisma.js.map +1 -1
- package/package.json +5 -2
- package/src/account/data-contract.ts +112 -0
- package/src/account/index.ts +11 -0
- package/src/admin/access.ts +267 -0
- package/src/admin/audit.ts +238 -0
- package/src/admin/bootstrap.ts +220 -0
- package/src/admin/client-index.ts +19 -0
- package/src/admin/client.tsx +187 -0
- package/src/admin/index.ts +91 -0
- package/src/admin/proof.ts +155 -0
- package/src/admin/second-factor-index.ts +16 -0
- package/src/admin/second-factor.ts +403 -0
- package/src/admin/testing-index.ts +13 -0
- package/src/admin/testing.ts +299 -0
- package/src/admin/view-as.ts +511 -0
- package/src/ai/config-index.ts +40 -0
- package/src/ai/config.ts +176 -0
- package/src/ai/images.ts +18 -0
- package/src/ai/index.ts +21 -0
- package/src/ai/pricing.ts +107 -0
- package/src/ai/provider.ts +403 -0
- package/src/analytics/browser.ts +400 -0
- package/src/analytics/client-index.ts +12 -0
- package/src/analytics/config.ts +89 -0
- package/src/analytics/index.ts +17 -0
- package/src/analytics/ingest.ts +81 -0
- package/src/analytics/react-index.ts +16 -0
- package/src/analytics/react.tsx +241 -0
- package/src/analytics/redact.ts +146 -0
- package/src/analytics/server-index.ts +17 -0
- package/src/analytics/server.ts +277 -0
- package/src/auth/index.ts +30 -0
- package/src/auth/second-factor-index.ts +16 -0
- package/src/auth/second-factor-next.ts +136 -0
- package/src/auth/second-factor-routes.ts +40 -0
- package/src/auth/second-factor.ts +226 -0
- package/src/auth/step-up-client-index.ts +13 -0
- package/src/auth/step-up-client.ts +23 -0
- package/src/auth/step-up-index.ts +19 -0
- package/src/auth/step-up-routes.ts +99 -0
- package/src/auth/step-up.ts +432 -0
- package/src/auth/two-factor-policy.ts +112 -0
- package/src/changelog/changelog.tsx +367 -0
- package/src/changelog/index.ts +12 -0
- package/src/editor/block-handle.tsx +312 -0
- package/src/editor/blocks.ts +169 -0
- package/src/editor/bubble-toolbar.tsx +208 -0
- package/src/editor/collapsible.ts +159 -0
- package/src/editor/content-index.ts +1 -0
- package/src/editor/content.tsx +343 -0
- package/src/editor/editor.tsx +333 -0
- package/src/editor/emoji-index.ts +1 -0
- package/src/editor/emoji-picker.tsx +114 -0
- package/src/editor/emoji.ts +436 -0
- package/src/editor/index.ts +6 -0
- package/src/editor/sections.ts +83 -0
- package/src/editor/suggestion-popup.tsx +136 -0
- package/src/editor/suggestions.ts +309 -0
- package/src/editor/ui.tsx +171 -0
- package/src/errors/error-shell.tsx +72 -0
- package/src/errors/global-error-shell.tsx +116 -0
- package/src/errors/index.ts +2 -0
- package/src/grid/columns.ts +48 -0
- package/src/grid/compare.ts +35 -0
- package/src/grid/hidden-columns.ts +95 -0
- package/src/grid/index.ts +27 -0
- package/src/grid/use-grid.ts +282 -0
- package/src/grid/views.ts +70 -0
- package/src/guides/guide-article.tsx +92 -0
- package/src/guides/guide-figure-zoom.tsx +533 -0
- package/src/guides/guide-figure.tsx +68 -0
- package/src/guides/guide-toc.tsx +133 -0
- package/src/guides/guides-landing.tsx +57 -0
- package/src/guides/guides-search.tsx +160 -0
- package/src/guides/guides-shell.tsx +60 -0
- package/src/guides/guides-sidebar.tsx +93 -0
- package/src/guides/index.ts +28 -0
- package/src/guides/library.ts +165 -0
- package/src/health/health.ts +115 -0
- package/src/health/index.ts +11 -0
- package/src/internal/cx.ts +4 -0
- package/src/internal/next-link.ts +23 -0
- package/src/jobs/counts.ts +151 -0
- package/src/jobs/cron-auth.ts +60 -0
- package/src/jobs/health.ts +315 -0
- package/src/jobs/idle.ts +86 -0
- package/src/jobs/index.ts +120 -0
- package/src/jobs/job-health-list.tsx +316 -0
- package/src/jobs/next-index.ts +4 -0
- package/src/jobs/next.ts +65 -0
- package/src/jobs/prisma-index.ts +12 -0
- package/src/jobs/prisma.ts +170 -0
- package/src/jobs/prune.ts +79 -0
- package/src/jobs/react-index.ts +35 -0
- package/src/jobs/registry.ts +189 -0
- package/src/jobs/scheduler.ts +437 -0
- package/src/jobs/store.ts +212 -0
- package/src/jobs/windows.ts +157 -0
- package/src/maintenance/core.ts +270 -0
- package/src/maintenance/gate.ts +171 -0
- package/src/maintenance/index.ts +40 -0
- package/src/maintenance/page.ts +76 -0
- package/src/maintenance/proxy.ts +88 -0
- package/src/maintenance/react-index.ts +7 -0
- package/src/maintenance/react.tsx +190 -0
- package/src/maintenance/route.ts +82 -0
- package/src/maintenance/unavailable.ts +61 -0
- package/src/markdown/figure.tsx +132 -0
- package/src/markdown/figures.ts +237 -0
- package/src/markdown/guide.tsx +536 -0
- package/src/markdown/index.ts +53 -0
- package/src/markdown/table-view.tsx +74 -0
- package/src/markdown/table.ts +90 -0
- package/src/password/common.ts +58 -0
- package/src/password/estimate-index.ts +18 -0
- package/src/password/estimate.ts +715 -0
- package/src/password/index.ts +40 -0
- package/src/password/password-input.tsx +116 -0
- package/src/password/password-strength.tsx +157 -0
- package/src/password/reveal.ts +151 -0
- package/src/rate-limit/better-auth.ts +94 -0
- package/src/rate-limit/index.ts +32 -0
- package/src/rate-limit/keys.ts +110 -0
- package/src/rate-limit/limiter.ts +276 -0
- package/src/rate-limit/prisma-index.ts +12 -0
- package/src/rate-limit/prisma.ts +157 -0
- package/src/rate-limit/store.ts +47 -0
- package/src/release/changelog.ts +364 -0
- package/src/release/index.ts +32 -0
- package/src/release/version.ts +64 -0
- package/src/setup-guide/bits.tsx +420 -0
- package/src/setup-guide/card.tsx +358 -0
- package/src/setup-guide/class-names.ts +144 -0
- package/src/setup-guide/context.tsx +239 -0
- package/src/setup-guide/guide.ts +271 -0
- package/src/setup-guide/index.ts +76 -0
- package/src/setup-guide/launcher.tsx +207 -0
- package/src/setup-guide/strip.tsx +127 -0
- package/src/styles/house.css +384 -0
- package/src/trail/index.ts +12 -0
- package/src/trail/rules.ts +84 -0
- package/src/trail/trail.tsx +367 -0
- package/src/ux/errors.ts +32 -0
- package/src/ux/feedback.tsx +41 -0
- package/src/ux/index.ts +44 -0
- package/src/ux/navigation-index.ts +16 -0
- package/src/ux/navigation-progress.tsx +270 -0
- package/src/ux/optimistic.ts +415 -0
- package/src/ux/use-action.ts +155 -0
- package/src/ux/use-optimistic-list.ts +227 -0
- package/src/ux/use-optimistic-value.ts +136 -0
- package/src/web-search/index.ts +13 -0
- package/src/web-search/providers.ts +177 -0
- package/src/web-search/types.ts +98 -0
- package/src/whats-new/body.tsx +190 -0
- package/src/whats-new/index.ts +43 -0
- package/src/whats-new/seen.ts +40 -0
- package/src/whats-new/select.ts +230 -0
- package/src/whats-new/use-whats-new.ts +109 -0
- package/src/wizard/index.ts +61 -0
- package/src/wizard/invalid.ts +152 -0
- package/src/wizard/react-hook-form-index.ts +11 -0
- package/src/wizard/rhf-adapter.ts +67 -0
- package/src/wizard/rules.ts +137 -0
- package/src/wizard/wizard.tsx +442 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The last boundary, as a component.
|
|
5
|
+
*
|
|
6
|
+
* A Next `global-error.tsx` replaces the root layout, so it renders its own
|
|
7
|
+
* `<html>` and `<body>` and cannot rely on providers, fonts, or anything else
|
|
8
|
+
* the layout sets up — by the time it runs, that is what failed. Which is why
|
|
9
|
+
* this uses no utility classes and no shared components: only an inline
|
|
10
|
+
* stylesheet, so the page holds up even if the app's CSS never arrives.
|
|
11
|
+
*
|
|
12
|
+
* That constraint is also what made this the single most-duplicated file in the
|
|
13
|
+
* portfolio: eight apps carrying sixty lines of near-identical inline CSS whose
|
|
14
|
+
* only real difference was five hex values. Those five are the props.
|
|
15
|
+
*
|
|
16
|
+
* The palette follows the OS theme through `prefers-color-scheme`, since there
|
|
17
|
+
* is no theme provider left to ask.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface GlobalErrorBrand {
|
|
21
|
+
/** Page background in light mode. */
|
|
22
|
+
background: string;
|
|
23
|
+
/** Body text in light mode. */
|
|
24
|
+
ink: string;
|
|
25
|
+
/** Secondary text in light mode. */
|
|
26
|
+
muted: string;
|
|
27
|
+
/** Hairlines in light mode. */
|
|
28
|
+
line: string;
|
|
29
|
+
/** The one colour that says which app this is. Used for the primary button. */
|
|
30
|
+
accent: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function GlobalErrorShell({
|
|
34
|
+
appName,
|
|
35
|
+
brand,
|
|
36
|
+
error,
|
|
37
|
+
reset,
|
|
38
|
+
homeHref = "/",
|
|
39
|
+
children,
|
|
40
|
+
}: {
|
|
41
|
+
/** Used in the heading: "<appName> couldn't start". */
|
|
42
|
+
appName: string;
|
|
43
|
+
brand: GlobalErrorBrand;
|
|
44
|
+
error: Error & { digest?: string };
|
|
45
|
+
reset: () => void;
|
|
46
|
+
homeHref?: string;
|
|
47
|
+
/** Replaces the default explanation, for an app that needs to say something else. */
|
|
48
|
+
children?: React.ReactNode;
|
|
49
|
+
}) {
|
|
50
|
+
return (
|
|
51
|
+
<html lang="en-GB">
|
|
52
|
+
<body>
|
|
53
|
+
<style>{`
|
|
54
|
+
:root {
|
|
55
|
+
color-scheme: light dark;
|
|
56
|
+
--ge-bg: ${brand.background};
|
|
57
|
+
--ge-ink: ${brand.ink};
|
|
58
|
+
--ge-muted: ${brand.muted};
|
|
59
|
+
--ge-line: ${brand.line};
|
|
60
|
+
--ge-brand: ${brand.accent};
|
|
61
|
+
}
|
|
62
|
+
@media (prefers-color-scheme: dark) {
|
|
63
|
+
:root {
|
|
64
|
+
--ge-bg: #0b1220;
|
|
65
|
+
--ge-ink: #e8edf5;
|
|
66
|
+
--ge-muted: #94a3b8;
|
|
67
|
+
--ge-line: #1e293b;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
body { margin: 0; background: var(--ge-bg); color: var(--ge-ink);
|
|
71
|
+
font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; }
|
|
72
|
+
.ge-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 3rem 1.5rem; }
|
|
73
|
+
.ge-card { max-width: 34rem; }
|
|
74
|
+
.ge-eyebrow { margin: 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
75
|
+
font-size: .75rem; letter-spacing: .14em; text-transform: uppercase; color: var(--ge-muted); }
|
|
76
|
+
.ge-title { margin: .75rem 0 0; font-size: 1.75rem; line-height: 1.2; font-weight: 600; }
|
|
77
|
+
.ge-body { margin: 1rem 0 0; line-height: 1.6; color: var(--ge-muted); }
|
|
78
|
+
.ge-actions { margin-top: 2rem; display: flex; gap: .75rem; flex-wrap: wrap; }
|
|
79
|
+
.ge-btn { padding: .625rem 1.25rem; border-radius: .5rem; font-size: .875rem; font-weight: 500;
|
|
80
|
+
cursor: pointer; border: 1px solid transparent; text-decoration: none; display: inline-block; }
|
|
81
|
+
.ge-btn-primary { background: var(--ge-brand); color: #ffffff; }
|
|
82
|
+
.ge-btn-secondary { border-color: var(--ge-line); color: var(--ge-ink); background: transparent; }
|
|
83
|
+
.ge-ref { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--ge-line);
|
|
84
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .75rem; color: var(--ge-muted); }
|
|
85
|
+
.ge-ref span { color: var(--ge-ink); user-select: all; }
|
|
86
|
+
`}</style>
|
|
87
|
+
<main className="ge-wrap">
|
|
88
|
+
<div className="ge-card">
|
|
89
|
+
<p className="ge-eyebrow">Application error</p>
|
|
90
|
+
<h1 className="ge-title">{appName} couldn’t start</h1>
|
|
91
|
+
{children ?? (
|
|
92
|
+
<p className="ge-body">
|
|
93
|
+
This is a fault in the app itself rather than in the page you asked for. Your data is untouched.
|
|
94
|
+
Reloading usually clears it; if it does not, it is on us to fix, and the reference below is what finds
|
|
95
|
+
it.
|
|
96
|
+
</p>
|
|
97
|
+
)}
|
|
98
|
+
<div className="ge-actions">
|
|
99
|
+
<button type="button" className="ge-btn ge-btn-primary" onClick={() => reset()}>
|
|
100
|
+
Reload
|
|
101
|
+
</button>
|
|
102
|
+
<a href={homeHref} className="ge-btn ge-btn-secondary">
|
|
103
|
+
Go home
|
|
104
|
+
</a>
|
|
105
|
+
</div>
|
|
106
|
+
{error.digest ? (
|
|
107
|
+
<p className="ge-ref">
|
|
108
|
+
Reference <span>{error.digest}</span>
|
|
109
|
+
</p>
|
|
110
|
+
) : null}
|
|
111
|
+
</div>
|
|
112
|
+
</main>
|
|
113
|
+
</body>
|
|
114
|
+
</html>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { GridValue } from "./compare.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the engine needs to know about a column — and nothing more.
|
|
5
|
+
*
|
|
6
|
+
* Deliberately free of `header`, `cell` and every other presentational field:
|
|
7
|
+
* the engine never renders anything, and each app's table already has its own
|
|
8
|
+
* column type with its own look. Hosts extend this interface and hand their own
|
|
9
|
+
* columns in; `useGrid` is generic over that type and gives the same objects
|
|
10
|
+
* back in `visibleColumns`, so nothing has to be mapped on the way through.
|
|
11
|
+
*/
|
|
12
|
+
export interface GridColumnSpec<T> {
|
|
13
|
+
/** Stable identity, used by sorting, filters, saved views and column choices. */
|
|
14
|
+
key: string;
|
|
15
|
+
/**
|
|
16
|
+
* Plain-text name for the column menu and the filter label. Falls back to
|
|
17
|
+
* `key` — which is a poor label, so hosts with a string header should pass it.
|
|
18
|
+
*/
|
|
19
|
+
label?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The underlying value, for sorting and for matching a search term.
|
|
22
|
+
*
|
|
23
|
+
* A cell renders arbitrary markup, which cannot be sorted or searched, so a
|
|
24
|
+
* column that wants either has to say what it actually holds. Declaring this
|
|
25
|
+
* is also what makes a column sortable at all — better than sorting by
|
|
26
|
+
* something meaningless.
|
|
27
|
+
*/
|
|
28
|
+
sortValue?: (row: T) => GridValue;
|
|
29
|
+
/**
|
|
30
|
+
* A short, repeating label — a status, a type, a person — offered as a filter
|
|
31
|
+
* on the column. Only offered when the rows hold more than one distinct
|
|
32
|
+
* value: a filter with a single option is clutter.
|
|
33
|
+
*/
|
|
34
|
+
filterValue?: (row: T) => string | null | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Keep this column out of the global search. Use it for a column whose value
|
|
37
|
+
* is an opaque id or a duplicate of another column's — matches against it
|
|
38
|
+
* confuse more than they help.
|
|
39
|
+
*/
|
|
40
|
+
unsearchable?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A column's distinct values, offered as a filter. */
|
|
44
|
+
export interface GridFacet {
|
|
45
|
+
key: string;
|
|
46
|
+
label: string;
|
|
47
|
+
values: string[];
|
|
48
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordering two cell values the way a reader expects them.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from AltEd, where the rule was learnt the hard way: a plain string
|
|
5
|
+
* comparison reads digits one character at a time and files every "Year 1x"
|
|
6
|
+
* before "Year 2", which looks exactly like a broken sort rather than a subtle
|
|
7
|
+
* one. `numeric` fixes that; `sensitivity: "base"` folds case and accents so
|
|
8
|
+
* "bertie" does not sort after "Cai".
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** What a column contributes to sorting. Anything else is the host's business. */
|
|
12
|
+
export type GridValue = string | number | Date | null | undefined;
|
|
13
|
+
|
|
14
|
+
const collator = new Intl.Collator("en-GB", { numeric: true, sensitivity: "base" });
|
|
15
|
+
|
|
16
|
+
/** Order two labels the way a reader expects. Nullish sorts as an empty string. */
|
|
17
|
+
export function compareValues(a: GridValue, b: GridValue): number {
|
|
18
|
+
const left = normalise(a);
|
|
19
|
+
const right = normalise(b);
|
|
20
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
21
|
+
return left === right ? 0 : left < right ? -1 : 1;
|
|
22
|
+
}
|
|
23
|
+
return collator.compare(String(left), String(right));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Compare two plain labels — for the value lists inside a column's filter. */
|
|
27
|
+
export function compareLabels(a: string, b: string): number {
|
|
28
|
+
return collator.compare(a, b);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function normalise(value: GridValue): string | number {
|
|
32
|
+
if (value === null || value === undefined) return "";
|
|
33
|
+
if (value instanceof Date) return value.getTime();
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState, useSyncExternalStore } from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Which columns the reader has switched off, remembered per grid.
|
|
7
|
+
*
|
|
8
|
+
* Kept separate from the engine so a grid driven by the server — paging and
|
|
9
|
+
* filtering in the database rather than in the browser — keeps the same
|
|
10
|
+
* behaviour and the same stored choice. The store belongs to the browser, not
|
|
11
|
+
* to whichever hook happens to be driving the table.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Shared empty array: `useSyncExternalStore` compares snapshots by identity. */
|
|
15
|
+
const NONE: string[] = [];
|
|
16
|
+
|
|
17
|
+
const storeKey = (grid: string) => `cx-grid:${grid}:hidden`;
|
|
18
|
+
|
|
19
|
+
// Parsing yields a fresh array each call, which would look like a new snapshot
|
|
20
|
+
// on every render, so the parse is cached against the text it came from.
|
|
21
|
+
const cache = new Map<string, { raw: string | null; value: string[] }>();
|
|
22
|
+
|
|
23
|
+
function read(grid: string | undefined): string[] {
|
|
24
|
+
if (!grid) return NONE;
|
|
25
|
+
let raw: string | null = null;
|
|
26
|
+
try {
|
|
27
|
+
raw = window.localStorage.getItem(storeKey(grid));
|
|
28
|
+
} catch {
|
|
29
|
+
// A blocked store is not worth breaking the grid over.
|
|
30
|
+
}
|
|
31
|
+
const cached = cache.get(grid);
|
|
32
|
+
if (cached && cached.raw === raw) return cached.value;
|
|
33
|
+
|
|
34
|
+
let value = NONE;
|
|
35
|
+
try {
|
|
36
|
+
if (raw) {
|
|
37
|
+
const parsed: unknown = JSON.parse(raw);
|
|
38
|
+
if (Array.isArray(parsed)) value = parsed.filter((key): key is string => typeof key === "string");
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
// A corrupt entry reads as "nothing hidden".
|
|
42
|
+
}
|
|
43
|
+
cache.set(grid, { raw, value });
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Another tab changing the saved columns should move this one too. */
|
|
48
|
+
function subscribe(onChange: () => void): () => void {
|
|
49
|
+
window.addEventListener("storage", onChange);
|
|
50
|
+
return () => window.removeEventListener("storage", onChange);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface HiddenColumns {
|
|
54
|
+
hiddenColumns: string[];
|
|
55
|
+
setHidden: (next: string[]) => void;
|
|
56
|
+
toggleColumn: (key: string) => void;
|
|
57
|
+
showAllColumns: () => void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useHiddenColumns(grid: string | undefined): HiddenColumns {
|
|
61
|
+
/**
|
|
62
|
+
* Read through `useSyncExternalStore` rather than copied into state by an
|
|
63
|
+
* effect — an effect meant one render with every column showing and a second
|
|
64
|
+
* with the reader's choice. `chosen` wins once they pick, which is also what
|
|
65
|
+
* keeps the control working where the store is blocked.
|
|
66
|
+
*/
|
|
67
|
+
const stored = useSyncExternalStore(
|
|
68
|
+
subscribe,
|
|
69
|
+
() => read(grid),
|
|
70
|
+
() => NONE,
|
|
71
|
+
);
|
|
72
|
+
const [chosen, setChosen] = useState<string[] | null>(null);
|
|
73
|
+
const hiddenColumns = chosen ?? stored;
|
|
74
|
+
|
|
75
|
+
function setHidden(next: string[]) {
|
|
76
|
+
setChosen(next);
|
|
77
|
+
if (!grid) return;
|
|
78
|
+
try {
|
|
79
|
+
window.localStorage.setItem(storeKey(grid), JSON.stringify(next));
|
|
80
|
+
} catch {
|
|
81
|
+
// Ignore — the choice just will not survive a reload.
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
hiddenColumns,
|
|
87
|
+
setHidden,
|
|
88
|
+
toggleColumn(key: string) {
|
|
89
|
+
setHidden(hiddenColumns.includes(key) ? hiddenColumns.filter((k) => k !== key) : [...hiddenColumns, key]);
|
|
90
|
+
},
|
|
91
|
+
showAllColumns() {
|
|
92
|
+
setHidden([]);
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/grid` — the arithmetic behind a list screen.
|
|
3
|
+
*
|
|
4
|
+
* Search, sorting, per-column filters, column visibility, saved views and
|
|
5
|
+
* paging, with no markup and no design system in them: React, and nothing else.
|
|
6
|
+
* Every app already has its own table primitives and its own look, so what it
|
|
7
|
+
* gets from here is the behaviour, not the appearance — the same bargain the
|
|
8
|
+
* error shell and the wizard offer.
|
|
9
|
+
*
|
|
10
|
+
* A host declares columns that extend `GridColumnSpec` with whatever its own
|
|
11
|
+
* table needs (`header`, `cell`, alignment, an inline editor) and hands them
|
|
12
|
+
* straight in; `useGrid` is generic over that type and returns the same objects
|
|
13
|
+
* in `visibleColumns`, so nothing is mapped on the way through.
|
|
14
|
+
*/
|
|
15
|
+
export type { GridColumnSpec, GridFacet } from "./columns.js";
|
|
16
|
+
export { compareLabels, compareValues, type GridValue } from "./compare.js";
|
|
17
|
+
export { useHiddenColumns, type HiddenColumns } from "./hidden-columns.js";
|
|
18
|
+
export {
|
|
19
|
+
DEFAULT_PAGE_SIZE,
|
|
20
|
+
useGrid,
|
|
21
|
+
type Grid,
|
|
22
|
+
type GridController,
|
|
23
|
+
type GridSort,
|
|
24
|
+
type SortDirection,
|
|
25
|
+
type UseGridOptions,
|
|
26
|
+
} from "./use-grid.js";
|
|
27
|
+
export { EMPTY_VIEW, parseGridView, type GridViewState } from "./views.js";
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useMemo, useState } from "react";
|
|
4
|
+
|
|
5
|
+
import type { GridColumnSpec, GridFacet } from "./columns.js";
|
|
6
|
+
import { compareLabels, compareValues } from "./compare.js";
|
|
7
|
+
import { useHiddenColumns } from "./hidden-columns.js";
|
|
8
|
+
import type { GridViewState } from "./views.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Search, sort, filter, hide columns and paginate one list — the behaviour
|
|
12
|
+
* every list screen in the portfolio should have and, before this, three apps
|
|
13
|
+
* had written three times.
|
|
14
|
+
*
|
|
15
|
+
* It is a hook over the host's own column type rather than a table library,
|
|
16
|
+
* because the thing that differs between those three apps is entirely the
|
|
17
|
+
* rendering: the cells hold arbitrary markup, each has its own table
|
|
18
|
+
* primitives, and adopting a library would mean rewriting every call site's
|
|
19
|
+
* column API to arrive at the same arithmetic. So the arithmetic is here and
|
|
20
|
+
* the markup stays where it belongs.
|
|
21
|
+
*
|
|
22
|
+
* Everything is client-side. These lists arrive already scoped to the signed-in
|
|
23
|
+
* account, so there is nothing to gain from another round trip and a great deal
|
|
24
|
+
* to gain from a filter that answers as it is typed. A list long enough to need
|
|
25
|
+
* server-side paging should drive `GridController` from its own hook instead —
|
|
26
|
+
* the toolbar contract below is what makes that swap invisible to the UI.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export type SortDirection = "asc" | "desc";
|
|
30
|
+
|
|
31
|
+
export interface GridSort {
|
|
32
|
+
key: string;
|
|
33
|
+
direction: SortDirection;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const DEFAULT_PAGE_SIZE = 25;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* What a toolbar needs from whichever hook is driving a table.
|
|
40
|
+
*
|
|
41
|
+
* `useGrid` satisfies it, and so can an app's own server-driven hook, which is
|
|
42
|
+
* what lets the search box, filter menus, column picker and saved views be
|
|
43
|
+
* written once and behave identically either way.
|
|
44
|
+
*/
|
|
45
|
+
export interface GridController {
|
|
46
|
+
query: string;
|
|
47
|
+
setQuery: (next: string) => void;
|
|
48
|
+
sort: GridSort | null;
|
|
49
|
+
toggleSort: (key: string) => void;
|
|
50
|
+
facetOptions: GridFacet[];
|
|
51
|
+
facets: Record<string, string[]>;
|
|
52
|
+
toggleFacet: (key: string, value: string) => void;
|
|
53
|
+
clearFacet: (key: string) => void;
|
|
54
|
+
activeFacetCount: number;
|
|
55
|
+
isFiltered: boolean;
|
|
56
|
+
reset: () => void;
|
|
57
|
+
total: number;
|
|
58
|
+
hiddenColumns: string[];
|
|
59
|
+
toggleColumn: (key: string) => void;
|
|
60
|
+
showAllColumns: () => void;
|
|
61
|
+
viewState: GridViewState;
|
|
62
|
+
applyView: (view: GridViewState) => void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface UseGridOptions<T, C extends GridColumnSpec<T>> {
|
|
66
|
+
rows: T[];
|
|
67
|
+
columns: C[];
|
|
68
|
+
/** How the list is ordered before anyone touches a header. */
|
|
69
|
+
initialSort?: GridSort;
|
|
70
|
+
/** A pre-filled search term — how a deep link lands on one row. */
|
|
71
|
+
initialQuery?: string;
|
|
72
|
+
pageSize?: number;
|
|
73
|
+
/** Extra text per row to match on, for values no column displays. */
|
|
74
|
+
searchExtra?: (row: T) => string | null | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Identifies this grid so column choices can be remembered and saved views
|
|
77
|
+
* can be filed against it. Without one the grid still works; it just forgets.
|
|
78
|
+
*/
|
|
79
|
+
storageKey?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface Grid<T, C extends GridColumnSpec<T>> extends GridController {
|
|
83
|
+
/** The rows to render: this page of the searched, filtered, sorted list. */
|
|
84
|
+
rows: T[];
|
|
85
|
+
/** Everything matching the search and filters, before paging. */
|
|
86
|
+
matched: T[];
|
|
87
|
+
/** Render these, not the full set — respects what the reader switched off. */
|
|
88
|
+
visibleColumns: C[];
|
|
89
|
+
page: number;
|
|
90
|
+
setPage: (page: number) => void;
|
|
91
|
+
pageCount: number;
|
|
92
|
+
pageSize: number;
|
|
93
|
+
setPageSize: (size: number) => void;
|
|
94
|
+
/** 1-based index of the first row on this page; 0 when there are none. */
|
|
95
|
+
from: number;
|
|
96
|
+
to: number;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function useGrid<T, C extends GridColumnSpec<T> = GridColumnSpec<T>>({
|
|
100
|
+
rows,
|
|
101
|
+
columns,
|
|
102
|
+
initialSort,
|
|
103
|
+
initialQuery,
|
|
104
|
+
pageSize: initialPageSize = DEFAULT_PAGE_SIZE,
|
|
105
|
+
searchExtra,
|
|
106
|
+
storageKey,
|
|
107
|
+
}: UseGridOptions<T, C>): Grid<T, C> {
|
|
108
|
+
const [query, setQuery] = useState(initialQuery ?? "");
|
|
109
|
+
const [sort, setSort] = useState<GridSort | null>(initialSort ?? null);
|
|
110
|
+
const [facets, setFacets] = useState<Record<string, string[]>>({});
|
|
111
|
+
const [pageSize, setPageSizeState] = useState(initialPageSize);
|
|
112
|
+
const [requestedPage, setPage] = useState(1);
|
|
113
|
+
const { hiddenColumns, setHidden, toggleColumn, showAllColumns } = useHiddenColumns(storageKey);
|
|
114
|
+
|
|
115
|
+
/** The columns actually rendered, in declared order. */
|
|
116
|
+
const visibleColumns = useMemo(
|
|
117
|
+
() => columns.filter((column) => !hiddenColumns.includes(column.key)),
|
|
118
|
+
[columns, hiddenColumns],
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
/** Columns that opted into a filter, with the distinct values they hold. */
|
|
122
|
+
const facetOptions = useMemo(
|
|
123
|
+
() =>
|
|
124
|
+
columns
|
|
125
|
+
.filter((column) => column.filterValue)
|
|
126
|
+
.map((column) => ({
|
|
127
|
+
key: column.key,
|
|
128
|
+
label: column.label ?? column.key,
|
|
129
|
+
values: [
|
|
130
|
+
...new Set(rows.map((row) => column.filterValue?.(row)).filter((v): v is string => Boolean(v))),
|
|
131
|
+
].sort(compareLabels),
|
|
132
|
+
}))
|
|
133
|
+
.filter((facet) => facet.values.length > 1),
|
|
134
|
+
[columns, rows],
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const matched = useMemo(() => {
|
|
138
|
+
const term = query.trim().toLowerCase();
|
|
139
|
+
|
|
140
|
+
const filtered = rows.filter((row) => {
|
|
141
|
+
// Every active filter must match — narrowing, not widening.
|
|
142
|
+
for (const [key, kept] of Object.entries(facets)) {
|
|
143
|
+
if (kept.length === 0) continue;
|
|
144
|
+
const column = columns.find((c) => c.key === key);
|
|
145
|
+
const value = column?.filterValue?.(row);
|
|
146
|
+
if (!value || !kept.includes(value)) return false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!term) return true;
|
|
150
|
+
|
|
151
|
+
const haystack = [...columns.filter((c) => !c.unsearchable).map((c) => valueOf(c, row)), searchExtra?.(row)]
|
|
152
|
+
.filter((value) => value !== null && value !== undefined)
|
|
153
|
+
.map((value) => (value instanceof Date ? value.toISOString() : String(value)))
|
|
154
|
+
.join(" ")
|
|
155
|
+
.toLowerCase();
|
|
156
|
+
|
|
157
|
+
return haystack.includes(term);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
if (!sort) return filtered;
|
|
161
|
+
const column = columns.find((c) => c.key === sort.key);
|
|
162
|
+
const sortValue = column?.sortValue;
|
|
163
|
+
if (!sortValue) return filtered;
|
|
164
|
+
|
|
165
|
+
// Copy before sorting: the caller's array is not ours to reorder.
|
|
166
|
+
return [...filtered].sort((a, b) => {
|
|
167
|
+
const result = compareValues(sortValue(a), sortValue(b));
|
|
168
|
+
return sort.direction === "asc" ? result : -result;
|
|
169
|
+
});
|
|
170
|
+
}, [rows, columns, query, facets, searchExtra, sort]);
|
|
171
|
+
|
|
172
|
+
const total = matched.length;
|
|
173
|
+
const pageCount = Math.max(1, Math.ceil(total / pageSize));
|
|
174
|
+
|
|
175
|
+
// Never strand someone on a page that no longer exists after a filter.
|
|
176
|
+
// Clamped here rather than corrected from an effect, which rendered the
|
|
177
|
+
// empty page once before putting it right.
|
|
178
|
+
const page = Math.min(requestedPage, pageCount);
|
|
179
|
+
|
|
180
|
+
const pageRows = useMemo(() => matched.slice((page - 1) * pageSize, page * pageSize), [matched, page, pageSize]);
|
|
181
|
+
|
|
182
|
+
/** Cycle a column: ascending → descending → unsorted. */
|
|
183
|
+
function toggleSort(key: string) {
|
|
184
|
+
setSort((current) => {
|
|
185
|
+
if (current?.key !== key) return { key, direction: "asc" };
|
|
186
|
+
if (current.direction === "asc") return { key, direction: "desc" };
|
|
187
|
+
return null;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** A new search starts at the beginning of the results, not on page 4. */
|
|
192
|
+
function search(next: string) {
|
|
193
|
+
setQuery(next);
|
|
194
|
+
setPage(1);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function toggleFacet(key: string, value: string) {
|
|
198
|
+
setPage(1);
|
|
199
|
+
setFacets((current) => {
|
|
200
|
+
const kept = current[key] ?? [];
|
|
201
|
+
const next = kept.includes(value) ? kept.filter((v) => v !== value) : [...kept, value];
|
|
202
|
+
if (next.length === 0) return withoutKey(current, key);
|
|
203
|
+
return { ...current, [key]: next };
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function clearFacet(key: string) {
|
|
208
|
+
setPage(1);
|
|
209
|
+
setFacets((current) => withoutKey(current, key));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function setPageSize(size: number) {
|
|
213
|
+
setPageSizeState(size);
|
|
214
|
+
setPage(1);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const activeFacetCount = Object.values(facets).reduce((sum, values) => sum + values.length, 0);
|
|
218
|
+
const isFiltered = query.trim().length > 0 || activeFacetCount > 0;
|
|
219
|
+
|
|
220
|
+
function reset() {
|
|
221
|
+
setQuery("");
|
|
222
|
+
setFacets({});
|
|
223
|
+
setPage(1);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Restore a saved view: search, sort, filters and columns together. */
|
|
227
|
+
function applyView(view: GridViewState) {
|
|
228
|
+
setQuery(view.query);
|
|
229
|
+
setSort(view.sort);
|
|
230
|
+
setFacets(view.facets);
|
|
231
|
+
setHidden(view.hiddenColumns);
|
|
232
|
+
if (view.pageSize) setPageSizeState(view.pageSize);
|
|
233
|
+
setPage(1);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
rows: pageRows,
|
|
238
|
+
matched,
|
|
239
|
+
total,
|
|
240
|
+
|
|
241
|
+
query,
|
|
242
|
+
setQuery: search,
|
|
243
|
+
|
|
244
|
+
sort,
|
|
245
|
+
toggleSort,
|
|
246
|
+
|
|
247
|
+
facetOptions,
|
|
248
|
+
facets,
|
|
249
|
+
toggleFacet,
|
|
250
|
+
clearFacet,
|
|
251
|
+
activeFacetCount,
|
|
252
|
+
isFiltered,
|
|
253
|
+
reset,
|
|
254
|
+
|
|
255
|
+
visibleColumns,
|
|
256
|
+
hiddenColumns,
|
|
257
|
+
toggleColumn,
|
|
258
|
+
showAllColumns,
|
|
259
|
+
|
|
260
|
+
viewState: { query, sort, facets, hiddenColumns, pageSize },
|
|
261
|
+
applyView,
|
|
262
|
+
|
|
263
|
+
page,
|
|
264
|
+
setPage,
|
|
265
|
+
pageCount,
|
|
266
|
+
pageSize,
|
|
267
|
+
setPageSize,
|
|
268
|
+
from: total === 0 ? 0 : (page - 1) * pageSize + 1,
|
|
269
|
+
to: Math.min(page * pageSize, total),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** The value a column contributes to the search haystack. */
|
|
274
|
+
function valueOf<T>(column: GridColumnSpec<T>, row: T) {
|
|
275
|
+
if (column.sortValue) return column.sortValue(row);
|
|
276
|
+
if (column.filterValue) return column.filterValue(row);
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function withoutKey(facets: Record<string, string[]>, key: string): Record<string, string[]> {
|
|
281
|
+
return Object.fromEntries(Object.entries(facets).filter(([candidate]) => candidate !== key));
|
|
282
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { GridSort } from "./use-grid.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Everything a saved view captures: how the list was searched, ordered,
|
|
5
|
+
* filtered and which columns were showing.
|
|
6
|
+
*
|
|
7
|
+
* This shape is written to a database or a browser store and read back later,
|
|
8
|
+
* possibly by a newer version of the app, so it is validated on the way in
|
|
9
|
+
* rather than trusted. A view saved before `pageSize` existed is still a valid
|
|
10
|
+
* view — every field is checked independently and a bad one is dropped, which
|
|
11
|
+
* is why this returns a repaired state rather than a boolean.
|
|
12
|
+
*/
|
|
13
|
+
export interface GridViewState {
|
|
14
|
+
query: string;
|
|
15
|
+
sort: GridSort | null;
|
|
16
|
+
/** Column key → the values kept. An absent key means "all values". */
|
|
17
|
+
facets: Record<string, string[]>;
|
|
18
|
+
hiddenColumns: string[];
|
|
19
|
+
/** Rows per page, when the view was saved with one. */
|
|
20
|
+
pageSize?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const EMPTY_VIEW: GridViewState = { query: "", sort: null, facets: {}, hiddenColumns: [] };
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Read a stored view, keeping what is intelligible and discarding what is not.
|
|
27
|
+
*
|
|
28
|
+
* Returns `null` only when the input is not an object at all. A partly corrupt
|
|
29
|
+
* view is worth more than no view: losing one stale filter is a smaller failure
|
|
30
|
+
* than a menu entry that throws when picked.
|
|
31
|
+
*/
|
|
32
|
+
export function parseGridView(input: unknown): GridViewState | null {
|
|
33
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) return null;
|
|
34
|
+
const raw = input as Record<string, unknown>;
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
query: typeof raw.query === "string" ? raw.query : "",
|
|
38
|
+
sort: parseSort(raw.sort),
|
|
39
|
+
facets: parseFacets(raw.facets),
|
|
40
|
+
hiddenColumns: parseStrings(raw.hiddenColumns),
|
|
41
|
+
...(typeof raw.pageSize === "number" && Number.isInteger(raw.pageSize) && raw.pageSize > 0
|
|
42
|
+
? { pageSize: raw.pageSize }
|
|
43
|
+
: {}),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function parseSort(input: unknown): GridSort | null {
|
|
48
|
+
if (typeof input !== "object" || input === null) return null;
|
|
49
|
+
const raw = input as Record<string, unknown>;
|
|
50
|
+
if (typeof raw.key !== "string" || raw.key.length === 0) return null;
|
|
51
|
+
if (raw.direction !== "asc" && raw.direction !== "desc") return null;
|
|
52
|
+
return { key: raw.key, direction: raw.direction };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parseFacets(input: unknown): Record<string, string[]> {
|
|
56
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) return {};
|
|
57
|
+
const out: Record<string, string[]> = {};
|
|
58
|
+
for (const [key, value] of Object.entries(input as Record<string, unknown>)) {
|
|
59
|
+
const values = parseStrings(value);
|
|
60
|
+
// An empty list means "all values" — the same as the key being absent, so
|
|
61
|
+
// it is dropped rather than stored as a filter that filters nothing.
|
|
62
|
+
if (values.length > 0) out[key] = values;
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function parseStrings(input: unknown): string[] {
|
|
68
|
+
if (!Array.isArray(input)) return [];
|
|
69
|
+
return input.filter((value): value is string => typeof value === "string");
|
|
70
|
+
}
|