@go-avro/avro-js 0.1.15 → 1.6.1-rc1

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 (51) hide show
  1. package/dist/auth/AuthManager.d.ts +32 -0
  2. package/dist/auth/AuthManager.js +138 -43
  3. package/dist/client/QueryClient.d.ts +68 -2
  4. package/dist/client/QueryClient.js +310 -96
  5. package/dist/client/cache.d.ts +24 -0
  6. package/dist/client/cache.js +51 -0
  7. package/dist/client/core/fetch.js +58 -49
  8. package/dist/client/core/policy.d.ts +17 -0
  9. package/dist/client/core/policy.js +35 -0
  10. package/dist/client/core/xhr.js +16 -14
  11. package/dist/client/hooks/bills.js +10 -14
  12. package/dist/client/hooks/catalog_items.js +18 -8
  13. package/dist/client/hooks/chats.js +2 -8
  14. package/dist/client/hooks/companies.js +11 -5
  15. package/dist/client/hooks/events.js +56 -47
  16. package/dist/client/hooks/groups.js +11 -26
  17. package/dist/client/hooks/jobs.js +3 -8
  18. package/dist/client/hooks/labels.js +11 -26
  19. package/dist/client/hooks/messages.js +21 -24
  20. package/dist/client/hooks/months.js +28 -22
  21. package/dist/client/hooks/payouts.js +3 -8
  22. package/dist/client/hooks/prepayments.js +28 -22
  23. package/dist/client/hooks/proposal.js +1 -0
  24. package/dist/client/hooks/routes.js +8 -6
  25. package/dist/client/hooks/sessions.js +21 -31
  26. package/dist/client/hooks/skills.js +6 -4
  27. package/dist/client/hooks/teams.js +6 -4
  28. package/dist/client/hooks/timecards.js +7 -7
  29. package/dist/client/hooks/waivers.js +6 -4
  30. package/dist/client/persist.js +21 -2
  31. package/dist/client/realtime.d.ts +21 -1
  32. package/dist/client/realtime.js +183 -51
  33. package/dist/types/api/Bill.js +2 -1
  34. package/dist/types/api/Company.d.ts +2 -0
  35. package/dist/types/api/Email.d.ts +3 -9
  36. package/dist/types/api/EmailNotification.d.ts +1 -1
  37. package/dist/types/api/Job.js +8 -4
  38. package/dist/types/api/LineItem.d.ts +4 -0
  39. package/dist/types/api/LineItem.js +10 -2
  40. package/dist/types/api/Prepayment.js +2 -1
  41. package/dist/types/api/Route.js +2 -1
  42. package/dist/types/api/RouteJob.js +2 -1
  43. package/dist/types/api/Session.js +2 -1
  44. package/dist/types/api/Task.d.ts +27 -2
  45. package/dist/types/api/Task.js +44 -8
  46. package/dist/types/api/_Event.js +2 -1
  47. package/dist/types/api/assignData.d.ts +9 -0
  48. package/dist/types/api/assignData.js +17 -0
  49. package/dist/version.d.ts +1 -1
  50. package/dist/version.js +1 -1
  51. package/package.json +7 -2
@@ -1,18 +1,24 @@
1
1
  import { useInfiniteQuery, useMutation } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, seedDetails, snapshotNs } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  import { LineItemStatus } from '../../types/api.js';
5
6
  AvroQueryClient.prototype.useGetPrepayments = function (body) {
6
7
  const queryClient = this.getQueryClient();
8
+ // Status buckets default to the backend's (unbilled only) and are
9
+ // always sent, so the key describes exactly what the server returns.
10
+ const unbilled = body.unbilled ?? true;
11
+ const billed = body.billed ?? false;
12
+ const paid = body.paid ?? false;
7
13
  const result = useInfiniteQuery({
8
14
  queryKey: keys.infinite('prepayments', this.companyId, {
9
15
  amt: body.amt ?? 50,
10
16
  known_ids: body.known_ids ?? [],
11
17
  unknown_ids: body.unknown_ids ?? [],
12
18
  query: body.query ?? '',
13
- unbilled: body.unbilled ?? true,
14
- billed: body.billed ?? true,
15
- paid: body.paid ?? true,
19
+ unbilled,
20
+ billed,
21
+ paid,
16
22
  taskId: body.taskId ?? '',
17
23
  sort_by: body.sort_by ?? null,
18
24
  direction: body.direction ?? null,
@@ -23,16 +29,21 @@ AvroQueryClient.prototype.useGetPrepayments = function (body) {
23
29
  return undefined;
24
30
  return allPages.flat().length; // next offset
25
31
  },
26
- queryFn: ({ pageParam = 0 }) => this.fetchPrepayments({ ...body, offset: pageParam }),
27
- enabled: body.enabled ?? true,
32
+ queryFn: ({ pageParam = 0 }) => this.fetchPrepayments({
33
+ amt: body.amt,
34
+ known_ids: body.known_ids,
35
+ unknown_ids: body.unknown_ids,
36
+ query: body.query,
37
+ unbilled,
38
+ billed,
39
+ paid,
40
+ task_id: body.taskId,
41
+ sort_by: body.sort_by,
42
+ direction: body.direction,
43
+ offset: pageParam,
44
+ }).then((items) => seedDetails(queryClient, 'prepayments', items)),
45
+ enabled: (body.enabled ?? true) && Boolean(this.companyId),
28
46
  });
29
- if (result.data) {
30
- result.data.pages.forEach((data_page) => {
31
- data_page.forEach((prepayment) => {
32
- queryClient.setQueryData(['prepayment', prepayment.id], prepayment);
33
- });
34
- });
35
- }
36
47
  return result;
37
48
  };
38
49
  AvroQueryClient.prototype.useUpdatePrepayments = function () {
@@ -56,11 +67,10 @@ AvroQueryClient.prototype.useUpdatePrepayments = function () {
56
67
  onMutate: async ({ prepayments, action }) => {
57
68
  await queryClient.cancelQueries({ queryKey: ['prepayments'] });
58
69
  await queryClient.cancelQueries({ queryKey: ['prepayments'] });
59
- const previousPrepayments = queryClient.getQueryData(['prepayments']);
60
- const previousPrepaymentObjs = prepayments.map((prepayment) => queryClient.getQueryData(['prepayment', prepayment.id]));
70
+ const previousPrepayments = snapshotNs(queryClient, 'prepayments');
61
71
  const prepaymentIds = prepayments.map((prepayment) => prepayment.id);
62
72
  prepaymentIds.forEach((prepaymentId, idx) => {
63
- queryClient.setQueryData(['prepayment', prepaymentId], (oldData) => {
73
+ queryClient.setQueryData(['prepayments', prepaymentId], (oldData) => {
64
74
  return oldData
65
75
  ? {
66
76
  ...oldData,
@@ -97,18 +107,14 @@ AvroQueryClient.prototype.useUpdatePrepayments = function () {
97
107
  }
98
108
  return oldData;
99
109
  });
100
- return { previousPrepayments, previousPrepaymentObjs };
110
+ return { previousPrepayments };
101
111
  },
102
112
  onSuccess: () => {
103
113
  queryClient.invalidateQueries({ queryKey: ['prepayments'] });
104
114
  },
105
115
  onError: (err, variables, context) => {
106
- if (context) {
107
- queryClient.setQueryData(['prepayments'], context.previousPrepayments);
108
- context.previousPrepaymentObjs.forEach((prepaymentObj) => {
109
- queryClient.setQueryData(['prepayment', prepaymentObj.id], prepaymentObj);
110
- });
111
- }
116
+ // The namespace snapshot covers lists and details.
117
+ restoreNs(queryClient, context?.previousPrepayments);
112
118
  },
113
119
  });
114
120
  };
@@ -40,5 +40,6 @@ AvroQueryClient.prototype.useGetProposal = function (proposal_id) {
40
40
  });
41
41
  return { ...proposal, tasks: proposal.tasks.map((task) => new Task(task)) };
42
42
  },
43
+ enabled: Boolean(proposal_id),
43
44
  });
44
45
  };
@@ -1,5 +1,6 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, snapshotNs } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  import { Route } from '../../types/api.js';
5
6
  const dedupeJobTasks = (payload) => payload.jobs
@@ -42,6 +43,7 @@ AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
42
43
  const routes = pages.flat().map((route) => new Route(route));
43
44
  return routes;
44
45
  },
46
+ enabled: Boolean(this.companyId),
45
47
  });
46
48
  };
47
49
  AvroQueryClient.prototype.useGetRoute = function (routeId) {
@@ -67,7 +69,7 @@ AvroQueryClient.prototype.useCreateRoute = function () {
67
69
  },
68
70
  onMutate: async ({ routeData: { optimize: _optimize, ...routeData } }) => {
69
71
  await queryClient.cancelQueries({ queryKey: ['routes'] });
70
- const previousRoutes = queryClient.getQueryData(['routes']);
72
+ const previousRoutes = snapshotNs(queryClient, 'routes');
71
73
  queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
72
74
  if (!oldData)
73
75
  return oldData;
@@ -97,7 +99,7 @@ AvroQueryClient.prototype.useCreateRoute = function () {
97
99
  },
98
100
  onError: (_err, _variables, context) => {
99
101
  if (context?.previousRoutes) {
100
- queryClient.setQueryData(['routes'], context.previousRoutes);
102
+ restoreNs(queryClient, context.previousRoutes);
101
103
  }
102
104
  },
103
105
  onSettled: () => {
@@ -152,7 +154,7 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
152
154
  await queryClient.cancelQueries({ queryKey: ['routes', routeId] });
153
155
  await queryClient.cancelQueries({ queryKey: ['routes'] });
154
156
  const previousRoute = queryClient.getQueryData(['routes', routeId]);
155
- const previousRoutes = queryClient.getQueryData(['routes']);
157
+ const previousRoutes = snapshotNs(queryClient, 'routes');
156
158
  queryClient.setQueryData(['routes', routeId], (oldData) => oldData ? new Route({ ...oldData, ...updates }) : undefined);
157
159
  queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
158
160
  if (!oldData)
@@ -179,7 +181,7 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
179
181
  queryClient.setQueryData(['routes', routeId], context.previousRoute);
180
182
  }
181
183
  if (context?.previousRoutes) {
182
- queryClient.setQueryData(['routes'], context.previousRoutes);
184
+ restoreNs(queryClient, context.previousRoutes);
183
185
  }
184
186
  },
185
187
  });
@@ -193,7 +195,7 @@ AvroQueryClient.prototype.useDeleteRoute = function () {
193
195
  onMutate: async ({ routeId }) => {
194
196
  await queryClient.cancelQueries({ queryKey: ['routes'] });
195
197
  await queryClient.cancelQueries({ queryKey: ['routes', routeId] });
196
- const previousRoutes = queryClient.getQueryData(['routes']);
198
+ const previousRoutes = snapshotNs(queryClient, 'routes');
197
199
  const previousRoute = queryClient.getQueryData(['routes', routeId]);
198
200
  queryClient.setQueryData(['routes', routeId], undefined);
199
201
  queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
@@ -216,7 +218,7 @@ AvroQueryClient.prototype.useDeleteRoute = function () {
216
218
  onError: (_err, variables, context) => {
217
219
  const { routeId } = variables;
218
220
  if (context?.previousRoutes) {
219
- queryClient.setQueryData(['routes'], context.previousRoutes);
221
+ restoreNs(queryClient, context.previousRoutes);
220
222
  }
221
223
  if (context?.previousRoute) {
222
224
  queryClient.setQueryData(['routes', routeId], context.previousRoute);
@@ -1,6 +1,7 @@
1
1
  import { useMutation, useQuery, useInfiniteQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
3
  import { keys, isListKey } from '../realtime.js';
4
+ import { removeOptimisticRow, seedDetails } from '../cache.js';
4
5
  import { Session } from '../../types/api.js';
5
6
  const NS = 'sessions';
6
7
  function listFilters(body) {
@@ -46,16 +47,9 @@ AvroQueryClient.prototype.useGetSessions = function (body = {}) {
46
47
  return undefined;
47
48
  return allPages.flat().length;
48
49
  },
49
- queryFn: ({ pageParam = 0 }) => this.fetchSessions({ ...body, amt, offset: pageParam }).then((sessions) => sessions.map((s) => new Session(s))),
50
+ queryFn: ({ pageParam = 0 }) => this.fetchSessions({ ...body, amt, offset: pageParam }).then((sessions) => seedDetails(queryClient, NS, sessions.map((s) => new Session(s)))),
50
51
  enabled: Boolean(this.companyId),
51
52
  });
52
- if (result.data) {
53
- result.data.pages.forEach((page) => {
54
- page.forEach((session) => {
55
- queryClient.setQueryData(keys.detail(NS, session.id), session);
56
- });
57
- });
58
- }
59
53
  return result;
60
54
  };
61
55
  /** The caller's own open sessions (any company). */
@@ -63,14 +57,9 @@ AvroQueryClient.prototype.useGetUserSessions = function () {
63
57
  const queryClient = this.getQueryClient();
64
58
  const result = useQuery({
65
59
  queryKey: keys.list(NS, 'mine', { active: true }),
66
- queryFn: () => this.get({ path: '/session' }).then((sessions) => (Array.isArray(sessions) ? sessions : []).map((s) => new Session(s))),
60
+ queryFn: () => this.get({ path: '/session' }).then((sessions) => seedDetails(queryClient, NS, (Array.isArray(sessions) ? sessions : []).map((s) => new Session(s)))),
67
61
  enabled: Boolean(this.companyId),
68
62
  });
69
- if (result.data) {
70
- result.data.forEach((session) => {
71
- queryClient.setQueryData(keys.detail(NS, session.id), session);
72
- });
73
- }
74
63
  return result;
75
64
  };
76
65
  /** Exactly the given sessions, in one request. */
@@ -80,14 +69,9 @@ AvroQueryClient.prototype.useGetCompanySessionsByIds = function (sessionIds, par
80
69
  const sortedIds = [...sessionIds].sort();
81
70
  const result = useQuery({
82
71
  queryKey: keys.list(NS, companyId, listFilters({ unknown_ids: sortedIds })),
83
- queryFn: () => this.fetchSessions({ unknown_ids: sortedIds, amt: sortedIds.length }).then((sessions) => sessions.map((s) => new Session(s))),
72
+ queryFn: () => this.fetchSessions({ unknown_ids: sortedIds, amt: sortedIds.length }, undefined, {}, companyId).then((sessions) => seedDetails(queryClient, NS, sessions.map((s) => new Session(s)))),
84
73
  enabled: Boolean(companyId) && sortedIds.length > 0 && (params.enabled ?? true),
85
74
  });
86
- if (result.data) {
87
- result.data.forEach((session) => {
88
- queryClient.setQueryData(keys.detail(NS, session.id), session);
89
- });
90
- }
91
75
  return result;
92
76
  };
93
77
  /** Currently-open sessions across the company, in one request. */
@@ -96,14 +80,9 @@ AvroQueryClient.prototype.useGetActiveCompanySessions = function (params = {}) {
96
80
  const companyId = params.companyId ?? this.companyId;
97
81
  const result = useQuery({
98
82
  queryKey: keys.list(NS, companyId, listFilters({ active: true })),
99
- queryFn: () => this.fetchSessions({ active: true, amt: 500 }).then((sessions) => sessions.map((s) => new Session(s))),
83
+ queryFn: () => this.fetchSessions({ active: true, amt: 500 }, undefined, {}, companyId).then((sessions) => seedDetails(queryClient, NS, sessions.map((s) => new Session(s)))),
100
84
  enabled: Boolean(companyId) && (params.enabled ?? true),
101
85
  });
102
- if (result.data) {
103
- result.data.forEach((session) => {
104
- queryClient.setQueryData(keys.detail(NS, session.id), session);
105
- });
106
- }
107
86
  return result;
108
87
  };
109
88
  // ─── Mutations ──────────────────────────────────────────────────────
@@ -129,7 +108,12 @@ AvroQueryClient.prototype.useCreateUserSession = function () {
129
108
  company_id: this.companyId,
130
109
  });
131
110
  patchSessionLists(queryClient, (items) => [optimistic, ...items]);
132
- return { previous };
111
+ return { previous, tempId: optimistic.id };
112
+ },
113
+ onSuccess: (_data, _vars, context) => {
114
+ // The echoed change (or the fallback refetch) brings the real row.
115
+ if (context?.tempId)
116
+ removeOptimisticRow(queryClient, NS, context.tempId);
133
117
  },
134
118
  onError: (_err, _vars, context) => {
135
119
  if (context?.previous)
@@ -190,13 +174,16 @@ AvroQueryClient.prototype.useUpdateUserSession = function () {
190
174
  onMutate: async ({ sessionId, updates, }) => {
191
175
  await queryClient.cancelQueries({ queryKey: [NS] });
192
176
  const previous = snapshotSessionLists(queryClient);
177
+ const previousDetail = queryClient.getQueryData(keys.detail(NS, sessionId));
193
178
  patchSessionLists(queryClient, (items) => items.map((session) => session.id === sessionId ? new Session({ ...session, ...updates }) : session));
194
179
  queryClient.setQueryData(keys.detail(NS, sessionId), (old) => old ? new Session({ ...old, ...updates }) : old);
195
- return { previous };
180
+ return { previous, previousDetail };
196
181
  },
197
- onError: (_err, _vars, context) => {
182
+ onError: (_err, { sessionId }, context) => {
198
183
  if (context?.previous)
199
184
  restoreSessionLists(queryClient, context.previous);
185
+ if (context?.previousDetail)
186
+ queryClient.setQueryData(keys.detail(NS, sessionId), context.previousDetail);
200
187
  },
201
188
  onSettled: () => {
202
189
  this.syncAfterMutation('session', 'timecard');
@@ -245,13 +232,16 @@ AvroQueryClient.prototype.useUpdateCompanySession = function () {
245
232
  onMutate: async ({ sessionId, updates }) => {
246
233
  await queryClient.cancelQueries({ queryKey: [NS] });
247
234
  const previous = snapshotSessionLists(queryClient);
235
+ const previousDetail = queryClient.getQueryData(keys.detail(NS, sessionId));
248
236
  patchSessionLists(queryClient, (items) => items.map((session) => session.id === sessionId ? new Session({ ...session, ...updates }) : session));
249
237
  queryClient.setQueryData(keys.detail(NS, sessionId), (old) => old ? new Session({ ...old, ...updates }) : old);
250
- return { previous };
238
+ return { previous, previousDetail };
251
239
  },
252
- onError: (_err, _vars, context) => {
240
+ onError: (_err, { sessionId }, context) => {
253
241
  if (context?.previous)
254
242
  restoreSessionLists(queryClient, context.previous);
243
+ if (context?.previousDetail)
244
+ queryClient.setQueryData(keys.detail(NS, sessionId), context.previousDetail);
255
245
  },
256
246
  onSettled: () => {
257
247
  this.syncAfterMutation('session', 'timecard');
@@ -1,5 +1,6 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, snapshotNs } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  AvroQueryClient.prototype.useCreateSkill = function () {
5
6
  const queryClient = this.getQueryClient();
@@ -44,6 +45,7 @@ AvroQueryClient.prototype.useGetSkills = function (body, total, onProgress) {
44
45
  const skills = pages.flat();
45
46
  return skills;
46
47
  },
48
+ enabled: Boolean(this.companyId),
47
49
  });
48
50
  };
49
51
  AvroQueryClient.prototype.useUpdateSkill = function () {
@@ -59,7 +61,7 @@ AvroQueryClient.prototype.useUpdateSkill = function () {
59
61
  onMutate: async ({ skillId, updates }) => {
60
62
  await queryClient.cancelQueries({ queryKey: ['skills'] });
61
63
  await queryClient.cancelQueries({ queryKey: ['skills', skillId] });
62
- const previousSkills = queryClient.getQueryData(['skills']);
64
+ const previousSkills = snapshotNs(queryClient, 'skills');
63
65
  const previousSkill = queryClient.getQueryData(['skills', skillId]);
64
66
  queryClient.setQueryData(['skills', skillId], (oldData) => {
65
67
  if (!oldData)
@@ -86,7 +88,7 @@ AvroQueryClient.prototype.useUpdateSkill = function () {
86
88
  onError: (_err, variables, context) => {
87
89
  const { skillId } = variables;
88
90
  if (context?.previousSkills) {
89
- queryClient.setQueryData(['skills'], context.previousSkills);
91
+ restoreNs(queryClient, context.previousSkills);
90
92
  }
91
93
  if (context?.previousSkill) {
92
94
  queryClient.setQueryData(['skills', skillId], context.previousSkill);
@@ -102,7 +104,7 @@ AvroQueryClient.prototype.useDeleteSkill = function () {
102
104
  },
103
105
  onMutate: async ({ skillId }) => {
104
106
  await queryClient.cancelQueries({ queryKey: ['skills'] });
105
- const previousSkills = queryClient.getQueryData(['skills']);
107
+ const previousSkills = snapshotNs(queryClient, 'skills');
106
108
  queryClient.setQueriesData({ queryKey: ['skills'] }, (oldData) => {
107
109
  if (!oldData)
108
110
  return oldData;
@@ -122,7 +124,7 @@ AvroQueryClient.prototype.useDeleteSkill = function () {
122
124
  },
123
125
  onError: (_err, variables, context) => {
124
126
  if (context?.previousSkills) {
125
- queryClient.setQueryData(['skills'], context.previousSkills);
127
+ restoreNs(queryClient, context.previousSkills);
126
128
  }
127
129
  },
128
130
  });
@@ -1,5 +1,6 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, snapshotNs } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  AvroQueryClient.prototype.useGetTeams = function (body, total, onProgress) {
5
6
  return useQuery({
@@ -29,6 +30,7 @@ AvroQueryClient.prototype.useGetTeams = function (body, total, onProgress) {
29
30
  const teams = pages.flat();
30
31
  return teams;
31
32
  },
33
+ enabled: Boolean(this.companyId),
32
34
  });
33
35
  };
34
36
  AvroQueryClient.prototype.useCreateTeam = function () {
@@ -59,7 +61,7 @@ AvroQueryClient.prototype.useUpdateTeam = function () {
59
61
  onMutate: async ({ teamId, teamData }) => {
60
62
  await queryClient.cancelQueries({ queryKey: ['teams'] });
61
63
  await queryClient.cancelQueries({ queryKey: ['teams', teamId] });
62
- const previousTeams = queryClient.getQueryData(['teams']);
64
+ const previousTeams = snapshotNs(queryClient, 'teams');
63
65
  const previousTeam = queryClient.getQueryData(['teams', teamId]);
64
66
  queryClient.setQueryData(['teams', teamId], (oldData) => {
65
67
  if (!oldData)
@@ -86,7 +88,7 @@ AvroQueryClient.prototype.useUpdateTeam = function () {
86
88
  onError: (_err, variables, context) => {
87
89
  const { teamId } = variables;
88
90
  if (context?.previousTeams) {
89
- queryClient.setQueryData(['teams'], context.previousTeams);
91
+ restoreNs(queryClient, context.previousTeams);
90
92
  }
91
93
  if (context?.previousTeam) {
92
94
  queryClient.setQueryData(['teams', teamId], context.previousTeam);
@@ -103,7 +105,7 @@ AvroQueryClient.prototype.useDeleteTeam = function () {
103
105
  onMutate: async ({ teamId }) => {
104
106
  await queryClient.cancelQueries({ queryKey: ['teams'] });
105
107
  await queryClient.cancelQueries({ queryKey: ['teams', teamId] });
106
- const previousRoutes = queryClient.getQueryData(['teams']);
108
+ const previousRoutes = snapshotNs(queryClient, 'teams');
107
109
  const previousRoute = queryClient.getQueryData(['teams', teamId]);
108
110
  queryClient.setQueryData(['teams', teamId], undefined);
109
111
  queryClient.setQueriesData({ queryKey: ['teams'] }, (oldData) => {
@@ -126,7 +128,7 @@ AvroQueryClient.prototype.useDeleteTeam = function () {
126
128
  onError: (_err, variables, context) => {
127
129
  const { teamId } = variables;
128
130
  if (context?.previousRoutes) {
129
- queryClient.setQueryData(['teams'], context.previousRoutes);
131
+ restoreNs(queryClient, context.previousRoutes);
130
132
  }
131
133
  if (context?.previousRoute) {
132
134
  queryClient.setQueryData(['teams', teamId], context.previousRoute);
@@ -1,15 +1,15 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { keys } from '../realtime.js';
3
4
  AvroQueryClient.prototype.useGetCompanyTimecards = function (params = {}) {
4
5
  const companyId = params.companyId ?? this.companyId;
5
6
  return useQuery({
6
- queryKey: [
7
- 'timecards',
8
- companyId,
9
- params.periodStart ?? null,
10
- params.userCompanyId ?? null,
11
- params.status ?? null,
12
- ],
7
+ // keys.list so realtime `timecard` changes patch it (matchTimecards).
8
+ queryKey: keys.list('timecards', companyId, {
9
+ periodStart: params.periodStart ?? null,
10
+ userCompanyId: params.userCompanyId ?? null,
11
+ status: params.status ?? null,
12
+ }),
13
13
  queryFn: async () => {
14
14
  const search = new URLSearchParams();
15
15
  if (typeof params.periodStart === 'number') {
@@ -1,5 +1,6 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, snapshotNs } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  AvroQueryClient.prototype.useCreateWaiver = function () {
5
6
  const queryClient = this.getQueryClient();
@@ -47,6 +48,7 @@ AvroQueryClient.prototype.useGetWaivers = function (body, total, onProgress) {
47
48
  const waivers = pages.flat();
48
49
  return waivers;
49
50
  },
51
+ enabled: Boolean(this.companyId),
50
52
  });
51
53
  };
52
54
  AvroQueryClient.prototype.useUpdateWaiver = function () {
@@ -64,7 +66,7 @@ AvroQueryClient.prototype.useUpdateWaiver = function () {
64
66
  await queryClient.cancelQueries({
65
67
  queryKey: ['waivers', waiverId],
66
68
  });
67
- const previousWaivers = queryClient.getQueryData(['waivers']);
69
+ const previousWaivers = snapshotNs(queryClient, 'waivers');
68
70
  const previousWaiver = queryClient.getQueryData(['waivers', waiverId]);
69
71
  queryClient.setQueryData(['waivers', waiverId], (oldData) => {
70
72
  if (!oldData)
@@ -91,7 +93,7 @@ AvroQueryClient.prototype.useUpdateWaiver = function () {
91
93
  onError: (_err, variables, context) => {
92
94
  const { waiverId } = variables;
93
95
  if (context?.previousWaivers) {
94
- queryClient.setQueryData(['waivers'], context.previousWaivers);
96
+ restoreNs(queryClient, context.previousWaivers);
95
97
  }
96
98
  if (context?.previousWaiver) {
97
99
  queryClient.setQueryData(['waivers', waiverId], context.previousWaiver);
@@ -109,7 +111,7 @@ AvroQueryClient.prototype.useDeleteWaiver = function () {
109
111
  },
110
112
  onMutate: async ({ waiverId }) => {
111
113
  await queryClient.cancelQueries({ queryKey: ['waivers'] });
112
- const previousWaivers = queryClient.getQueryData(['waivers']);
114
+ const previousWaivers = snapshotNs(queryClient, 'waivers');
113
115
  queryClient.setQueriesData({ queryKey: ['waivers'] }, (oldData) => {
114
116
  if (!oldData)
115
117
  return oldData;
@@ -129,7 +131,7 @@ AvroQueryClient.prototype.useDeleteWaiver = function () {
129
131
  },
130
132
  onError: (_err, variables, context) => {
131
133
  if (context?.previousWaivers) {
132
- queryClient.setQueryData(['waivers'], context.previousWaivers);
134
+ restoreNs(queryClient, context.previousWaivers);
133
135
  }
134
136
  },
135
137
  });
@@ -71,9 +71,28 @@ export const PERSISTED_NAMESPACES = [
71
71
  'health',
72
72
  ];
73
73
  /** JSON loses class instances; restore the ones hooks hand out. */
74
+ /** Revive list (`T[]`), paged (`{pages}`) and detail (`{id,…}`) data alike. */
75
+ function reviver(construct) {
76
+ return (data) => {
77
+ if (Array.isArray(data))
78
+ return data.map((raw) => construct(raw));
79
+ if (data && typeof data === 'object') {
80
+ const pages = data.pages;
81
+ if (Array.isArray(pages)) {
82
+ return {
83
+ ...data,
84
+ pages: pages.map((p) => Array.isArray(p) ? p.map((raw) => construct(raw)) : p),
85
+ };
86
+ }
87
+ if (typeof data.id === 'string')
88
+ return construct(data);
89
+ }
90
+ return data;
91
+ };
92
+ }
74
93
  const REVIVERS = {
75
- jobs: (data) => (Array.isArray(data) ? data.map((raw) => constructJob(raw)) : data),
76
- routes: (data) => (Array.isArray(data) ? data.map((raw) => new Route(raw)) : data),
94
+ jobs: reviver(constructJob),
95
+ routes: reviver((raw) => new Route(raw)),
77
96
  };
78
97
  export class QueryPersister {
79
98
  constructor(qc, opts) {
@@ -43,7 +43,13 @@ export interface EntitySpec {
43
43
  export declare function constructJob(raw: any): Job;
44
44
  export declare const REGISTRY: Record<string, EntitySpec>;
45
45
  export declare function removeEntity(qc: QueryClient, ns: string, id: string): void;
46
- export declare function upsertEntity(qc: QueryClient, spec: EntitySpec, item: any): void;
46
+ /**
47
+ * Write an entity into its detail entry and every list it belongs in.
48
+ * `company` is the company the change came from: list scopes are company
49
+ * ids, so another company's lists are left alone (a member of two
50
+ * companies keeps both companies' lists cached).
51
+ */
52
+ export declare function upsertEntity(qc: QueryClient, spec: EntitySpec, item: any, company?: string | null): void;
47
53
  export declare function matchesList(type: string, key: readonly unknown[], item: any): ListMatch;
48
54
  export declare function invalidateType(qc: QueryClient, spec: EntitySpec): void;
49
55
  export declare class ChangeApplier {
@@ -51,7 +57,21 @@ export declare class ChangeApplier {
51
57
  private qc;
52
58
  private pending;
53
59
  private timer;
60
+ /**
61
+ * Latest change sequence per `type:id`. Every change (enqueue, inline
62
+ * data, delete) takes a fresh number; a fetch only applies results for
63
+ * ids whose number is unchanged since it started, so an in-flight
64
+ * fetch can't resurrect a deleted entity or overwrite a newer one.
65
+ */
66
+ private seq;
67
+ private latest;
68
+ /** Company each pending `type:id` change came from (see upsertEntity). */
69
+ private companyOf;
70
+ private inflight;
54
71
  constructor(client: AvroQueryClient, qc: QueryClient);
72
+ private mark;
73
+ /** Settle a change applied inline: only an in-flight fetch needs the mark. */
74
+ private markApplied;
55
75
  apply(payload: ChangesPayload): void;
56
76
  private enqueue;
57
77
  flush(): Promise<void>;