@connextar/house 0.23.1 → 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.
Files changed (180) hide show
  1. package/README.md +22 -3
  2. package/dist/analytics/browser.d.ts +1 -0
  3. package/dist/analytics/browser.d.ts.map +1 -1
  4. package/dist/analytics/browser.js +20 -3
  5. package/dist/analytics/browser.js.map +1 -1
  6. package/dist/analytics/config.d.ts +5 -0
  7. package/dist/analytics/config.d.ts.map +1 -1
  8. package/dist/analytics/config.js +4 -1
  9. package/dist/analytics/config.js.map +1 -1
  10. package/dist/analytics/react.d.ts.map +1 -1
  11. package/dist/analytics/react.js +4 -2
  12. package/dist/analytics/react.js.map +1 -1
  13. package/dist/analytics/redact.d.ts.map +1 -1
  14. package/dist/analytics/redact.js +6 -1
  15. package/dist/analytics/redact.js.map +1 -1
  16. package/package.json +5 -2
  17. package/src/account/data-contract.ts +112 -0
  18. package/src/account/index.ts +11 -0
  19. package/src/admin/access.ts +267 -0
  20. package/src/admin/audit.ts +238 -0
  21. package/src/admin/bootstrap.ts +220 -0
  22. package/src/admin/client-index.ts +19 -0
  23. package/src/admin/client.tsx +187 -0
  24. package/src/admin/index.ts +91 -0
  25. package/src/admin/proof.ts +155 -0
  26. package/src/admin/second-factor-index.ts +16 -0
  27. package/src/admin/second-factor.ts +403 -0
  28. package/src/admin/testing-index.ts +13 -0
  29. package/src/admin/testing.ts +299 -0
  30. package/src/admin/view-as.ts +511 -0
  31. package/src/ai/config-index.ts +40 -0
  32. package/src/ai/config.ts +176 -0
  33. package/src/ai/images.ts +18 -0
  34. package/src/ai/index.ts +21 -0
  35. package/src/ai/pricing.ts +107 -0
  36. package/src/ai/provider.ts +403 -0
  37. package/src/analytics/browser.ts +400 -0
  38. package/src/analytics/client-index.ts +12 -0
  39. package/src/analytics/config.ts +89 -0
  40. package/src/analytics/index.ts +17 -0
  41. package/src/analytics/ingest.ts +81 -0
  42. package/src/analytics/react-index.ts +16 -0
  43. package/src/analytics/react.tsx +241 -0
  44. package/src/analytics/redact.ts +146 -0
  45. package/src/analytics/server-index.ts +17 -0
  46. package/src/analytics/server.ts +277 -0
  47. package/src/auth/index.ts +30 -0
  48. package/src/auth/second-factor-index.ts +16 -0
  49. package/src/auth/second-factor-next.ts +136 -0
  50. package/src/auth/second-factor-routes.ts +40 -0
  51. package/src/auth/second-factor.ts +226 -0
  52. package/src/auth/step-up-client-index.ts +13 -0
  53. package/src/auth/step-up-client.ts +23 -0
  54. package/src/auth/step-up-index.ts +19 -0
  55. package/src/auth/step-up-routes.ts +99 -0
  56. package/src/auth/step-up.ts +432 -0
  57. package/src/auth/two-factor-policy.ts +112 -0
  58. package/src/changelog/changelog.tsx +367 -0
  59. package/src/changelog/index.ts +12 -0
  60. package/src/editor/block-handle.tsx +312 -0
  61. package/src/editor/blocks.ts +169 -0
  62. package/src/editor/bubble-toolbar.tsx +208 -0
  63. package/src/editor/collapsible.ts +159 -0
  64. package/src/editor/content-index.ts +1 -0
  65. package/src/editor/content.tsx +343 -0
  66. package/src/editor/editor.tsx +333 -0
  67. package/src/editor/emoji-index.ts +1 -0
  68. package/src/editor/emoji-picker.tsx +114 -0
  69. package/src/editor/emoji.ts +436 -0
  70. package/src/editor/index.ts +6 -0
  71. package/src/editor/sections.ts +83 -0
  72. package/src/editor/suggestion-popup.tsx +136 -0
  73. package/src/editor/suggestions.ts +309 -0
  74. package/src/editor/ui.tsx +171 -0
  75. package/src/errors/error-shell.tsx +72 -0
  76. package/src/errors/global-error-shell.tsx +116 -0
  77. package/src/errors/index.ts +2 -0
  78. package/src/grid/columns.ts +48 -0
  79. package/src/grid/compare.ts +35 -0
  80. package/src/grid/hidden-columns.ts +95 -0
  81. package/src/grid/index.ts +27 -0
  82. package/src/grid/use-grid.ts +282 -0
  83. package/src/grid/views.ts +70 -0
  84. package/src/guides/guide-article.tsx +92 -0
  85. package/src/guides/guide-figure-zoom.tsx +533 -0
  86. package/src/guides/guide-figure.tsx +68 -0
  87. package/src/guides/guide-toc.tsx +133 -0
  88. package/src/guides/guides-landing.tsx +57 -0
  89. package/src/guides/guides-search.tsx +160 -0
  90. package/src/guides/guides-shell.tsx +60 -0
  91. package/src/guides/guides-sidebar.tsx +93 -0
  92. package/src/guides/index.ts +28 -0
  93. package/src/guides/library.ts +165 -0
  94. package/src/health/health.ts +115 -0
  95. package/src/health/index.ts +11 -0
  96. package/src/internal/cx.ts +4 -0
  97. package/src/internal/next-link.ts +23 -0
  98. package/src/jobs/counts.ts +151 -0
  99. package/src/jobs/cron-auth.ts +60 -0
  100. package/src/jobs/health.ts +315 -0
  101. package/src/jobs/idle.ts +86 -0
  102. package/src/jobs/index.ts +120 -0
  103. package/src/jobs/job-health-list.tsx +316 -0
  104. package/src/jobs/next-index.ts +4 -0
  105. package/src/jobs/next.ts +65 -0
  106. package/src/jobs/prisma-index.ts +12 -0
  107. package/src/jobs/prisma.ts +170 -0
  108. package/src/jobs/prune.ts +79 -0
  109. package/src/jobs/react-index.ts +35 -0
  110. package/src/jobs/registry.ts +189 -0
  111. package/src/jobs/scheduler.ts +437 -0
  112. package/src/jobs/store.ts +212 -0
  113. package/src/jobs/windows.ts +157 -0
  114. package/src/maintenance/core.ts +270 -0
  115. package/src/maintenance/gate.ts +171 -0
  116. package/src/maintenance/index.ts +40 -0
  117. package/src/maintenance/page.ts +76 -0
  118. package/src/maintenance/proxy.ts +88 -0
  119. package/src/maintenance/react-index.ts +7 -0
  120. package/src/maintenance/react.tsx +190 -0
  121. package/src/maintenance/route.ts +82 -0
  122. package/src/maintenance/unavailable.ts +61 -0
  123. package/src/markdown/figure.tsx +132 -0
  124. package/src/markdown/figures.ts +237 -0
  125. package/src/markdown/guide.tsx +536 -0
  126. package/src/markdown/index.ts +53 -0
  127. package/src/markdown/table-view.tsx +74 -0
  128. package/src/markdown/table.ts +90 -0
  129. package/src/password/common.ts +58 -0
  130. package/src/password/estimate-index.ts +18 -0
  131. package/src/password/estimate.ts +715 -0
  132. package/src/password/index.ts +40 -0
  133. package/src/password/password-input.tsx +116 -0
  134. package/src/password/password-strength.tsx +157 -0
  135. package/src/password/reveal.ts +151 -0
  136. package/src/rate-limit/better-auth.ts +94 -0
  137. package/src/rate-limit/index.ts +32 -0
  138. package/src/rate-limit/keys.ts +110 -0
  139. package/src/rate-limit/limiter.ts +276 -0
  140. package/src/rate-limit/prisma-index.ts +12 -0
  141. package/src/rate-limit/prisma.ts +157 -0
  142. package/src/rate-limit/store.ts +47 -0
  143. package/src/release/changelog.ts +364 -0
  144. package/src/release/index.ts +32 -0
  145. package/src/release/version.ts +64 -0
  146. package/src/setup-guide/bits.tsx +420 -0
  147. package/src/setup-guide/card.tsx +358 -0
  148. package/src/setup-guide/class-names.ts +144 -0
  149. package/src/setup-guide/context.tsx +239 -0
  150. package/src/setup-guide/guide.ts +271 -0
  151. package/src/setup-guide/index.ts +76 -0
  152. package/src/setup-guide/launcher.tsx +207 -0
  153. package/src/setup-guide/strip.tsx +127 -0
  154. package/src/styles/house.css +384 -0
  155. package/src/trail/index.ts +12 -0
  156. package/src/trail/rules.ts +84 -0
  157. package/src/trail/trail.tsx +367 -0
  158. package/src/ux/errors.ts +32 -0
  159. package/src/ux/feedback.tsx +41 -0
  160. package/src/ux/index.ts +44 -0
  161. package/src/ux/navigation-index.ts +16 -0
  162. package/src/ux/navigation-progress.tsx +270 -0
  163. package/src/ux/optimistic.ts +415 -0
  164. package/src/ux/use-action.ts +155 -0
  165. package/src/ux/use-optimistic-list.ts +227 -0
  166. package/src/ux/use-optimistic-value.ts +136 -0
  167. package/src/web-search/index.ts +13 -0
  168. package/src/web-search/providers.ts +177 -0
  169. package/src/web-search/types.ts +98 -0
  170. package/src/whats-new/body.tsx +190 -0
  171. package/src/whats-new/index.ts +43 -0
  172. package/src/whats-new/seen.ts +40 -0
  173. package/src/whats-new/select.ts +230 -0
  174. package/src/whats-new/use-whats-new.ts +109 -0
  175. package/src/wizard/index.ts +61 -0
  176. package/src/wizard/invalid.ts +152 -0
  177. package/src/wizard/react-hook-form-index.ts +11 -0
  178. package/src/wizard/rhf-adapter.ts +67 -0
  179. package/src/wizard/rules.ts +137 -0
  180. package/src/wizard/wizard.tsx +442 -0
@@ -0,0 +1,239 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+
5
+ import { useActionReporter } from "../ux/feedback.js";
6
+ import type { SetupGuide, SetupReward } from "./guide.js";
7
+
8
+ /**
9
+ * The guide, shared by everything that draws it: a launcher in the header, a
10
+ * card on the home page, a strip on each screen where steps are done.
11
+ *
12
+ * The host reads the guide on the server and hands it in. What is *done* is
13
+ * always the server's answer — this never decides that. Paying for it is a
14
+ * separate request, made when the server's answer shows something done and
15
+ * unpaid, or shortly after any save, so the reward lands while the person is
16
+ * still looking at what earned it.
17
+ *
18
+ * Two things are deliberately the host's: the transport (this package makes no
19
+ * HTTP requests and knows no routes) and what a reward is called. MAS has no
20
+ * credits; an app that gives nothing at all passes steps with no reward and
21
+ * gets a checklist that still works.
22
+ */
23
+
24
+ export interface SetupSyncResult {
25
+ guide: SetupGuide;
26
+ rewards: SetupReward[];
27
+ }
28
+
29
+ export interface SetupGuideTransport {
30
+ /**
31
+ * Pay for everything done and unpaid, and answer with the guide as it now
32
+ * stands. `null` means the request did not succeed — the provider keeps what
33
+ * it has and says nothing, because a failed sync is not the person's problem
34
+ * and will be retried by the next save.
35
+ */
36
+ sync: (pathway: string) => Promise<SetupSyncResult | null>;
37
+ /** Hide the guide, or bring it back. `false` means it did not take. */
38
+ setHidden: (pathway: string, hidden: boolean) => Promise<boolean>;
39
+ }
40
+
41
+ /** What one of these is called, singular and plural. */
42
+ export interface RewardWords {
43
+ one: string;
44
+ many: string;
45
+ }
46
+
47
+ export const DEFAULT_REWARD_WORDS: RewardWords = { one: "credit", many: "credits" };
48
+
49
+ export function rewardWord(count: number, words: RewardWords): string {
50
+ return count === 1 ? words.one : words.many;
51
+ }
52
+
53
+ export interface SetupLinkProps {
54
+ href: string;
55
+ children: React.ReactNode;
56
+ className?: string;
57
+ onClick?: () => void;
58
+ target?: string;
59
+ rel?: string;
60
+ title?: string;
61
+ "aria-label"?: string;
62
+ }
63
+
64
+ /**
65
+ * How a link is drawn. The default is a plain `<a>`, which works everywhere and
66
+ * costs a full page load; an app with a router passes its own so the guide
67
+ * navigates the way the rest of the app does.
68
+ */
69
+ export type SetupLinkComponent = (props: SetupLinkProps) => React.ReactNode;
70
+
71
+ const PlainLink: SetupLinkComponent = ({ children, ...rest }) => <a {...rest}>{children}</a>;
72
+
73
+ export interface SetupGuideState {
74
+ guide: SetupGuide | null;
75
+ /** Somebody viewing this account rather than living in it: shown, never paid or changed. */
76
+ readOnly: boolean;
77
+ words: RewardWords;
78
+ Link: SetupLinkComponent;
79
+ panelOpen: boolean;
80
+ setPanelOpen: (open: boolean) => void;
81
+ setHidden: (hidden: boolean) => Promise<void>;
82
+ /** Paid a moment ago, for the launcher to flash. */
83
+ justEarned: number | null;
84
+ }
85
+
86
+ const SetupGuideContext = React.createContext<SetupGuideState | null>(null);
87
+
88
+ /** Null outside the provider, and for accounts with no guide — every part checks. */
89
+ export function useSetupGuide(): SetupGuideState | null {
90
+ return React.useContext(SetupGuideContext);
91
+ }
92
+
93
+ /** Long enough that a burst of saves asks one question rather than five. */
94
+ const SETTLE_MS = 1200;
95
+ /** How long the launcher keeps showing what just landed. */
96
+ const FLASH_MS = 4000;
97
+
98
+ export interface SetupGuideProviderProps {
99
+ initial: SetupGuide | null;
100
+ transport: SetupGuideTransport;
101
+ readOnly?: boolean;
102
+ words?: RewardWords;
103
+ /**
104
+ * A window event the app fires after a successful write. Without one the
105
+ * guide only catches up when the page is read again, which is correct but
106
+ * slower — the reward lands on the next navigation rather than in front of
107
+ * the person who earned it.
108
+ */
109
+ changeEvent?: string;
110
+ settleMs?: number;
111
+ /** Called when bringing a hidden guide back, which needs a fresh read. */
112
+ onRefresh?: () => void;
113
+ link?: SetupLinkComponent;
114
+ children: React.ReactNode;
115
+ }
116
+
117
+ export function SetupGuideProvider({
118
+ initial,
119
+ transport,
120
+ readOnly = false,
121
+ words = DEFAULT_REWARD_WORDS,
122
+ changeEvent,
123
+ settleMs = SETTLE_MS,
124
+ onRefresh,
125
+ link = PlainLink,
126
+ children,
127
+ }: SetupGuideProviderProps) {
128
+ const report = useActionReporter();
129
+ const [guide, setGuide] = React.useState(initial);
130
+ // A fresh reading from the server replaces ours: it knows things we cannot,
131
+ // like a learner added in another tab.
132
+ const [seen, setSeen] = React.useState(initial);
133
+ if (initial !== seen) {
134
+ setSeen(initial);
135
+ setGuide(initial);
136
+ }
137
+ const [panelOpen, setPanelOpen] = React.useState(false);
138
+ const [justEarned, setJustEarned] = React.useState<number | null>(null);
139
+ const inFlight = React.useRef(false);
140
+
141
+ const pathway = guide?.pathway ?? null;
142
+ const listening = Boolean(guide && !guide.hidden && !guide.complete && !readOnly && changeEvent);
143
+
144
+ // Read through a ref so the sync function's identity never changes, and the
145
+ // listeners below are not torn down and rebuilt on every render.
146
+ const latest = React.useRef({ transport, words, report });
147
+ React.useEffect(() => {
148
+ latest.current = { transport, words, report };
149
+ });
150
+
151
+ const sync = React.useCallback(async () => {
152
+ if (!pathway || inFlight.current) return;
153
+ inFlight.current = true;
154
+ const { transport: post, words: said, report: tell } = latest.current;
155
+ let result: SetupSyncResult | null = null;
156
+ try {
157
+ result = await post.sync(pathway);
158
+ } finally {
159
+ inFlight.current = false;
160
+ }
161
+ if (!result) return;
162
+ setGuide(result.guide);
163
+
164
+ const earned = result.rewards.reduce((total, reward) => total + reward.reward, 0);
165
+ if (result.rewards.length === 0 || earned === 0) return;
166
+ setJustEarned(earned);
167
+ const left = result.guide.totalReward - result.guide.earnedReward;
168
+ const what =
169
+ result.rewards.length === 1
170
+ ? (result.rewards[0]?.title ?? "A setup step")
171
+ : `${result.rewards.length} steps done`;
172
+ tell({
173
+ tone: "success",
174
+ title: `+${earned} ${rewardWord(earned, said)} — ${what}`,
175
+ description:
176
+ left > 0
177
+ ? `${result.guide.earnedReward} of ${result.guide.totalReward} earned so far — ${left} more to earn.`
178
+ : `All ${result.guide.totalReward} earned, and they are yours to keep.`,
179
+ });
180
+ }, [pathway]);
181
+
182
+ // Something done and not yet paid, as the server reads it: pay for it now.
183
+ // Deferred rather than called straight from the effect, which would be
184
+ // setting state during an effect's own render pass.
185
+ const pending = guide && !guide.hidden && !readOnly ? guide.pendingKeys.join(",") : "";
186
+ React.useEffect(() => {
187
+ if (!pending) return;
188
+ const timer = setTimeout(() => void sync(), 0);
189
+ return () => clearTimeout(timer);
190
+ }, [pending, sync]);
191
+
192
+ // Most saves never refresh the page, so ask once the saving has settled.
193
+ React.useEffect(() => {
194
+ if (!listening || !changeEvent) return;
195
+ let timer: ReturnType<typeof setTimeout> | undefined;
196
+ const onChange = () => {
197
+ clearTimeout(timer);
198
+ timer = setTimeout(() => void sync(), settleMs);
199
+ };
200
+ window.addEventListener(changeEvent, onChange);
201
+ return () => {
202
+ clearTimeout(timer);
203
+ window.removeEventListener(changeEvent, onChange);
204
+ };
205
+ }, [listening, changeEvent, settleMs, sync]);
206
+
207
+ React.useEffect(() => {
208
+ if (justEarned === null) return;
209
+ const timer = setTimeout(() => setJustEarned(null), FLASH_MS);
210
+ return () => clearTimeout(timer);
211
+ }, [justEarned]);
212
+
213
+ const setHidden = React.useCallback(
214
+ async (hidden: boolean) => {
215
+ if (!pathway || readOnly) return;
216
+ // Optimistic: hiding something should feel like closing it, not like
217
+ // asking permission to close it.
218
+ setGuide((current) => (current ? { ...current, hidden } : current));
219
+ const ok = await latest.current.transport.setHidden(pathway, hidden);
220
+ if (!ok) {
221
+ setGuide((current) => (current ? { ...current, hidden: !hidden } : current));
222
+ latest.current.report({ tone: "error", title: "Could not change the setup guide" });
223
+ return;
224
+ }
225
+ if (hidden) setPanelOpen(false);
226
+ // A hidden guide skipped working out what was done, so bringing it back
227
+ // needs the server asked again.
228
+ else onRefresh?.();
229
+ },
230
+ [pathway, readOnly, onRefresh],
231
+ );
232
+
233
+ const value = React.useMemo<SetupGuideState>(
234
+ () => ({ guide, readOnly, words, Link: link, panelOpen, setPanelOpen, setHidden, justEarned }),
235
+ [guide, readOnly, words, link, panelOpen, setHidden, justEarned],
236
+ );
237
+
238
+ return <SetupGuideContext.Provider value={value}>{children}</SetupGuideContext.Provider>;
239
+ }
@@ -0,0 +1,271 @@
1
+ /**
2
+ * What a new account is asked to do, how far through it is, and what that has
3
+ * earned. No React and no DOM, so the rules are tested without a browser and
4
+ * hold whatever draws them.
5
+ *
6
+ * **Nothing about progress is stored.** A step is *done* when the account holds
7
+ * what it asks for — a learner, a price, a published course — however it got
8
+ * there; the host reads that fresh each time. What *is* stored is the reward:
9
+ * one entry per step per account, keyed on the step, so a step pays once
10
+ * however many times its thing is made, deleted and made again. That split is
11
+ * the whole design, and it is why `done` and `earned` arrive here as two
12
+ * separate sets rather than one status per step.
13
+ *
14
+ * A step key is permanent. Rewards are keyed on it, so renaming one pays
15
+ * everybody a second time.
16
+ */
17
+
18
+ import type { ReactNode } from "react";
19
+
20
+ /** Short numbered instructions. `**bold**` marks the app's own labels, `_italic_` an example. */
21
+ export interface SetupHowTo {
22
+ title: string;
23
+ steps: readonly string[];
24
+ }
25
+
26
+ /** Where to read more. Rendered as a link, so the host decides what the href means. */
27
+ export interface SetupGuideLink {
28
+ href: string;
29
+ title: string;
30
+ }
31
+
32
+ export interface SetupStepDef {
33
+ /** Stable forever: rewards are keyed on it. */
34
+ key: string;
35
+ title: string;
36
+ /** What finishing it pays. Omit, or 0, for a step that is progress only. */
37
+ reward?: number;
38
+ how?: SetupHowTo;
39
+ guide?: SetupGuideLink;
40
+ }
41
+
42
+ export interface SetupItemDef {
43
+ key: string;
44
+ title: string;
45
+ description?: string;
46
+ /** Drawn beside the title. The host's own icon element, so this package needs no icon set of its own. */
47
+ icon?: ReactNode;
48
+ /** Where the item is done. */
49
+ href: string;
50
+ actionLabel: string;
51
+ /**
52
+ * The screen that shows this item's steps, when it has several. Everywhere
53
+ * else shows the item's progress and a way here.
54
+ */
55
+ screen?: string;
56
+ /**
57
+ * Step keys that must be done before this item can be started. An item nobody
58
+ * can start yet says so instead of offering a button that leads nowhere useful.
59
+ */
60
+ requires?: readonly string[];
61
+ /** What to say while it is blocked, e.g. "After your first learner". */
62
+ blockedLabel?: string;
63
+ /**
64
+ * This item's steps are choices made inside one flow that saves nothing until
65
+ * its last press, so they tick as they are made and nothing exists to pay for
66
+ * until the whole thing is built.
67
+ *
68
+ * **It describes the host's flow; it is not a rule this module enforces.**
69
+ * `rewardsOwed` pays whatever is done and unpaid, as it does for every other
70
+ * item — what keeps a half-built thing from paying is that the host's records
71
+ * cannot say a step is done before the thing exists. Setting it changes only
72
+ * what is *said* on screen, and it is read as "not built yet" only while
73
+ * nothing has been earned: once the first reward lands, the thing plainly
74
+ * does exist, and going on saying "when it is built" would be untrue
75
+ * (round 19's 19.16).
76
+ */
77
+ paysOnComplete?: boolean;
78
+ steps: readonly SetupStepDef[];
79
+ }
80
+
81
+ // ---------------------------------------------------------------------------
82
+ // The guide as one account sees it
83
+ // ---------------------------------------------------------------------------
84
+
85
+ export interface GuideStep extends SetupStepDef {
86
+ /** The account holds what the step asks for. */
87
+ done: boolean;
88
+ /** Its reward has been paid. */
89
+ earned: boolean;
90
+ /** Always a number here, whatever the definition left out. */
91
+ reward: number;
92
+ }
93
+
94
+ export interface GuideItem extends Omit<SetupItemDef, "steps"> {
95
+ steps: GuideStep[];
96
+ reward: number;
97
+ earnedReward: number;
98
+ doneCount: number;
99
+ done: boolean;
100
+ /** Why it cannot be started yet, when it cannot. */
101
+ blocked: string | null;
102
+ }
103
+
104
+ export interface SetupGuide {
105
+ /** Which set of steps this is — the host's own name for it. */
106
+ pathway: string;
107
+ items: GuideItem[];
108
+ totalReward: number;
109
+ earnedReward: number;
110
+ doneItems: number;
111
+ /** The first item not done and not blocked — what the guide leads with. */
112
+ nextKey: string | null;
113
+ /** Every step done and paid. */
114
+ complete: boolean;
115
+ /** Done but not yet paid. A sync would pay exactly these. */
116
+ pendingKeys: string[];
117
+ hidden: boolean;
118
+ }
119
+
120
+ /** What one step paid, for announcing it. */
121
+ export interface SetupReward {
122
+ key: string;
123
+ title: string;
124
+ reward: number;
125
+ }
126
+
127
+ export interface BuildGuideInput {
128
+ pathway: string;
129
+ items: readonly SetupItemDef[];
130
+ /** Step keys the account's own records satisfy. */
131
+ done: ReadonlySet<string>;
132
+ /** Step keys already paid for. */
133
+ earned: ReadonlySet<string>;
134
+ hidden?: boolean;
135
+ }
136
+
137
+ const rewardOf = (step: SetupStepDef): number => step.reward ?? 0;
138
+
139
+ const sum = (numbers: readonly number[]): number => numbers.reduce((total, n) => total + n, 0);
140
+
141
+ /** Every step of every item, in order. */
142
+ export function stepsOf(items: readonly SetupItemDef[]): SetupStepDef[] {
143
+ return items.flatMap((item) => [...item.steps]);
144
+ }
145
+
146
+ /** Every step key, for checking what a request names before trusting it. */
147
+ export function stepKeysOf(items: readonly SetupItemDef[]): Set<string> {
148
+ return new Set(stepsOf(items).map((step) => step.key));
149
+ }
150
+
151
+ /**
152
+ * Whether an item can be started yet.
153
+ *
154
+ * Only while nothing of it is done: an item that somehow got started stays
155
+ * open, because telling somebody they cannot do a thing they have half done is
156
+ * worse than a requirement quietly not applying.
157
+ */
158
+ function blockedBy(item: SetupItemDef, done: ReadonlySet<string>, doneCount: number): string | null {
159
+ if (doneCount > 0 || !item.requires?.length) return null;
160
+ const unmet = item.requires.some((key) => !done.has(key));
161
+ return unmet ? (item.blockedLabel ?? "Not yet") : null;
162
+ }
163
+
164
+ /** The guide as it stands for one account. Pure: the same inputs always give the same answer. */
165
+ export function buildGuide({ pathway, items, done, earned, hidden = false }: BuildGuideInput): SetupGuide {
166
+ const built: GuideItem[] = items.map((def) => {
167
+ const steps: GuideStep[] = def.steps.map((step) => ({
168
+ ...step,
169
+ reward: rewardOf(step),
170
+ done: done.has(step.key),
171
+ earned: earned.has(step.key),
172
+ }));
173
+ const doneCount = steps.filter((step) => step.done).length;
174
+ return {
175
+ ...def,
176
+ steps,
177
+ reward: sum(steps.map((step) => step.reward)),
178
+ earnedReward: sum(steps.filter((step) => step.earned).map((step) => step.reward)),
179
+ doneCount,
180
+ // An item with no steps at all is not "done" — it has nothing to be done about.
181
+ done: steps.length > 0 && doneCount === steps.length,
182
+ blocked: blockedBy(def, done, doneCount),
183
+ };
184
+ });
185
+
186
+ const all = built.flatMap((item) => item.steps);
187
+ return {
188
+ pathway,
189
+ items: built,
190
+ totalReward: sum(all.map((step) => step.reward)),
191
+ earnedReward: sum(all.filter((step) => step.earned).map((step) => step.reward)),
192
+ doneItems: built.filter((item) => item.done).length,
193
+ nextKey: built.find((item) => !item.done && !item.blocked)?.key ?? null,
194
+ complete: all.length > 0 && all.every((step) => step.done && step.earned),
195
+ pendingKeys: all.filter((step) => step.done && !step.earned).map((step) => step.key),
196
+ hidden,
197
+ };
198
+ }
199
+
200
+ export function findItem(guide: SetupGuide | null | undefined, key: string): GuideItem | null {
201
+ return guide?.items.find((item) => item.key === key) ?? null;
202
+ }
203
+
204
+ /**
205
+ * What counts as done: what the records show now, plus everything already paid
206
+ * for.
207
+ *
208
+ * A reward, once earned, is not taken back — so a step whose thing has since
209
+ * been deleted stays ticked rather than reopening and asking to be paid a
210
+ * second time. Hosts pass the result of this as `done`.
211
+ */
212
+ export function doneSteps(facts: ReadonlySet<string>, earned: ReadonlySet<string>): Set<string> {
213
+ return new Set([...facts, ...earned]);
214
+ }
215
+
216
+ /**
217
+ * The steps this account has done and not been paid for — what a sync owes it.
218
+ *
219
+ * Kept here, away from the database, because *which* steps are owed is a rule
220
+ * and writing the entries is a transaction. The host loops over this and writes
221
+ * one row each, letting its unique index refuse a second payment: two tabs
222
+ * asking at the same moment both get this list, and only one of them gets to
223
+ * write each row.
224
+ */
225
+ export function rewardsOwed(
226
+ items: readonly SetupItemDef[],
227
+ done: ReadonlySet<string>,
228
+ earned: ReadonlySet<string>,
229
+ ): SetupReward[] {
230
+ return stepsOf(items)
231
+ .filter((step) => done.has(step.key) && !earned.has(step.key) && rewardOf(step) > 0)
232
+ .map((step) => ({ key: step.key, title: step.title, reward: rewardOf(step) }));
233
+ }
234
+
235
+ export interface ItemProgress {
236
+ /** Done, plus anything `ready` counts as good as done. */
237
+ progressed: number;
238
+ total: number;
239
+ percent: number;
240
+ /** The first step neither done nor ready. */
241
+ nextKey: string | null;
242
+ }
243
+
244
+ /**
245
+ * How far through one item is.
246
+ *
247
+ * `ready` is for a flow that saves nothing until its last step: its choices are
248
+ * made but nothing exists yet, so they tick without being done. They count
249
+ * towards the bar — the point of the bar is to show the end approaching — but
250
+ * never towards what has been earned.
251
+ */
252
+ export function itemProgress(item: GuideItem, ready?: ReadonlySet<string>): ItemProgress {
253
+ const isReady = (step: GuideStep) => !step.done && Boolean(ready?.has(step.key));
254
+ const progressed = item.steps.filter((step) => step.done || isReady(step)).length;
255
+ const total = item.steps.length;
256
+ return {
257
+ progressed,
258
+ total,
259
+ percent: total === 0 ? 0 : Math.round((progressed / total) * 100),
260
+ nextKey: item.steps.find((step) => !step.done && !isReady(step))?.key ?? null,
261
+ };
262
+ }
263
+
264
+ /** How a step reads: done, ready to be paid, the one to do next, or still to do. */
265
+ export type StepState = "done" | "ready" | "next" | "todo";
266
+
267
+ export function stepState(step: GuideStep, nextKey: string | null, ready?: ReadonlySet<string>): StepState {
268
+ if (step.done) return "done";
269
+ if (ready?.has(step.key)) return "ready";
270
+ return step.key === nextKey ? "next" : "todo";
271
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * `@connextar/house/setup-guide` — the checklist a new account works through,
3
+ * and what finishing a step earns.
4
+ *
5
+ * It exists because the hard parts are not the checklist. They are: knowing
6
+ * what is done without storing it (so a thing made any other way still counts),
7
+ * paying for it exactly once however many tabs ask at the same moment, and
8
+ * saying so while the person is still looking at what they just did. Those are
9
+ * the same three problems in every app, and they have one right answer each.
10
+ *
11
+ * What the host supplies is everything that is genuinely its own: the steps,
12
+ * how each one is read from its records, where the reward is written, and what
13
+ * a reward is even called. This package holds no routes, makes no requests,
14
+ * imports no toast library and knows nothing about a database.
15
+ *
16
+ * The three surfaces are a card for a home page, a strip for the screen where
17
+ * an item's steps are actually done, and a launcher for the header. The panel
18
+ * the launcher opens is the app's own sheet — see `launcher.tsx`.
19
+ */
20
+ export {
21
+ buildGuide,
22
+ doneSteps,
23
+ findItem,
24
+ itemProgress,
25
+ rewardsOwed,
26
+ stepKeysOf,
27
+ stepState,
28
+ stepsOf,
29
+ type BuildGuideInput,
30
+ type GuideItem,
31
+ type GuideStep,
32
+ type ItemProgress,
33
+ type SetupGuide,
34
+ type SetupGuideLink,
35
+ type SetupHowTo,
36
+ type SetupItemDef,
37
+ type SetupReward,
38
+ type SetupStepDef,
39
+ type StepState,
40
+ } from "./guide.js";
41
+
42
+ export {
43
+ DEFAULT_REWARD_WORDS,
44
+ rewardWord,
45
+ SetupGuideProvider,
46
+ useSetupGuide,
47
+ type RewardWords,
48
+ type SetupGuideProviderProps,
49
+ type SetupGuideState,
50
+ type SetupGuideTransport,
51
+ type SetupLinkComponent,
52
+ type SetupLinkProps,
53
+ type SetupSyncResult,
54
+ } from "./context.js";
55
+
56
+ export { SETUP_BAND_STYLE, SETUP_GUIDE_CLASS_NAMES, type SetupGuideClassNames } from "./class-names.js";
57
+
58
+ export {
59
+ GuideIconLink,
60
+ HowText,
61
+ HowToButton,
62
+ ItemHelp,
63
+ MiniProgress,
64
+ ProgressRing,
65
+ RewardChip,
66
+ RewardRing,
67
+ Segments,
68
+ StepCircle,
69
+ useClassNames,
70
+ type ClassNameOverrides,
71
+ type RewardState,
72
+ } from "./bits.js";
73
+
74
+ export { SetupCard, type SetupActions, type SetupCardProps } from "./card.js";
75
+ export { SetupStrip, type SetupStripProps } from "./strip.js";
76
+ export { SetupGuideList, SetupGuideSummary, SetupLauncherButton } from "./launcher.js";