@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,367 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ChevronLeft } from "lucide-react";
|
|
4
|
+
import { Link } from "../internal/next-link.js";
|
|
5
|
+
import { usePathname } from "next/navigation.js";
|
|
6
|
+
import {
|
|
7
|
+
createContext,
|
|
8
|
+
useCallback,
|
|
9
|
+
useContext,
|
|
10
|
+
useEffect,
|
|
11
|
+
useLayoutEffect,
|
|
12
|
+
useRef,
|
|
13
|
+
useState,
|
|
14
|
+
type ReactNode,
|
|
15
|
+
} from "react";
|
|
16
|
+
|
|
17
|
+
import { cx } from "../internal/cx.js";
|
|
18
|
+
import { codeFor, isCrumb, nextTrail, type Crumb } from "./rules.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The navigation trail on screen — see `./rules.ts` for the rules.
|
|
22
|
+
*
|
|
23
|
+
* Pages say where they are with `useTrail(chain)` (or `<TrailHere>` from a
|
|
24
|
+
* server page); the provider works out the trail and keeps it in
|
|
25
|
+
* `sessionStorage`, so it belongs to one tab and survives a reload but not a
|
|
26
|
+
* new window. A reference link calls `carryTrail()` before navigating — or
|
|
27
|
+
* carries `data-trail-carry` — which is what lets the destination keep the way
|
|
28
|
+
* back to the page it came from.
|
|
29
|
+
*
|
|
30
|
+
* Two renderings of the same crumbs: stacked vertical pills pinned to the left
|
|
31
|
+
* of the page on wide screens, and a pinned bar with Back across the top on
|
|
32
|
+
* narrow ones. Both appear only when there is somewhere to go back to — two
|
|
33
|
+
* crumbs or more — and only on the page that registered them.
|
|
34
|
+
*
|
|
35
|
+
* A trail belongs to one scope. Switching who is signed in, or which view of
|
|
36
|
+
* the product they are in, starts a new one: the old crumbs may lead to pages
|
|
37
|
+
* the new scope cannot see, so the stored trail carries the `scope` it was made
|
|
38
|
+
* in, and one from another scope is dropped.
|
|
39
|
+
*
|
|
40
|
+
* Storage keys need no per-app prefix: sessionStorage is already per origin.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const STORE = "house:trail";
|
|
44
|
+
const CARRY = "house:trail:carry";
|
|
45
|
+
/** A carry older than this was a navigation that never landed on a trail page. */
|
|
46
|
+
const CARRY_MS = 15_000;
|
|
47
|
+
|
|
48
|
+
interface TrailState {
|
|
49
|
+
path: string;
|
|
50
|
+
crumbs: Crumb[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type Register = (path: string, chain: Crumb[]) => void;
|
|
54
|
+
|
|
55
|
+
// Two contexts: the trail itself, which changes, and the function that sets it,
|
|
56
|
+
// which changes only with the scope. Pages depend only on the second —
|
|
57
|
+
// depending on a context that changes whenever they register would register
|
|
58
|
+
// again, forever.
|
|
59
|
+
const TrailStateContext = createContext<TrailState | null>(null);
|
|
60
|
+
const TrailRegisterContext = createContext<Register | null>(null);
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A short digest of the scope.
|
|
64
|
+
*
|
|
65
|
+
* The scope is only ever compared, never read back, so there is no reason to
|
|
66
|
+
* write down the thing itself — and an app may reasonably scope a trail by
|
|
67
|
+
* something it would rather not leave in storage. Not a boundary on its own:
|
|
68
|
+
* for a scope that carries a secret, see `persist`.
|
|
69
|
+
*/
|
|
70
|
+
function digest(scope: string): string {
|
|
71
|
+
let a = 0x811c9dc5;
|
|
72
|
+
let b = 0x9e3779b9;
|
|
73
|
+
for (let i = 0; i < scope.length; i++) {
|
|
74
|
+
const code = scope.charCodeAt(i);
|
|
75
|
+
a = Math.imul(a ^ code, 0x01000193);
|
|
76
|
+
b = Math.imul(b + code, 0x85ebca6b) ^ (b >>> 13);
|
|
77
|
+
}
|
|
78
|
+
return `${(a >>> 0).toString(36)}.${(b >>> 0).toString(36)}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Whether a reference link carried the trail here. Holds nothing but a timestamp. */
|
|
82
|
+
function readCarried(): boolean {
|
|
83
|
+
try {
|
|
84
|
+
const stamp = Number(sessionStorage.getItem(CARRY) ?? 0);
|
|
85
|
+
return stamp > 0 && Date.now() - stamp < CARRY_MS;
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function readStored(scope: string): Crumb[] {
|
|
92
|
+
try {
|
|
93
|
+
const parsed: unknown = JSON.parse(sessionStorage.getItem(STORE) ?? "null");
|
|
94
|
+
const stored =
|
|
95
|
+
typeof parsed === "object" && parsed !== null ? (parsed as { scope?: unknown; crumbs?: unknown }) : {};
|
|
96
|
+
return stored.scope === digest(scope) && Array.isArray(stored.crumbs) ? stored.crumbs.filter(isCrumb) : [];
|
|
97
|
+
} catch {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* `scope` is what the trail is made in — typically `${user.id}:${view}`.
|
|
104
|
+
* Mount it once in the layout that frames the pages which register crumbs.
|
|
105
|
+
*
|
|
106
|
+
* **`persist={false}` keeps the trail in memory only.** A crumb carries the
|
|
107
|
+
* `href` it points at, so on a door whose URLs *are* the credential — a
|
|
108
|
+
* share link with a token in the path — persisting would leave that token in
|
|
109
|
+
* `sessionStorage` for anything with DOM access to read. Turn it off there: the
|
|
110
|
+
* trail still works for as long as the tab stays on the page, and simply starts
|
|
111
|
+
* again after a reload (round 19's 19.15).
|
|
112
|
+
*/
|
|
113
|
+
export function TrailProvider({
|
|
114
|
+
scope = "",
|
|
115
|
+
persist = true,
|
|
116
|
+
children,
|
|
117
|
+
}: {
|
|
118
|
+
scope?: string;
|
|
119
|
+
persist?: boolean;
|
|
120
|
+
children: ReactNode;
|
|
121
|
+
}) {
|
|
122
|
+
const [state, setState] = useState<TrailState | null>(null);
|
|
123
|
+
const [shownScope, setShownScope] = useState(scope);
|
|
124
|
+
// Where the trail lives when it is not written down.
|
|
125
|
+
const held = useRef<Crumb[]>([]);
|
|
126
|
+
|
|
127
|
+
// Switched scope on this page: the trail on screen goes with the old one.
|
|
128
|
+
if (shownScope !== scope) {
|
|
129
|
+
setShownScope(scope);
|
|
130
|
+
setState(null);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const register = useCallback(
|
|
134
|
+
(path: string, chain: Crumb[]) => {
|
|
135
|
+
const previous = persist ? readStored(scope) : held.current;
|
|
136
|
+
const crumbs = nextTrail(previous, chain, readCarried());
|
|
137
|
+
held.current = crumbs;
|
|
138
|
+
try {
|
|
139
|
+
if (persist) sessionStorage.setItem(STORE, JSON.stringify({ scope: digest(scope), crumbs }));
|
|
140
|
+
sessionStorage.removeItem(CARRY);
|
|
141
|
+
} catch {
|
|
142
|
+
// Storage can be unavailable (private windows); the trail just won't persist.
|
|
143
|
+
}
|
|
144
|
+
setState({ path, crumbs });
|
|
145
|
+
},
|
|
146
|
+
[scope, persist],
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// A link marked `data-trail-carry` — a reference in read-only text — carries
|
|
150
|
+
// the trail, as the editor's references do.
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
const onClick = (event: MouseEvent) => {
|
|
153
|
+
if (event.target instanceof Element && event.target.closest("a[data-trail-carry]")) carryTrail();
|
|
154
|
+
};
|
|
155
|
+
document.addEventListener("click", onClick, true);
|
|
156
|
+
return () => document.removeEventListener("click", onClick, true);
|
|
157
|
+
}, []);
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<TrailRegisterContext.Provider value={register}>
|
|
161
|
+
<TrailStateContext.Provider value={state}>{children}</TrailStateContext.Provider>
|
|
162
|
+
</TrailRegisterContext.Provider>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Say where this page sits: its chain, root first, ending with itself. Pass null
|
|
168
|
+
* while the page doesn't know yet (nothing selected). Re-registers when the
|
|
169
|
+
* chain's keys, labels or links change — not on every render.
|
|
170
|
+
*/
|
|
171
|
+
export function useTrail(chain: Crumb[] | null): void {
|
|
172
|
+
const register = useContext(TrailRegisterContext);
|
|
173
|
+
const pathname = usePathname();
|
|
174
|
+
const signature = chain ? JSON.stringify(chain.map((c) => [c.key, c.label, c.href])) : "";
|
|
175
|
+
const latest = useRef(chain);
|
|
176
|
+
useLayoutEffect(() => {
|
|
177
|
+
latest.current = chain;
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
const current = latest.current;
|
|
182
|
+
if (register && current && current.length > 0) register(pathname, current);
|
|
183
|
+
}, [register, pathname, signature]);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** For a server page: say where it sits in the trail. Renders nothing. */
|
|
187
|
+
export function TrailHere({ chain }: { chain: Crumb[] }) {
|
|
188
|
+
useTrail(chain);
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Before following a reference: the next page keeps the trail that led to it. */
|
|
193
|
+
export function carryTrail(): void {
|
|
194
|
+
try {
|
|
195
|
+
sessionStorage.setItem(CARRY, String(Date.now()));
|
|
196
|
+
} catch {
|
|
197
|
+
// No storage, no trail — the link still works.
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The trail for the page on screen, or null when there is nothing to go back to. */
|
|
202
|
+
export function useActiveTrail(): Crumb[] | null {
|
|
203
|
+
const state = useContext(TrailStateContext);
|
|
204
|
+
const pathname = usePathname();
|
|
205
|
+
if (!state || state.path !== pathname || state.crumbs.length < 2) return null;
|
|
206
|
+
return state.crumbs;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Renders its children only while no trail is showing. A page's static
|
|
211
|
+
* breadcrumb and the trail answer the same question — how do I get back? — so
|
|
212
|
+
* when the trail is up, it answers alone.
|
|
213
|
+
*/
|
|
214
|
+
export function UnlessTrail({ children }: { children: ReactNode }) {
|
|
215
|
+
return useActiveTrail() ? null : <>{children}</>;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Stacked vertical pills pinned to the left of the page (wide screens). */
|
|
219
|
+
export function TrailRail({ className, top = "4rem" }: { className?: string; top?: string }) {
|
|
220
|
+
const crumbs = useActiveTrail();
|
|
221
|
+
if (!crumbs) return null;
|
|
222
|
+
const last = crumbs.length - 1;
|
|
223
|
+
|
|
224
|
+
return (
|
|
225
|
+
<nav
|
|
226
|
+
aria-label="Where you've been"
|
|
227
|
+
className={cx(
|
|
228
|
+
"house-trail-rail sticky hidden shrink-0 self-start border-r border-border lg:flex print:hidden",
|
|
229
|
+
className,
|
|
230
|
+
)}
|
|
231
|
+
style={{ top, height: `calc(100dvh - ${top})` }}
|
|
232
|
+
>
|
|
233
|
+
<ol className="flex">
|
|
234
|
+
{crumbs.map((crumb, index) => {
|
|
235
|
+
const current = index === last;
|
|
236
|
+
const body = (
|
|
237
|
+
<>
|
|
238
|
+
<span
|
|
239
|
+
className={cx(
|
|
240
|
+
"rounded px-1 py-0.5 font-mono text-[9px] leading-none font-bold",
|
|
241
|
+
current ? "bg-primary text-primary-foreground" : "bg-muted-foreground/70 text-background",
|
|
242
|
+
)}
|
|
243
|
+
aria-hidden
|
|
244
|
+
>
|
|
245
|
+
{codeFor(crumb)}
|
|
246
|
+
</span>
|
|
247
|
+
<span
|
|
248
|
+
className={cx(
|
|
249
|
+
"max-h-[calc(100dvh-10rem)] overflow-hidden text-[12.5px] font-semibold text-ellipsis whitespace-nowrap [writing-mode:vertical-rl] [transform:rotate(180deg)]",
|
|
250
|
+
current ? "text-foreground" : "text-muted-foreground",
|
|
251
|
+
)}
|
|
252
|
+
>
|
|
253
|
+
{crumb.label}
|
|
254
|
+
</span>
|
|
255
|
+
</>
|
|
256
|
+
);
|
|
257
|
+
const shared =
|
|
258
|
+
"house-trail-pill flex h-full w-11 flex-col items-center gap-2.5 border-r border-border py-3.5";
|
|
259
|
+
// Each step deeper is a shade further from the card; the stylesheet
|
|
260
|
+
// turns the step into a tint, so HSL-triplet themes can map the colours.
|
|
261
|
+
const style = { "--house-trail-step": Math.min(index, 5) } as React.CSSProperties;
|
|
262
|
+
return (
|
|
263
|
+
<li key={crumb.key} className="flex">
|
|
264
|
+
{current ? (
|
|
265
|
+
<span aria-current="page" title={crumb.label} className={shared} style={style} data-current="">
|
|
266
|
+
{body}
|
|
267
|
+
</span>
|
|
268
|
+
) : (
|
|
269
|
+
<Link
|
|
270
|
+
href={crumb.href}
|
|
271
|
+
title={`Back to ${crumb.label}`}
|
|
272
|
+
className={cx(
|
|
273
|
+
shared,
|
|
274
|
+
"transition-colors hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none focus-visible:ring-inset",
|
|
275
|
+
)}
|
|
276
|
+
style={style}
|
|
277
|
+
>
|
|
278
|
+
{body}
|
|
279
|
+
</Link>
|
|
280
|
+
)}
|
|
281
|
+
</li>
|
|
282
|
+
);
|
|
283
|
+
})}
|
|
284
|
+
</ol>
|
|
285
|
+
</nav>
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** The same trail as a pinned bar across the top, with Back (narrow screens). */
|
|
290
|
+
export function TrailBar({ className, top = "4rem" }: { className?: string; top?: string }) {
|
|
291
|
+
const crumbs = useActiveTrail();
|
|
292
|
+
const listRef = useRef<HTMLOListElement>(null);
|
|
293
|
+
|
|
294
|
+
// The end of the trail is where you are; keep it in view on a narrow bar.
|
|
295
|
+
useEffect(() => {
|
|
296
|
+
const list = listRef.current;
|
|
297
|
+
if (list) list.scrollLeft = list.scrollWidth;
|
|
298
|
+
}, [crumbs]);
|
|
299
|
+
|
|
300
|
+
const previous = crumbs?.at(-2);
|
|
301
|
+
if (!crumbs || !previous) return null;
|
|
302
|
+
const last = crumbs.length - 1;
|
|
303
|
+
|
|
304
|
+
return (
|
|
305
|
+
<nav
|
|
306
|
+
aria-label="Where you've been"
|
|
307
|
+
// Full-height screens subtract the bar via `--trail-bar`, set in
|
|
308
|
+
// house.css while this is on screen.
|
|
309
|
+
data-trail-bar=""
|
|
310
|
+
className={cx(
|
|
311
|
+
"sticky z-20 -mx-4 -mt-6 mb-4 flex items-center gap-1 border-b border-border bg-background/95 px-2 py-1.5 backdrop-blur sm:-mx-6 lg:hidden print:hidden",
|
|
312
|
+
className,
|
|
313
|
+
)}
|
|
314
|
+
style={{ top }}
|
|
315
|
+
>
|
|
316
|
+
<Link
|
|
317
|
+
href={previous.href}
|
|
318
|
+
className="flex min-h-8 shrink-0 items-center gap-0.5 rounded-md px-1.5 text-sm font-medium text-primary hover:bg-accent"
|
|
319
|
+
aria-label={`Back to ${previous.label}`}
|
|
320
|
+
>
|
|
321
|
+
<ChevronLeft className="size-4" aria-hidden />
|
|
322
|
+
Back
|
|
323
|
+
</Link>
|
|
324
|
+
<ol ref={listRef} className="flex min-w-0 flex-1 items-center overflow-x-auto text-sm whitespace-nowrap">
|
|
325
|
+
{crumbs.map((crumb, index) => (
|
|
326
|
+
<li key={crumb.key} className="flex items-center">
|
|
327
|
+
{index > 0 ? (
|
|
328
|
+
<span className="px-1 text-muted-foreground/60" aria-hidden>
|
|
329
|
+
/
|
|
330
|
+
</span>
|
|
331
|
+
) : null}
|
|
332
|
+
{index === last ? (
|
|
333
|
+
<span aria-current="page" className="max-w-[12rem] truncate font-medium">
|
|
334
|
+
{crumb.label}
|
|
335
|
+
</span>
|
|
336
|
+
) : (
|
|
337
|
+
<Link
|
|
338
|
+
href={crumb.href}
|
|
339
|
+
className="inline-flex min-h-8 max-w-[9rem] items-center truncate rounded px-1 text-muted-foreground hover:text-foreground"
|
|
340
|
+
>
|
|
341
|
+
{crumb.label}
|
|
342
|
+
</Link>
|
|
343
|
+
)}
|
|
344
|
+
</li>
|
|
345
|
+
))}
|
|
346
|
+
</ol>
|
|
347
|
+
</nav>
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Scroll to the element the URL's `#hash` names, once it exists. The router
|
|
353
|
+
* tries on arrival, but a streamed page can arrive before the element does — a
|
|
354
|
+
* crumb pointing at `/home#item-…` otherwise lands at the top of the page.
|
|
355
|
+
*/
|
|
356
|
+
export function ScrollToHash() {
|
|
357
|
+
useEffect(() => {
|
|
358
|
+
const go = () => {
|
|
359
|
+
const id = decodeURIComponent(window.location.hash.slice(1));
|
|
360
|
+
if (id) document.getElementById(id)?.scrollIntoView({ block: "start" });
|
|
361
|
+
};
|
|
362
|
+
go();
|
|
363
|
+
window.addEventListener("hashchange", go);
|
|
364
|
+
return () => window.removeEventListener("hashchange", go);
|
|
365
|
+
}, []);
|
|
366
|
+
return null;
|
|
367
|
+
}
|
package/src/ux/errors.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one thing an optimistic commit has to be able to say: "that didn't work,
|
|
3
|
+
* and here is what to tell the person".
|
|
4
|
+
*
|
|
5
|
+
* The hooks in this folder take a commit function that throws on failure —
|
|
6
|
+
* the only failure contract every transport already has. An app whose client
|
|
7
|
+
* returns a result object instead of throwing wraps it once (see
|
|
8
|
+
* `commitApi` in `lib/api.ts`) rather than teaching this layer its envelope.
|
|
9
|
+
*/
|
|
10
|
+
export class ActionError extends Error {
|
|
11
|
+
/** HTTP status, where the transport had one. */
|
|
12
|
+
readonly status?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Whatever the refusal carried. A conflict usually carries the record as it
|
|
15
|
+
* now is, which is what a caller merges into rather than rolling back to.
|
|
16
|
+
*/
|
|
17
|
+
readonly data?: unknown;
|
|
18
|
+
|
|
19
|
+
constructor(message: string, options: { status?: number; data?: unknown; cause?: unknown } = {}) {
|
|
20
|
+
super(message, { cause: options.cause });
|
|
21
|
+
this.name = "ActionError";
|
|
22
|
+
this.status = options.status;
|
|
23
|
+
this.data = options.data;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The wording to show for a thrown value, whatever it turned out to be. */
|
|
28
|
+
export function errorMessage(error: unknown, fallback = "Something went wrong"): string {
|
|
29
|
+
if (error instanceof Error && error.message) return error.message;
|
|
30
|
+
if (typeof error === "string" && error) return error;
|
|
31
|
+
return fallback;
|
|
32
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How this layer tells someone what happened, without knowing what the app
|
|
7
|
+
* uses to tell them.
|
|
8
|
+
*
|
|
9
|
+
* A toast library is a design decision, and the whole point of these hooks is
|
|
10
|
+
* that they can be lifted into a shared package. So the hooks report an event
|
|
11
|
+
* and the app decides what an event looks like — one provider at the root,
|
|
12
|
+
* wired to whatever that app already has.
|
|
13
|
+
*/
|
|
14
|
+
export interface ActionFeedback {
|
|
15
|
+
tone: "success" | "error";
|
|
16
|
+
title: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type ActionReporter = (feedback: ActionFeedback) => void;
|
|
21
|
+
|
|
22
|
+
const ReporterContext = React.createContext<ActionReporter | null>(null);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Without a provider a failure still has to go somewhere: swallowing it is how
|
|
26
|
+
* an optimistic update becomes a lie. The console is a poor place to tell
|
|
27
|
+
* someone something, but it is not silence, and it shows up in a test.
|
|
28
|
+
*/
|
|
29
|
+
const fallbackReporter: ActionReporter = (feedback) => {
|
|
30
|
+
if (feedback.tone !== "error") return;
|
|
31
|
+
console.error(`[action] ${feedback.title}${feedback.description ? `: ${feedback.description}` : ""}`);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function ActionFeedbackProvider({ report, children }: { report: ActionReporter; children: React.ReactNode }) {
|
|
35
|
+
return <ReporterContext.Provider value={report}>{children}</ReporterContext.Provider>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The app's reporter, or the console fallback. Stable across renders. */
|
|
39
|
+
export function useActionReporter(): ActionReporter {
|
|
40
|
+
return React.useContext(ReporterContext) ?? fallbackReporter;
|
|
41
|
+
}
|
package/src/ux/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/ux` — the interface answers the click.
|
|
3
|
+
*
|
|
4
|
+
* Optimistic updates and pending state, with no framework in them: React, and
|
|
5
|
+
* nothing else. The half that needs Next — the navigation bar and the spinner
|
|
6
|
+
* on the link you clicked — is `@connextar/house/ux/navigation`, kept apart so
|
|
7
|
+
* that `next` stays the optional peer dependency it is declared to be. A
|
|
8
|
+
* barrel that re-exported both made it mandatory for anyone importing so much
|
|
9
|
+
* as the pure patch engine, which a smoke test caught before 0.3.0 shipped.
|
|
10
|
+
*
|
|
11
|
+
* What this module needs from the host arrives as a function (`commit`), a
|
|
12
|
+
* provider (`ActionFeedbackProvider`) or a class name — never as an import.
|
|
13
|
+
*/
|
|
14
|
+
export { ActionError, errorMessage } from "./errors.js";
|
|
15
|
+
export { ActionFeedbackProvider, useActionReporter, type ActionFeedback, type ActionReporter } from "./feedback.js";
|
|
16
|
+
export {
|
|
17
|
+
applyPatches,
|
|
18
|
+
applyValuePatches,
|
|
19
|
+
isRedundant,
|
|
20
|
+
isRedundantValue,
|
|
21
|
+
isTempKey,
|
|
22
|
+
reconcile,
|
|
23
|
+
reconcileValue,
|
|
24
|
+
sameRow,
|
|
25
|
+
settlePatch,
|
|
26
|
+
settleValuePatch,
|
|
27
|
+
shouldDropSettled,
|
|
28
|
+
shouldDropSettledValue,
|
|
29
|
+
supersede,
|
|
30
|
+
supersedeValue,
|
|
31
|
+
tempKey,
|
|
32
|
+
type Patch,
|
|
33
|
+
type PatchKind,
|
|
34
|
+
type RowKey,
|
|
35
|
+
type ValuePatch,
|
|
36
|
+
} from "./optimistic.js";
|
|
37
|
+
export { useAction, useKeyedAction, type Action, type ActionOptions, type KeyedAction } from "./use-action.js";
|
|
38
|
+
export {
|
|
39
|
+
useOptimisticList,
|
|
40
|
+
type OptimisticList,
|
|
41
|
+
type OptimisticListOptions,
|
|
42
|
+
type WriteOptions,
|
|
43
|
+
} from "./use-optimistic-list.js";
|
|
44
|
+
export { useOptimisticValue, type OptimisticValue, type OptimisticValueOptions } from "./use-optimistic-value.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/ux/navigation` — feedback for moving between screens.
|
|
3
|
+
*
|
|
4
|
+
* Its own entry point because it is the only part of `ux` that imports `next`,
|
|
5
|
+
* which is an optional peer dependency: an app on another framework (or any
|
|
6
|
+
* app's Vitest suite running under plain Node) can still have the optimistic
|
|
7
|
+
* hooks from `@connextar/house/ux` without Next installed.
|
|
8
|
+
*/
|
|
9
|
+
export {
|
|
10
|
+
BUSY_ATTRIBUTE,
|
|
11
|
+
NavigationProgress,
|
|
12
|
+
startNavigation,
|
|
13
|
+
startRouteProgress,
|
|
14
|
+
useAppRouter,
|
|
15
|
+
useNavigate,
|
|
16
|
+
} from "./navigation-progress.js";
|