@coffre/ui 0.1.10 → 0.1.12

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 (172) hide show
  1. package/dist/client/_coffre/assets/account-CyJDFhek.js +2 -0
  2. package/dist/client/_coffre/assets/account-DHSs5h2f.js +1 -0
  3. package/dist/client/_coffre/assets/affordances-BQD5B421.js +1 -0
  4. package/dist/client/_coffre/assets/audit-Bcyp_dU1.js +2 -0
  5. package/dist/client/_coffre/assets/audit-rMy9mU9Q.js +1 -0
  6. package/dist/client/_coffre/assets/auth.device-DuvaComb.js +1 -0
  7. package/dist/client/_coffre/assets/auth.device-yBt_sLZW.js +2 -0
  8. package/dist/client/_coffre/assets/directory-C4BMGC7-.js +1 -0
  9. package/dist/client/_coffre/assets/dist-BNkBTkxj.js +1 -0
  10. package/dist/client/_coffre/assets/globals-BJkzu6T5.css +1 -0
  11. package/dist/client/_coffre/assets/grants-CyDW25eX.js +1 -0
  12. package/dist/client/_coffre/assets/icons-BRl8DlXW.js +1 -0
  13. package/dist/client/_coffre/assets/index-CA1mtrIx.js +12 -0
  14. package/dist/client/_coffre/assets/link-CqlAJWBd.js +1 -0
  15. package/dist/client/_coffre/assets/login-B73b6bsJ.js +1 -0
  16. package/dist/client/_coffre/assets/login-BO1KAGPU.js +2 -0
  17. package/dist/client/_coffre/assets/offboarding--5vSKNHv.js +1 -0
  18. package/dist/client/_coffre/assets/page-Dngv8FMr.js +1 -0
  19. package/dist/client/_coffre/assets/preload-helper-DBha3tys.js +1 -0
  20. package/dist/client/_coffre/assets/principal-page-C4qDD-d8.js +1 -0
  21. package/dist/client/_coffre/assets/projects._project._environment-eBKxOZ0_.js +2 -0
  22. package/dist/client/_coffre/assets/projects._project._environment-or_AX6o5.js +3 -0
  23. package/dist/client/_coffre/assets/projects._project.index-DHdXfEh-.js +2 -0
  24. package/dist/client/_coffre/assets/projects._project.index-iynQgenH.js +1 -0
  25. package/dist/client/_coffre/assets/projects.index-CGW8mRil.js +1 -0
  26. package/dist/client/_coffre/assets/row-state-BlarvG7E.js +1 -0
  27. package/dist/client/_coffre/assets/settings-D-7RJIDf.js +1 -0
  28. package/dist/client/_coffre/assets/suspense-q_eJaZ18.js +1 -0
  29. package/dist/client/_coffre/assets/tokens._token-0u7midqA.js +2 -0
  30. package/dist/client/_coffre/assets/tokens._token-C53NqScg.js +1 -0
  31. package/dist/client/_coffre/assets/tokens.index-DBZTLnbi.js +1 -0
  32. package/dist/client/_coffre/assets/ui-BPWFJNz7.js +46 -0
  33. package/dist/client/_coffre/assets/unregistered-CLhgtwUt.js +1 -0
  34. package/dist/client/_coffre/assets/use-action-LULgF26O.js +1 -0
  35. package/dist/client/_coffre/assets/use-change-Bd6NZJj9.js +1 -0
  36. package/dist/client/_coffre/assets/use-shell-Dx92Huhb.js +1 -0
  37. package/dist/client/_coffre/assets/useQuery-BIIZz1CB.js +1 -0
  38. package/dist/client/_coffre/assets/useSuspenseQuery-tP9HFZ8w.js +1 -0
  39. package/dist/client/_coffre/assets/users._user-BXtaL8Ee.js +2 -0
  40. package/dist/client/_coffre/assets/users._user-CNYVkXaY.js +1 -0
  41. package/dist/client/_coffre/assets/users.index-Cs9tibIv.js +1 -0
  42. package/dist/server/.vite/manifest.json +342 -246
  43. package/dist/server/_coffre/assets/_tanstack-start-manifest_v-T9tMsCwo.js +162 -0
  44. package/dist/server/_coffre/assets/{account-CUQYGCYh.js → account-BUhhOS5-.js} +150 -117
  45. package/dist/server/_coffre/assets/{account-DMlUoBxQ.js → account-B_h55GCs.js} +10 -24
  46. package/dist/server/_coffre/assets/{affordances-BNaXjMOb.js → affordances-CKANXDTG.js} +2 -22
  47. package/dist/server/_coffre/assets/{async-local-storage-C0cwgkEr.js → async-local-storage-C10uQtMM.js} +3 -2
  48. package/dist/server/_coffre/assets/{audit-CCdSLmvK.js → audit-B8Ck358-.js} +46 -16
  49. package/dist/server/_coffre/assets/audit-DFsXfeAQ.js +31 -0
  50. package/dist/server/_coffre/assets/{auth.device-BoFM1jcf.js → auth.device-BXf_JjT7.js} +5 -5
  51. package/dist/server/_coffre/assets/{auth.device-C05u5ROO.js → auth.device-cqjWACvB.js} +7 -5
  52. package/dist/server/_coffre/assets/{directory-GsnYkse0.js → directory-C9SfNgpM.js} +87 -112
  53. package/dist/server/_coffre/assets/{use-action-DwEmf58L.js → dist-CnnV3t2g.js} +3 -48
  54. package/dist/server/_coffre/assets/globals-BJkzu6T5.css +1 -0
  55. package/dist/server/_coffre/assets/grants--315KmXu.js +158 -0
  56. package/dist/server/_coffre/assets/{page-BdnhpwvH.js → icons-CWsTEfpn.js} +125 -436
  57. package/dist/server/_coffre/assets/lazyRouteComponent-CqBNCGuZ.js +480 -0
  58. package/dist/server/_coffre/assets/link-42ZWdjzZ.js +422 -0
  59. package/dist/server/_coffre/assets/{login-DUtCQju5.js → login-CQSkYW_D.js} +7 -5
  60. package/dist/server/_coffre/assets/{login-B2njIq_o.js → login-LBpXXfsQ.js} +2 -2
  61. package/dist/server/_coffre/assets/matchContext-qLfYbC6_.js +56 -0
  62. package/dist/server/_coffre/assets/{offboarding-8yMa7ViM.js → offboarding-CykelNis.js} +11 -79
  63. package/dist/server/_coffre/assets/page-BGeDoQO5.js +3531 -0
  64. package/dist/server/_coffre/assets/{principal-page-B8zPcS2b.js → principal-page-CRdoAQOq.js} +51 -42
  65. package/dist/server/_coffre/assets/{projects._project._environment-DdIOc7wx.js → projects._project._environment-BXU1Mee5.js} +4 -11
  66. package/dist/server/_coffre/assets/{projects._project._environment-BZ5Z80V8.js → projects._project._environment-BrDqn8kW.js} +129 -748
  67. package/dist/server/_coffre/assets/{projects._project.index-CuEAUwGn.js → projects._project.index-CAxBU4tY.js} +134 -96
  68. package/dist/server/_coffre/assets/{projects._project.index-DvbcU1kh.js → projects._project.index-CYyUVTCl.js} +5 -4
  69. package/dist/server/_coffre/assets/{projects.index-5HI0wOuG.js → projects.index-D6ZoDE_0.js} +57 -55
  70. package/dist/server/_coffre/assets/{react-dom-qCTiwGGR.js → react-dom-bBoJXlK2.js} +417 -51
  71. package/dist/server/_coffre/assets/{router-CQDoql8s.js → router-Bu2qtyqw.js} +376 -92
  72. package/dist/server/_coffre/assets/row-state-CAItu3n_.js +117 -0
  73. package/dist/server/_coffre/assets/{settings-BVz78wjP.js → settings-ByeF6hBa.js} +8 -6
  74. package/dist/server/_coffre/assets/suspense-beD01bb8.js +602 -0
  75. package/dist/server/_coffre/assets/tokens._token-CM01QPCq.js +10 -0
  76. package/dist/server/_coffre/assets/{tokens._token-CWs3Mtki.js → tokens._token-D2E4P3Qc.js} +97 -79
  77. package/dist/server/_coffre/assets/{tokens.index-Ck0LN7aC.js → tokens.index-Cmrgn3mU.js} +9 -6
  78. package/dist/server/_coffre/assets/{ui-D_slKpSF.js → ui-DvlGkmaH.js} +7 -836
  79. package/dist/server/_coffre/assets/{unregistered-Z17da4Rc.js → unregistered-DSArejP9.js} +5 -3
  80. package/dist/server/_coffre/assets/use-action-C2wz34kq.js +47 -0
  81. package/dist/server/_coffre/assets/use-change-CJpGSNIq.js +1212 -0
  82. package/dist/server/_coffre/assets/use-shell-C3iYcyP1.js +462 -0
  83. package/dist/server/_coffre/assets/useQuery-ChxaQ4Cb.js +8 -0
  84. package/dist/server/_coffre/assets/{useStore-DWGPqzXR.js → useRouter-CaUSUNsw.js} +1 -420
  85. package/dist/server/_coffre/assets/useSuspenseQuery-BjAmlGZl.js +117 -0
  86. package/dist/server/_coffre/assets/users._user-BVeR9fZP.js +10 -0
  87. package/dist/server/_coffre/assets/{users._user-CMwj-VI9.js → users._user-Cih2Etf4.js} +4 -5
  88. package/dist/server/_coffre/assets/{users.index-fWHv6A30.js → users.index-Bo2_a95G.js} +9 -6
  89. package/dist/server/index.js +6 -5
  90. package/package.json +5 -3
  91. package/src/components/directory.tsx +75 -104
  92. package/src/components/grants.tsx +60 -112
  93. package/src/components/icons.tsx +0 -34
  94. package/src/components/offboarding.tsx +1 -52
  95. package/src/components/principal-page.tsx +54 -67
  96. package/src/components/principal-picker.tsx +7 -16
  97. package/src/components/row-state.tsx +95 -0
  98. package/src/components/service-tokens.tsx +75 -64
  99. package/src/components/shell.tsx +1 -1
  100. package/src/lib/announce.ts +36 -0
  101. package/src/lib/changes.ts +498 -0
  102. package/src/lib/coffre.ts +1 -1
  103. package/src/lib/optimistic.ts +233 -0
  104. package/src/lib/queries.ts +385 -0
  105. package/src/lib/use-action.ts +20 -15
  106. package/src/lib/use-change.ts +42 -0
  107. package/src/lib/use-shell.ts +17 -0
  108. package/src/router.tsx +17 -9
  109. package/src/routes/__root.tsx +8 -38
  110. package/src/routes/account.tsx +163 -154
  111. package/src/routes/audit.tsx +51 -72
  112. package/src/routes/auth.device.tsx +4 -2
  113. package/src/routes/login.tsx +3 -2
  114. package/src/routes/projects.$project.$environment.tsx +223 -238
  115. package/src/routes/projects.$project.index.tsx +136 -118
  116. package/src/routes/projects.index.tsx +117 -145
  117. package/src/routes/settings.tsx +7 -7
  118. package/src/routes/tokens.$token.tsx +14 -17
  119. package/src/routes/tokens.index.tsx +8 -4
  120. package/src/routes/unregistered.tsx +3 -2
  121. package/src/routes/users.$user.tsx +4 -4
  122. package/src/routes/users.index.tsx +8 -4
  123. package/src/shared/models.ts +1 -5
  124. package/src/styles/globals.css +80 -50
  125. package/dist/client/_coffre/assets/account-B48wuSF8.js +0 -1
  126. package/dist/client/_coffre/assets/account-BkNyIDT7.js +0 -2
  127. package/dist/client/_coffre/assets/affordances-CmoY-8Xf.js +0 -1
  128. package/dist/client/_coffre/assets/audit-BvwKYb-5.js +0 -2
  129. package/dist/client/_coffre/assets/audit-D02H-zxS.js +0 -1
  130. package/dist/client/_coffre/assets/auth.device-BP5FY0K1.js +0 -1
  131. package/dist/client/_coffre/assets/auth.device-DYOONgFF.js +0 -2
  132. package/dist/client/_coffre/assets/coffre-BBp7cKVa.js +0 -1
  133. package/dist/client/_coffre/assets/directory-Bu0EIxqI.js +0 -1
  134. package/dist/client/_coffre/assets/globals-Bh2x1gBM.css +0 -1
  135. package/dist/client/_coffre/assets/grants-DavcwofW.js +0 -1
  136. package/dist/client/_coffre/assets/index-CDPlq5rM.js +0 -12
  137. package/dist/client/_coffre/assets/login-C-wTm_t1.js +0 -2
  138. package/dist/client/_coffre/assets/login-CLLv1om9.js +0 -1
  139. package/dist/client/_coffre/assets/offboarding-DWsE2FSa.js +0 -1
  140. package/dist/client/_coffre/assets/page-DbSS7pYH.js +0 -1
  141. package/dist/client/_coffre/assets/preload-helper-DduCCcmc.js +0 -46
  142. package/dist/client/_coffre/assets/principal-page-Bp_iwuSd.js +0 -1
  143. package/dist/client/_coffre/assets/projects._project._environment-BMeX_B8l.js +0 -2
  144. package/dist/client/_coffre/assets/projects._project._environment-CT0A-hIU.js +0 -3
  145. package/dist/client/_coffre/assets/projects._project.index-C_xYOGLT.js +0 -2
  146. package/dist/client/_coffre/assets/projects._project.index-Chcf-cUd.js +0 -1
  147. package/dist/client/_coffre/assets/projects.index-CfOtb1X5.js +0 -2
  148. package/dist/client/_coffre/assets/projects.index-DSjOhLDh.js +0 -1
  149. package/dist/client/_coffre/assets/settings-BC9cUJNK.js +0 -1
  150. package/dist/client/_coffre/assets/settings-Bkj_E21y.js +0 -2
  151. package/dist/client/_coffre/assets/tokens._token-BA0KqvaW.js +0 -1
  152. package/dist/client/_coffre/assets/tokens._token-Dh9jLOkm.js +0 -2
  153. package/dist/client/_coffre/assets/tokens.index-BgAMWkHT.js +0 -2
  154. package/dist/client/_coffre/assets/tokens.index-CHueIAv1.js +0 -1
  155. package/dist/client/_coffre/assets/unregistered-D5go1AfR.js +0 -1
  156. package/dist/client/_coffre/assets/use-action-l__xZDYY.js +0 -1
  157. package/dist/client/_coffre/assets/users._user-Dwqa0gv5.js +0 -1
  158. package/dist/client/_coffre/assets/users._user-xJAb3T6Y.js +0 -2
  159. package/dist/client/_coffre/assets/users.index-BJoF-L37.js +0 -2
  160. package/dist/client/_coffre/assets/users.index-YUjtbTGy.js +0 -1
  161. package/dist/server/_coffre/assets/_tanstack-start-manifest_v-BKg2AlPu.js +0 -133
  162. package/dist/server/_coffre/assets/audit-CThXn2v7.js +0 -74
  163. package/dist/server/_coffre/assets/coffre-C2ANsTAz.js +0 -281
  164. package/dist/server/_coffre/assets/globals-Bh2x1gBM.css +0 -1
  165. package/dist/server/_coffre/assets/grants-B6k4Fu9z.js +0 -243
  166. package/dist/server/_coffre/assets/projects.index-uiomWX_l.js +0 -54
  167. package/dist/server/_coffre/assets/settings-4oRyDizv.js +0 -18
  168. package/dist/server/_coffre/assets/tokens._token-CF_deAwq.js +0 -18
  169. package/dist/server/_coffre/assets/tokens.index-DF_07KEe.js +0 -10
  170. package/dist/server/_coffre/assets/users._user-DUzwkSKT.js +0 -10
  171. package/dist/server/_coffre/assets/users.index-BbuweTwH.js +0 -10
  172. package/src/components/syncs.tsx +0 -581
@@ -0,0 +1,42 @@
1
+ import { useMutation, useMutationState, useQueryClient, type QueryKey } from '@tanstack/react-query';
2
+ import { useCallback } from 'react';
3
+
4
+ import {
5
+ changeKey,
6
+ changeOptions,
7
+ dismiss,
8
+ failedAdds,
9
+ itemStatus,
10
+ type Change,
11
+ type ChangeContext,
12
+ } from './optimistic';
13
+
14
+ /** Make a change that shows before the server answers (`lib/optimistic.ts`). */
15
+ export function useChange<TData, TItem, TVars, TResult>(change: Change<TData, TItem, TVars, TResult>) {
16
+ const queryClient = useQueryClient();
17
+ return useMutation(changeOptions(queryClient, change)).mutate;
18
+ }
19
+
20
+ /** How each item of a list stands, from the changes made to it here. */
21
+ export function useChangeStatus(list: QueryKey) {
22
+ const queryClient = useQueryClient();
23
+ const changes = useMutationState({
24
+ filters: { mutationKey: changeKey(list) },
25
+ select: (mutation) => ({
26
+ mutationId: mutation.mutationId,
27
+ state: mutation.state as typeof mutation.state & { context: ChangeContext | undefined },
28
+ }),
29
+ });
30
+
31
+ const status = useCallback((id: string) => itemStatus(changes, id), [changes]);
32
+
33
+ /** Adds the server refused, whose items are not in `present`: rendered as rows of their own. */
34
+ const refusedAdds = useCallback(
35
+ <TVars,>(present: readonly string[]) => failedAdds<TVars>(changes, present),
36
+ [changes],
37
+ );
38
+
39
+ const forget = useCallback((mutationId: number) => dismiss(queryClient, mutationId), [queryClient]);
40
+
41
+ return { status, failedAdds: refusedAdds, dismiss: forget };
42
+ }
@@ -0,0 +1,17 @@
1
+ import { useSuspenseQueries } from '@tanstack/react-query';
2
+ import { useMemo } from 'react';
3
+
4
+ import { useCoffre } from './coffre';
5
+ import { queries, shellOf, type Shell } from './queries';
6
+
7
+ /**
8
+ * The shell as a component reads it: from the same queries the root loader
9
+ * fetched, so it follows any change that refetches them.
10
+ */
11
+ export function useShell(): Shell {
12
+ const client = useCoffre();
13
+ const [auth, me, projects] = useSuspenseQueries({
14
+ queries: [queries.auth(client), queries.me(client), queries.projects(client)],
15
+ });
16
+ return useMemo(() => shellOf(auth.data, me.data, projects.data), [auth.data, me.data, projects.data]);
17
+ }
package/src/router.tsx CHANGED
@@ -1,30 +1,38 @@
1
1
  import { createClient, type CoffreClient } from '@coffre/client';
2
+ import type { QueryClient } from '@tanstack/react-query';
2
3
  import { createRouter as createTanStackRouter } from '@tanstack/react-router';
4
+ import { setupRouterSsrQueryIntegration } from '@tanstack/react-router-ssr-query';
3
5
  import { getGlobalStartContext } from '@tanstack/react-start';
4
6
  import { routeTree } from './routeTree.gen';
5
7
  import { NotFound, RouteError } from './components/route-states';
8
+ import { createQueryClient } from './lib/queries';
6
9
 
7
10
  /**
8
11
  * The router factory. TanStack Start calls this once per request on the server
9
- * and once on the client.
12
+ * and once on the client, so each gets a query cache of its own: a request's
13
+ * reads are never another visitor's. The server's reads travel with the page,
14
+ * so the browser starts from them rather than asking again.
10
15
  *
11
- * Note what is deliberately absent: any caching of loader data. Every screen in
12
- * this app reports authorisation state that another person can change from
13
- * another tab, so a stale project list or grant table is not a cosmetic
14
- * problem. `staleTime: 0` means a navigation back to a screen refetches it,
15
- * which is the behaviour the Next.js version got from `force-dynamic`.
16
+ * Loaders read through the query cache (`lib/queries.ts`, which says how
17
+ * fresh it keeps a page), so the router keeps no copy of its own: it runs a
18
+ * route's loader on every navigation and every hover, and the cache answers
19
+ * whatever it already holds. A hover is then a read the click reuses.
16
20
  */
17
21
  export function getRouter() {
18
- return createTanStackRouter({
22
+ const queryClient = createQueryClient();
23
+ const router = createTanStackRouter({
19
24
  routeTree,
20
25
  defaultPreload: 'intent',
21
26
  defaultStaleTime: 0,
27
+ defaultPreloadStaleTime: 0,
22
28
  scrollRestoration: true,
23
29
  defaultNotFoundComponent: NotFound,
24
30
  defaultErrorComponent: RouteError,
25
31
  ssr: { nonce: cspNonce() },
26
- context: { client: requestClient() },
32
+ context: { client: requestClient(), queryClient },
27
33
  });
34
+ setupRouterSsrQueryIntegration({ router, queryClient });
35
+ return router;
28
36
  }
29
37
 
30
38
  /**
@@ -66,7 +74,7 @@ function cspNonce(): string | undefined {
66
74
  type RequestContext = { cspNonce: string; client: CoffreClient };
67
75
 
68
76
  /** What every loader and component can reach through the router. */
69
- export type RouterContext = { client: CoffreClient };
77
+ export type RouterContext = { client: CoffreClient; queryClient: QueryClient };
70
78
 
71
79
  // Start's server entry reads this `Register`, and its context helpers the
72
80
  // one below: the two do not merge, so each hears of the request context.
@@ -1,4 +1,3 @@
1
- import { CoffreError, type CoffreClient } from '@coffre/client';
2
1
  import type { ReactNode } from 'react';
3
2
  import {
4
3
  createRootRouteWithContext,
@@ -11,13 +10,14 @@ import {
11
10
  } from '@tanstack/react-router';
12
11
  import { Toaster } from 'sonner';
13
12
  import globalsCss from '../styles/globals.css?url';
14
- import { deriveUiCapabilities } from '../lib/capabilities';
13
+ import { loadShell } from '../lib/queries';
14
+ import { useShell } from '../lib/use-shell';
15
15
  import type { RouterContext } from '../router';
16
- import type { Me } from '../shared/models';
17
16
  import { Brand, Shell, sidebarBootScript } from '../components/shell';
18
17
  import { TooltipProvider } from '../components/ui';
19
18
  import { ThemeToggle, themeBootScript } from '../components/theme';
20
19
  import { Agentation } from '../components/agentation';
20
+ import { LiveRegion } from '../components/row-state';
21
21
  import { markSvg } from '../components/mark';
22
22
  import faviconPng from '../assets/favicon-32.png?url';
23
23
  import appleTouchIcon from '../assets/apple-touch-icon.png?url';
@@ -43,8 +43,8 @@ export const Route = createRootRouteWithContext<RouterContext>()({
43
43
  }),
44
44
 
45
45
  // Identity and the project tree, which the shell needs on every screen.
46
- loader: async ({ context: { client }, location }) => {
47
- const shell = await loadShell(client);
46
+ loader: async ({ context: { client, queryClient }, location }) => {
47
+ const shell = await loadShell(queryClient, client);
48
48
 
49
49
  if (shell.registrationRequired && location.pathname !== '/unregistered') {
50
50
  throw redirect({ to: '/unregistered' });
@@ -70,44 +70,12 @@ export const Route = createRootRouteWithContext<RouterContext>()({
70
70
  ) {
71
71
  throw redirect({ to: '/projects' });
72
72
  }
73
-
74
- return shell;
75
73
  },
76
74
 
77
75
  shellComponent: RootDocument,
78
76
  component: RootComponent,
79
77
  });
80
78
 
81
- /**
82
- * Who is looking, how this instance signs people in, and the projects they
83
- * can see. Three calls at once: for someone signed out or not yet a member,
84
- * the project list is refused, and they get the sign-in or closed-door page.
85
- */
86
- async function loadShell(client: CoffreClient) {
87
- const [auth, me, projects] = await Promise.all([
88
- client.auth(),
89
- client.me().catch((error: unknown) => {
90
- if (error instanceof CoffreError && error.status === 401) return null;
91
- throw error;
92
- }),
93
- client.projects.list().then(
94
- ({ projects }) => projects,
95
- () => [],
96
- ),
97
- ]);
98
- const member: Me | null = me?.registered === true ? me : null;
99
- return {
100
- auth,
101
- principal: me === null ? null : me.principal,
102
- instanceRole: member?.instanceRole ?? null,
103
- projects,
104
- capabilities: deriveUiCapabilities(member, projects),
105
- registrationRequired: me !== null && !me.registered,
106
- /** A member the vault refuses: their record failed its integrity check. */
107
- accessTampered: me?.tampered === true,
108
- };
109
- }
110
-
111
79
  function RootDocument({ children }: { children: ReactNode }) {
112
80
  return (
113
81
  <html lang="en" suppressHydrationWarning>
@@ -150,7 +118,7 @@ function Toasts() {
150
118
  }
151
119
 
152
120
  function RootComponent() {
153
- const { principal, instanceRole, projects, capabilities } = Route.useLoaderData();
121
+ const { principal, instanceRole, projects, capabilities } = useShell();
154
122
  const pathname = useRouterState({ select: (state) => state.location.pathname });
155
123
 
156
124
  // Sign-in gets no navigation. Every destination in the sidebar and the
@@ -171,6 +139,7 @@ function RootComponent() {
171
139
  </main>
172
140
  </div>
173
141
  <Toasts />
142
+ <LiveRegion />
174
143
  <Agentation />
175
144
  </TooltipProvider>
176
145
  );
@@ -192,6 +161,7 @@ function RootComponent() {
192
161
  </Shell>
193
162
 
194
163
  <Toasts />
164
+ <LiveRegion />
195
165
  <Agentation />
196
166
  </TooltipProvider>
197
167
  );
@@ -1,14 +1,19 @@
1
1
  import type { IdentityRow, SessionRow } from '@coffre/client';
2
- import { createFileRoute, useLoaderData } from '@tanstack/react-router';
3
- import { toast } from 'sonner';
2
+ import { useSuspenseQueries } from '@tanstack/react-query';
3
+ import { createFileRoute } from '@tanstack/react-router';
4
+ import { Fragment } from 'react';
5
+ import { useShell } from '../lib/use-shell';
4
6
  import { Card, Fact, PageHeader } from '../components/page';
5
7
  import { ThemeCards } from '../components/theme';
6
8
  import { InstanceRole } from '../components/directory';
7
9
  import { ConfirmButton, EmptyState, ErrorLine, Notice, Timestamp } from '../components/ui';
8
10
  import { Monitor, ProviderMark, SignOut, Terminal, X } from '../components/icons';
9
11
  import { signinErrorMessage } from '../lib/signin-errors';
10
- import { uiResult, useCoffre } from '../lib/coffre';
11
- import { useAction } from '../lib/use-action';
12
+ import { useCoffre } from '../lib/coffre';
13
+ import { endSession, unlinkIdentity } from '../lib/changes';
14
+ import { loadShell, queries } from '../lib/queries';
15
+ import { useChange, useChangeStatus } from '../lib/use-change';
16
+ import { RowFailure, RowPending, rowClass } from '../components/row-state';
12
17
 
13
18
  type Search = { linked?: string; error?: string };
14
19
 
@@ -27,32 +32,21 @@ export const Route = createFileRoute('/account')({
27
32
  }
28
33
  return out;
29
34
  },
30
- // The sign-in half: which accounts you sign in with, where you are signed
31
- // in, and which providers you could link. Absent behind Cloudflare Access,
32
- // which owns sessions there.
33
- loader: async ({ context: { client }, parentMatchPromise }) => {
34
- const auth = (await parentMatchPromise).loaderData?.auth;
35
- if (auth === undefined || auth.signin === null) {
36
- return {
37
- ok: true as const,
38
- signin: false,
39
- providers: [] as { id: string; label: string; brand: string }[],
40
- identities: [] as IdentityRow[],
41
- sessions: [] as SessionRow[],
42
- };
43
- }
44
- const { providers } = auth.signin;
45
- return uiResult(async () => {
46
- const [{ identities }, { sessions }] = await Promise.all([client.identities.list(), client.sessions.list()]);
47
- return { signin: true, providers, identities, sessions };
48
- });
35
+ // The sign-in half: which accounts you sign in with and where you are
36
+ // signed in. Absent behind Cloudflare Access, which owns sessions there.
37
+ loader: async ({ context: { client, queryClient } }) => {
38
+ const shell = await loadShell(queryClient, client);
39
+ if (shell.auth.signin === null || shell.principal?.type !== 'user') return;
40
+ await Promise.all([
41
+ queryClient.fetchQuery(queries.identities(client)),
42
+ queryClient.fetchQuery(queries.sessions(client)),
43
+ ]);
49
44
  },
50
45
  component: AccountPage,
51
46
  });
52
47
 
53
48
  function AccountPage() {
54
- const { principal, instanceRole } = useLoaderData({ from: '__root__' });
55
- const signin = Route.useLoaderData();
49
+ const { principal, instanceRole, auth } = useShell();
56
50
 
57
51
  return (
58
52
  <>
@@ -87,21 +81,29 @@ function AccountPage() {
87
81
  </Card>
88
82
  )}
89
83
 
90
- {!signin.ok ? (
91
- <ErrorLine error={signin.error} />
92
- ) : (
93
- signin.signin &&
94
- principal?.type === 'user' && (
95
- <>
96
- <SigninAccounts email={principal.id} {...signin} />
97
- <Sessions sessions={signin.sessions} providers={signin.providers} />
98
- </>
99
- )
84
+ {auth.signin !== null && principal?.type === 'user' && (
85
+ <SignIn email={principal.id} providers={auth.signin.providers} />
100
86
  )}
101
87
  </>
102
88
  );
103
89
  }
104
90
 
91
+ /** Which accounts you sign in with, and where you are signed in. */
92
+ function SignIn({ email, providers }: { email: string; providers: Provider[] }) {
93
+ const client = useCoffre();
94
+ const [{ data: identities }, { data: sessions }] = useSuspenseQueries({
95
+ queries: [queries.identities(client), queries.sessions(client)],
96
+ });
97
+ if (!identities.ok) return <ErrorLine error={identities.error} />;
98
+ if (!sessions.ok) return <ErrorLine error={sessions.error} />;
99
+ return (
100
+ <>
101
+ <SigninAccounts email={email} providers={providers} identities={identities.identities} />
102
+ <Sessions sessions={sessions.sessions} providers={providers} />
103
+ </>
104
+ );
105
+ }
106
+
105
107
  /**
106
108
  * Sign-out is a form post: a GET could be triggered by any image tag on any
107
109
  * page, and the server's origin check covers form posts.
@@ -134,8 +136,9 @@ function SigninAccounts({
134
136
  }) {
135
137
  const { linked, error } = Route.useSearch();
136
138
  const message = signinErrorMessage(error);
137
- const coffre = useCoffre();
138
- const { pending, error: actionError, run } = useAction();
139
+ const change = unlinkIdentity(useCoffre());
140
+ const unlink = useChange(change);
141
+ const { status, dismiss } = useChangeStatus(change.list.queryKey);
139
142
 
140
143
  return (
141
144
  <Card
@@ -143,12 +146,12 @@ function SigninAccounts({
143
146
  title="Sign-in accounts"
144
147
  description="coffre recognises you by these accounts, never by an email address alone. A new account is linked here, while you are signed in."
145
148
  >
146
- {(linked !== undefined || message !== null || actionError !== null) && (
149
+ {(linked !== undefined || message !== null) && (
147
150
  <div className="card-body">
148
151
  {linked !== undefined && (
149
152
  <Notice tone="good">{providerLabel(providers, linked)} account linked.</Notice>
150
153
  )}
151
- <ErrorLine error={message ?? actionError} />
154
+ <ErrorLine error={message} />
152
155
  </div>
153
156
  )}
154
157
 
@@ -174,61 +177,68 @@ function SigninAccounts({
174
177
  {identities.map((identity) => {
175
178
  const label = providerLabel(providers, identity.provider);
176
179
  const brand = providers.find((p) => p.id === identity.provider)?.brand ?? 'oidc';
180
+ const state = status(identity.id);
177
181
  return (
178
- <tr key={identity.id}>
179
- <td>
180
- <span className="cell-account">
181
- <ProviderMark brand={brand} />
182
- <span className="cell-stack">
183
- <span>{label}</span>
184
- {identity.email !== null && <small className="mono">{identity.email}</small>}
182
+ <Fragment key={identity.id}>
183
+ <tr className={rowClass(state)}>
184
+ <td>
185
+ <span className="cell-account">
186
+ <ProviderMark brand={brand} />
187
+ <span className="cell-stack">
188
+ <span>{label}</span>
189
+ {identity.email !== null && <small className="mono">{identity.email}</small>}
190
+ </span>
185
191
  </span>
186
- </span>
187
- </td>
188
- <td className="nowrap">
189
- <Timestamp iso={identity.createdAt} />
190
- </td>
191
- <td className="nowrap cell-muted">
192
- {identity.lastSignInAt === null ? (
193
- 'Never'
194
- ) : (
195
- <Timestamp iso={identity.lastSignInAt} display="relative" />
196
- )}
197
- </td>
198
- <td className="col-actions">
199
- <ConfirmButton
200
- trigger={
201
- <button className="act" disabled={pending}>
202
- <X size={13} />
203
- Unlink
204
- </button>
205
- }
206
- title={`Unlink this ${label} account?`}
207
- body={
208
- identities.length === 1 ? (
209
- <>
210
- It stops signing you in, and every session it opened ends now,
211
- this one included if you signed in with it. Your next sign-in
212
- with any account whose verified email is{' '}
213
- <span className="mono">{email}</span> links that account instead.
214
- </>
215
- ) : (
216
- <>
217
- It stops signing you in, and every session it opened ends now,
218
- this one included if you signed in with it.
219
- </>
220
- )
221
- }
222
- confirmLabel="Unlink"
223
- onConfirm={() =>
224
- run(
225
- () => coffre.identities.unlink(identity.id),
226
- () => toast.success(`${label} account unlinked`),
227
- )
228
- }
229
- />
230
- </td>
231
- </tr>
192
+ </td>
193
+ <td className="nowrap">
194
+ <Timestamp iso={identity.createdAt} />
195
+ </td>
196
+ <td className="nowrap cell-muted">
197
+ {identity.lastSignInAt === null ? (
198
+ 'Never'
199
+ ) : (
200
+ <Timestamp iso={identity.lastSignInAt} display="relative" />
201
+ )}
202
+ </td>
203
+ <td className="col-actions">
204
+ {state.state === 'pending' ? (
205
+ <RowPending status={state} />
206
+ ) : (
207
+ <ConfirmButton
208
+ trigger={
209
+ <button className="act">
210
+ <X size={13} />
211
+ Unlink
212
+ </button>
213
+ }
214
+ title={`Unlink this ${label} account?`}
215
+ body={
216
+ identities.length === 1 ? (
217
+ <>
218
+ It stops signing you in, and every session it opened ends now,
219
+ this one included if you signed in with it. Your next sign-in
220
+ with any account whose verified email is{' '}
221
+ <span className="mono">{email}</span> links that account instead.
222
+ </>
223
+ ) : (
224
+ <>
225
+ It stops signing you in, and every session it opened ends now,
226
+ this one included if you signed in with it.
227
+ </>
228
+ )
229
+ }
230
+ confirmLabel="Unlink"
231
+ onConfirm={() => unlink({ ...identity, label })}
232
+ />
233
+ )}
234
+ </td>
235
+ </tr>
236
+ <RowFailure
237
+ status={state}
238
+ columns={4}
239
+ onDismiss={() => state.state === 'failed' && dismiss(state.mutationId)}
240
+ />
241
+ </Fragment>
232
242
  );
233
243
  })}
234
244
  </tbody>
@@ -264,8 +274,9 @@ function Sessions({
264
274
  sessions: SessionRow[];
265
275
  providers: Provider[];
266
276
  }) {
267
- const coffre = useCoffre();
268
- const { pending, error, run } = useAction();
277
+ const change = endSession(useCoffre());
278
+ const end = useChange(change);
279
+ const { status, dismiss } = useChangeStatus(change.list.queryKey);
269
280
 
270
281
  return (
271
282
  <Card
@@ -273,11 +284,6 @@ function Sessions({
273
284
  title="Sessions"
274
285
  description="Browsers and command lines signed in as you. Ending one signs it out at its next request."
275
286
  >
276
- {error !== null && (
277
- <div className="card-body">
278
- <ErrorLine error={error} />
279
- </div>
280
- )}
281
287
  {sessions.length === 0 ? (
282
288
  <EmptyState title="No sessions">Nothing is signed in as you right now.</EmptyState>
283
289
  ) : (
@@ -294,62 +300,65 @@ function Sessions({
294
300
  </tr>
295
301
  </thead>
296
302
  <tbody>
297
- {sessions.map((session) => (
298
- <tr key={session.id}>
299
- <td>
300
- <span className="cell-account">
301
- {session.kind === 'cli' ? <Terminal size={15} /> : <Monitor size={15} />}
302
- <span className="cell-stack">
303
- <span>
304
- {session.label ?? (session.kind === 'cli' ? 'Command line' : 'Browser')}{' '}
305
- {session.current && <span className="tag tag-blue">this browser</span>}
303
+ {sessions.map((session) => {
304
+ const state = status(session.id);
305
+ return (
306
+ <Fragment key={session.id}>
307
+ <tr className={rowClass(state)}>
308
+ <td>
309
+ <span className="cell-account">
310
+ {session.kind === 'cli' ? <Terminal size={15} /> : <Monitor size={15} />}
311
+ <span className="cell-stack">
312
+ <span>
313
+ {session.label ?? (session.kind === 'cli' ? 'Command line' : 'Browser')}{' '}
314
+ {session.current && <span className="tag tag-blue">this browser</span>}
315
+ </span>
316
+ <small>
317
+ {session.kind === 'cli'
318
+ ? 'coffre login'
319
+ : `via ${providerLabel(providers, session.provider)}`}
320
+ {session.lastUsedIp !== null && (
321
+ <>
322
+ {' · '}
323
+ <span className="mono">{session.lastUsedIp}</span>
324
+ </>
325
+ )}
326
+ </small>
327
+ </span>
306
328
  </span>
307
- <small>
308
- {session.kind === 'cli'
309
- ? 'coffre login'
310
- : `via ${providerLabel(providers, session.provider)}`}
311
- {session.lastUsedIp !== null && (
312
- <>
313
- {' · '}
314
- <span className="mono">{session.lastUsedIp}</span>
315
- </>
316
- )}
317
- </small>
318
- </span>
319
- </span>
320
- </td>
321
- <td className="nowrap cell-muted">
322
- <Timestamp iso={session.lastUsedAt ?? session.createdAt} display="relative" />
323
- </td>
324
- <td className="nowrap cell-muted">
325
- <Timestamp iso={session.expiresAt} display="relative" />
326
- </td>
327
- <td className="col-actions">
328
- {session.current ? (
329
- <form method="post" action="/auth/signout">
330
- <button className="act" type="submit">
331
- <SignOut size={13} />
332
- Sign out
333
- </button>
334
- </form>
335
- ) : (
336
- <button
337
- className="act"
338
- disabled={pending}
339
- onClick={() =>
340
- run(
341
- () => coffre.sessions.revoke(session.id),
342
- () => toast.success('Session ended'),
343
- )
344
- }
345
- >
346
- <X size={13} />
347
- End
348
- </button>
349
- )}
350
- </td>
351
- </tr>
352
- ))}
329
+ </td>
330
+ <td className="nowrap cell-muted">
331
+ <Timestamp iso={session.lastUsedAt ?? session.createdAt} display="relative" />
332
+ </td>
333
+ <td className="nowrap cell-muted">
334
+ <Timestamp iso={session.expiresAt} display="relative" />
335
+ </td>
336
+ <td className="col-actions">
337
+ {session.current ? (
338
+ <form method="post" action="/auth/signout">
339
+ <button className="act" type="submit">
340
+ <SignOut size={13} />
341
+ Sign out
342
+ </button>
343
+ </form>
344
+ ) : state.state === 'pending' ? (
345
+ <RowPending status={state} />
346
+ ) : (
347
+ <button className="act" onClick={() => end(session)}>
348
+ <X size={13} />
349
+ End
350
+ </button>
351
+ )}
352
+ </td>
353
+ </tr>
354
+ <RowFailure
355
+ status={state}
356
+ columns={4}
357
+ onDismiss={() => state.state === 'failed' && dismiss(state.mutationId)}
358
+ />
359
+ </Fragment>
360
+ );
361
+ })}
353
362
  </tbody>
354
363
  </table>
355
364
  </div>