@terreno/ui 57.21.0 → 58.0.0-beta.2

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 (221) hide show
  1. package/.ai/guidelines/core.md +1 -1
  2. package/CHANGELOG.md +89 -0
  3. package/dist/ActionSheet.d.ts +9 -9
  4. package/dist/ActionSheet.d.ts.map +1 -1
  5. package/dist/ActionSheet.js +11 -4
  6. package/dist/ActionSheet.js.map +1 -1
  7. package/dist/AnnouncementNavigator.d.ts +7 -1
  8. package/dist/AnnouncementNavigator.d.ts.map +1 -1
  9. package/dist/AnnouncementNavigator.js +15 -16
  10. package/dist/AnnouncementNavigator.js.map +1 -1
  11. package/dist/Avatar.d.ts.map +1 -1
  12. package/dist/Avatar.js +1 -1
  13. package/dist/Avatar.js.map +1 -1
  14. package/dist/Box.d.ts.map +1 -1
  15. package/dist/Box.js +3 -1
  16. package/dist/Box.js.map +1 -1
  17. package/dist/Common.d.ts +16 -12
  18. package/dist/Common.d.ts.map +1 -1
  19. package/dist/Common.js.map +1 -1
  20. package/dist/ConsentFormScreen.d.ts.map +1 -1
  21. package/dist/ConsentFormScreen.js.map +1 -1
  22. package/dist/ConsentNavigator.d.ts +5 -3
  23. package/dist/ConsentNavigator.d.ts.map +1 -1
  24. package/dist/ConsentNavigator.js +4 -4
  25. package/dist/ConsentNavigator.js.map +1 -1
  26. package/dist/CustomSelectField.js.map +1 -1
  27. package/dist/DataTable.d.ts.map +1 -1
  28. package/dist/DataTable.js +37 -12
  29. package/dist/DataTable.js.map +1 -1
  30. package/dist/DateTimeField.js.map +1 -1
  31. package/dist/DropdownPanel.d.ts +0 -5
  32. package/dist/DropdownPanel.d.ts.map +1 -1
  33. package/dist/DropdownPanel.js +0 -5
  34. package/dist/DropdownPanel.js.map +1 -1
  35. package/dist/FilterAccordion.d.ts.map +1 -1
  36. package/dist/FilterAccordion.js.map +1 -1
  37. package/dist/FilterBoolean.d.ts.map +1 -1
  38. package/dist/FilterBoolean.js.map +1 -1
  39. package/dist/FilterSelectMenu.js +1 -1
  40. package/dist/FilterSelectMenu.js.map +1 -1
  41. package/dist/GPTChat.d.ts.map +1 -1
  42. package/dist/GPTChat.js.map +1 -1
  43. package/dist/Heading.d.ts.map +1 -1
  44. package/dist/Heading.js +6 -12
  45. package/dist/Heading.js.map +1 -1
  46. package/dist/HeightField.d.ts.map +1 -1
  47. package/dist/HeightField.js +1 -1
  48. package/dist/HeightField.js.map +1 -1
  49. package/dist/Hyperlink.d.ts.map +1 -1
  50. package/dist/Hyperlink.js +6 -1
  51. package/dist/Hyperlink.js.map +1 -1
  52. package/dist/Image.d.ts +1 -1
  53. package/dist/MarkdownEditorField.d.ts.map +1 -1
  54. package/dist/MarkdownEditorField.js +1 -1
  55. package/dist/MarkdownEditorField.js.map +1 -1
  56. package/dist/OfflineBanner.d.ts +3 -2
  57. package/dist/OfflineBanner.d.ts.map +1 -1
  58. package/dist/OfflineBanner.js +3 -2
  59. package/dist/OfflineBanner.js.map +1 -1
  60. package/dist/OtpField.d.ts.map +1 -1
  61. package/dist/OtpField.js +6 -3
  62. package/dist/OtpField.js.map +1 -1
  63. package/dist/PickerSelect.d.ts +4 -4
  64. package/dist/PickerSelect.d.ts.map +1 -1
  65. package/dist/PickerSelect.js.map +1 -1
  66. package/dist/ProvisionalConsentSigner.d.ts +3 -2
  67. package/dist/ProvisionalConsentSigner.d.ts.map +1 -1
  68. package/dist/ProvisionalConsentSigner.js +2 -2
  69. package/dist/ProvisionalConsentSigner.js.map +1 -1
  70. package/dist/SidebarNavigation.js +2 -2
  71. package/dist/SidebarNavigation.js.map +1 -1
  72. package/dist/SidebarNavigation.native.js +2 -2
  73. package/dist/SidebarNavigation.native.js.map +1 -1
  74. package/dist/Skeleton.js.map +1 -1
  75. package/dist/Text.d.ts.map +1 -1
  76. package/dist/Text.js +27 -50
  77. package/dist/Text.js.map +1 -1
  78. package/dist/TextField.d.ts.map +1 -1
  79. package/dist/TextField.js.map +1 -1
  80. package/dist/ThumbsUpDownFeedback.d.ts.map +1 -1
  81. package/dist/ThumbsUpDownFeedback.js.map +1 -1
  82. package/dist/ToastNotifications.d.ts.map +1 -1
  83. package/dist/ToastNotifications.js.map +1 -1
  84. package/dist/Tooltip.d.ts.map +1 -1
  85. package/dist/Tooltip.js.map +1 -1
  86. package/dist/Unifier.d.ts +1 -1
  87. package/dist/WebAddressAutocomplete.js.map +1 -1
  88. package/dist/WebDropdownMenu.d.ts +3 -3
  89. package/dist/WebDropdownMenu.d.ts.map +1 -1
  90. package/dist/WebDropdownMenu.js +1 -1
  91. package/dist/WebDropdownMenu.js.map +1 -1
  92. package/dist/announcementPlatform.d.ts +5 -0
  93. package/dist/announcementPlatform.d.ts.map +1 -1
  94. package/dist/announcementPlatform.js +9 -0
  95. package/dist/announcementPlatform.js.map +1 -1
  96. package/dist/asks/AskCard.d.ts +2 -2
  97. package/dist/asks/AskCard.d.ts.map +1 -1
  98. package/dist/dataTableColumnWidths.d.ts +76 -0
  99. package/dist/dataTableColumnWidths.d.ts.map +1 -0
  100. package/dist/dataTableColumnWidths.js +181 -0
  101. package/dist/dataTableColumnWidths.js.map +1 -0
  102. package/dist/index.d.ts +2 -1
  103. package/dist/index.d.ts.map +1 -1
  104. package/dist/index.js.map +1 -1
  105. package/dist/login/LoginScreen.js.map +1 -1
  106. package/dist/requestClient.d.ts +71 -0
  107. package/dist/requestClient.d.ts.map +1 -0
  108. package/dist/requestClient.js +139 -0
  109. package/dist/requestClient.js.map +1 -0
  110. package/dist/security/PasswordCard.d.ts.map +1 -1
  111. package/dist/security/PasswordCard.js.map +1 -1
  112. package/dist/security/TwoFactorSettingsCard.d.ts.map +1 -1
  113. package/dist/security/TwoFactorSettingsCard.js +1 -1
  114. package/dist/security/TwoFactorSettingsCard.js.map +1 -1
  115. package/dist/signUp/SignUpScreen.js.map +1 -1
  116. package/dist/table/Table.d.ts.map +1 -1
  117. package/dist/table/Table.js.map +1 -1
  118. package/dist/table/TableTitle.d.ts.map +1 -1
  119. package/dist/table/TableTitle.js +2 -1
  120. package/dist/table/TableTitle.js.map +1 -1
  121. package/dist/textFontSizes.d.ts +12 -0
  122. package/dist/textFontSizes.d.ts.map +1 -0
  123. package/dist/textFontSizes.js +21 -0
  124. package/dist/textFontSizes.js.map +1 -0
  125. package/dist/useAcknowledgeAnnouncement.d.ts +8 -63
  126. package/dist/useAcknowledgeAnnouncement.d.ts.map +1 -1
  127. package/dist/useAcknowledgeAnnouncement.js +31 -80
  128. package/dist/useAcknowledgeAnnouncement.js.map +1 -1
  129. package/dist/useAnnouncements.d.ts +14 -47
  130. package/dist/useAnnouncements.d.ts.map +1 -1
  131. package/dist/useAnnouncements.js +30 -57
  132. package/dist/useAnnouncements.js.map +1 -1
  133. package/dist/useConsentForms.d.ts +6 -38
  134. package/dist/useConsentForms.d.ts.map +1 -1
  135. package/dist/useConsentForms.js +25 -48
  136. package/dist/useConsentForms.js.map +1 -1
  137. package/dist/useProvisionalConsent.d.ts +7 -58
  138. package/dist/useProvisionalConsent.d.ts.map +1 -1
  139. package/dist/useProvisionalConsent.js +57 -67
  140. package/dist/useProvisionalConsent.js.map +1 -1
  141. package/dist/useSubmitConsent.d.ts +9 -39
  142. package/dist/useSubmitConsent.d.ts.map +1 -1
  143. package/dist/useSubmitConsent.js +10 -39
  144. package/dist/useSubmitConsent.js.map +1 -1
  145. package/package.json +38 -38
  146. package/src/ActionSheet.test.tsx +4 -4
  147. package/src/ActionSheet.tsx +18 -5
  148. package/src/AnnouncementNavigator.test.tsx +215 -315
  149. package/src/AnnouncementNavigator.tsx +25 -20
  150. package/src/Avatar.tsx +2 -9
  151. package/src/Box.test.tsx +3 -3
  152. package/src/Box.tsx +4 -2
  153. package/src/Common.ts +19 -15
  154. package/src/ConsentFormScreen.tsx +2 -1
  155. package/src/ConsentNavigator.test.tsx +166 -242
  156. package/src/ConsentNavigator.tsx +9 -13
  157. package/src/CustomSelectField.tsx +3 -3
  158. package/src/DataTable.test.tsx +69 -1
  159. package/src/DataTable.tsx +72 -19
  160. package/src/DateTimeField.tsx +4 -4
  161. package/src/DropdownPanel.tsx +0 -6
  162. package/src/FilterAccordion.tsx +3 -3
  163. package/src/FilterBoolean.tsx +3 -3
  164. package/src/FilterSelectMenu.tsx +1 -1
  165. package/src/GPTChat.tsx +10 -9
  166. package/src/Heading.tsx +7 -14
  167. package/src/HeightField.tsx +9 -2
  168. package/src/Hyperlink.tsx +6 -3
  169. package/src/Link.test.tsx +2 -2
  170. package/src/MarkdownEditorField.tsx +10 -2
  171. package/src/MarkdownView.test.tsx +10 -6
  172. package/src/Modal.test.tsx +3 -1
  173. package/src/ModalSheet.test.tsx +6 -6
  174. package/src/OfflineBanner.tsx +3 -2
  175. package/src/OtpField.test.tsx +44 -0
  176. package/src/OtpField.tsx +8 -4
  177. package/src/PickerSelect.tsx +3 -2
  178. package/src/ProvisionalConsentSigner.test.tsx +140 -113
  179. package/src/ProvisionalConsentSigner.tsx +5 -4
  180. package/src/SidebarNavigation.native.tsx +2 -2
  181. package/src/SidebarNavigation.tsx +2 -2
  182. package/src/Skeleton.tsx +1 -1
  183. package/src/TapToEdit.test.tsx +2 -2
  184. package/src/Text.test.tsx +1 -1
  185. package/src/Text.tsx +25 -51
  186. package/src/TextField.tsx +2 -1
  187. package/src/ThumbsUpDownFeedback.tsx +3 -3
  188. package/src/ToastNotifications.tsx +3 -2
  189. package/src/ToastNotifications.webModal.test.tsx +4 -1
  190. package/src/Tooltip.tsx +2 -1
  191. package/src/UserInactivity.test.tsx +2 -3
  192. package/src/WebAddressAutocomplete.tsx +3 -3
  193. package/src/WebDropdownMenu.tsx +16 -5
  194. package/src/__snapshots__/DataTable.test.tsx.snap +13 -0
  195. package/src/announcementPlatform.ts +10 -0
  196. package/src/asks/AskCard.test.tsx +14 -12
  197. package/src/asks/AskCard.tsx +2 -2
  198. package/src/bunSetup.ts +42 -0
  199. package/src/dataTableColumnWidths.test.ts +266 -0
  200. package/src/dataTableColumnWidths.ts +272 -0
  201. package/src/index.tsx +9 -2
  202. package/src/login/LoginScreen.tsx +2 -2
  203. package/src/requestClient.test.ts +288 -0
  204. package/src/requestClient.ts +228 -0
  205. package/src/security/PasswordCard.tsx +7 -5
  206. package/src/security/TwoFactorSettingsCard.tsx +10 -3
  207. package/src/signUp/SignUpScreen.tsx +2 -2
  208. package/src/table/Table.tsx +1 -2
  209. package/src/table/TableTitle.tsx +2 -1
  210. package/src/test-utils.tsx +76 -0
  211. package/src/textFontSizes.ts +31 -0
  212. package/src/useAcknowledgeAnnouncement.test.ts +101 -193
  213. package/src/useAcknowledgeAnnouncement.ts +69 -147
  214. package/src/useAnnouncements.test.ts +122 -186
  215. package/src/useAnnouncements.ts +49 -105
  216. package/src/useConsentForms.test.ts +59 -112
  217. package/src/useConsentForms.ts +37 -87
  218. package/src/useProvisionalConsent.test.ts +74 -150
  219. package/src/useProvisionalConsent.ts +79 -125
  220. package/src/useSubmitConsent.test.ts +59 -85
  221. package/src/useSubmitConsent.ts +22 -77
@@ -0,0 +1,288 @@
1
+ import {describe, expect, it, mock} from "bun:test";
2
+ import {act, renderHook, waitFor} from "@testing-library/react-native";
3
+ import {useCallback} from "react";
4
+
5
+ import {
6
+ invalidateClientTags,
7
+ type TerrenoRequestClient,
8
+ unwrapDataEnvelope,
9
+ useClientMutation,
10
+ useClientQuery,
11
+ } from "./requestClient";
12
+ import {createFakeRequestClient, fakeHttpError} from "./test-utils";
13
+
14
+ // Mirror of `@terreno/syncdb/api` ApiRequestArgs / ApiClient (ui cannot import syncdb).
15
+ // The assignment below fails to compile if TerrenoRequestClient stops being a structural
16
+ // subset of the syncdb client.
17
+ interface SyncdbApiRequestArgs {
18
+ url: string;
19
+ method?: string;
20
+ body?: BodyInit | Record<string, unknown> | readonly unknown[] | null;
21
+ params?: Record<string, unknown>;
22
+ headers?: Record<string, string>;
23
+ credentials?: RequestCredentials;
24
+ signal?: AbortSignal;
25
+ timeoutMs?: number;
26
+ parseAs?: "json" | "text" | "blob";
27
+ unwrap?: boolean;
28
+ }
29
+
30
+ interface SyncdbApiClientShape {
31
+ readonly baseUrl: string | undefined;
32
+ request: <T = unknown>(args: SyncdbApiRequestArgs) => Promise<T>;
33
+ invalidateTags: (tags: readonly string[]) => void;
34
+ reset: () => void;
35
+ }
36
+
37
+ const deferred = <T>(): {
38
+ promise: Promise<T>;
39
+ reject: (error: unknown) => void;
40
+ resolve: (value: T) => void;
41
+ } => {
42
+ let resolve: (value: T) => void = () => {};
43
+ let reject: (error: unknown) => void = () => {};
44
+ const promise = new Promise<T>((res, rej) => {
45
+ resolve = res;
46
+ reject = rej;
47
+ });
48
+ return {promise, reject, resolve};
49
+ };
50
+
51
+ const useCountQuery = (
52
+ client: TerrenoRequestClient,
53
+ providesTag?: string
54
+ ): ReturnType<typeof useClientQuery<number>> => {
55
+ const fetcher = useCallback(() => client.request<number>({url: "/count"}), [client]);
56
+ return useClientQuery({client, fetcher, providesTag});
57
+ };
58
+
59
+ describe("TerrenoRequestClient", () => {
60
+ it("accepts a syncdb ApiClient structurally", async () => {
61
+ const syncdbClient: SyncdbApiClientShape = {
62
+ baseUrl: "http://api.test",
63
+ invalidateTags: () => {},
64
+ request: async <T>(args: SyncdbApiRequestArgs): Promise<T> => args.url as T,
65
+ reset: () => {},
66
+ };
67
+ const client: TerrenoRequestClient = syncdbClient;
68
+ expect(await client.request<string>({url: "/consents/pending"})).toBe("/consents/pending");
69
+ });
70
+ });
71
+
72
+ describe("useClientQuery", () => {
73
+ it("loads on mount and exposes data", async () => {
74
+ const {client} = createFakeRequestClient({"GET /count": () => 3});
75
+ const {result} = renderHook(() => useCountQuery(client));
76
+ expect(result.current.isLoading).toBe(true);
77
+ await waitFor(() => expect(result.current.data).toBe(3));
78
+ expect(result.current.isLoading).toBe(false);
79
+ expect(result.current.isFetching).toBe(false);
80
+ expect(result.current.error).toBeUndefined();
81
+ });
82
+
83
+ it("surfaces request errors and recovers on refetch", async () => {
84
+ let shouldFail = true;
85
+ const {client} = createFakeRequestClient({
86
+ "GET /count": () => {
87
+ if (shouldFail) {
88
+ throw fakeHttpError(500);
89
+ }
90
+ return 7;
91
+ },
92
+ });
93
+ const {result} = renderHook(() => useCountQuery(client));
94
+ await waitFor(() => expect((result.current.error as {status?: number})?.status).toBe(500));
95
+ expect(result.current.isLoading).toBe(false);
96
+
97
+ shouldFail = false;
98
+ await act(async () => {
99
+ await result.current.refetch();
100
+ });
101
+ expect(result.current.data).toBe(7);
102
+ expect(result.current.error).toBeUndefined();
103
+ });
104
+
105
+ it("joins an in-flight request on refetch", async () => {
106
+ const pending = deferred<number>();
107
+ const {client, calls} = createFakeRequestClient({"GET /count": () => pending.promise});
108
+ const {result} = renderHook(() => useCountQuery(client));
109
+ let refetchPromise: Promise<void> = Promise.resolve();
110
+ act(() => {
111
+ refetchPromise = result.current.refetch();
112
+ });
113
+ expect(calls).toHaveLength(1);
114
+ await act(async () => {
115
+ pending.resolve(5);
116
+ await refetchPromise;
117
+ });
118
+ expect(result.current.data).toBe(5);
119
+ });
120
+
121
+ it("keeps data while refetching so isLoading stays false", async () => {
122
+ let value = 1;
123
+ const next = deferred<number>();
124
+ const {client} = createFakeRequestClient({
125
+ "GET /count": () => (value === 1 ? value : next.promise),
126
+ });
127
+ const {result} = renderHook(() => useCountQuery(client));
128
+ await waitFor(() => expect(result.current.data).toBe(1));
129
+ value = 2;
130
+ let refetchPromise: Promise<void> = Promise.resolve();
131
+ act(() => {
132
+ refetchPromise = result.current.refetch();
133
+ });
134
+ expect(result.current.isFetching).toBe(true);
135
+ expect(result.current.isLoading).toBe(false);
136
+ await act(async () => {
137
+ next.resolve(2);
138
+ await refetchPromise;
139
+ });
140
+ expect(result.current.data).toBe(2);
141
+ });
142
+
143
+ it("refetches with a fresh request when its tag is invalidated", async () => {
144
+ let value = 1;
145
+ const {client, calls} = createFakeRequestClient({"GET /count": () => value});
146
+ const {result} = renderHook(() => useCountQuery(client, "Counts"));
147
+ await waitFor(() => expect(result.current.data).toBe(1));
148
+
149
+ value = 2;
150
+ await act(async () => {
151
+ invalidateClientTags({client, tags: ["Counts"]});
152
+ });
153
+ await waitFor(() => expect(result.current.data).toBe(2));
154
+ expect(calls).toHaveLength(2);
155
+ });
156
+
157
+ it("ignores invalidations for other tags and other clients", async () => {
158
+ const first = createFakeRequestClient({"GET /count": () => 1});
159
+ const second = createFakeRequestClient({"GET /count": () => 1});
160
+ const {result} = renderHook(() => useCountQuery(first.client, "Counts"));
161
+ await waitFor(() => expect(result.current.data).toBe(1));
162
+
163
+ await act(async () => {
164
+ invalidateClientTags({client: first.client, tags: ["Other"]});
165
+ invalidateClientTags({client: second.client, tags: ["Counts"]});
166
+ });
167
+ expect(first.calls).toHaveLength(1);
168
+ });
169
+
170
+ it("stops listening for invalidations after unmount", async () => {
171
+ const {client, calls} = createFakeRequestClient({"GET /count": () => 1});
172
+ const {result, unmount} = renderHook(() => useCountQuery(client, "Counts"));
173
+ await waitFor(() => expect(result.current.data).toBe(1));
174
+ unmount();
175
+ invalidateClientTags({client, tags: ["Counts"]});
176
+ expect(calls).toHaveLength(1);
177
+ });
178
+
179
+ it("drops a stale response when a newer request was started", async () => {
180
+ const firstResponse = deferred<number>();
181
+ let callCount = 0;
182
+ const {client} = createFakeRequestClient({
183
+ "GET /count": () => {
184
+ callCount += 1;
185
+ return callCount === 1 ? firstResponse.promise : 2;
186
+ },
187
+ });
188
+ const {result} = renderHook(() => useCountQuery(client, "Counts"));
189
+ await act(async () => {
190
+ invalidateClientTags({client, tags: ["Counts"]});
191
+ });
192
+ await waitFor(() => expect(result.current.data).toBe(2));
193
+ await act(async () => {
194
+ firstResponse.resolve(1);
195
+ await firstResponse.promise;
196
+ });
197
+ expect(result.current.data).toBe(2);
198
+ });
199
+
200
+ it("drops a stale error when a newer request was started", async () => {
201
+ const firstResponse = deferred<number>();
202
+ let callCount = 0;
203
+ const {client} = createFakeRequestClient({
204
+ "GET /count": () => {
205
+ callCount += 1;
206
+ return callCount === 1 ? firstResponse.promise : 2;
207
+ },
208
+ });
209
+ const {result} = renderHook(() => useCountQuery(client, "Counts"));
210
+ await act(async () => {
211
+ invalidateClientTags({client, tags: ["Counts"]});
212
+ });
213
+ await waitFor(() => expect(result.current.data).toBe(2));
214
+ await act(async () => {
215
+ firstResponse.reject(fakeHttpError(500));
216
+ await firstResponse.promise.catch(() => {});
217
+ });
218
+ expect(result.current.error).toBeUndefined();
219
+ });
220
+ });
221
+
222
+ describe("useClientMutation", () => {
223
+ it("tracks loading and resolves when the request succeeds", async () => {
224
+ const pending = deferred<void>();
225
+ const run = mock((_value: string) => pending.promise);
226
+ const {result} = renderHook(() => useClientMutation({run}));
227
+ let mutatePromise: Promise<void> = Promise.resolve();
228
+ act(() => {
229
+ mutatePromise = result.current.mutate("a");
230
+ });
231
+ expect(result.current.isLoading).toBe(true);
232
+ await act(async () => {
233
+ pending.resolve();
234
+ await mutatePromise;
235
+ });
236
+ expect(result.current.isLoading).toBe(false);
237
+ expect(run).toHaveBeenCalledWith("a");
238
+ });
239
+
240
+ it("stores and rethrows the request error", async () => {
241
+ const failure = fakeHttpError(400);
242
+ const {result} = renderHook(() =>
243
+ useClientMutation({
244
+ run: async (): Promise<void> => {
245
+ throw failure;
246
+ },
247
+ })
248
+ );
249
+ let caught: unknown;
250
+ await act(async () => {
251
+ await result.current.mutate(undefined).catch((error: unknown) => {
252
+ caught = error;
253
+ });
254
+ });
255
+ expect(caught).toBe(failure);
256
+ expect(result.current.error).toBe(failure);
257
+ expect(result.current.isLoading).toBe(false);
258
+ });
259
+
260
+ it("keeps a stable mutate identity and calls the latest run", async () => {
261
+ const firstRun = mock(async (): Promise<void> => {});
262
+ const secondRun = mock(async (): Promise<void> => {});
263
+ const {result, rerender} = renderHook(
264
+ ({run}: {run: () => Promise<void>}) => useClientMutation({run}),
265
+ {initialProps: {run: firstRun}}
266
+ );
267
+ const firstMutate = result.current.mutate;
268
+ rerender({run: secondRun});
269
+ expect(result.current.mutate).toBe(firstMutate);
270
+ await act(async () => {
271
+ await result.current.mutate(undefined);
272
+ });
273
+ expect(firstRun).not.toHaveBeenCalled();
274
+ expect(secondRun).toHaveBeenCalled();
275
+ });
276
+ });
277
+
278
+ describe("unwrapDataEnvelope", () => {
279
+ it("unwraps {data} envelopes", () => {
280
+ expect(unwrapDataEnvelope<number[]>({data: [1]})).toEqual([1]);
281
+ });
282
+
283
+ it("returns bare payloads unchanged", () => {
284
+ expect(unwrapDataEnvelope<number[]>([1])).toEqual([1]);
285
+ expect(unwrapDataEnvelope<{current: null}>({current: null})).toEqual({current: null});
286
+ expect(unwrapDataEnvelope(undefined)).toBeUndefined();
287
+ });
288
+ });
@@ -0,0 +1,228 @@
1
+ import {useCallback, useEffect, useRef, useState} from "react";
2
+
3
+ /**
4
+ * One request made through a {@link TerrenoRequestClient}. A narrow subset of the
5
+ * `@terreno/syncdb/api` `ApiRequestArgs` shape, so a syncdb `ApiClient` satisfies
6
+ * {@link TerrenoRequestClient} without `@terreno/ui` depending on syncdb.
7
+ */
8
+ export interface TerrenoRequestArgs {
9
+ /** Path or absolute URL. Relative paths are resolved against the client's base URL. */
10
+ url: string;
11
+ /** HTTP method; defaults to `GET`. */
12
+ method?: string;
13
+ /** Query-string parameters. `undefined` values are omitted. */
14
+ params?: Record<string, unknown>;
15
+ /** JSON request body. */
16
+ body?: Record<string, unknown> | null;
17
+ headers?: Record<string, string>;
18
+ /** Unwrap `{data}` response envelopes (default `true`). */
19
+ unwrap?: boolean;
20
+ }
21
+
22
+ /**
23
+ * Minimal request client the consent and announcement components use. Pass the app's
24
+ * `@terreno/syncdb/api` client (`createApiClient(...)`), or any object with a matching
25
+ * `request` method. Failed requests should reject with an error carrying the HTTP
26
+ * `status` so 401/403 responses fall through to the app's auth handling.
27
+ */
28
+ export interface TerrenoRequestClient {
29
+ request: <T = unknown>(args: TerrenoRequestArgs) => Promise<T>;
30
+ }
31
+
32
+ type TagListener = () => void;
33
+
34
+ const tagListenersByClient = new WeakMap<TerrenoRequestClient, Map<string, Set<TagListener>>>();
35
+
36
+ /** Listen for invalidations of `tag` on `client`. Returns unsubscribe. */
37
+ const subscribeClientTag = ({
38
+ client,
39
+ listener,
40
+ tag,
41
+ }: {
42
+ client: TerrenoRequestClient;
43
+ listener: TagListener;
44
+ tag: string;
45
+ }): (() => void) => {
46
+ let byTag = tagListenersByClient.get(client);
47
+ if (!byTag) {
48
+ byTag = new Map();
49
+ tagListenersByClient.set(client, byTag);
50
+ }
51
+ let listeners = byTag.get(tag);
52
+ if (!listeners) {
53
+ listeners = new Set();
54
+ byTag.set(tag, listeners);
55
+ }
56
+ listeners.add(listener);
57
+ return (): void => {
58
+ listeners.delete(listener);
59
+ };
60
+ };
61
+
62
+ /**
63
+ * Refetch every mounted {@link useClientQuery} on `client` that provides one of `tags`.
64
+ * Mutations call this after success, the way RTK `invalidatesTags` did.
65
+ */
66
+ export const invalidateClientTags = ({
67
+ client,
68
+ tags,
69
+ }: {
70
+ client: TerrenoRequestClient;
71
+ tags: readonly string[];
72
+ }): void => {
73
+ const byTag = tagListenersByClient.get(client);
74
+ if (!byTag) {
75
+ return;
76
+ }
77
+ for (const tag of tags) {
78
+ for (const listener of [...(byTag.get(tag) ?? [])]) {
79
+ listener();
80
+ }
81
+ }
82
+ };
83
+
84
+ export interface ClientQueryResult<T> {
85
+ data: T | undefined;
86
+ error: unknown;
87
+ /** True while a request is in flight and none has succeeded yet. */
88
+ isLoading: boolean;
89
+ /** True while any request is in flight. */
90
+ isFetching: boolean;
91
+ /** Fetch again. Joins a request that is already in flight. */
92
+ refetch: () => Promise<void>;
93
+ }
94
+
95
+ /**
96
+ * Run `fetcher` on mount and whenever its identity changes, keeping loading / error /
97
+ * data in React state. `providesTag` refetches the query when a mutation invalidates it.
98
+ */
99
+ export const useClientQuery = <T>({
100
+ client,
101
+ fetcher,
102
+ providesTag,
103
+ }: {
104
+ client: TerrenoRequestClient;
105
+ /** Memoize with `useCallback`; a new identity triggers a new request. */
106
+ fetcher: () => Promise<T>;
107
+ providesTag?: string;
108
+ }): ClientQueryResult<T> => {
109
+ const [data, setData] = useState<T | undefined>(undefined);
110
+ const [hasData, setHasData] = useState(false);
111
+ const [error, setError] = useState<unknown>(undefined);
112
+ const [isFetching, setIsFetching] = useState(true);
113
+ const generationRef = useRef(0);
114
+ const inFlightRef = useRef<Promise<void> | undefined>(undefined);
115
+ const isMountedRef = useRef(true);
116
+
117
+ const run = useCallback((): Promise<void> => {
118
+ generationRef.current += 1;
119
+ const generation = generationRef.current;
120
+ setIsFetching(true);
121
+ setError(undefined);
122
+ const promise = fetcher().then(
123
+ (result) => {
124
+ if (generation !== generationRef.current || !isMountedRef.current) {
125
+ return;
126
+ }
127
+ setData(result);
128
+ setHasData(true);
129
+ setIsFetching(false);
130
+ },
131
+ (fetchError: unknown) => {
132
+ if (generation !== generationRef.current || !isMountedRef.current) {
133
+ return;
134
+ }
135
+ setError(fetchError);
136
+ setIsFetching(false);
137
+ }
138
+ );
139
+ const tracked = promise.finally(() => {
140
+ if (inFlightRef.current === tracked) {
141
+ inFlightRef.current = undefined;
142
+ }
143
+ });
144
+ inFlightRef.current = tracked;
145
+ return tracked;
146
+ }, [fetcher]);
147
+
148
+ const refetch = useCallback((): Promise<void> => {
149
+ return inFlightRef.current ?? run();
150
+ }, [run]);
151
+
152
+ // Fetch on mount and whenever the fetcher (client, URL, or params) changes.
153
+ useEffect(() => {
154
+ isMountedRef.current = true;
155
+ void run();
156
+ return (): void => {
157
+ isMountedRef.current = false;
158
+ inFlightRef.current = undefined;
159
+ };
160
+ }, [run]);
161
+
162
+ // Refetch with a fresh request when a mutation invalidates this query's tag.
163
+ useEffect(() => {
164
+ if (!providesTag) {
165
+ return undefined;
166
+ }
167
+ return subscribeClientTag({
168
+ client,
169
+ listener: () => {
170
+ void run();
171
+ },
172
+ tag: providesTag,
173
+ });
174
+ }, [client, providesTag, run]);
175
+
176
+ return {
177
+ data,
178
+ error,
179
+ isFetching,
180
+ isLoading: isFetching && !hasData,
181
+ refetch,
182
+ };
183
+ };
184
+
185
+ export interface ClientMutationResult<A> {
186
+ /** Run the mutation; rejects with the request error. */
187
+ mutate: (args: A) => Promise<void>;
188
+ error: unknown;
189
+ isLoading: boolean;
190
+ }
191
+
192
+ /**
193
+ * Track loading / error state for a request that changes server state. `mutate` keeps a
194
+ * stable identity across renders so it is safe in effect dependency lists.
195
+ */
196
+ export const useClientMutation = <A>({
197
+ run,
198
+ }: {
199
+ run: (args: A) => Promise<void>;
200
+ }): ClientMutationResult<A> => {
201
+ const [error, setError] = useState<unknown>(undefined);
202
+ const [pendingCount, setPendingCount] = useState(0);
203
+ const runRef = useRef(run);
204
+ runRef.current = run;
205
+
206
+ const mutate = useCallback(async (args: A): Promise<void> => {
207
+ setPendingCount((count) => count + 1);
208
+ setError(undefined);
209
+ try {
210
+ await runRef.current(args);
211
+ } catch (mutationError) {
212
+ setError(mutationError);
213
+ throw mutationError;
214
+ } finally {
215
+ setPendingCount((count) => count - 1);
216
+ }
217
+ }, []);
218
+
219
+ return {error, isLoading: pendingCount > 0, mutate};
220
+ };
221
+
222
+ /** Accept both bare payloads and `{data}` envelopes (when a client does not unwrap). */
223
+ export const unwrapDataEnvelope = <T>(value: unknown): T | undefined => {
224
+ if (value && typeof value === "object" && !Array.isArray(value) && "data" in value) {
225
+ return (value as {data?: T}).data;
226
+ }
227
+ return value as T | undefined;
228
+ };
@@ -1,5 +1,5 @@
1
1
  import {type FC, useCallback, useEffect, useRef, useState} from "react";
2
- import {type TextInput, View} from "react-native";
2
+ import {type TextInputInstance, View} from "react-native";
3
3
 
4
4
  import {Box} from "../Box";
5
5
  import {Button} from "../Button";
@@ -98,16 +98,18 @@ export const PasswordCard: FC<PasswordCardProps> = ({
98
98
  const [successMessage, setSuccessMessage] = useState<string | undefined>(undefined);
99
99
  const isSubmittingRef = useRef(false);
100
100
  const previousModeRef = useRef(mode);
101
- const inputRefs = useRef<Partial<Record<keyof PasswordFieldErrors, TextInput | null>>>({});
101
+ const inputRefs = useRef<Partial<Record<keyof PasswordFieldErrors, TextInputInstance | null>>>(
102
+ {}
103
+ );
102
104
  const formRef = useRef<FormContainerHandle>(null);
103
105
 
104
- const setCurrentInputRef = useCallback((ref: TextInput | null): void => {
106
+ const setCurrentInputRef = useCallback((ref: TextInputInstance | null): void => {
105
107
  inputRefs.current.currentPassword = ref;
106
108
  }, []);
107
- const setNewInputRef = useCallback((ref: TextInput | null): void => {
109
+ const setNewInputRef = useCallback((ref: TextInputInstance | null): void => {
108
110
  inputRefs.current.newPassword = ref;
109
111
  }, []);
110
- const setConfirmInputRef = useCallback((ref: TextInput | null): void => {
112
+ const setConfirmInputRef = useCallback((ref: TextInputInstance | null): void => {
111
113
  inputRefs.current.confirmPassword = ref;
112
114
  }, []);
113
115
 
@@ -1,6 +1,13 @@
1
1
  import * as Clipboard from "expo-clipboard";
2
2
  import {type FC, type ReactElement, useCallback, useEffect, useRef, useState} from "react";
3
- import {AccessibilityInfo, Platform, Share, type TextInput, View} from "react-native";
3
+ import {
4
+ AccessibilityInfo,
5
+ Platform,
6
+ Share,
7
+ type TextInputInstance,
8
+ View,
9
+ type ViewInstance,
10
+ } from "react-native";
4
11
  import QRCode from "react-native-qrcode-svg";
5
12
 
6
13
  import {Box} from "../Box";
@@ -223,8 +230,8 @@ export const TwoFactorSettingsCard: FC<TwoFactorSettingsCardProps> = ({
223
230
  const [pendingAction, setPendingAction] = useState<PendingAction | undefined>(undefined);
224
231
  const [actionError, setActionError] = useState<string | undefined>(undefined);
225
232
  const [copyFeedback, setCopyFeedback] = useState<CopyFeedback | undefined>(undefined);
226
- const passwordInputRef = useRef<TextInput | null>(null);
227
- const stepHeadingRef = useRef<View | null>(null);
233
+ const passwordInputRef = useRef<TextInputInstance | null>(null);
234
+ const stepHeadingRef = useRef<ViewInstance | null>(null);
228
235
  const isFirstRenderRef = useRef(true);
229
236
 
230
237
  const isBusy = disabled || pendingAction !== undefined;
@@ -1,6 +1,6 @@
1
1
  import type {FC} from "react";
2
2
  import {useCallback, useRef, useState} from "react";
3
- import type {TextInput} from "react-native";
3
+ import type {TextInputInstance} from "react-native";
4
4
 
5
5
  import {Box} from "../Box";
6
6
  import {Button} from "../Button";
@@ -94,7 +94,7 @@ export const SignUpScreen: FC<SignUpScreenProps> = ({
94
94
  const isSubmitDisabled = loading || !requiredFieldsFilled || !allRequirementsMet;
95
95
 
96
96
  const formRef = useRef<FormContainerHandle>(null);
97
- const inputRefs = useRef<Record<string, TextInput | null>>({});
97
+ const inputRefs = useRef<Record<string, TextInputInstance | null>>({});
98
98
  // Blocks a second submission (Enter plus a press, or a double Enter) before `loading` lands.
99
99
  const isSubmittingRef = useRef(false);
100
100
 
@@ -1,6 +1,5 @@
1
1
  import React, {Children, type ReactElement, useEffect} from "react";
2
- import {ScrollView} from "react-native";
3
- import type {DimensionValue} from "react-native/Libraries/StyleSheet/StyleSheetTypes";
2
+ import {type DimensionValue, ScrollView} from "react-native";
4
3
 
5
4
  import {Box} from "../Box";
6
5
  import type {ColumnSortInterface, TableProps, TableRowProps} from "../Common";
@@ -2,6 +2,7 @@ import type {FC} from "react";
2
2
  import {Text} from "react-native";
3
3
 
4
4
  import type {TableTitleProps} from "../Common";
5
+ import {DATA_TABLE_HEADER_FONT_SIZE} from "../dataTableColumnWidths";
5
6
  import {useTheme} from "../Theme";
6
7
 
7
8
  export const TableTitle: FC<TableTitleProps> = ({title, align = "left"}) => {
@@ -17,7 +18,7 @@ export const TableTitle: FC<TableTitleProps> = ({title, align = "left"}) => {
17
18
  color: theme.text.primary,
18
19
  flexWrap: "wrap",
19
20
  fontFamily: "text",
20
- fontSize: 10,
21
+ fontSize: DATA_TABLE_HEADER_FONT_SIZE,
21
22
  fontWeight: "700",
22
23
  lineHeight: 16,
23
24
  overflow: "hidden",