@feltdb/feltkit 0.3.17 → 0.3.19

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 (188) hide show
  1. package/dist/app.d.ts +246 -13
  2. package/dist/app.js +215 -6
  3. package/dist/app.js.map +1 -1
  4. package/dist/appboundry/build.d.ts +70 -0
  5. package/dist/appboundry/build.js +62 -2
  6. package/dist/appboundry/build.js.map +1 -1
  7. package/dist/appboundry/guest.js +57 -3
  8. package/dist/appboundry/guest.js.map +1 -1
  9. package/dist/appboundry/host.js +7 -0
  10. package/dist/appboundry/host.js.map +1 -1
  11. package/dist/authorize.js +1 -0
  12. package/dist/authorize.js.map +1 -1
  13. package/dist/cli/compose.d.ts +31 -0
  14. package/dist/cli/compose.js +274 -0
  15. package/dist/cli/compose.js.map +1 -0
  16. package/dist/cli/create.d.ts +201 -0
  17. package/dist/cli/create.js +1139 -0
  18. package/dist/cli/create.js.map +1 -0
  19. package/dist/cli/deploy-appboundry-self-hosted.d.ts +2 -0
  20. package/dist/cli/deploy-appboundry-self-hosted.js +1 -1
  21. package/dist/cli/deploy-appboundry-self-hosted.js.map +1 -1
  22. package/dist/cli/deploy-appboundry.js +3 -0
  23. package/dist/cli/deploy-appboundry.js.map +1 -1
  24. package/dist/cli/deploy.d.ts +1 -0
  25. package/dist/cli/deploy.js +66 -6
  26. package/dist/cli/deploy.js.map +1 -1
  27. package/dist/cli/domain.d.ts +174 -0
  28. package/dist/cli/domain.js +1119 -0
  29. package/dist/cli/domain.js.map +1 -0
  30. package/dist/cli/main.d.ts +2 -0
  31. package/dist/cli/main.js +141 -7
  32. package/dist/cli/main.js.map +1 -1
  33. package/dist/cli/shell.d.ts +7 -0
  34. package/dist/cli/shell.js +59 -0
  35. package/dist/cli/shell.js.map +1 -0
  36. package/dist/client/index.d.ts +192 -5
  37. package/dist/client/index.js +301 -28
  38. package/dist/client/index.js.map +1 -1
  39. package/dist/component.d.ts +174 -0
  40. package/dist/component.js +77 -0
  41. package/dist/component.js.map +1 -0
  42. package/dist/composition/catalog.d.ts +3 -0
  43. package/dist/composition/catalog.js +217 -0
  44. package/dist/composition/catalog.js.map +1 -0
  45. package/dist/composition/contract.d.ts +165 -0
  46. package/dist/composition/contract.js +12 -0
  47. package/dist/composition/contract.js.map +1 -0
  48. package/dist/composition/execute.d.ts +110 -0
  49. package/dist/composition/execute.js +350 -0
  50. package/dist/composition/execute.js.map +1 -0
  51. package/dist/composition/generate.d.ts +105 -0
  52. package/dist/composition/generate.js +274 -0
  53. package/dist/composition/generate.js.map +1 -0
  54. package/dist/composition/plan.d.ts +131 -0
  55. package/dist/composition/plan.js +558 -0
  56. package/dist/composition/plan.js.map +1 -0
  57. package/dist/composition/profiles.d.ts +20 -0
  58. package/dist/composition/profiles.js +81 -0
  59. package/dist/composition/profiles.js.map +1 -0
  60. package/dist/composition/reconcile.d.ts +153 -0
  61. package/dist/composition/reconcile.js +282 -0
  62. package/dist/composition/reconcile.js.map +1 -0
  63. package/dist/composition/recover.d.ts +89 -0
  64. package/dist/composition/recover.js +407 -0
  65. package/dist/composition/recover.js.map +1 -0
  66. package/dist/composition/resolve.d.ts +2 -0
  67. package/dist/composition/resolve.js +208 -0
  68. package/dist/composition/resolve.js.map +1 -0
  69. package/dist/dashboard/assets/{index-DnIOVGF-.js → index-BrC1AtZ1.js} +5 -5
  70. package/dist/dashboard/index.html +1 -1
  71. package/dist/docs.d.ts +1 -1
  72. package/dist/docs.js +2 -0
  73. package/dist/docs.js.map +1 -1
  74. package/dist/index.d.ts +7 -3
  75. package/dist/index.js +2 -0
  76. package/dist/index.js.map +1 -1
  77. package/dist/react/auth.d.ts +131 -0
  78. package/dist/react/auth.js +830 -0
  79. package/dist/react/auth.js.map +1 -0
  80. package/dist/react/context.d.ts +8 -0
  81. package/dist/react/context.js +14 -0
  82. package/dist/react/context.js.map +1 -0
  83. package/dist/react/index.d.ts +3 -18
  84. package/dist/react/index.js +5 -13
  85. package/dist/react/index.js.map +1 -1
  86. package/dist/runtime/accounts.d.ts +33 -2
  87. package/dist/runtime/accounts.js +167 -27
  88. package/dist/runtime/accounts.js.map +1 -1
  89. package/dist/runtime/activity.d.ts +8 -1
  90. package/dist/runtime/activity.js.map +1 -1
  91. package/dist/runtime/audit.d.ts +43 -0
  92. package/dist/runtime/audit.js +52 -0
  93. package/dist/runtime/audit.js.map +1 -0
  94. package/dist/runtime/authority.d.ts +8 -1
  95. package/dist/runtime/authority.js +11 -2
  96. package/dist/runtime/authority.js.map +1 -1
  97. package/dist/runtime/challenges.d.ts +51 -0
  98. package/dist/runtime/challenges.js +121 -0
  99. package/dist/runtime/challenges.js.map +1 -0
  100. package/dist/runtime/changes.d.ts +22 -0
  101. package/dist/runtime/changes.js +51 -3
  102. package/dist/runtime/changes.js.map +1 -1
  103. package/dist/runtime/claims.d.ts +76 -0
  104. package/dist/runtime/claims.js +100 -0
  105. package/dist/runtime/claims.js.map +1 -0
  106. package/dist/runtime/context.d.ts +47 -5
  107. package/dist/runtime/contract.js +17 -3
  108. package/dist/runtime/contract.js.map +1 -1
  109. package/dist/runtime/data.d.ts +50 -4
  110. package/dist/runtime/data.js +113 -35
  111. package/dist/runtime/data.js.map +1 -1
  112. package/dist/runtime/escape-hatch.d.ts +37 -0
  113. package/dist/runtime/escape-hatch.js +76 -0
  114. package/dist/runtime/escape-hatch.js.map +1 -0
  115. package/dist/runtime/instance.js +2 -1
  116. package/dist/runtime/instance.js.map +1 -1
  117. package/dist/runtime/live.d.ts +48 -0
  118. package/dist/runtime/live.js +154 -0
  119. package/dist/runtime/live.js.map +1 -0
  120. package/dist/runtime/outbox.js +6 -0
  121. package/dist/runtime/outbox.js.map +1 -1
  122. package/dist/runtime/runtime.d.ts +103 -5
  123. package/dist/runtime/runtime.js +411 -41
  124. package/dist/runtime/runtime.js.map +1 -1
  125. package/dist/runtime/search.d.ts +54 -0
  126. package/dist/runtime/search.js +66 -0
  127. package/dist/runtime/search.js.map +1 -0
  128. package/dist/runtime/services-rules.d.ts +12 -0
  129. package/dist/runtime/services-rules.js +8 -0
  130. package/dist/runtime/services-rules.js.map +1 -0
  131. package/dist/runtime/services.js +5 -3
  132. package/dist/runtime/services.js.map +1 -1
  133. package/dist/runtime/sessions.d.ts +16 -2
  134. package/dist/runtime/sessions.js +40 -4
  135. package/dist/runtime/sessions.js.map +1 -1
  136. package/dist/runtime/tenancy.d.ts +198 -0
  137. package/dist/runtime/tenancy.js +397 -0
  138. package/dist/runtime/tenancy.js.map +1 -0
  139. package/dist/runtime/tenant-scope.d.ts +9 -0
  140. package/dist/runtime/tenant-scope.js +127 -0
  141. package/dist/runtime/tenant-scope.js.map +1 -0
  142. package/dist/runtime/transactions.d.ts +83 -3
  143. package/dist/runtime/transactions.js +53 -11
  144. package/dist/runtime/transactions.js.map +1 -1
  145. package/dist/server/admin.js +52 -0
  146. package/dist/server/admin.js.map +1 -1
  147. package/dist/server/components.d.ts +44 -0
  148. package/dist/server/components.js +212 -0
  149. package/dist/server/components.js.map +1 -0
  150. package/dist/server/index.d.ts +4 -1
  151. package/dist/server/index.js +324 -63
  152. package/dist/server/index.js.map +1 -1
  153. package/dist/server/mcp.js +6 -3
  154. package/dist/server/mcp.js.map +1 -1
  155. package/dist/server/project.d.ts +27 -0
  156. package/dist/server/project.js +63 -0
  157. package/dist/server/project.js.map +1 -1
  158. package/dist/server/public.d.ts +2 -0
  159. package/dist/server/public.js +2 -0
  160. package/dist/server/public.js.map +1 -1
  161. package/dist/server/session-http.d.ts +48 -0
  162. package/dist/server/session-http.js +116 -0
  163. package/dist/server/session-http.js.map +1 -0
  164. package/dist/server/tenancy.d.ts +40 -0
  165. package/dist/server/tenancy.js +191 -0
  166. package/dist/server/tenancy.js.map +1 -0
  167. package/dist/testing/index.d.ts +19 -0
  168. package/dist/testing/index.js +22 -2
  169. package/dist/testing/index.js.map +1 -1
  170. package/docs/AGENT-OPERATIONS.md +1 -0
  171. package/docs/APPBOUNDRY.md +11 -1
  172. package/docs/ARCHITECTURE.md +5 -3
  173. package/docs/AUTH.md +267 -0
  174. package/docs/COMPONENTS.md +91 -0
  175. package/docs/GUARANTEES.md +74 -7
  176. package/docs/README.md +4 -2
  177. package/docs/RESOLVER.md +4 -2
  178. package/docs/SECURITY.md +5 -4
  179. package/docs/TRANSACTIONS.md +98 -1
  180. package/docs/UPSTREAM-GAPS.md +2 -0
  181. package/package.json +15 -12
  182. package/skills/.claude-plugin/marketplace.json +1 -1
  183. package/skills/.claude-plugin/plugin.json +1 -1
  184. package/skills/feltkit-data/SKILL.md +6 -0
  185. package/skills/feltkit-services/SKILL.md +1 -1
  186. package/dist/runtime/account-links.d.ts +0 -19
  187. package/dist/runtime/account-links.js +0 -45
  188. package/dist/runtime/account-links.js.map +0 -1
@@ -0,0 +1,830 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ /**
3
+ * Authentication, account and workspace screens for FeltKit apps (`@feltdb/feltkit/react`).
4
+ *
5
+ * const auth = useAuth(); // { loading, user, account, tenant, settings, signOut, refresh }
6
+ * if (!auth.user) return <SignIn />; // password, magic link, providers, sign-up, reset, verify
7
+ * <TenantSwitcher /> <TenantSettings /> <AccountSettings /> <AcceptInvitation /> <AuthNotices />
8
+ *
9
+ * Every component talks to the FeltKit server through the client (`useFeltKit()`): sessions are the
10
+ * server's HttpOnly cookie in the browser, and every rule (who may invite, which roles, which tenant)
11
+ * is enforced on the server; the screens only show what the server says is possible.
12
+ *
13
+ * Styling: one small stylesheet (classes `fk-*`) is added to the page once. Theme it with CSS custom
14
+ * properties on any ancestor (they default to the app's own `--accent`, `--surface`, `--ink`, `--muted`,
15
+ * `--line`, `--radius` when it has them, else to the system's colors, light and dark):
16
+ *
17
+ * .my-app { --fk-accent: #7c3aed; --fk-radius: 14px; --fk-font: "Inter", sans-serif; }
18
+ *
19
+ * Every component takes `className`, and `<FeltKitAuthStyles disabled />` (or `unstyled` on
20
+ * `<SignIn>`) leaves all styling to you: the markup is plain, labelled form elements.
21
+ */
22
+ import { useCallback, useEffect, useId, useInsertionEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
23
+ import { useFeltKit } from "./context.js";
24
+ // ── Styles ──────────────────────────────────────────────────────────────────
25
+ const CSS = `
26
+ .fk-auth{--_accent:var(--fk-accent,var(--accent,#2563eb));--_accent-ink:var(--fk-accent-ink,var(--accent-ink,#fff));--_surface:var(--fk-surface,var(--surface,Canvas));--_ink:var(--fk-ink,var(--ink,CanvasText));--_muted:var(--fk-muted,var(--muted,color-mix(in srgb,CanvasText 60%,Canvas)));--_line:var(--fk-line,var(--line,color-mix(in srgb,CanvasText 16%,Canvas)));--_danger:var(--fk-danger,var(--danger,#c0262d));--_soft:color-mix(in srgb,var(--_accent) 12%,var(--_surface));--_radius:var(--fk-radius,var(--radius,10px));font-family:var(--fk-font,inherit);color:var(--_ink);font-size:15px;line-height:1.5;box-sizing:border-box}
27
+ .fk-auth *,.fk-auth *::before,.fk-auth *::after{box-sizing:inherit}
28
+ .fk-card{background:var(--_surface);border:1px solid var(--_line);border-radius:var(--_radius);padding:24px;width:100%;max-width:420px;margin:0 auto}
29
+ .fk-screen{min-height:100%;display:flex;align-items:flex-start;justify-content:center;padding:clamp(16px,6vh,64px) 16px}
30
+ .fk-brand{display:flex;align-items:center;gap:10px;margin-bottom:16px;font-weight:600}
31
+ .fk-brand img{width:32px;height:32px;border-radius:8px}
32
+ .fk-title{font-size:20px;font-weight:650;margin:0 0 4px}
33
+ .fk-sub{color:var(--_muted);margin:0 0 18px;font-size:14px}
34
+ .fk-form{display:flex;flex-direction:column;gap:12px}
35
+ .fk-field{display:flex;flex-direction:column;gap:5px}
36
+ .fk-field label{font-size:13px;font-weight:550}
37
+ .fk-hint{font-size:12.5px;color:var(--_muted)}
38
+ .fk-input,.fk-select{width:100%;padding:10px 12px;border:1px solid var(--_line);border-radius:calc(var(--_radius) - 2px);background:var(--_surface);color:var(--_ink);font:inherit;min-height:42px}
39
+ .fk-input:focus-visible,.fk-select:focus-visible,.fk-btn:focus-visible,.fk-link:focus-visible,.fk-menu button:focus-visible{outline:2px solid var(--_accent);outline-offset:2px}
40
+ .fk-input[aria-invalid=true]{border-color:var(--_danger)}
41
+ .fk-btn{display:inline-flex;align-items:center;justify-content:center;gap:8px;min-height:42px;padding:9px 16px;border-radius:calc(var(--_radius) - 2px);border:1px solid transparent;background:var(--_accent);color:var(--_accent-ink);font:inherit;font-weight:600;cursor:pointer;text-decoration:none}
42
+ .fk-btn[disabled]{opacity:.6;cursor:default}
43
+ .fk-btn.fk-secondary{background:transparent;color:var(--_ink);border-color:var(--_line)}
44
+ .fk-btn.fk-danger{background:transparent;color:var(--_danger);border-color:color-mix(in srgb,var(--_danger) 40%,var(--_line))}
45
+ .fk-btn.fk-small{min-height:32px;padding:5px 10px;font-size:13px}
46
+ .fk-btn.fk-block{width:100%}
47
+ .fk-link{background:none;border:0;padding:0;color:var(--_accent);font:inherit;cursor:pointer;text-decoration:underline;text-underline-offset:2px}
48
+ .fk-row{display:flex;gap:8px;align-items:center;flex-wrap:wrap}
49
+ .fk-between{display:flex;justify-content:space-between;align-items:center;gap:12px;flex-wrap:wrap}
50
+ .fk-divider{display:flex;align-items:center;gap:10px;color:var(--_muted);font-size:12.5px;margin:6px 0}
51
+ .fk-divider::before,.fk-divider::after{content:"";flex:1;height:1px;background:var(--_line)}
52
+ .fk-alert{padding:10px 12px;border-radius:calc(var(--_radius) - 2px);font-size:14px;border:1px solid}
53
+ .fk-alert.fk-error{color:var(--_danger);background:color-mix(in srgb,var(--_danger) 8%,var(--_surface));border-color:color-mix(in srgb,var(--_danger) 30%,var(--_line))}
54
+ .fk-alert.fk-ok{color:var(--_ink);background:var(--_soft);border-color:color-mix(in srgb,var(--_accent) 30%,var(--_line))}
55
+ .fk-tabs{display:flex;gap:4px;padding:3px;border:1px solid var(--_line);border-radius:calc(var(--_radius) - 2px);margin-bottom:14px}
56
+ .fk-tabs button{flex:1;border:0;background:transparent;padding:7px 8px;border-radius:calc(var(--_radius) - 4px);font:inherit;font-size:14px;color:var(--_muted);cursor:pointer}
57
+ .fk-tabs button[aria-selected=true]{background:var(--_soft);color:var(--_ink);font-weight:600}
58
+ .fk-section{border:1px solid var(--_line);border-radius:var(--_radius);background:var(--_surface);padding:18px;margin-bottom:16px}
59
+ .fk-section h3{margin:0 0 4px;font-size:15.5px}
60
+ .fk-list{list-style:none;margin:0;padding:0}
61
+ .fk-list li{display:flex;align-items:center;justify-content:space-between;gap:12px;padding:10px 0;border-top:1px solid var(--_line);flex-wrap:wrap}
62
+ .fk-list li:first-child{border-top:0}
63
+ .fk-who{display:flex;flex-direction:column;min-width:0}
64
+ .fk-who strong{font-weight:600;overflow:hidden;text-overflow:ellipsis}
65
+ .fk-who span{color:var(--_muted);font-size:13px;overflow:hidden;text-overflow:ellipsis}
66
+ .fk-badge{font-size:12px;padding:2px 8px;border-radius:999px;background:var(--_soft);color:var(--_ink);white-space:nowrap}
67
+ .fk-empty{color:var(--_muted);font-size:14px;padding:8px 0}
68
+ .fk-code{letter-spacing:.3em;font-variant-numeric:tabular-nums;text-align:center;font-size:20px}
69
+ .fk-switcher{position:relative;display:inline-block}
70
+ .fk-switcher>.fk-btn{max-width:260px}
71
+ .fk-switcher>.fk-btn span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
72
+ .fk-menu{position:absolute;z-index:50;top:calc(100% + 6px);left:0;min-width:240px;max-width:min(320px,calc(100vw - 32px));background:var(--_surface);border:1px solid var(--_line);border-radius:var(--_radius);padding:6px;box-shadow:0 8px 28px rgba(0,0,0,.16)}
73
+ .fk-menu button{display:flex;justify-content:space-between;gap:10px;width:100%;text-align:left;border:0;background:transparent;padding:9px 10px;border-radius:calc(var(--_radius) - 4px);font:inherit;color:var(--_ink);cursor:pointer}
74
+ .fk-menu button:hover,.fk-menu button[aria-current=true]{background:var(--_soft)}
75
+ .fk-menu hr{border:0;border-top:1px solid var(--_line);margin:6px 0}
76
+ .fk-spinner{width:16px;height:16px;border-radius:50%;border:2px solid currentColor;border-right-color:transparent;animation:fk-spin .8s linear infinite;display:inline-block}
77
+ @keyframes fk-spin{to{transform:rotate(360deg)}}
78
+ @media (prefers-reduced-motion:reduce){.fk-spinner{animation:none}}
79
+ .fk-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
80
+ @media (max-width:480px){.fk-card{padding:18px;border-radius:0;border-left:0;border-right:0}.fk-screen{padding:0}.fk-list li{align-items:flex-start}}
81
+ `;
82
+ let styleDisabled = false;
83
+ /** Turn the built-in stylesheet off (render it once near the root): every class stays, unstyled. */
84
+ export function FeltKitAuthStyles({ disabled }) {
85
+ styleDisabled = Boolean(disabled);
86
+ if (disabled && typeof document !== "undefined")
87
+ document.querySelector("style[data-feltkit-auth]")?.remove();
88
+ return null;
89
+ }
90
+ function useStyles() {
91
+ useInsertionEffect(() => {
92
+ if (styleDisabled || typeof document === "undefined" || document.querySelector("style[data-feltkit-auth]"))
93
+ return;
94
+ const style = document.createElement("style");
95
+ style.setAttribute("data-feltkit-auth", "");
96
+ style.textContent = CSS;
97
+ document.head.prepend(style); // first, so the app's own styles override
98
+ }, []);
99
+ }
100
+ const cx = (...names) => names.filter(Boolean).join(" ");
101
+ function message(error) {
102
+ if (error && typeof error === "object" && "message" in error)
103
+ return String(error.message);
104
+ return String(error);
105
+ }
106
+ function Spinner({ label = "Loading" }) {
107
+ return (_jsxs("span", { role: "status", "aria-live": "polite", children: [_jsx("span", { className: "fk-spinner", "aria-hidden": true }), _jsx("span", { className: "fk-sr", children: label })] }));
108
+ }
109
+ function Alert({ kind, children }) {
110
+ return (_jsx("div", { className: cx("fk-alert", kind === "error" ? "fk-error" : "fk-ok"), role: kind === "error" ? "alert" : "status", children: children }));
111
+ }
112
+ function Field(props) {
113
+ const id = useId();
114
+ const hint = props.hint ? `${id}-hint` : undefined;
115
+ return (_jsxs("div", { className: "fk-field", children: [_jsx("label", { htmlFor: id, children: props.label }), props.children(id, hint), props.hint && (_jsx("span", { id: hint, className: "fk-hint", children: props.hint }))] }));
116
+ }
117
+ /** Run an async action with busy / error / done state. */
118
+ function useAction() {
119
+ const [busy, setBusy] = useState(false);
120
+ const [error, setError] = useState(null);
121
+ const [done, setDone] = useState(null);
122
+ const live = useRef(true);
123
+ // Set on every mount: React's StrictMode mounts, unmounts and mounts again in development.
124
+ useEffect(() => {
125
+ live.current = true;
126
+ return () => void (live.current = false);
127
+ }, []);
128
+ const run = useCallback(async (work) => {
129
+ setBusy(true);
130
+ setError(null);
131
+ setDone(null);
132
+ try {
133
+ const ok = await work();
134
+ if (live.current && ok)
135
+ setDone(ok);
136
+ }
137
+ catch (e) {
138
+ if (live.current)
139
+ setError(message(e));
140
+ }
141
+ finally {
142
+ if (live.current)
143
+ setBusy(false);
144
+ }
145
+ }, []);
146
+ return { busy, error, done, run, setError, setDone };
147
+ }
148
+ // ── Session and redirects ─────────────────────────────────────────────────────
149
+ const INVITE_KEY = "feltkit.invitation";
150
+ /** The page's redirect, shared by every component of a client: read once, cleared for all. */
151
+ class RedirectStore {
152
+ client;
153
+ state = { redirect: null, pending: true };
154
+ #listeners = new Set();
155
+ #started = false;
156
+ constructor(client) {
157
+ this.client = client;
158
+ }
159
+ subscribe = (listener) => {
160
+ this.#listeners.add(listener);
161
+ if (!this.#started) {
162
+ this.#started = true;
163
+ void this.client.handleRedirect().catch(() => null).then((redirect) => {
164
+ let value = redirect;
165
+ try {
166
+ if (redirect?.kind === "invitation")
167
+ sessionStorage.setItem(INVITE_KEY, redirect.token);
168
+ else if (!redirect) {
169
+ const saved = sessionStorage.getItem(INVITE_KEY);
170
+ if (saved)
171
+ value = { kind: "invitation", token: saved };
172
+ }
173
+ }
174
+ catch {
175
+ /* storage unavailable: the invitation lasts until the page reloads */
176
+ }
177
+ this.#set({ redirect: value, pending: false });
178
+ });
179
+ }
180
+ return () => void this.#listeners.delete(listener);
181
+ };
182
+ get = () => this.state;
183
+ clear = () => {
184
+ try {
185
+ sessionStorage.removeItem(INVITE_KEY);
186
+ }
187
+ catch {
188
+ /* nothing kept */
189
+ }
190
+ this.#set({ redirect: null, pending: false });
191
+ };
192
+ #set(state) {
193
+ this.state = state;
194
+ for (const l of [...this.#listeners])
195
+ l();
196
+ }
197
+ }
198
+ const redirectStores = new WeakMap();
199
+ /**
200
+ * What a link into the app carried (sign-in link, verification, reset, invitation, provider sign-in),
201
+ * read once per page load and shared by every component. An invitation is kept for this tab until it
202
+ * is accepted or dismissed, so signing in first does not lose it.
203
+ */
204
+ export function useAuthRedirect() {
205
+ const client = useFeltKit();
206
+ let store = redirectStores.get(client);
207
+ if (!store)
208
+ redirectStores.set(client, (store = new RedirectStore(client)));
209
+ const state = useSyncExternalStore(store.subscribe, store.get, store.get);
210
+ return { ...state, clear: store.clear };
211
+ }
212
+ /**
213
+ * One shared copy of a server resource per client (the session, the tenant list): fetched once for
214
+ * every component that shows it, again when the session changes, and on refresh().
215
+ */
216
+ class Shared {
217
+ load;
218
+ fallback;
219
+ value = undefined;
220
+ #listeners = new Set();
221
+ #loading = null;
222
+ #generation = 0;
223
+ constructor(load, fallback) {
224
+ this.load = load;
225
+ this.fallback = fallback;
226
+ }
227
+ subscribe = (listener) => {
228
+ this.#listeners.add(listener);
229
+ if (this.value === undefined && !this.#loading)
230
+ void this.refresh();
231
+ return () => void this.#listeners.delete(listener);
232
+ };
233
+ get = () => this.value;
234
+ refresh = () => {
235
+ const generation = ++this.#generation;
236
+ this.#loading = this.load()
237
+ .then((v) => v, () => this.fallback(this.value))
238
+ .then((v) => {
239
+ if (generation !== this.#generation)
240
+ return;
241
+ this.value = v;
242
+ this.#loading = null;
243
+ for (const l of [...this.#listeners])
244
+ l();
245
+ });
246
+ return this.#loading;
247
+ };
248
+ }
249
+ const sessions = new WeakMap();
250
+ function sharedSession(client) {
251
+ let shared = sessions.get(client);
252
+ if (!shared) {
253
+ const created = new Shared(() => client.session(), (previous) => previous ?? { principal: null, permissions: [] });
254
+ client.onSessionChange(() => void created.refresh());
255
+ // A membership changed (removed, role changed): the session's tenant and role may have too.
256
+ client.onChange((collections) => {
257
+ if (collections.includes("_feltkit_memberships"))
258
+ void created.refresh();
259
+ });
260
+ sessions.set(client, (shared = created));
261
+ }
262
+ return shared;
263
+ }
264
+ /** The current session: who is signed in, their account and tenant, and what the app offers. */
265
+ export function useAuth() {
266
+ const client = useFeltKit();
267
+ const shared = sharedSession(client);
268
+ const session = useSyncExternalStore(shared.subscribe, shared.get, () => undefined);
269
+ return useMemo(() => {
270
+ const permissions = session?.permissions ?? [];
271
+ return {
272
+ loading: session === undefined,
273
+ user: session?.principal ?? null,
274
+ account: session?.account ?? null,
275
+ tenant: session?.tenant ?? null,
276
+ permissions,
277
+ settings: session?.accounts ?? null,
278
+ tenancy: session?.tenancy ?? null,
279
+ session,
280
+ can: (permission) => permissions.some((p) => p === "*" || p === permission || (p.endsWith(".*") && permission.startsWith(p.slice(0, -1)))),
281
+ signOut: () => client.signOut(),
282
+ refresh: () => void shared.refresh(),
283
+ };
284
+ }, [client, session, shared]);
285
+ }
286
+ const tenantStores = new WeakMap();
287
+ function sharedTenants(client) {
288
+ let shared = tenantStores.get(client);
289
+ if (!shared) {
290
+ const created = new Shared(async () => {
291
+ try {
292
+ return { info: await client.tenants(), error: null };
293
+ }
294
+ catch (e) {
295
+ // Signed out, or an app without tenancy: no tenants (not an error to show).
296
+ const code = e.code;
297
+ return { info: null, error: code === "UNAUTHORIZED" || code === "NOT_FOUND" ? null : message(e) };
298
+ }
299
+ }, () => ({ info: null, error: null }));
300
+ client.onSessionChange(() => void created.refresh());
301
+ client.onChange((collections) => {
302
+ if (collections.includes("_feltkit_memberships"))
303
+ void created.refresh();
304
+ });
305
+ tenantStores.set(client, (shared = created));
306
+ }
307
+ return shared;
308
+ }
309
+ /** Your tenants and the current one (`auth.tenant`). */
310
+ export function useTenant() {
311
+ const client = useFeltKit();
312
+ const auth = useAuth();
313
+ const shared = sharedTenants(client);
314
+ const state = useSyncExternalStore(shared.subscribe, shared.get, () => undefined);
315
+ const active = Boolean(auth.user) && Boolean(auth.tenancy);
316
+ return useMemo(() => ({
317
+ loading: active && state === undefined,
318
+ error: state?.error ?? null,
319
+ current: (active ? state?.info?.current : null) ?? auth.tenant,
320
+ tenants: active ? state?.info?.tenants ?? [] : [],
321
+ settings: active ? state?.info?.settings ?? null : null,
322
+ switchTo: async (tenantId) => {
323
+ await client.switchTenant(tenantId);
324
+ },
325
+ create: async (name) => {
326
+ await client.createTenant(name);
327
+ },
328
+ refresh: () => void shared.refresh(),
329
+ }), [client, state, active, auth.tenant, shared]);
330
+ }
331
+ /** A safe return path: a path on this site, never another origin. */
332
+ export function safeReturnPath(value, fallback = "/") {
333
+ if (!value || typeof value !== "string" || !value.startsWith("/") || value.startsWith("//") || value.includes("\\"))
334
+ return fallback;
335
+ return value;
336
+ }
337
+ /**
338
+ * Sign in or create an account: password, e-mailed link (and code), Google / GitHub / OpenID Connect,
339
+ * forgot password, and the screens links back into the app need (reset, verification result,
340
+ * expired or used links). Offers only what the app enables.
341
+ */
342
+ export function SignIn(props) {
343
+ useStyles();
344
+ const client = useFeltKit();
345
+ const auth = useAuth();
346
+ const { redirect, clear } = useAuthRedirect();
347
+ const settings = auth.settings;
348
+ const [mode, setMode] = useState(props.initialMode ?? "sign-in");
349
+ const [email, setEmail] = useState(props.email ?? "");
350
+ const [password, setPassword] = useState("");
351
+ const [name, setName] = useState("");
352
+ const [code, setCode] = useState("");
353
+ const action = useAction();
354
+ const [notice, setNotice] = useState(null);
355
+ const [resetToken, setResetToken] = useState(null);
356
+ // Links back into the app: reset (choose a password), verification result, failed links.
357
+ useEffect(() => {
358
+ if (!redirect)
359
+ return;
360
+ if (redirect.kind === "reset") {
361
+ setResetToken(redirect.token);
362
+ setMode("reset");
363
+ }
364
+ else if (redirect.kind === "verified")
365
+ setNotice({ kind: "ok", text: `${redirect.email} is verified. Sign in to continue.` });
366
+ else if (redirect.kind === "verify-failed" || redirect.kind === "magic-link-failed" || redirect.kind === "error" || redirect.kind === "email-change-failed")
367
+ setNotice({ kind: "error", text: redirect.message });
368
+ else if (redirect.kind === "email-changed")
369
+ setNotice({ kind: "ok", text: `Your address is now ${redirect.email}.` });
370
+ else if (redirect.kind === "signed-in")
371
+ props.onSignedIn?.();
372
+ if (redirect.kind !== "invitation")
373
+ clear();
374
+ // eslint-disable-next-line react-hooks/exhaustive-deps
375
+ }, [redirect]);
376
+ const done = () => {
377
+ setPassword("");
378
+ props.onSignedIn?.();
379
+ };
380
+ const password_ = settings?.password !== false;
381
+ const magic = Boolean(settings?.magicLink);
382
+ const providers = settings?.providers ?? [];
383
+ const canSignUp = Boolean(settings?.signUp) && password_;
384
+ if (auth.loading) {
385
+ return (_jsx(Frame, { props: props, children: _jsx(Spinner, {}) }));
386
+ }
387
+ if (!settings) {
388
+ return (_jsx(Frame, { props: props, children: _jsx(Alert, { kind: "error", children: "This app has no sign-in configured (auth.accounts)." }) }));
389
+ }
390
+ const submit = (work) => (e) => {
391
+ e.preventDefault();
392
+ setNotice(null);
393
+ void action.run(work);
394
+ };
395
+ let body;
396
+ if (mode === "reset" && resetToken) {
397
+ body = (_jsxs("form", { className: "fk-form", onSubmit: submit(async () => {
398
+ await client.resetPassword(resetToken, password);
399
+ setResetToken(null);
400
+ done();
401
+ }), children: [_jsx("h2", { className: "fk-title", children: "Choose a new password" }), _jsx("p", { className: "fk-sub", children: "Every other session of your account will be signed out." }), _jsx(Field, { label: "New password", hint: "At least 12 characters.", children: (id, hint) => _jsx("input", { id: id, "aria-describedby": hint, className: "fk-input", type: "password", autoComplete: "new-password", required: true, minLength: 12, value: password, onChange: (e) => setPassword(e.target.value), autoFocus: true }) }), _jsx("button", { className: "fk-btn fk-block", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Saving" }) : "Set password and sign in" }), _jsx("button", { type: "button", className: "fk-link", onClick: () => setMode("forgot"), children: "The link expired? Send a new one" })] }));
402
+ }
403
+ else if (mode === "magic-sent") {
404
+ body = (_jsxs("form", { className: "fk-form", onSubmit: submit(async () => {
405
+ await client.signInWithCode(email, code);
406
+ done();
407
+ }), children: [_jsx("h2", { className: "fk-title", children: "Check your e-mail" }), _jsxs("p", { className: "fk-sub", children: ["If ", email, " can sign in here, we sent it a sign-in link and an 8-digit code. They work once, for 15 minutes."] }), _jsx(Field, { label: "Code", hint: "Or open the link on this device.", children: (id, hint) => _jsx("input", { id: id, "aria-describedby": hint, className: "fk-input fk-code", inputMode: "numeric", autoComplete: "one-time-code", pattern: "[0-9 ]{8,9}", maxLength: 9, required: true, value: code, onChange: (e) => setCode(e.target.value), autoFocus: true }) }), _jsx("button", { className: "fk-btn fk-block", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Signing in" }) : "Sign in" }), _jsx("button", { type: "button", className: "fk-link", onClick: () => setMode("magic"), children: "Use a different address" })] }));
408
+ }
409
+ else if (mode === "forgot") {
410
+ body = (_jsxs("form", { className: "fk-form", onSubmit: submit(async () => (await client.requestPasswordReset(email)).message), children: [_jsx("h2", { className: "fk-title", children: "Reset your password" }), _jsx("p", { className: "fk-sub", children: "We will e-mail you a link to choose a new one." }), _jsx(EmailField, { email: email, setEmail: setEmail }), _jsx("button", { className: "fk-btn fk-block", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Sending" }) : "Send reset link" }), _jsx("button", { type: "button", className: "fk-link", onClick: () => setMode("sign-in"), children: "Back to sign in" })] }));
411
+ }
412
+ else if (mode === "magic") {
413
+ body = (_jsxs("form", { className: "fk-form", onSubmit: submit(async () => {
414
+ await client.requestMagicLink(email);
415
+ setMode("magic-sent");
416
+ }), children: [_jsx("h2", { className: "fk-title", children: props.title ?? "Sign in with e-mail" }), _jsx("p", { className: "fk-sub", children: "We will send you a link and a code. No password needed." }), _jsx(EmailField, { email: email, setEmail: setEmail }), _jsx("button", { className: "fk-btn fk-block", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Sending" }) : "E-mail me a sign-in link" }), password_ && _jsx("button", { type: "button", className: "fk-link", onClick: () => setMode("sign-in"), children: "Use a password instead" })] }));
417
+ }
418
+ else {
419
+ const signingUp = mode === "sign-up";
420
+ body = (_jsxs(_Fragment, { children: [_jsx("h2", { className: "fk-title", children: props.title ?? (signingUp ? "Create your account" : "Sign in") }), _jsx("p", { className: "fk-sub", children: signingUp ? "It takes a minute." : "Welcome back." }), canSignUp && (_jsxs("div", { className: "fk-tabs", role: "tablist", "aria-label": "Sign in or create an account", children: [_jsx("button", { type: "button", role: "tab", "aria-selected": !signingUp, onClick: () => setMode("sign-in"), children: "Sign in" }), _jsx("button", { type: "button", role: "tab", "aria-selected": signingUp, onClick: () => setMode("sign-up"), children: "Create account" })] })), providers.length > 0 && (_jsxs("div", { className: "fk-form", children: [providers.map((p) => (_jsxs("button", { type: "button", className: "fk-btn fk-secondary fk-block", onClick: () => client.signInWith(p.id, safeReturnPath(props.returnTo, globalThis.location?.pathname ?? "/")), children: ["Continue with ", p.name] }, p.id))), (password_ || magic) && _jsx("div", { className: "fk-divider", children: "or" })] })), password_ ? (_jsxs("form", { className: "fk-form", onSubmit: submit(async () => {
421
+ if (signingUp) {
422
+ const principal = await client.signUp({ email, password, ...(name.trim() ? { name: name.trim() } : {}) });
423
+ if (!principal) {
424
+ setMode("sign-in");
425
+ return "Check your e-mail: open the link we sent to verify your address, then sign in.";
426
+ }
427
+ }
428
+ else
429
+ await client.signIn({ email, password });
430
+ done();
431
+ }), children: [signingUp && (_jsx(Field, { label: "Name", children: (id) => _jsx("input", { id: id, className: "fk-input", autoComplete: "name", value: name, onChange: (e) => setName(e.target.value) }) })), _jsx(EmailField, { email: email, setEmail: setEmail }), _jsx(Field, { label: "Password", hint: signingUp ? "At least 12 characters." : undefined, children: (id, hint) => _jsx("input", { id: id, "aria-describedby": hint, className: "fk-input", type: "password", autoComplete: signingUp ? "new-password" : "current-password", required: true, minLength: signingUp ? 12 : 1, value: password, onChange: (e) => setPassword(e.target.value) }) }), _jsx("button", { className: "fk-btn fk-block", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Working" }) : signingUp ? "Create account" : "Sign in" }), _jsxs("div", { className: "fk-between", children: [!signingUp && settings.email && _jsx("button", { type: "button", className: "fk-link", onClick: () => setMode("forgot"), children: "Forgot password?" }), magic && _jsx("button", { type: "button", className: "fk-link", onClick: () => setMode("magic"), children: "E-mail me a sign-in link" })] })] })) : magic ? (_jsxs("form", { className: "fk-form", onSubmit: submit(async () => {
432
+ await client.requestMagicLink(email);
433
+ setMode("magic-sent");
434
+ }), children: [_jsx(EmailField, { email: email, setEmail: setEmail }), _jsx("button", { className: "fk-btn fk-block", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Sending" }) : "E-mail me a sign-in link" })] })) : null, auth.session?.devUsers?.some((u) => u.type === "user") ? _jsx(DevUsers, { users: auth.session.devUsers.filter((u) => u.type === "user"), onDone: done }) : null] }));
435
+ }
436
+ return (_jsxs(Frame, { props: props, children: [notice && _jsx(Alert, { kind: notice.kind, children: notice.text }), action.error && _jsx(Alert, { kind: "error", children: action.error }), action.done && _jsx(Alert, { kind: "ok", children: action.done }), (notice || action.error || action.done) && _jsx("div", { style: { height: 12 } }), body] }));
437
+ }
438
+ function Frame({ props, children }) {
439
+ useStyles();
440
+ const inner = (_jsxs(_Fragment, { children: [props.brand && (props.brand.name || props.brand.logo) && (_jsxs("div", { className: "fk-brand", children: [props.brand.logo && _jsx("img", { src: props.brand.logo, alt: "" }), props.brand.name && _jsx("span", { children: props.brand.name })] })), children] }));
441
+ if (props.bare)
442
+ return _jsx("div", { className: cx("fk-auth", props.className), children: inner });
443
+ return (_jsx("div", { className: cx("fk-auth", "fk-screen", props.className), children: _jsx("div", { className: "fk-card", children: inner }) }));
444
+ }
445
+ function EmailField({ email, setEmail }) {
446
+ return (_jsx(Field, { label: "E-mail", children: (id) => _jsx("input", { id: id, className: "fk-input", type: "email", autoComplete: "email", inputMode: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value) }) }));
447
+ }
448
+ function DevUsers({ users, onDone }) {
449
+ const client = useFeltKit();
450
+ return (_jsxs("div", { className: "fk-form", style: { marginTop: 14 }, children: [_jsx("div", { className: "fk-divider", children: "development users" }), _jsx("div", { className: "fk-row", children: users.map((u) => (_jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", onClick: () => void client.signInAsDevUser(u.id).then(onDone), children: u.name }, u.id))) })] }));
451
+ }
452
+ // ── Notices for links opened while signed in ───────────────────────────────────
453
+ /** Shows what a link opened while signed in did (address verified or changed, a link that failed). */
454
+ export function AuthNotices({ className }) {
455
+ useStyles();
456
+ const { redirect, clear } = useAuthRedirect();
457
+ const auth = useAuth();
458
+ const [shown, setShown] = useState(null);
459
+ useEffect(() => {
460
+ if (!redirect || !auth.user)
461
+ return;
462
+ if (redirect.kind === "verified")
463
+ setShown({ kind: "ok", text: `${redirect.email} is verified.` });
464
+ else if (redirect.kind === "email-changed")
465
+ setShown({ kind: "ok", text: `Your address is now ${redirect.email}.` });
466
+ else if ("message" in redirect)
467
+ setShown({ kind: "error", text: redirect.message });
468
+ else
469
+ return;
470
+ clear();
471
+ auth.refresh();
472
+ // eslint-disable-next-line react-hooks/exhaustive-deps
473
+ }, [redirect, auth.user]);
474
+ if (!shown)
475
+ return null;
476
+ return (_jsx("div", { className: cx("fk-auth", className), children: _jsxs("div", { className: "fk-between fk-alert fk-ok", role: shown.kind === "error" ? "alert" : "status", children: [_jsx("span", { children: shown.text }), _jsx("button", { type: "button", className: "fk-link", onClick: () => setShown(null), children: "Dismiss" })] }) }));
477
+ }
478
+ /** A reminder to verify the account's address, with a button to send the link again. */
479
+ export function VerifyEmailBanner({ className }) {
480
+ useStyles();
481
+ const client = useFeltKit();
482
+ const auth = useAuth();
483
+ const action = useAction();
484
+ if (!auth.account || auth.account.emailVerified || !auth.settings?.email)
485
+ return null;
486
+ return (_jsxs("div", { className: cx("fk-auth", className), children: [_jsxs("div", { className: "fk-alert fk-ok fk-between", role: "status", children: [_jsx("span", { children: action.done ?? `Verify ${auth.account.email} to secure your account.` }), !action.done && (_jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", disabled: action.busy, onClick: () => void action.run(async () => (await client.requestVerification(auth.account.email)).message), children: action.busy ? _jsx(Spinner, { label: "Sending" }) : "Send link" }))] }), action.error && _jsx(Alert, { kind: "error", children: action.error })] }));
487
+ }
488
+ // ── Invitations ───────────────────────────────────────────────────────────────
489
+ /**
490
+ * Accept an invitation: the one in the link that opened the app (kept for this tab until used), or
491
+ * `token`. Shows what it is for; asks the person to sign in (as the invited address) first when
492
+ * needed; handles expired, withdrawn, used and wrong-account invitations. Renders nothing when there
493
+ * is no invitation.
494
+ */
495
+ export function AcceptInvitation(props) {
496
+ useStyles();
497
+ const client = useFeltKit();
498
+ const auth = useAuth();
499
+ const { redirect, clear } = useAuthRedirect();
500
+ const token = props.token ?? (redirect?.kind === "invitation" ? redirect.token : null);
501
+ const [preview, setPreview] = useState(null);
502
+ const [failed, setFailed] = useState(null);
503
+ const action = useAction();
504
+ useEffect(() => {
505
+ if (!token)
506
+ return;
507
+ let live = true;
508
+ client.previewInvitation(token).then((p) => live && setPreview(p), (e) => live && setFailed(message(e)));
509
+ return () => void (live = false);
510
+ }, [client, token]);
511
+ if (!token)
512
+ return null;
513
+ const dismiss = () => {
514
+ clear();
515
+ props.onDismiss?.();
516
+ };
517
+ const word = auth.tenancy?.name ?? "workspace";
518
+ let body;
519
+ if (failed) {
520
+ body = (_jsxs(_Fragment, { children: [_jsx("h2", { className: "fk-title", children: "Invitation not found" }), _jsx(Alert, { kind: "error", children: failed }), _jsx("div", { style: { height: 12 } }), _jsx("button", { type: "button", className: "fk-btn fk-secondary", onClick: dismiss, children: "Continue" })] }));
521
+ }
522
+ else if (!preview || auth.loading) {
523
+ body = _jsx(Spinner, { label: "Loading the invitation" });
524
+ }
525
+ else if (preview.status !== "open") {
526
+ const why = { expired: "This invitation has expired. Ask the person who invited you for a new one.", revoked: "This invitation was withdrawn.", accepted: "This invitation was already used." }[preview.status];
527
+ body = (_jsxs(_Fragment, { children: [_jsxs("h2", { className: "fk-title", children: ["Join ", preview.tenantName] }), _jsx(Alert, { kind: "error", children: why }), _jsx("div", { style: { height: 12 } }), _jsx("button", { type: "button", className: "fk-btn fk-secondary", onClick: dismiss, children: "Continue" })] }));
528
+ }
529
+ else if (!auth.user) {
530
+ return (_jsx("div", { className: cx("fk-auth", "fk-screen", props.className), children: _jsxs("div", { className: "fk-card", children: [props.brand && (props.brand.name || props.brand.logo) && (_jsxs("div", { className: "fk-brand", children: [props.brand.logo && _jsx("img", { src: props.brand.logo, alt: "" }), props.brand.name && _jsx("span", { children: props.brand.name })] })), _jsxs("h2", { className: "fk-title", children: ["Join ", preview.tenantName] }), _jsxs("p", { className: "fk-sub", children: [preview.inviterName ? `${preview.inviterName} invited` : "You are invited", " ", preview.email, " to join this ", word, " as ", preview.role, ". Sign in or create an account with that address to accept."] }), _jsx(SignIn, { bare: true, email: preview.email, title: "Sign in to accept" })] }) }));
531
+ }
532
+ else {
533
+ const wrong = auth.account && auth.account.email !== preview.email;
534
+ body = (_jsxs(_Fragment, { children: [_jsxs("h2", { className: "fk-title", children: ["Join ", preview.tenantName] }), _jsxs("p", { className: "fk-sub", children: [preview.inviterName ? `${preview.inviterName} invited you` : "You are invited", " to this ", word, " as ", _jsx("strong", { children: preview.role }), "."] }), wrong && _jsxs(Alert, { kind: "error", children: ["This invitation is for ", preview.email, ", and you are signed in as ", auth.account.email, ". Sign out and sign in with the invited address."] }), action.error && _jsx(Alert, { kind: "error", children: action.error }), _jsx("div", { style: { height: 12 } }), _jsxs("div", { className: "fk-row", children: [wrong ? (_jsx("button", { type: "button", className: "fk-btn", onClick: () => void auth.signOut(), children: "Sign out" })) : (_jsx("button", { type: "button", className: "fk-btn", disabled: action.busy, onClick: () => void action.run(async () => {
535
+ const tenant = await client.acceptInvitation(token);
536
+ clear();
537
+ props.onAccepted?.(tenant);
538
+ }), children: action.busy ? _jsx(Spinner, { label: "Joining" }) : `Join ${preview.tenantName}` })), _jsx("button", { type: "button", className: "fk-btn fk-secondary", onClick: dismiss, children: "Not now" })] })] }));
539
+ }
540
+ return (_jsx("div", { className: cx("fk-auth", "fk-screen", props.className), children: _jsx("div", { className: "fk-card", children: body }) }));
541
+ }
542
+ // ── Workspaces ────────────────────────────────────────────────────────────────
543
+ /**
544
+ * The current workspace, and a menu to switch to another or create one. Keyboard: Enter/Space opens,
545
+ * arrows move, Escape closes. Switching re-checks your membership on the server and re-runs every query.
546
+ */
547
+ export function TenantSwitcher(props) {
548
+ useStyles();
549
+ const tenant = useTenant();
550
+ const auth = useAuth();
551
+ const [open, setOpen] = useState(false);
552
+ const [creating, setCreating] = useState(false);
553
+ const [name, setName] = useState("");
554
+ const action = useAction();
555
+ const menuId = useId();
556
+ const root = useRef(null);
557
+ const word = auth.tenancy?.name ?? "workspace";
558
+ useEffect(() => {
559
+ if (!open)
560
+ return;
561
+ const onDown = (e) => !root.current?.contains(e.target) && setOpen(false);
562
+ document.addEventListener("mousedown", onDown);
563
+ root.current?.querySelector(".fk-menu button")?.focus();
564
+ return () => document.removeEventListener("mousedown", onDown);
565
+ }, [open]);
566
+ if (!auth.tenancy || !auth.user)
567
+ return null;
568
+ const onKey = (e) => {
569
+ if (e.key === "Escape") {
570
+ setOpen(false);
571
+ root.current?.querySelector(":scope > button")?.focus();
572
+ }
573
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
574
+ e.preventDefault();
575
+ const items = [...(root.current?.querySelectorAll(".fk-menu button") ?? [])];
576
+ const at = items.indexOf(document.activeElement);
577
+ items[(at + (e.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus();
578
+ }
579
+ };
580
+ const canCreate = props.allowCreate !== false && tenant.settings?.create !== "none";
581
+ return (_jsxs("div", { className: cx("fk-auth", "fk-switcher", props.className), ref: root, onKeyDown: onKey, children: [_jsxs("button", { type: "button", className: "fk-btn fk-secondary", "aria-haspopup": "true", "aria-expanded": open, "aria-controls": menuId, onClick: () => setOpen((o) => !o), children: [_jsx("span", { children: tenant.current?.name ?? `Choose a ${word}` }), _jsx("svg", { width: "12", height: "12", viewBox: "0 0 12 12", "aria-hidden": true, children: _jsx("path", { d: "M3 4.5 6 7.5 9 4.5", fill: "none", stroke: "currentColor", strokeWidth: "1.5" }) })] }), open && (_jsxs("div", { className: "fk-menu", id: menuId, role: "menu", "aria-label": `Your ${word}s`, children: [tenant.loading && _jsx(Spinner, {}), !tenant.loading && tenant.tenants.length === 0 && _jsxs("div", { className: "fk-empty", style: { padding: "6px 10px" }, children: ["No ", word, "s yet."] }), tenant.tenants.map((t) => (_jsxs("button", { type: "button", role: "menuitemradio", "aria-checked": t.id === tenant.current?.id, "aria-current": t.id === tenant.current?.id, onClick: () => void action.run(async () => {
582
+ if (t.id !== tenant.current?.id)
583
+ await tenant.switchTo(t.id);
584
+ setOpen(false);
585
+ props.onSwitched?.({ id: t.id, name: t.name, role: t.role });
586
+ }), children: [_jsx("span", { children: t.name }), _jsx("span", { className: "fk-badge", children: t.role })] }, t.id))), canCreate && _jsx("hr", {}), canCreate && !creating && (_jsxs("button", { type: "button", role: "menuitem", onClick: () => setCreating(true), children: ["+ New ", word] })), canCreate && creating && (_jsxs("form", { className: "fk-form", style: { padding: 6 }, onSubmit: (e) => {
587
+ e.preventDefault();
588
+ void action.run(async () => {
589
+ await tenant.create(name);
590
+ setName("");
591
+ setCreating(false);
592
+ setOpen(false);
593
+ });
594
+ }, children: [_jsx(Field, { label: `${word[0].toUpperCase()}${word.slice(1)} name`, children: (id) => _jsx("input", { id: id, className: "fk-input", required: true, maxLength: 80, value: name, onChange: (e) => setName(e.target.value), autoFocus: true }) }), _jsx("button", { className: "fk-btn fk-small", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Creating" }) : "Create" })] })), action.error && _jsx(Alert, { kind: "error", children: action.error })] }))] }));
595
+ }
596
+ /** For a person with no workspace yet: create one (or wait for an invitation). */
597
+ export function CreateTenant(props) {
598
+ useStyles();
599
+ const tenant = useTenant();
600
+ const auth = useAuth();
601
+ const [name, setName] = useState("");
602
+ const action = useAction();
603
+ const word = auth.tenancy?.name ?? "workspace";
604
+ const allowed = tenant.settings?.create !== "none";
605
+ return (_jsx("div", { className: cx("fk-auth", "fk-screen", props.className), children: _jsxs("div", { className: "fk-card", children: [_jsx("h2", { className: "fk-title", children: props.title ?? (allowed ? `Create a ${word}` : `No ${word} yet`) }), _jsx("p", { className: "fk-sub", children: allowed ? `A ${word} is where your team works together. You can invite people next.` : `Ask someone to invite you to their ${word}.` }), tenant.tenants.length > 0 && (_jsxs("div", { className: "fk-form", style: { marginBottom: 14 }, children: [tenant.tenants.map((t) => (_jsxs("button", { type: "button", className: "fk-btn fk-secondary fk-block", onClick: () => void action.run(() => tenant.switchTo(t.id)), children: ["Open ", t.name] }, t.id))), allowed && _jsx("div", { className: "fk-divider", children: "or" })] })), allowed && (_jsxs("form", { className: "fk-form", onSubmit: (e) => {
606
+ e.preventDefault();
607
+ void action.run(async () => {
608
+ await tenant.create(name);
609
+ props.onCreated?.();
610
+ });
611
+ }, children: [_jsx(Field, { label: "Name", children: (id) => _jsx("input", { id: id, className: "fk-input", required: true, maxLength: 80, value: name, onChange: (e) => setName(e.target.value) }) }), _jsx("button", { className: "fk-btn fk-block", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Creating" }) : `Create ${word}` })] })), action.error && _jsx(Alert, { kind: "error", children: action.error })] }) }));
612
+ }
613
+ /** Members of the current workspace: roles (for those who may manage), removal, and leaving. */
614
+ export function MembersPanel(props) {
615
+ useStyles();
616
+ const client = useFeltKit();
617
+ const tenant = useTenant();
618
+ const [members, setMembers] = useState(null);
619
+ const [tick, setTick] = useState(0);
620
+ const action = useAction();
621
+ const can = tenant.settings?.can;
622
+ const word = tenant.settings?.name ?? "workspace";
623
+ useEffect(() => {
624
+ if (!tenant.current)
625
+ return;
626
+ let live = true;
627
+ client.members().then((m) => live && setMembers(m), (e) => live && action.setError(message(e)));
628
+ return () => void (live = false);
629
+ // eslint-disable-next-line react-hooks/exhaustive-deps
630
+ }, [client, tenant.current?.id, tick]);
631
+ if (!tenant.current)
632
+ return null;
633
+ const roles = tenant.settings?.roles ?? [];
634
+ const rank = (role) => roles.length - roles.indexOf(role);
635
+ const myRole = tenant.current.role;
636
+ const manageable = (m) => Boolean(can?.manageMembers) && !m.you && (myRole === roles[0] || rank(m.role) < rank(myRole));
637
+ const refresh = () => setTick((t) => t + 1);
638
+ return (_jsxs("section", { className: cx("fk-auth", "fk-section", props.className), "aria-labelledby": "fk-members-title", children: [_jsx("h3", { id: "fk-members-title", children: "Members" }), _jsxs("p", { className: "fk-sub", children: ["People in ", tenant.current.name, "."] }), action.error && _jsx(Alert, { kind: "error", children: action.error }), action.done && _jsx(Alert, { kind: "ok", children: action.done }), members === null ? (_jsx(Spinner, { label: "Loading members" })) : members.length === 0 ? (_jsx("div", { className: "fk-empty", children: "No members." })) : (_jsx("ul", { className: "fk-list", children: members.map((m) => (_jsxs("li", { children: [_jsxs("div", { className: "fk-who", children: [_jsxs("strong", { children: [m.displayName, m.you ? " (you)" : ""] }), m.email && _jsx("span", { children: m.email })] }), _jsxs("div", { className: "fk-row", children: [manageable(m) ? (_jsx("select", { className: "fk-select", style: { width: "auto", minHeight: 34 }, "aria-label": `Role of ${m.displayName}`, value: m.role, disabled: action.busy, onChange: (e) => {
639
+ const role = e.target.value; // read now: the controlled select re-renders before the call returns
640
+ void action.run(async () => {
641
+ await client.setMemberRole(m.principalId, role);
642
+ refresh();
643
+ return `${m.displayName} is now ${role}.`;
644
+ });
645
+ }, children: [...new Set([m.role, ...(can?.assign ?? [])])].map((r) => _jsx("option", { value: r, children: r }, r)) })) : (_jsx("span", { className: "fk-badge", children: m.role })), manageable(m) && (_jsx("button", { type: "button", className: "fk-btn fk-danger fk-small", disabled: action.busy, onClick: () => {
646
+ if (globalThis.confirm && !globalThis.confirm(`Remove ${m.displayName} from ${tenant.current.name}?`))
647
+ return;
648
+ void action.run(async () => {
649
+ await client.removeMember(m.principalId);
650
+ refresh();
651
+ return `${m.displayName} was removed.`;
652
+ });
653
+ }, children: "Remove" })), m.you && (_jsxs("button", { type: "button", className: "fk-btn fk-secondary fk-small", disabled: action.busy, onClick: () => {
654
+ if (globalThis.confirm && !globalThis.confirm(`Leave ${tenant.current.name}? You will need a new invitation to come back.`))
655
+ return;
656
+ void action.run(async () => {
657
+ await client.leaveTenant();
658
+ });
659
+ }, children: ["Leave ", word] }))] })] }, m.principalId))) }))] }));
660
+ }
661
+ /** Invite people to the current workspace by e-mail; see, copy and withdraw open invitations. */
662
+ export function InvitationsPanel(props) {
663
+ useStyles();
664
+ const client = useFeltKit();
665
+ const tenant = useTenant();
666
+ const can = tenant.settings?.can;
667
+ const [list, setList] = useState(null);
668
+ const [email, setEmail] = useState("");
669
+ const [role, setRole] = useState("");
670
+ const [last, setLast] = useState(null);
671
+ const [copied, setCopied] = useState(false);
672
+ const [tick, setTick] = useState(0);
673
+ const action = useAction();
674
+ const assignable = can?.assign ?? [];
675
+ useEffect(() => {
676
+ if (!tenant.current || !can?.manageMembers)
677
+ return;
678
+ let live = true;
679
+ client.invitations().then((i) => live && setList(i), (e) => live && action.setError(message(e)));
680
+ return () => void (live = false);
681
+ // eslint-disable-next-line react-hooks/exhaustive-deps
682
+ }, [client, tenant.current?.id, can?.manageMembers, tick]);
683
+ useEffect(() => {
684
+ if (!role && assignable.length)
685
+ setRole(assignable.includes("member") ? "member" : assignable[assignable.length - 1]);
686
+ }, [assignable, role]);
687
+ if (!tenant.current || !can?.manageMembers || tenant.settings?.invitations === false)
688
+ return null;
689
+ const open = (list ?? []).filter((i) => i.status === "open");
690
+ return (_jsxs("section", { className: cx("fk-auth", "fk-section", props.className), "aria-labelledby": "fk-invite-title", children: [_jsx("h3", { id: "fk-invite-title", children: "Invite people" }), _jsx("p", { className: "fk-sub", children: "They get an e-mail with a link that works once." }), _jsx("form", { className: "fk-form", onSubmit: (e) => {
691
+ e.preventDefault();
692
+ void action.run(async () => {
693
+ const result = await client.invite({ email, role });
694
+ setLast({ link: result.link, sent: result.sent, email });
695
+ setEmail("");
696
+ setCopied(false);
697
+ setTick((t) => t + 1);
698
+ });
699
+ }, children: _jsxs("div", { className: "fk-row", style: { alignItems: "flex-end" }, children: [_jsx("div", { style: { flex: "1 1 220px" }, children: _jsx(Field, { label: "E-mail", children: (id) => _jsx("input", { id: id, className: "fk-input", type: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value) }) }) }), _jsx("div", { style: { flex: "0 1 160px" }, children: _jsx(Field, { label: "Role", children: (id) => (_jsx("select", { id: id, className: "fk-select", value: role, onChange: (e) => setRole(e.target.value), children: assignable.map((r) => _jsx("option", { value: r, children: r }, r)) })) }) }), _jsx("button", { className: "fk-btn", disabled: action.busy || !role, children: action.busy ? _jsx(Spinner, { label: "Inviting" }) : "Invite" })] }) }), action.error && _jsx(Alert, { kind: "error", children: action.error }), last && (_jsxs("div", { className: "fk-alert fk-ok", role: "status", style: { marginTop: 12 }, children: [_jsx("div", { children: last.sent ? `We e-mailed an invitation to ${last.email}.` : `Share this link with ${last.email}:` }), _jsxs("div", { className: "fk-row", style: { marginTop: 8 }, children: [_jsx("input", { className: "fk-input", readOnly: true, value: last.link, "aria-label": "Invitation link", onFocus: (e) => e.currentTarget.select(), style: { flex: 1, minWidth: 0 } }), _jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", onClick: () => void navigator.clipboard?.writeText(last.link).then(() => setCopied(true)), children: copied ? "Copied" : "Copy link" })] })] })), _jsx("h3", { style: { marginTop: 18 }, children: "Open invitations" }), list === null ? (_jsx(Spinner, { label: "Loading invitations" })) : open.length === 0 ? (_jsx("div", { className: "fk-empty", children: "No open invitations." })) : (_jsx("ul", { className: "fk-list", children: open.map((i) => (_jsxs("li", { children: [_jsxs("div", { className: "fk-who", children: [_jsx("strong", { children: i.email }), _jsxs("span", { children: [i.role, " \u00B7 expires ", new Date(i.expiresAt).toLocaleDateString()] })] }), _jsx("button", { type: "button", className: "fk-btn fk-danger fk-small", disabled: action.busy, onClick: () => void action.run(async () => {
700
+ await client.revokeInvitation(i.id);
701
+ setTick((t) => t + 1);
702
+ return `The invitation to ${i.email} was withdrawn.`;
703
+ }), children: "Withdraw" })] }, i.id))) }))] }));
704
+ }
705
+ /** Workspace settings: its name (for roles with tenant.update), members, invitations. */
706
+ export function TenantSettings(props) {
707
+ useStyles();
708
+ const client = useFeltKit();
709
+ const tenant = useTenant();
710
+ const [name, setName] = useState("");
711
+ const action = useAction();
712
+ useEffect(() => setName(tenant.current?.name ?? ""), [tenant.current?.name]);
713
+ if (!tenant.current)
714
+ return null;
715
+ return (_jsxs("div", { className: cx("fk-auth", props.className), children: [tenant.settings?.can?.update && (_jsxs("section", { className: "fk-section", "aria-labelledby": "fk-tenant-name", children: [_jsx("h3", { id: "fk-tenant-name", children: "Name" }), _jsxs("form", { className: "fk-row", onSubmit: (e) => {
716
+ e.preventDefault();
717
+ void action.run(async () => {
718
+ await client.renameTenant(name);
719
+ tenant.refresh();
720
+ return "Saved.";
721
+ });
722
+ }, children: [_jsx("input", { className: "fk-input", "aria-label": "Name", style: { flex: 1, minWidth: 0 }, required: true, maxLength: 80, value: name, onChange: (e) => setName(e.target.value) }), _jsx("button", { className: "fk-btn", disabled: action.busy || name === tenant.current.name, children: "Save" })] }), action.error && _jsx(Alert, { kind: "error", children: action.error }), action.done && _jsx(Alert, { kind: "ok", children: action.done })] })), _jsx(MembersPanel, {}), _jsx(InvitationsPanel, {})] }));
723
+ }
724
+ // ── Account settings ──────────────────────────────────────────────────────────
725
+ /** Profile, address, password, connected providers, active sessions, sign out. */
726
+ export function AccountSettings(props) {
727
+ useStyles();
728
+ const client = useFeltKit();
729
+ const auth = useAuth();
730
+ const [account, setAccount] = useState(null);
731
+ const [sessions, setSessions] = useState(null);
732
+ const [tick, setTick] = useState(0);
733
+ const [loadError, setLoadError] = useState(null);
734
+ const refresh = () => setTick((t) => t + 1);
735
+ useEffect(() => {
736
+ if (!auth.account)
737
+ return;
738
+ let live = true;
739
+ Promise.all([client.account(), client.sessions()]).then(([a, s]) => {
740
+ if (!live)
741
+ return;
742
+ setAccount(a);
743
+ setSessions(s);
744
+ }, (e) => live && setLoadError(message(e)));
745
+ return () => void (live = false);
746
+ }, [client, auth.account, tick]);
747
+ if (!auth.user)
748
+ return null;
749
+ if (!auth.account) {
750
+ return (_jsx("div", { className: cx("fk-auth", props.className), children: _jsxs("section", { className: "fk-section", children: [_jsx("h3", { children: "Account" }), _jsxs("p", { className: "fk-sub", children: ["Signed in as ", auth.user.displayName ?? auth.user.id, ". This identity has no account settings."] }), _jsx("button", { type: "button", className: "fk-btn fk-secondary", onClick: () => void auth.signOut().then(props.onSignedOut), children: "Sign out" })] }) }));
751
+ }
752
+ if (loadError)
753
+ return _jsx("div", { className: cx("fk-auth", props.className), children: _jsx(Alert, { kind: "error", children: loadError }) });
754
+ if (!account || !sessions)
755
+ return _jsx("div", { className: cx("fk-auth", props.className), children: _jsx(Spinner, { label: "Loading your account" }) });
756
+ return (_jsxs("div", { className: cx("fk-auth", props.className), children: [_jsx(ProfileSection, { account: account, onSaved: refresh }), _jsx(EmailSection, { account: account, canEmail: Boolean(auth.settings?.email) }), _jsx(PasswordSection, { account: account, onChanged: refresh }), _jsx(ProvidersSection, { account: account, available: auth.settings?.providers ?? [], onChanged: refresh }), _jsx(SessionsSection, { sessions: sessions, onChanged: refresh, onSignedOut: props.onSignedOut })] }));
757
+ }
758
+ function ProfileSection({ account, onSaved }) {
759
+ const client = useFeltKit();
760
+ const [name, setName] = useState(account.name ?? "");
761
+ const action = useAction();
762
+ return (_jsxs("section", { className: "fk-section", "aria-labelledby": "fk-profile", children: [_jsx("h3", { id: "fk-profile", children: "Profile" }), _jsxs("form", { className: "fk-row", style: { marginTop: 8 }, onSubmit: (e) => {
763
+ e.preventDefault();
764
+ void action.run(async () => {
765
+ await client.updateProfile({ name });
766
+ onSaved();
767
+ return "Saved.";
768
+ });
769
+ }, children: [_jsx("input", { className: "fk-input", "aria-label": "Name", placeholder: "Your name", style: { flex: 1, minWidth: 0 }, required: true, maxLength: 120, value: name, onChange: (e) => setName(e.target.value) }), _jsx("button", { className: "fk-btn", disabled: action.busy || name === (account.name ?? ""), children: "Save" })] }), action.error && _jsx(Alert, { kind: "error", children: action.error }), action.done && _jsx(Alert, { kind: "ok", children: action.done })] }));
770
+ }
771
+ function EmailSection({ account, canEmail }) {
772
+ const client = useFeltKit();
773
+ const [next, setNext] = useState("");
774
+ const [editing, setEditing] = useState(false);
775
+ const action = useAction();
776
+ return (_jsxs("section", { className: "fk-section", "aria-labelledby": "fk-email", children: [_jsx("h3", { id: "fk-email", children: "E-mail" }), _jsxs("div", { className: "fk-between", children: [_jsxs("div", { className: "fk-who", children: [_jsx("strong", { children: account.email }), _jsx("span", { children: account.emailVerified ? "Verified" : "Not verified" })] }), canEmail && (_jsxs("div", { className: "fk-row", children: [!account.emailVerified && (_jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", disabled: action.busy, onClick: () => void action.run(async () => (await client.requestVerification(account.email)).message), children: "Send verification link" })), _jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", onClick: () => setEditing((v) => !v), "aria-expanded": editing, children: "Change" })] }))] }), editing && (_jsxs("form", { className: "fk-row", style: { marginTop: 12 }, onSubmit: (e) => {
777
+ e.preventDefault();
778
+ void action.run(async () => {
779
+ const { message: text } = await client.requestEmailChange(next);
780
+ setEditing(false);
781
+ setNext("");
782
+ return text;
783
+ });
784
+ }, children: [_jsx("input", { className: "fk-input", type: "email", "aria-label": "New e-mail address", placeholder: "new@example.com", style: { flex: 1, minWidth: 0 }, required: true, value: next, onChange: (e) => setNext(e.target.value) }), _jsx("button", { className: "fk-btn", disabled: action.busy, children: "Send confirmation" })] })), action.error && _jsx(Alert, { kind: "error", children: action.error }), action.done && _jsx(Alert, { kind: "ok", children: action.done })] }));
785
+ }
786
+ function PasswordSection({ account, onChanged }) {
787
+ const client = useFeltKit();
788
+ const [current, setCurrent] = useState("");
789
+ const [next, setNext] = useState("");
790
+ const action = useAction();
791
+ return (_jsxs("section", { className: "fk-section", "aria-labelledby": "fk-password", children: [_jsx("h3", { id: "fk-password", children: "Password" }), _jsx("p", { className: "fk-sub", children: account.hasPassword ? "Changing it signs out your other sessions." : "Set a password to sign in without a provider." }), _jsxs("form", { className: "fk-form", onSubmit: (e) => {
792
+ e.preventDefault();
793
+ void action.run(async () => {
794
+ await client.changePassword({ ...(account.hasPassword ? { current } : {}), password: next });
795
+ setCurrent("");
796
+ setNext("");
797
+ onChanged();
798
+ return "Password changed. Your other sessions were signed out.";
799
+ });
800
+ }, children: [account.hasPassword && (_jsx(Field, { label: "Current password", children: (id) => _jsx("input", { id: id, className: "fk-input", type: "password", autoComplete: "current-password", required: true, value: current, onChange: (e) => setCurrent(e.target.value) }) })), _jsx(Field, { label: "New password", hint: "At least 12 characters.", children: (id, hint) => _jsx("input", { id: id, "aria-describedby": hint, className: "fk-input", type: "password", autoComplete: "new-password", required: true, minLength: 12, value: next, onChange: (e) => setNext(e.target.value) }) }), _jsx("div", { children: _jsx("button", { className: "fk-btn", disabled: action.busy, children: action.busy ? _jsx(Spinner, { label: "Saving" }) : account.hasPassword ? "Change password" : "Set password" }) })] }), action.error && _jsx(Alert, { kind: "error", children: action.error }), action.done && _jsx(Alert, { kind: "ok", children: action.done })] }));
801
+ }
802
+ function ProvidersSection({ account, available, onChanged }) {
803
+ const client = useFeltKit();
804
+ const action = useAction();
805
+ if (!available.length && !account.providers.length)
806
+ return null;
807
+ return (_jsxs("section", { className: "fk-section", "aria-labelledby": "fk-providers", children: [_jsx("h3", { id: "fk-providers", children: "Connected sign-in" }), _jsx("ul", { className: "fk-list", children: available.map((p) => {
808
+ const connected = account.providers.includes(p.id);
809
+ return (_jsxs("li", { children: [_jsxs("div", { className: "fk-who", children: [_jsx("strong", { children: p.name }), _jsx("span", { children: connected ? "Connected" : "Not connected" })] }), connected ? (_jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", disabled: action.busy, onClick: () => void action.run(async () => {
810
+ await client.unlinkProvider(p.id);
811
+ onChanged();
812
+ return `${p.name} was disconnected.`;
813
+ }), children: "Disconnect" })) : (_jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", onClick: () => client.signInWith(p.id, globalThis.location?.pathname ?? "/"), children: "Connect" }))] }, p.id));
814
+ }) }), action.error && _jsx(Alert, { kind: "error", children: action.error }), action.done && _jsx(Alert, { kind: "ok", children: action.done })] }));
815
+ }
816
+ function SessionsSection({ sessions, onChanged, onSignedOut }) {
817
+ const client = useFeltKit();
818
+ const action = useAction();
819
+ const others = sessions.filter((s) => !s.current).length;
820
+ return (_jsxs("section", { className: "fk-section", "aria-labelledby": "fk-sessions", children: [_jsxs("div", { className: "fk-between", children: [_jsx("h3", { id: "fk-sessions", children: "Sessions" }), _jsxs("div", { className: "fk-row", children: [others > 0 && (_jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", disabled: action.busy, onClick: () => void action.run(async () => {
821
+ const n = await client.signOutEverywhere({ keepCurrent: true });
822
+ onChanged();
823
+ return `Signed out of ${n} other session${n === 1 ? "" : "s"}.`;
824
+ }), children: "Sign out other sessions" })), _jsx("button", { type: "button", className: "fk-btn fk-danger fk-small", onClick: () => void client.signOut().then(onSignedOut), children: "Sign out" })] })] }), _jsx("ul", { className: "fk-list", style: { marginTop: 8 }, children: sessions.map((s) => (_jsxs("li", { children: [_jsxs("div", { className: "fk-who", children: [_jsxs("strong", { children: [s.label ?? s.kind, s.current ? " · this device" : ""] }), _jsxs("span", { children: ["Started ", new Date(s.createdAt).toLocaleString(), s.expiresAt ? ` · until ${new Date(s.expiresAt).toLocaleDateString()}` : ""] })] }), !s.current && (_jsx("button", { type: "button", className: "fk-btn fk-secondary fk-small", disabled: action.busy, onClick: () => void action.run(async () => {
825
+ await client.revokeSession(s.ref);
826
+ onChanged();
827
+ return "Signed out of that session.";
828
+ }), children: "Sign out" }))] }, s.ref))) }), action.error && _jsx(Alert, { kind: "error", children: action.error }), action.done && _jsx(Alert, { kind: "ok", children: action.done })] }));
829
+ }
830
+ //# sourceMappingURL=auth.js.map