@coffre/ui 0.1.10 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/_coffre/assets/account-BJAdDvZf.js +2 -0
- package/dist/client/_coffre/assets/account-D0jLpk5c.js +1 -0
- package/dist/client/_coffre/assets/affordances-Bq2-oUB5.js +1 -0
- package/dist/client/_coffre/assets/audit-C-pcBgKe.js +2 -0
- package/dist/client/_coffre/assets/audit-CHGlDeBB.js +1 -0
- package/dist/client/_coffre/assets/{auth.device-BP5FY0K1.js → auth.device-BndOUjse.js} +1 -1
- package/dist/client/_coffre/assets/auth.device-CWHNQn9W.js +2 -0
- package/dist/client/_coffre/assets/directory-nDExpkAz.js +1 -0
- package/dist/client/_coffre/assets/dist-QVHMLatb.js +1 -0
- package/dist/client/_coffre/assets/globals-CKBoTlyH.css +1 -0
- package/dist/client/_coffre/assets/grants-CjoOnxzz.js +1 -0
- package/dist/client/_coffre/assets/icons-BQ27Ds9u.js +1 -0
- package/dist/client/_coffre/assets/index-CUGytaO3.js +12 -0
- package/dist/client/_coffre/assets/link-DP8SVWon.js +1 -0
- package/dist/client/_coffre/assets/login-CeaxDMcr.js +2 -0
- package/dist/client/_coffre/assets/login-DrMgvqZn.js +1 -0
- package/dist/client/_coffre/assets/offboarding-CNr_1tnl.js +1 -0
- package/dist/client/_coffre/assets/page-CGmvjSy4.js +1 -0
- package/dist/client/_coffre/assets/preload-helper-ClBkJ19f.js +1 -0
- package/dist/client/_coffre/assets/principal-page-DNLb_q_e.js +1 -0
- package/dist/client/_coffre/assets/projects._project._environment-CHzbboGp.js +3 -0
- package/dist/client/_coffre/assets/projects._project._environment-DDxu_HHD.js +2 -0
- package/dist/client/_coffre/assets/projects._project.index-CIAsjvg6.js +2 -0
- package/dist/client/_coffre/assets/projects._project.index-DZ9l6d-1.js +1 -0
- package/dist/client/_coffre/assets/projects.index-4eUXreE_.js +1 -0
- package/dist/client/_coffre/assets/row-state-ByET1OKX.js +1 -0
- package/dist/client/_coffre/assets/settings-D2zp1Ssu.js +1 -0
- package/dist/client/_coffre/assets/suspense-C7KwSnTG.js +1 -0
- package/dist/client/_coffre/assets/tokens._token-DWmbqzNk.js +2 -0
- package/dist/client/_coffre/assets/tokens._token-Dui_4KfS.js +1 -0
- package/dist/client/_coffre/assets/tokens.index-CSi5Wxwf.js +1 -0
- package/dist/client/_coffre/assets/ui-BdipqGpP.js +46 -0
- package/dist/client/_coffre/assets/unregistered-CA7B4C1m.js +1 -0
- package/dist/client/_coffre/assets/use-action-DRm9yVhV.js +1 -0
- package/dist/client/_coffre/assets/use-change-BRRMJocl.js +1 -0
- package/dist/client/_coffre/assets/use-shell-BMGlJ25Q.js +1 -0
- package/dist/client/_coffre/assets/useQuery-D__2KBOs.js +1 -0
- package/dist/client/_coffre/assets/useSuspenseQuery-CSXyDALB.js +1 -0
- package/dist/client/_coffre/assets/users._user-BkNrX7Qf.js +1 -0
- package/dist/client/_coffre/assets/users._user-jbSvtdOg.js +2 -0
- package/dist/client/_coffre/assets/users.index-CKnQ1G76.js +1 -0
- package/dist/server/.vite/manifest.json +343 -246
- package/dist/server/_coffre/assets/_tanstack-start-manifest_v-Dx-c3XVk.js +162 -0
- package/dist/server/_coffre/assets/{account-CUQYGCYh.js → account-BocTPU1J.js} +150 -117
- package/dist/server/_coffre/assets/{account-DMlUoBxQ.js → account-CL8hOGhO.js} +10 -24
- package/dist/server/_coffre/assets/{affordances-BNaXjMOb.js → affordances-CKANXDTG.js} +2 -22
- package/dist/server/_coffre/assets/{async-local-storage-C0cwgkEr.js → async-local-storage-C10uQtMM.js} +3 -2
- package/dist/server/_coffre/assets/{audit-CCdSLmvK.js → audit-Bcka4vA6.js} +46 -16
- package/dist/server/_coffre/assets/audit-CSRQYIhu.js +31 -0
- package/dist/server/_coffre/assets/{auth.device-C05u5ROO.js → auth.device-CMcUISZY.js} +7 -5
- package/dist/server/_coffre/assets/{auth.device-BoFM1jcf.js → auth.device-DqO0T-rU.js} +5 -5
- package/dist/server/_coffre/assets/{directory-GsnYkse0.js → directory-CyYx6Jx1.js} +87 -112
- package/dist/server/_coffre/assets/{use-action-DwEmf58L.js → dist-CnnV3t2g.js} +3 -48
- package/dist/server/_coffre/assets/globals-CKBoTlyH.css +1 -0
- package/dist/server/_coffre/assets/grants-CRijabX0.js +158 -0
- package/dist/server/_coffre/assets/{page-BdnhpwvH.js → icons-Dy-5ccDF.js} +3 -196
- package/dist/server/_coffre/assets/lazyRouteComponent-CqBNCGuZ.js +480 -0
- package/dist/server/_coffre/assets/link-42ZWdjzZ.js +422 -0
- package/dist/server/_coffre/assets/{login-DUtCQju5.js → login-BI9tzJEy.js} +7 -5
- package/dist/server/_coffre/assets/{login-B2njIq_o.js → login-C-v88YwP.js} +2 -2
- package/dist/server/_coffre/assets/matchContext-qLfYbC6_.js +56 -0
- package/dist/server/_coffre/assets/{offboarding-8yMa7ViM.js → offboarding-1H0LavTd.js} +6 -4
- package/dist/server/_coffre/assets/page-D_mO0-sn.js +3592 -0
- package/dist/server/_coffre/assets/{principal-page-B8zPcS2b.js → principal-page-DQDoXute.js} +51 -42
- package/dist/server/_coffre/assets/{projects._project._environment-BZ5Z80V8.js → projects._project._environment-C-yVwC92.js} +168 -248
- package/dist/server/_coffre/assets/{projects._project._environment-DdIOc7wx.js → projects._project._environment-D_5R40of.js} +4 -11
- package/dist/server/_coffre/assets/{projects._project.index-CuEAUwGn.js → projects._project.index-A2BImFIT.js} +134 -96
- package/dist/server/_coffre/assets/{projects._project.index-DvbcU1kh.js → projects._project.index-BPPKxKrt.js} +5 -4
- package/dist/server/_coffre/assets/{projects.index-5HI0wOuG.js → projects.index-543IsPzY.js} +57 -55
- package/dist/server/_coffre/assets/{react-dom-qCTiwGGR.js → react-dom-bBoJXlK2.js} +417 -51
- package/dist/server/_coffre/assets/{router-CQDoql8s.js → router-CM3vK8QO.js} +376 -92
- package/dist/server/_coffre/assets/row-state-XBtDoIA4.js +117 -0
- package/dist/server/_coffre/assets/{settings-BVz78wjP.js → settings-BLVU74ym.js} +8 -6
- package/dist/server/_coffre/assets/suspense-CzYTwxOj.js +602 -0
- package/dist/server/_coffre/assets/tokens._token-B-hBgwBW.js +10 -0
- package/dist/server/_coffre/assets/{tokens._token-CWs3Mtki.js → tokens._token-aubAUonP.js} +97 -79
- package/dist/server/_coffre/assets/{tokens.index-Ck0LN7aC.js → tokens.index-4D3jwp5v.js} +9 -6
- package/dist/server/_coffre/assets/{ui-D_slKpSF.js → ui-BI3Bf72q.js} +7 -836
- package/dist/server/_coffre/assets/{unregistered-Z17da4Rc.js → unregistered-CagDxMyw.js} +5 -3
- package/dist/server/_coffre/assets/use-action-tzBZvjjQ.js +47 -0
- package/dist/server/_coffre/assets/use-change-Dggqxy5y.js +1212 -0
- package/dist/server/_coffre/assets/use-shell-CwStDfzX.js +462 -0
- package/dist/server/_coffre/assets/useQuery-C6_dBjem.js +8 -0
- package/dist/server/_coffre/assets/{useStore-DWGPqzXR.js → useRouter-CaUSUNsw.js} +1 -420
- package/dist/server/_coffre/assets/useSuspenseQuery-BuapQt_P.js +117 -0
- package/dist/server/_coffre/assets/{users._user-CMwj-VI9.js → users._user-137WNyxn.js} +4 -5
- package/dist/server/_coffre/assets/users._user-C252Iilj.js +10 -0
- package/dist/server/_coffre/assets/{users.index-fWHv6A30.js → users.index-DQHsfnal.js} +9 -6
- package/dist/server/index.js +6 -5
- package/package.json +5 -3
- package/src/components/directory.tsx +75 -104
- package/src/components/grants.tsx +60 -112
- package/src/components/principal-page.tsx +54 -67
- package/src/components/principal-picker.tsx +7 -16
- package/src/components/row-state.tsx +95 -0
- package/src/components/service-tokens.tsx +75 -64
- package/src/components/shell.tsx +1 -1
- package/src/components/syncs.tsx +42 -26
- package/src/lib/announce.ts +36 -0
- package/src/lib/changes.ts +498 -0
- package/src/lib/optimistic.ts +233 -0
- package/src/lib/queries.ts +393 -0
- package/src/lib/use-action.ts +20 -15
- package/src/lib/use-change.ts +42 -0
- package/src/lib/use-shell.ts +17 -0
- package/src/router.tsx +17 -9
- package/src/routes/__root.tsx +8 -38
- package/src/routes/account.tsx +163 -154
- package/src/routes/audit.tsx +51 -72
- package/src/routes/auth.device.tsx +4 -2
- package/src/routes/login.tsx +3 -2
- package/src/routes/projects.$project.$environment.tsx +226 -231
- package/src/routes/projects.$project.index.tsx +136 -118
- package/src/routes/projects.index.tsx +117 -145
- package/src/routes/settings.tsx +7 -7
- package/src/routes/tokens.$token.tsx +14 -17
- package/src/routes/tokens.index.tsx +8 -4
- package/src/routes/unregistered.tsx +3 -2
- package/src/routes/users.$user.tsx +4 -4
- package/src/routes/users.index.tsx +8 -4
- package/src/styles/globals.css +78 -1
- package/dist/client/_coffre/assets/account-B48wuSF8.js +0 -1
- package/dist/client/_coffre/assets/account-BkNyIDT7.js +0 -2
- package/dist/client/_coffre/assets/affordances-CmoY-8Xf.js +0 -1
- package/dist/client/_coffre/assets/audit-BvwKYb-5.js +0 -2
- package/dist/client/_coffre/assets/audit-D02H-zxS.js +0 -1
- package/dist/client/_coffre/assets/auth.device-DYOONgFF.js +0 -2
- package/dist/client/_coffre/assets/coffre-BBp7cKVa.js +0 -1
- package/dist/client/_coffre/assets/directory-Bu0EIxqI.js +0 -1
- package/dist/client/_coffre/assets/globals-Bh2x1gBM.css +0 -1
- package/dist/client/_coffre/assets/grants-DavcwofW.js +0 -1
- package/dist/client/_coffre/assets/index-CDPlq5rM.js +0 -12
- package/dist/client/_coffre/assets/login-C-wTm_t1.js +0 -2
- package/dist/client/_coffre/assets/login-CLLv1om9.js +0 -1
- package/dist/client/_coffre/assets/offboarding-DWsE2FSa.js +0 -1
- package/dist/client/_coffre/assets/page-DbSS7pYH.js +0 -1
- package/dist/client/_coffre/assets/preload-helper-DduCCcmc.js +0 -46
- package/dist/client/_coffre/assets/principal-page-Bp_iwuSd.js +0 -1
- package/dist/client/_coffre/assets/projects._project._environment-BMeX_B8l.js +0 -2
- package/dist/client/_coffre/assets/projects._project._environment-CT0A-hIU.js +0 -3
- package/dist/client/_coffre/assets/projects._project.index-C_xYOGLT.js +0 -2
- package/dist/client/_coffre/assets/projects._project.index-Chcf-cUd.js +0 -1
- package/dist/client/_coffre/assets/projects.index-CfOtb1X5.js +0 -2
- package/dist/client/_coffre/assets/projects.index-DSjOhLDh.js +0 -1
- package/dist/client/_coffre/assets/settings-BC9cUJNK.js +0 -1
- package/dist/client/_coffre/assets/settings-Bkj_E21y.js +0 -2
- package/dist/client/_coffre/assets/tokens._token-BA0KqvaW.js +0 -1
- package/dist/client/_coffre/assets/tokens._token-Dh9jLOkm.js +0 -2
- package/dist/client/_coffre/assets/tokens.index-BgAMWkHT.js +0 -2
- package/dist/client/_coffre/assets/tokens.index-CHueIAv1.js +0 -1
- package/dist/client/_coffre/assets/unregistered-D5go1AfR.js +0 -1
- package/dist/client/_coffre/assets/use-action-l__xZDYY.js +0 -1
- package/dist/client/_coffre/assets/users._user-Dwqa0gv5.js +0 -1
- package/dist/client/_coffre/assets/users._user-xJAb3T6Y.js +0 -2
- package/dist/client/_coffre/assets/users.index-BJoF-L37.js +0 -2
- package/dist/client/_coffre/assets/users.index-YUjtbTGy.js +0 -1
- package/dist/server/_coffre/assets/_tanstack-start-manifest_v-BKg2AlPu.js +0 -133
- package/dist/server/_coffre/assets/audit-CThXn2v7.js +0 -74
- package/dist/server/_coffre/assets/coffre-C2ANsTAz.js +0 -281
- package/dist/server/_coffre/assets/globals-Bh2x1gBM.css +0 -1
- package/dist/server/_coffre/assets/grants-B6k4Fu9z.js +0 -243
- package/dist/server/_coffre/assets/projects.index-uiomWX_l.js +0 -54
- package/dist/server/_coffre/assets/settings-4oRyDizv.js +0 -18
- package/dist/server/_coffre/assets/tokens._token-CF_deAwq.js +0 -18
- package/dist/server/_coffre/assets/tokens.index-DF_07KEe.js +0 -10
- package/dist/server/_coffre/assets/users._user-DUzwkSKT.js +0 -10
- package/dist/server/_coffre/assets/users.index-BbuweTwH.js +0 -10
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { Mutation, MutationOptions, QueryClient, QueryKey } from '@tanstack/react-query';
|
|
2
|
+
|
|
3
|
+
import { announce } from './announce.ts';
|
|
4
|
+
import { failureMessage } from './coffre.ts';
|
|
5
|
+
import { refresh } from './queries.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Changes that show before the server answers.
|
|
9
|
+
*
|
|
10
|
+
* An add or an edit shows at once, as it will be, marked as saving; a removal
|
|
11
|
+
* keeps its row, struck through, until the server confirms it, since access
|
|
12
|
+
* must never look gone while it still holds. Either way, if the server
|
|
13
|
+
* refuses, the list goes back to what it was and the row says why
|
|
14
|
+
* (`failureMessage`: the API's own sentence where it gives one), until
|
|
15
|
+
* dismissed or retried.
|
|
16
|
+
*
|
|
17
|
+
* Several changes can be in flight at once, so a failure undoes its own
|
|
18
|
+
* items only, not the whole list as it was when it started; and the list is
|
|
19
|
+
* read back from the server once the last change to it has landed, so one
|
|
20
|
+
* landing does not erase another still on its way.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** What a row is waiting for. */
|
|
24
|
+
export type Pending = 'saving' | 'removing';
|
|
25
|
+
|
|
26
|
+
/** One item a change touches, and how. */
|
|
27
|
+
export type Target = { id: string; kind: Pending };
|
|
28
|
+
|
|
29
|
+
/** How a change is worded on its row: "Archiving…", "Not archived." */
|
|
30
|
+
export type Words = { pending: string; done: string; failed: string };
|
|
31
|
+
|
|
32
|
+
const WORDS: Record<Pending, Words> = {
|
|
33
|
+
saving: { pending: 'Saving…', done: 'saved', failed: 'Not saved.' },
|
|
34
|
+
removing: { pending: 'Removing…', done: 'removed', failed: 'Not removed.' },
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** How one cached query holds a list of items, each with an identity. */
|
|
38
|
+
export type ListShape<TData, TItem> = {
|
|
39
|
+
queryKey: QueryKey;
|
|
40
|
+
items: (data: TData) => TItem[];
|
|
41
|
+
withItems: (data: TData, items: TItem[]) => TData;
|
|
42
|
+
id: (item: TItem) => string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type Change<TData, TItem, TVars, TResult> = {
|
|
46
|
+
list: ListShape<TData, TItem>;
|
|
47
|
+
/** What the change is called, for the screen reader: "access for dev@acme.example". */
|
|
48
|
+
label: (vars: TVars) => string;
|
|
49
|
+
/**
|
|
50
|
+
* The items it touches, whose rows show it: saved ones as they will be,
|
|
51
|
+
* removed ones as they are until confirmed. A rename touches both names.
|
|
52
|
+
*/
|
|
53
|
+
targets: (vars: TVars) => Target[];
|
|
54
|
+
/** How removals are worded, when not as removals: archiving, revoking. */
|
|
55
|
+
removing?: Words;
|
|
56
|
+
/** The items as they will be once the saves land; removals are left for the server. */
|
|
57
|
+
apply?: (items: TItem[], vars: TVars) => TItem[];
|
|
58
|
+
/** The items once the server confirms; by default, without those removed. Archiving keeps them, archived. */
|
|
59
|
+
confirmed?: (items: TItem[], vars: TVars, removed: ReadonlySet<string>) => TItem[];
|
|
60
|
+
/** Anything else it touches, refetched once it lands (`affects` in `queries.ts`). */
|
|
61
|
+
affects: (vars: TVars, result: TResult | undefined) => QueryKey[];
|
|
62
|
+
run: (vars: TVars) => Promise<TResult>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** What `onMutate` keeps: the list as it was, and what the rows should show. */
|
|
66
|
+
export type ChangeContext = { previous: unknown; targets: Target[]; words: Record<Pending, Words> };
|
|
67
|
+
|
|
68
|
+
/** Every optimistic change is keyed under this, then its list's key. */
|
|
69
|
+
export const CHANGE = 'change';
|
|
70
|
+
|
|
71
|
+
export function changeKey(list: QueryKey): QueryKey {
|
|
72
|
+
return [CHANGE, ...list];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function changeOptions<TData, TItem, TVars, TResult>(
|
|
76
|
+
queryClient: QueryClient,
|
|
77
|
+
change: Change<TData, TItem, TVars, TResult>,
|
|
78
|
+
): MutationOptions<TResult, unknown, TVars, ChangeContext> {
|
|
79
|
+
const { list } = change;
|
|
80
|
+
const mutationKey = changeKey(list.queryKey);
|
|
81
|
+
return {
|
|
82
|
+
mutationKey,
|
|
83
|
+
mutationFn: change.run,
|
|
84
|
+
onMutate: async (vars) => {
|
|
85
|
+
const targets = change.targets(vars);
|
|
86
|
+
const words = { saving: WORDS.saving, removing: change.removing ?? WORDS.removing };
|
|
87
|
+
announce(`${change.label(vars)}: ${words[mainKind(targets)].pending}`);
|
|
88
|
+
// A read already on its way would land over the change.
|
|
89
|
+
await queryClient.cancelQueries({ queryKey: list.queryKey });
|
|
90
|
+
const previous = queryClient.getQueryData<TData>(list.queryKey);
|
|
91
|
+
if (change.apply !== undefined && previous !== undefined) {
|
|
92
|
+
const apply = change.apply;
|
|
93
|
+
queryClient.setQueryData<TData>(list.queryKey, (current) =>
|
|
94
|
+
current === undefined ? current : list.withItems(current, apply(list.items(current), vars)),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
return { previous, targets, words };
|
|
98
|
+
},
|
|
99
|
+
onError: (error, vars, context) => {
|
|
100
|
+
if (context === undefined) return;
|
|
101
|
+
announce(
|
|
102
|
+
`${change.label(vars)}: ${context.words[mainKind(context.targets)].failed} ${failureMessage(error)}`,
|
|
103
|
+
{ urgent: true },
|
|
104
|
+
);
|
|
105
|
+
if (context.previous === undefined || change.apply === undefined) return;
|
|
106
|
+
const previous = context.previous as TData;
|
|
107
|
+
const saved = context.targets.filter((target) => target.kind === 'saving').map((target) => target.id);
|
|
108
|
+
queryClient.setQueryData<TData>(list.queryKey, (current) =>
|
|
109
|
+
current === undefined ? current : revert(list, current, previous, saved),
|
|
110
|
+
);
|
|
111
|
+
},
|
|
112
|
+
onSuccess: (_result, vars, context) => {
|
|
113
|
+
if (context === undefined) return;
|
|
114
|
+
announce(`${capitalize(change.label(vars))} ${context.words[mainKind(context.targets)].done}`);
|
|
115
|
+
// Confirmed: the rows removed can go now, before the list is read back.
|
|
116
|
+
const gone = new Set(
|
|
117
|
+
context.targets.filter((target) => target.kind === 'removing').map((target) => target.id),
|
|
118
|
+
);
|
|
119
|
+
if (gone.size > 0) {
|
|
120
|
+
const confirmed =
|
|
121
|
+
change.confirmed ?? ((items: TItem[]) => items.filter((item) => !gone.has(list.id(item))));
|
|
122
|
+
queryClient.setQueryData<TData>(list.queryKey, (current) =>
|
|
123
|
+
current === undefined ? current : list.withItems(current, confirmed(list.items(current), vars, gone)),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
onSettled: (result, _error, vars) => {
|
|
128
|
+
// This one still counts as in flight here.
|
|
129
|
+
if (queryClient.isMutating({ mutationKey }) > 1) return;
|
|
130
|
+
// Not awaited: the row settles when the server answers, and the list
|
|
131
|
+
// is read back behind it.
|
|
132
|
+
void refresh(queryClient, [list.queryKey, ...change.affects(vars, result)]);
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The list with one change undone: each target as it was before, wherever
|
|
139
|
+
* it now is, and gone if it was not there. Everything else stays as it is,
|
|
140
|
+
* including other changes still in flight.
|
|
141
|
+
*/
|
|
142
|
+
export function revert<TData, TItem>(
|
|
143
|
+
list: ListShape<TData, TItem>,
|
|
144
|
+
current: TData,
|
|
145
|
+
previous: TData,
|
|
146
|
+
targets: string[],
|
|
147
|
+
): TData {
|
|
148
|
+
const before = list.items(previous);
|
|
149
|
+
let items = list.items(current).filter((item) => !targets.includes(list.id(item)));
|
|
150
|
+
for (const id of targets) {
|
|
151
|
+
const index = before.findIndex((item) => list.id(item) === id);
|
|
152
|
+
if (index === -1) continue;
|
|
153
|
+
// Back where it was, counting only what is still there before it.
|
|
154
|
+
const after = before
|
|
155
|
+
.slice(0, index)
|
|
156
|
+
.filter((item) => items.some((kept) => list.id(kept) === list.id(item))).length;
|
|
157
|
+
items = [...items.slice(0, after), before[index]!, ...items.slice(after)];
|
|
158
|
+
}
|
|
159
|
+
return list.withItems(current, items);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** What a change mostly does, to word it as a whole: a save, unless it only removes. */
|
|
163
|
+
function mainKind(targets: Target[]): Pending {
|
|
164
|
+
return targets.length > 0 && targets.every((target) => target.kind === 'removing') ? 'removing' : 'saving';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** What a row shows: nothing, a change on its way, or one the server refused. */
|
|
168
|
+
export type ItemStatus =
|
|
169
|
+
| { state: 'idle' }
|
|
170
|
+
| { state: 'pending'; kind: Pending; words: Words }
|
|
171
|
+
| { state: 'failed'; kind: Pending; words: Words; error: string; mutationId: number };
|
|
172
|
+
|
|
173
|
+
type Tracked = Pick<Mutation<unknown, unknown, unknown, ChangeContext>, 'mutationId' | 'state'>;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* An item's status, from the changes to its list: the latest one that
|
|
177
|
+
* touches it decides, so retrying clears an old failure.
|
|
178
|
+
*/
|
|
179
|
+
export function itemStatus(changes: readonly Tracked[], id: string): ItemStatus {
|
|
180
|
+
let latest: Tracked | undefined;
|
|
181
|
+
for (const change of changes) {
|
|
182
|
+
if (change.state.context?.targets.some((target) => target.id === id) !== true) continue;
|
|
183
|
+
if (latest === undefined || change.state.submittedAt >= latest.state.submittedAt) latest = change;
|
|
184
|
+
}
|
|
185
|
+
const context = latest?.state.context;
|
|
186
|
+
if (latest === undefined || context === undefined) return { state: 'idle' };
|
|
187
|
+
const kind = context.targets.find((target) => target.id === id)!.kind;
|
|
188
|
+
const words = context.words[kind];
|
|
189
|
+
if (latest.state.status === 'pending') return { state: 'pending', kind, words };
|
|
190
|
+
if (latest.state.status === 'error') {
|
|
191
|
+
const error = failureMessage(latest.state.error);
|
|
192
|
+
return { state: 'failed', kind, words, error, mutationId: latest.mutationId };
|
|
193
|
+
}
|
|
194
|
+
return { state: 'idle' };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** A refused add: no row is left to say so, so it gets one of its own. */
|
|
198
|
+
export type FailedAdd<TVars> = {
|
|
199
|
+
mutationId: number;
|
|
200
|
+
vars: TVars;
|
|
201
|
+
/** The ids it would have added. */
|
|
202
|
+
ids: string[];
|
|
203
|
+
status: Extract<ItemStatus, { state: 'failed' }>;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The adds the server refused whose items are not listed: each is shown as
|
|
208
|
+
* a row of its own. Asked again and refused again, an add is listed once,
|
|
209
|
+
* as the latest refusal.
|
|
210
|
+
*/
|
|
211
|
+
export function failedAdds<TVars>(changes: readonly Tracked[], present: readonly string[]): FailedAdd<TVars>[] {
|
|
212
|
+
return changes.flatMap((change) => {
|
|
213
|
+
const context = change.state.context;
|
|
214
|
+
if (change.state.status !== 'error' || context === undefined) return [];
|
|
215
|
+
const added = context.targets.filter((target) => target.kind === 'saving' && !present.includes(target.id));
|
|
216
|
+
if (added.length === 0 || added.length < context.targets.length) return [];
|
|
217
|
+
const status = itemStatus(changes, added[0]!.id);
|
|
218
|
+
if (status.state !== 'failed' || status.mutationId !== change.mutationId) return [];
|
|
219
|
+
const ids = added.map((target) => target.id);
|
|
220
|
+
return [{ mutationId: change.mutationId, vars: change.state.variables as TVars, ids, status }];
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Forget a failure once its row has said so and been dismissed. */
|
|
225
|
+
export function dismiss(queryClient: QueryClient, mutationId: number): void {
|
|
226
|
+
const cache = queryClient.getMutationCache();
|
|
227
|
+
const mutation = cache.getAll().find((entry) => entry.mutationId === mutationId);
|
|
228
|
+
if (mutation !== undefined) cache.remove(mutation);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function capitalize(text: string): string {
|
|
232
|
+
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
233
|
+
}
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import type { AuthInfo, CoffreClient, Member } from '@coffre/client';
|
|
2
|
+
import { QueryClient, queryOptions, type QueryKey } from '@tanstack/react-query';
|
|
3
|
+
|
|
4
|
+
import { deriveUiCapabilities } from './capabilities.ts';
|
|
5
|
+
import { memberRef, statusOf, uiResult } from './coffre.ts';
|
|
6
|
+
import type { DirectoryPrincipal, GrantRow, Me, ProjectSummary } from '../shared/models';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Every read the pages make, cached by TanStack Query: one cache per request
|
|
10
|
+
* on the server, whose reads the page carries to the browser's, and one in the
|
|
11
|
+
* browser for the whole visit.
|
|
12
|
+
*
|
|
13
|
+
* How fresh a page is. A read is reused for `FRESH_MS`, refetched when the
|
|
14
|
+
* window regains focus, and refetched at once by any change made here, which
|
|
15
|
+
* names what it touches (`affects`), so your own changes always show.
|
|
16
|
+
* Someone else's change shows within that window. Nothing is decided on a
|
|
17
|
+
* cached read: the server authorizes every call, so a stale screen can offer
|
|
18
|
+
* an action the server then refuses, never grant one. The audit log and the
|
|
19
|
+
* chain's verification are the exception, read fresh on every visit: their
|
|
20
|
+
* whole point is to be current.
|
|
21
|
+
*/
|
|
22
|
+
export const FRESH_MS = 10_000;
|
|
23
|
+
|
|
24
|
+
export function createQueryClient(): QueryClient {
|
|
25
|
+
return new QueryClient({
|
|
26
|
+
defaultOptions: {
|
|
27
|
+
queries: {
|
|
28
|
+
staleTime: FRESH_MS,
|
|
29
|
+
refetchOnWindowFocus: true,
|
|
30
|
+
// A read either answers or says why it cannot (`uiResult`), so a
|
|
31
|
+
// throw is a fault, and asking again would only repeat it.
|
|
32
|
+
retry: false,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type Place = { project: string; environment: string };
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The keys, which `affects` names too. A project's keys start with it, so
|
|
42
|
+
* one invalidation covers everything read under it.
|
|
43
|
+
*/
|
|
44
|
+
export const keys = {
|
|
45
|
+
me: ['me'],
|
|
46
|
+
auth: ['auth'],
|
|
47
|
+
projects: ['projects'],
|
|
48
|
+
grants: (project: string) => ['grants', project],
|
|
49
|
+
secrets: ({ project, environment }: Place) => ['secrets', project, environment],
|
|
50
|
+
syncs: ({ project, environment }: Place) => ['syncs', project, environment],
|
|
51
|
+
directory: ['directory'],
|
|
52
|
+
report: (member: string) => ['report', member],
|
|
53
|
+
credentials: (member: string) => ['credentials', member],
|
|
54
|
+
identities: ['identities'],
|
|
55
|
+
sessions: ['sessions'],
|
|
56
|
+
audit: ['audit'],
|
|
57
|
+
} satisfies Record<string, QueryKey | ((...args: never[]) => QueryKey)>;
|
|
58
|
+
|
|
59
|
+
/** One member's grants, as rows. */
|
|
60
|
+
function grantRows(members: Member[]): GrantRow[] {
|
|
61
|
+
return members.flatMap((member) =>
|
|
62
|
+
member.grants.map((grant) => ({
|
|
63
|
+
id: grant.id,
|
|
64
|
+
principalType: member.principalType,
|
|
65
|
+
principalId: member.principalId,
|
|
66
|
+
role: grant.role,
|
|
67
|
+
roleName: grant.roleName,
|
|
68
|
+
permissions: grant.permissions,
|
|
69
|
+
scope: grant.environment === null ? ('project' as const) : ('environment' as const),
|
|
70
|
+
environmentSlug: grant.environment,
|
|
71
|
+
expiresAt: grant.expiresAt,
|
|
72
|
+
})),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function listDirectory(client: CoffreClient) {
|
|
77
|
+
return uiResult(async () => {
|
|
78
|
+
const { members, removed } = await client.members.list();
|
|
79
|
+
const principals: DirectoryPrincipal[] = members.map(
|
|
80
|
+
({ principalType, principalId, instanceRole, isRootAdmin, tampered }) => ({
|
|
81
|
+
principalType,
|
|
82
|
+
principalId,
|
|
83
|
+
instanceRole,
|
|
84
|
+
isRootAdmin,
|
|
85
|
+
tampered,
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
return { principals, removed };
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const readProjects = (client: CoffreClient) => uiResult(() => client.projects.list());
|
|
93
|
+
|
|
94
|
+
/** The projects you can see, or why they could not be listed. */
|
|
95
|
+
export type ProjectsResult = Awaited<ReturnType<typeof readProjects>>;
|
|
96
|
+
|
|
97
|
+
export type AuditSearch = { decision?: 'deny'; actorId?: string; detail?: '1' };
|
|
98
|
+
|
|
99
|
+
export const AUDIT_PAGE_SIZE = 200;
|
|
100
|
+
|
|
101
|
+
export const queries = {
|
|
102
|
+
/** Who is looking; null when nobody is signed in. */
|
|
103
|
+
me: (client: CoffreClient) =>
|
|
104
|
+
queryOptions({
|
|
105
|
+
queryKey: keys.me,
|
|
106
|
+
queryFn: async (): Promise<Me | null> => {
|
|
107
|
+
try {
|
|
108
|
+
return await client.me();
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (statusOf(error) === 401) return null;
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
}),
|
|
115
|
+
|
|
116
|
+
/** How this instance signs people in. */
|
|
117
|
+
auth: (client: CoffreClient) => queryOptions({ queryKey: keys.auth, queryFn: (): Promise<AuthInfo> => client.auth() }),
|
|
118
|
+
|
|
119
|
+
/** The projects you can see, each with its distinct secret count. */
|
|
120
|
+
projects: (client: CoffreClient) =>
|
|
121
|
+
queryOptions({ queryKey: keys.projects, queryFn: () => readProjects(client) }),
|
|
122
|
+
|
|
123
|
+
/** Who holds what in a project, for those who manage its access. */
|
|
124
|
+
grants: (client: CoffreClient, project: string) =>
|
|
125
|
+
queryOptions({
|
|
126
|
+
queryKey: keys.grants(project),
|
|
127
|
+
queryFn: () => uiResult(async () => ({ grants: grantRows((await client.members.list(project)).members) })),
|
|
128
|
+
}),
|
|
129
|
+
|
|
130
|
+
secrets: (client: CoffreClient, place: Place) =>
|
|
131
|
+
queryOptions({
|
|
132
|
+
queryKey: keys.secrets(place),
|
|
133
|
+
queryFn: () => uiResult(() => client.secrets.list(`${place.project}/${place.environment}`)),
|
|
134
|
+
}),
|
|
135
|
+
|
|
136
|
+
syncs: (client: CoffreClient, place: Place) =>
|
|
137
|
+
queryOptions({
|
|
138
|
+
queryKey: keys.syncs(place),
|
|
139
|
+
queryFn: () => uiResult(() => client.syncs.list(`${place.project}/${place.environment}`)),
|
|
140
|
+
}),
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Everyone in the directory, and who was removed. The API shows grant
|
|
144
|
+
* managers the members of their projects; the directory pages stay the
|
|
145
|
+
* owners' own, and asking for anyone else would only log a refusal, so
|
|
146
|
+
* nobody else is asked.
|
|
147
|
+
*/
|
|
148
|
+
directory: (client: CoffreClient, owner: boolean) =>
|
|
149
|
+
queryOptions({
|
|
150
|
+
queryKey: [...keys.directory, { owner }],
|
|
151
|
+
queryFn: () =>
|
|
152
|
+
owner
|
|
153
|
+
? listDirectory(client)
|
|
154
|
+
: Promise.resolve({
|
|
155
|
+
ok: false as const,
|
|
156
|
+
error: 'Only owners can manage users and service accounts.',
|
|
157
|
+
signedOut: false,
|
|
158
|
+
}),
|
|
159
|
+
}),
|
|
160
|
+
|
|
161
|
+
/** What one member can reach and has seen; null for anyone but owners, who alone may ask. */
|
|
162
|
+
report: (client: CoffreClient, member: string, owner: boolean) =>
|
|
163
|
+
queryOptions({
|
|
164
|
+
queryKey: [...keys.report(member), { owner }],
|
|
165
|
+
queryFn: () =>
|
|
166
|
+
owner
|
|
167
|
+
? uiResult(async () => {
|
|
168
|
+
try {
|
|
169
|
+
return { report: await client.members.get(member) };
|
|
170
|
+
} catch (error) {
|
|
171
|
+
// No such member is an answer, not a failure.
|
|
172
|
+
if (statusOf(error) === 404) return { report: null };
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
: Promise.resolve(null),
|
|
177
|
+
}),
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* A token's credentials; null where they cannot be listed. Only owners
|
|
181
|
+
* issue and see them, and only coffre's own sign-in issues any.
|
|
182
|
+
*/
|
|
183
|
+
credentials: (client: CoffreClient, member: string, allowed: boolean) =>
|
|
184
|
+
queryOptions({
|
|
185
|
+
queryKey: [...keys.credentials(member), { allowed }],
|
|
186
|
+
queryFn: () => (allowed ? uiResult(() => client.tokens.list(member)) : Promise.resolve(null)),
|
|
187
|
+
}),
|
|
188
|
+
|
|
189
|
+
/** The accounts you sign in with, under coffre's own sign-in. */
|
|
190
|
+
identities: (client: CoffreClient) =>
|
|
191
|
+
queryOptions({ queryKey: keys.identities, queryFn: () => uiResult(() => client.identities.list()) }),
|
|
192
|
+
|
|
193
|
+
/** Where you are signed in, under coffre's own sign-in. */
|
|
194
|
+
sessions: (client: CoffreClient) =>
|
|
195
|
+
queryOptions({ queryKey: keys.sessions, queryFn: () => uiResult(() => client.sessions.list()) }),
|
|
196
|
+
|
|
197
|
+
/** A page of the log, as filtered: always read fresh. */
|
|
198
|
+
auditEntries: (client: CoffreClient, search: AuditSearch) =>
|
|
199
|
+
queryOptions({
|
|
200
|
+
queryKey: [...keys.audit, 'entries', search],
|
|
201
|
+
staleTime: 0,
|
|
202
|
+
// Detail is left out by the server unless asked for, and counted.
|
|
203
|
+
queryFn: () =>
|
|
204
|
+
uiResult(() =>
|
|
205
|
+
client.audit.list({
|
|
206
|
+
limit: AUDIT_PAGE_SIZE,
|
|
207
|
+
decision: search.decision,
|
|
208
|
+
actor: search.actorId,
|
|
209
|
+
detail: search.detail,
|
|
210
|
+
}),
|
|
211
|
+
),
|
|
212
|
+
}),
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Whether the log holds: verified on every visit, and never waited for.
|
|
216
|
+
* It re-reads the whole log, so it takes as long as the log is long; the
|
|
217
|
+
* page shows the entries first and the verdict when it comes.
|
|
218
|
+
*/
|
|
219
|
+
auditChain: (client: CoffreClient) =>
|
|
220
|
+
queryOptions({ queryKey: [...keys.audit, 'chain'], staleTime: 0, queryFn: () => verifyChain(client) }),
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Whether the log holds, or why that is not known. Verifying is for owners:
|
|
225
|
+
* anyone else reads their projects' part of the log, and a part cannot be
|
|
226
|
+
* checked as a chain, so for them it is a fact about the page, not a fault.
|
|
227
|
+
*/
|
|
228
|
+
async function verifyChain(client: CoffreClient) {
|
|
229
|
+
try {
|
|
230
|
+
const result = await client.audit.verify();
|
|
231
|
+
return result.ok
|
|
232
|
+
? {
|
|
233
|
+
integrity: 'intact' as const,
|
|
234
|
+
through: result.through,
|
|
235
|
+
entries: result.entries,
|
|
236
|
+
checkpoint: result.checkpoint,
|
|
237
|
+
pending: result.pending ?? 0,
|
|
238
|
+
}
|
|
239
|
+
: {
|
|
240
|
+
integrity: 'broken' as const,
|
|
241
|
+
through: result.through,
|
|
242
|
+
failedAtSeq: result.failedAtSeq,
|
|
243
|
+
author: result.author,
|
|
244
|
+
reason: result.reason,
|
|
245
|
+
};
|
|
246
|
+
} catch (error) {
|
|
247
|
+
const status = statusOf(error);
|
|
248
|
+
if (status === 403) return { integrity: 'owners-only' as const };
|
|
249
|
+
return {
|
|
250
|
+
integrity: 'unknown' as const,
|
|
251
|
+
problem: status === undefined ? 'the request never got an answer' : `the request failed with HTTP ${status}`,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export type ChainResult = Awaited<ReturnType<typeof verifyChain>>;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Who is looking, how this instance signs people in, and the projects they
|
|
260
|
+
* can see: what the shell needs on every screen. For someone signed out or
|
|
261
|
+
* not yet a member the project list is refused, and they get the sign-in or
|
|
262
|
+
* closed-door page.
|
|
263
|
+
*/
|
|
264
|
+
export function shellOf(
|
|
265
|
+
auth: AuthInfo,
|
|
266
|
+
me: Me | null,
|
|
267
|
+
projects: ProjectsResult,
|
|
268
|
+
) {
|
|
269
|
+
const listed = projects.ok ? projects.projects : [];
|
|
270
|
+
const member: Me | null = me?.registered === true ? me : null;
|
|
271
|
+
return {
|
|
272
|
+
auth,
|
|
273
|
+
principal: me === null ? null : me.principal,
|
|
274
|
+
instanceRole: member?.instanceRole ?? null,
|
|
275
|
+
projects: listed,
|
|
276
|
+
capabilities: deriveUiCapabilities(member, listed),
|
|
277
|
+
registrationRequired: me !== null && !me.registered,
|
|
278
|
+
/** A member the vault refuses: their record failed its integrity check. */
|
|
279
|
+
accessTampered: me?.tampered === true,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export type Shell = ReturnType<typeof shellOf>;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The shell, as every loader reads it: from the cache while fresh, so a
|
|
287
|
+
* child route never asks again for what the root has.
|
|
288
|
+
*/
|
|
289
|
+
export async function loadShell(queryClient: QueryClient, client: CoffreClient): Promise<Shell> {
|
|
290
|
+
const [auth, me, projects] = await Promise.all([
|
|
291
|
+
queryClient.fetchQuery(queries.auth(client)),
|
|
292
|
+
queryClient.fetchQuery(queries.me(client)),
|
|
293
|
+
queryClient.fetchQuery(queries.projects(client)),
|
|
294
|
+
]);
|
|
295
|
+
return shellOf(auth, me, projects);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* One project and, when you manage its access, its grants. A project that is
|
|
300
|
+
* not there for you fails with no message: the page words that itself.
|
|
301
|
+
*/
|
|
302
|
+
export function projectOf(projects: ProjectsResult, slug: string) {
|
|
303
|
+
if (!projects.ok) return projects;
|
|
304
|
+
const project = projects.projects.find((entry) => entry.slug === slug);
|
|
305
|
+
if (project === undefined) return { ok: false as const, error: null };
|
|
306
|
+
return { ok: true as const, project, managesAccess: project.permissions.includes('grant.manage') };
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** What a project page reads: the project list, and its grants when you manage them. */
|
|
310
|
+
export async function loadProject(queryClient: QueryClient, client: CoffreClient, slug: string) {
|
|
311
|
+
const found = projectOf(await queryClient.fetchQuery(queries.projects(client)), slug);
|
|
312
|
+
if (found.ok && found.managesAccess) await queryClient.fetchQuery(queries.grants(client, slug));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** The directory pages' read: the directory, for owners. */
|
|
316
|
+
export async function loadDirectory(queryClient: QueryClient, client: CoffreClient) {
|
|
317
|
+
const shell = await loadShell(queryClient, client);
|
|
318
|
+
return queryClient.fetchQuery(queries.directory(client, shell.capabilities.canManageGrants));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Everything one user's or token's page reads: the report (owners only), the
|
|
323
|
+
* grants of every project where you manage access, and a token's
|
|
324
|
+
* credentials. There is no "grants of this member" call, so each project you
|
|
325
|
+
* manage is asked for its grants and the page keeps this member's. Projects
|
|
326
|
+
* where you do not hold grant.manage are skipped rather than asked: the
|
|
327
|
+
* refusal would land in the audit log as a denial in your name.
|
|
328
|
+
*/
|
|
329
|
+
export async function loadPrincipal(
|
|
330
|
+
queryClient: QueryClient,
|
|
331
|
+
client: CoffreClient,
|
|
332
|
+
principalType: 'user' | 'service',
|
|
333
|
+
principalId: string,
|
|
334
|
+
) {
|
|
335
|
+
const shell = await loadShell(queryClient, client);
|
|
336
|
+
const member = memberRef(principalType, principalId);
|
|
337
|
+
const owner = shell.capabilities.canManageGrants;
|
|
338
|
+
await Promise.all([
|
|
339
|
+
queryClient.fetchQuery(queries.report(client, member, owner)),
|
|
340
|
+
principalType === 'service'
|
|
341
|
+
? queryClient.fetchQuery(queries.credentials(client, member, owner && shell.auth.signin !== null))
|
|
342
|
+
: null,
|
|
343
|
+
...managedProjects(shell.projects).map((project) => queryClient.fetchQuery(queries.grants(client, project.slug))),
|
|
344
|
+
]);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export function managedProjects(projects: ProjectSummary[]): ProjectSummary[] {
|
|
348
|
+
return projects.filter((project) => project.permissions.includes('grant.manage'));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* What each change touches, which `useAction` refetches when it lands. Every
|
|
353
|
+
* change also lands in the audit log, which is always read fresh.
|
|
354
|
+
*/
|
|
355
|
+
export const affects = {
|
|
356
|
+
/** A project or environment made, renamed or archived: the tree, and where you hold access. */
|
|
357
|
+
places: (): QueryKey[] => [keys.projects, keys.me],
|
|
358
|
+
/** Secrets written, renamed, restored or archived: the environment, its syncs, which push them, and the counts. */
|
|
359
|
+
secrets: (place: Place): QueryKey[] => [keys.secrets(place), keys.syncs(place), keys.projects],
|
|
360
|
+
syncs: (place: Place): QueryKey[] => [keys.syncs(place)],
|
|
361
|
+
/**
|
|
362
|
+
* Someone's access in a project changed: its grants, their report, and, as
|
|
363
|
+
* it may be your own, what you can see.
|
|
364
|
+
*/
|
|
365
|
+
access: (project: string, member: string): QueryKey[] => [
|
|
366
|
+
keys.grants(project),
|
|
367
|
+
keys.report(member),
|
|
368
|
+
keys.projects,
|
|
369
|
+
keys.me,
|
|
370
|
+
],
|
|
371
|
+
/** Someone let in: the directory. */
|
|
372
|
+
admission: (): QueryKey[] => [keys.directory],
|
|
373
|
+
/**
|
|
374
|
+
* An instance role changed: the directory, their report, and, as it may be
|
|
375
|
+
* your own, what you can see and do.
|
|
376
|
+
*/
|
|
377
|
+
role: (member: string): QueryKey[] => [keys.directory, keys.report(member), keys.me, keys.projects],
|
|
378
|
+
/** Someone removed: the directory, their report, and every project's grants, which removal ends. */
|
|
379
|
+
removal: (member: string): QueryKey[] => [keys.directory, keys.report(member), ['grants']],
|
|
380
|
+
credentials: (member: string): QueryKey[] => [keys.credentials(member)],
|
|
381
|
+
/** Unlinking an account also ends the sessions it signed in. */
|
|
382
|
+
identities: (): QueryKey[] => [keys.identities, keys.sessions],
|
|
383
|
+
sessions: (): QueryKey[] => [keys.sessions],
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Refetch what a change touched: at once where it is on screen, on next use
|
|
388
|
+
* elsewhere. Every change lands in the audit log, so the log and its
|
|
389
|
+
* verification are always among them.
|
|
390
|
+
*/
|
|
391
|
+
export async function refresh(queryClient: QueryClient, touched: QueryKey[]): Promise<void> {
|
|
392
|
+
await Promise.all([...touched, keys.audit].map((queryKey) => queryClient.invalidateQueries({ queryKey })));
|
|
393
|
+
}
|
package/src/lib/use-action.ts
CHANGED
|
@@ -1,30 +1,35 @@
|
|
|
1
|
+
import { useQueryClient, type QueryKey } from '@tanstack/react-query';
|
|
1
2
|
import { useState } from 'react';
|
|
2
|
-
import { useRouter } from '@tanstack/react-router';
|
|
3
3
|
|
|
4
4
|
import { failureMessage } from './coffre';
|
|
5
|
+
import { refresh } from './queries';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
|
-
* Run a change against the API, then
|
|
8
|
-
*
|
|
8
|
+
* Run a change against the API, then refetch what it touched. A failed call
|
|
9
|
+
* leaves the page as it was and says why, in `error`.
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* loader instead, so the sidebar's project tree stays honest without anyone
|
|
16
|
-
* having to remember it exists.
|
|
11
|
+
* Each change names what it touches (`affects` in `lib/queries.ts`), and
|
|
12
|
+
* those queries are refetched before the change counts as done: at once where
|
|
13
|
+
* they are on screen, such as the sidebar's project tree, and on next use
|
|
14
|
+
* where they are not. So your own change always shows, and nothing else is
|
|
15
|
+
* asked for again.
|
|
17
16
|
*/
|
|
18
17
|
export function useAction() {
|
|
19
|
-
const
|
|
18
|
+
const queryClient = useQueryClient();
|
|
20
19
|
const [pending, setPending] = useState(false);
|
|
21
20
|
const [error, setError] = useState<string | null>(null);
|
|
22
21
|
|
|
23
22
|
async function run<T>(
|
|
24
23
|
fn: () => Promise<T>,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
{
|
|
25
|
+
affects,
|
|
26
|
+
onSuccess,
|
|
27
|
+
}: {
|
|
28
|
+
affects: QueryKey[] | ((result: T) => QueryKey[]);
|
|
29
|
+
// `unknown` rather than `void`, so a one-expression callback can end in
|
|
30
|
+
// `toast.success(...)` -- which returns an id -- without a block body.
|
|
31
|
+
onSuccess?: (result: T) => unknown;
|
|
32
|
+
},
|
|
28
33
|
): Promise<void> {
|
|
29
34
|
setPending(true);
|
|
30
35
|
let result: T;
|
|
@@ -38,7 +43,7 @@ export function useAction() {
|
|
|
38
43
|
|
|
39
44
|
setError(null);
|
|
40
45
|
try {
|
|
41
|
-
await
|
|
46
|
+
await refresh(queryClient, typeof affects === 'function' ? affects(result) : affects);
|
|
42
47
|
await onSuccess?.(result);
|
|
43
48
|
} catch {
|
|
44
49
|
setError('The change was saved, but the page could not refresh. Reload before you retry.');
|