@indietabletop/appkit 3.6.0-2 → 4.0.0-0

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 (60) hide show
  1. package/lib/AppConfig/AppConfig.tsx +48 -0
  2. package/lib/AuthCard/AuthCard.stories.ts +38 -0
  3. package/lib/AuthCard/AuthCard.tsx +64 -0
  4. package/lib/AuthCard/style.css.ts +49 -0
  5. package/lib/DialogTrigger/index.tsx +2 -2
  6. package/lib/DocumentTitle/DocumentTitle.tsx +9 -0
  7. package/lib/HistoryState.ts +21 -0
  8. package/lib/Letterhead/style.css.ts +2 -0
  9. package/lib/LetterheadForm/index.tsx +53 -0
  10. package/lib/LetterheadForm/style.css.ts +8 -0
  11. package/lib/LoadingIndicator.tsx +1 -0
  12. package/lib/MiddotSeparated/MiddotSeparated.stories.ts +32 -0
  13. package/lib/MiddotSeparated/MiddotSeparated.tsx +24 -0
  14. package/lib/MiddotSeparated/style.css.ts +10 -0
  15. package/lib/ModernIDB/bindings/factory.tsx +8 -3
  16. package/lib/QRCode/QRCode.stories.tsx +47 -0
  17. package/lib/QRCode/QRCode.tsx +49 -0
  18. package/lib/QRCode/style.css.ts +19 -0
  19. package/lib/ShareButton/ShareButton.tsx +141 -0
  20. package/lib/SubscribeCard/LetterheadInfoCard.tsx +23 -0
  21. package/lib/SubscribeCard/SubscribeByEmailCard.stories.tsx +83 -0
  22. package/lib/SubscribeCard/SubscribeByEmailCard.tsx +177 -0
  23. package/lib/SubscribeCard/SubscribeCard.stories.tsx +27 -23
  24. package/lib/SubscribeCard/SubscribeCard.tsx +17 -16
  25. package/lib/account/AccountIssueView.tsx +40 -0
  26. package/lib/account/AlreadyLoggedInView.tsx +44 -0
  27. package/lib/account/CurrentUserFetcher.stories.tsx +325 -0
  28. package/lib/account/CurrentUserFetcher.tsx +133 -0
  29. package/lib/account/FailureFallbackView.tsx +36 -0
  30. package/lib/account/JoinCard.stories.tsx +264 -0
  31. package/lib/account/JoinCard.tsx +291 -0
  32. package/lib/account/LoadingView.tsx +14 -0
  33. package/lib/account/LoginCard.stories.tsx +316 -0
  34. package/lib/account/LoginCard.tsx +141 -0
  35. package/lib/account/LoginView.tsx +136 -0
  36. package/lib/account/NoConnectionView.tsx +34 -0
  37. package/lib/account/PasswordResetCard.stories.tsx +247 -0
  38. package/lib/account/PasswordResetCard.tsx +296 -0
  39. package/lib/account/UserMismatchView.tsx +61 -0
  40. package/lib/account/VerifyPage.tsx +217 -0
  41. package/lib/account/style.css.ts +57 -0
  42. package/lib/account/types.ts +9 -0
  43. package/lib/account/useCurrentUserResult.tsx +38 -0
  44. package/lib/class-names.ts +1 -1
  45. package/lib/client.ts +54 -7
  46. package/lib/globals.css.ts +9 -0
  47. package/lib/hrefs.ts +48 -0
  48. package/lib/idToDate.ts +8 -0
  49. package/lib/index.ts +19 -1
  50. package/lib/mailto.test.ts +66 -0
  51. package/lib/mailto.ts +40 -0
  52. package/lib/types.ts +17 -0
  53. package/lib/useEnsureValue.ts +31 -0
  54. package/lib/utm.ts +89 -0
  55. package/package.json +3 -2
  56. package/lib/ClientContext/ClientContext.tsx +0 -25
  57. package/lib/LoginPage/LoginPage.stories.tsx +0 -107
  58. package/lib/LoginPage/LoginPage.tsx +0 -204
  59. package/lib/LoginPage/style.css.ts +0 -17
  60. package/lib/Title/index.tsx +0 -4
@@ -0,0 +1,325 @@
1
+ import { Story } from "@storybook/addon-docs/blocks";
2
+ import type { Meta, StoryObj } from "@storybook/react-vite";
3
+ import { http, HttpResponse } from "msw";
4
+ import { fn } from "storybook/test";
5
+ import { sleep } from "../sleep.ts";
6
+ import type { CurrentUser, SessionInfo } from "../types.ts";
7
+ import { CurrentUserFetcher } from "./CurrentUserFetcher.tsx";
8
+
9
+ function createMocks(options?: { responseSpeed?: number }) {
10
+ const simulateNetwork = () => sleep(options?.responseSpeed ?? 2000);
11
+
12
+ const john: CurrentUser = {
13
+ id: "john",
14
+ email: "john@example.com",
15
+ isVerified: true,
16
+ };
17
+
18
+ const mary: CurrentUser = {
19
+ id: "mary",
20
+ email: "mary@example.com",
21
+ isVerified: true,
22
+ };
23
+
24
+ const vernon: CurrentUser = {
25
+ id: "vernon",
26
+ email: "vernon@example.com",
27
+ isVerified: false,
28
+ };
29
+
30
+ const sessionInfo: SessionInfo = {
31
+ createdTs: 123,
32
+ expiresTs: 123,
33
+ };
34
+
35
+ return {
36
+ data: { john, mary, vernon },
37
+ handlers: {
38
+ refreshTokens: {
39
+ success: () => {
40
+ return http.post(
41
+ "http://mock.api/v1/sessions/access-tokens",
42
+ async () => {
43
+ await simulateNetwork();
44
+ return HttpResponse.json({ sessionInfo });
45
+ },
46
+ );
47
+ },
48
+
49
+ failed: () => {
50
+ return http.post(
51
+ "http://mock.api/v1/sessions/access-tokens",
52
+ async () => {
53
+ await simulateNetwork();
54
+ return HttpResponse.text("Refresh token expired or missing", {
55
+ status: 401,
56
+ });
57
+ },
58
+ );
59
+ },
60
+ },
61
+
62
+ getCurrentUser: {
63
+ success: (currentUser: CurrentUser) => {
64
+ return http.get("http://mock.api/v1/users/me", async () => {
65
+ await simulateNetwork();
66
+ return HttpResponse.json(currentUser);
67
+ });
68
+ },
69
+
70
+ /**
71
+ * Cookie is valid, but user doesn't exist any more. This can happen
72
+ * after user deletion.
73
+ */
74
+ notFound: () => {
75
+ return http.get("http://mock.api/v1/users/me", async () => {
76
+ await simulateNetwork();
77
+ return HttpResponse.text("User not found", { status: 404 });
78
+ });
79
+ },
80
+
81
+ noConnection: () => {
82
+ return http.get("http://mock.api/v1/users/me", async () => {
83
+ return HttpResponse.error();
84
+ });
85
+ },
86
+
87
+ unknownFailure: () => {
88
+ return http.get("http://mock.api/v1/users/me", async () => {
89
+ await simulateNetwork();
90
+ return HttpResponse.text("Internal server error", { status: 500 });
91
+ });
92
+ },
93
+
94
+ /**
95
+ * Auth cookies no longer valid to make this request.
96
+ */
97
+ notAuthenticated: () => {
98
+ return http.get("http://mock.api/v1/users/me", async () => {
99
+ await simulateNetwork();
100
+ return HttpResponse.text("Not authenticated", { status: 401 });
101
+ });
102
+ },
103
+ },
104
+
105
+ createNewSession: {
106
+ success: (currentUser: CurrentUser) => {
107
+ return http.post("http://mock.api/v1/sessions", async () => {
108
+ await simulateNetwork();
109
+ return HttpResponse.json({ currentUser, sessionInfo });
110
+ });
111
+ },
112
+
113
+ invalidCredentials: () => {
114
+ return http.post("http://mock.api/v1/sessions", async () => {
115
+ await simulateNetwork();
116
+ return HttpResponse.text("Credentials do not match", {
117
+ status: 401,
118
+ });
119
+ });
120
+ },
121
+
122
+ userNotFound: () => {
123
+ return http.post("http://mock.api/v1/sessions", async () => {
124
+ await simulateNetwork();
125
+ return HttpResponse.text("User not found", { status: 404 });
126
+ });
127
+ },
128
+
129
+ unknownFailure: () => {
130
+ return http.post("http://mock.api/v1/sessions", async () => {
131
+ await simulateNetwork();
132
+ return HttpResponse.text("Internal server error", { status: 500 });
133
+ });
134
+ },
135
+ },
136
+
137
+ requestVerify: {
138
+ success: () => {
139
+ return http.post(
140
+ "http://mock.api/v1/user-verification-tokens",
141
+ async () => {
142
+ await simulateNetwork();
143
+ return HttpResponse.json({ message: "OK", tokenId: "1" });
144
+ },
145
+ );
146
+ },
147
+ },
148
+
149
+ verify: {
150
+ success: () => {
151
+ return http.put(
152
+ "http://mock.api/v1/user-verification-tokens/:id",
153
+ async () => {
154
+ await simulateNetwork();
155
+ return HttpResponse.json({ message: "OK" });
156
+ },
157
+ );
158
+ },
159
+ },
160
+ },
161
+ };
162
+ }
163
+
164
+ const { data, handlers } = createMocks({ responseSpeed: 700 });
165
+
166
+ const meta = {
167
+ title: "Account/Current User Fetcher",
168
+ component: CurrentUserFetcher,
169
+ tags: ["autodocs"],
170
+ args: {
171
+ localUser: null,
172
+ onLogout: fn(),
173
+ onClearLocalContent: fn(),
174
+ onLogin: fn(),
175
+ onServerLogout: fn(),
176
+ children: (
177
+ <div
178
+ style={{
179
+ minBlockSize: "20rem",
180
+ backgroundColor: "white",
181
+ display: "flex",
182
+ alignItems: "center",
183
+ justifyContent: "center",
184
+ borderRadius: "1rem",
185
+ }}
186
+ >
187
+ App content
188
+ </div>
189
+ ),
190
+ },
191
+ parameters: {
192
+ msw: {
193
+ handlers: {
194
+ refreshTokens: handlers.refreshTokens.failed(),
195
+ },
196
+ },
197
+ },
198
+ } satisfies Meta<typeof CurrentUserFetcher>;
199
+
200
+ export default meta;
201
+
202
+ type Story = StoryObj<typeof meta>;
203
+
204
+ /**
205
+ * The default case, in which a local user is provided (so they have previously
206
+ * logged in), and the current user request returns the same user as is
207
+ * the local user (determined by the user id).
208
+ */
209
+ export const Default: Story = {
210
+ args: {
211
+ localUser: data.john,
212
+ },
213
+ parameters: {
214
+ msw: {
215
+ handlers: {
216
+ getCurrentUser: handlers.getCurrentUser.success(data.john),
217
+ },
218
+ },
219
+ },
220
+ };
221
+
222
+ /**
223
+ * In this case, no local user is provided and the component simply renders
224
+ * its children. There should be no network request in this case.
225
+ */
226
+ export const NoLocalUser: Story = {};
227
+
228
+ /**
229
+ * In this case, the local user is provided and the current user request returns
230
+ * error 401. The user should supply their credentials again.
231
+ */
232
+ export const SessionExpired: Story = {
233
+ args: {
234
+ localUser: data.john,
235
+ },
236
+ parameters: {
237
+ msw: {
238
+ handlers: {
239
+ getCurrentUser: handlers.getCurrentUser.notAuthenticated(),
240
+ },
241
+ },
242
+ },
243
+ };
244
+
245
+ /**
246
+ * In this case, the local user is provided and the current user request returns
247
+ * a different user (determined by user ID).
248
+ *
249
+ * This case can happen when user A logs into app X, then user B logs into
250
+ * app Y. Returning to app X, user B will be logged into ITC with their account
251
+ * but local data belong to user A.
252
+ *
253
+ * In practice this should be a rare case, but with unpleasant circumstances
254
+ * if not handled correctly.
255
+ */
256
+ export const UserMismatch: Story = {
257
+ args: {
258
+ localUser: data.john,
259
+ },
260
+
261
+ parameters: {
262
+ msw: {
263
+ handlers: {
264
+ getCurrentUser: handlers.getCurrentUser.success(data.mary),
265
+ },
266
+ },
267
+ },
268
+ };
269
+
270
+ /**
271
+ */
272
+ export const UserUnverified: Story = {
273
+ args: {
274
+ localUser: data.vernon,
275
+ },
276
+
277
+ parameters: {
278
+ msw: {
279
+ handlers: {
280
+ getCurrentUser: handlers.getCurrentUser.success(data.vernon),
281
+ requestVerify: handlers.requestVerify.success(),
282
+ verify: handlers.verify.success(),
283
+ refreshTokens: handlers.refreshTokens.success(),
284
+ },
285
+ },
286
+ },
287
+ };
288
+
289
+ /**
290
+ * In this case, the local user is provided and the current user request returns
291
+ * 404, indicating that the current user no longer exists.
292
+ *
293
+ * This can happen if users delete their accounts but
294
+ */
295
+ export const UserNotFound: Story = {
296
+ args: {
297
+ localUser: data.john,
298
+ },
299
+
300
+ parameters: {
301
+ msw: {
302
+ handlers: {
303
+ getCurrentUser: handlers.getCurrentUser.notFound(),
304
+ },
305
+ },
306
+ },
307
+ };
308
+
309
+ /**
310
+ * The proactive user session check has failed due to an error that doesn't
311
+ * carry any special meaning.
312
+ */
313
+ export const UnknownFailure: Story = {
314
+ args: {
315
+ localUser: data.john,
316
+ },
317
+
318
+ parameters: {
319
+ msw: {
320
+ handlers: {
321
+ getCurrentUser: handlers.getCurrentUser.unknownFailure(),
322
+ },
323
+ },
324
+ },
325
+ };
@@ -0,0 +1,133 @@
1
+ import { useState, type ReactNode } from "react";
2
+ import { ModalDialog } from "../ModalDialog/index.tsx";
3
+ import type { CurrentUser } from "../types.ts";
4
+ import { AccountIssueView } from "./AccountIssueView.tsx";
5
+ import { LoginView } from "./LoginView.tsx";
6
+ import type { EventHandler, EventHandlerWithReload } from "./types.ts";
7
+ import { useCurrentUserResult } from "./useCurrentUserResult.tsx";
8
+ import { UserMismatchView } from "./UserMismatchView.tsx";
9
+ import { VerifyAccountView } from "./VerifyPage.tsx";
10
+
11
+ type CurrentUserFetcherProps = {
12
+ /**
13
+ * Current user as stored in persistent storage.
14
+ *
15
+ * If this property is set, the component will attempt to fetch latest data
16
+ * from the server and store it (via ITC client).
17
+ */
18
+ localUser: CurrentUser | null;
19
+ onLogin: EventHandlerWithReload;
20
+ onClearLocalContent: EventHandler;
21
+ onLogout: EventHandlerWithReload;
22
+ onServerLogout: EventHandlerWithReload;
23
+ children: ReactNode;
24
+ };
25
+
26
+ /**
27
+ * Fetches fresh current user data if local data is provided.
28
+ *
29
+ * This component uses the Indie Tabletop Client under the hood, so if new
30
+ * data is successfully fetched, the onCurrentUser callback will be invoked,
31
+ * and it is up to the configuration of the client to store the data.
32
+ *
33
+ * Importantly, this component also handles the various user account issues
34
+ * that we could run into: expired session, user mismatch and account deletion.
35
+ *
36
+ * All other errors are ignored. This allows users to use the app in offline
37
+ * more, and doesn't interrupt their session if some unexpected error happens,
38
+ * which they cannot do anything about anyways.
39
+ */
40
+ export function CurrentUserFetcher(props: CurrentUserFetcherProps) {
41
+ const {
42
+ localUser,
43
+ children,
44
+ onLogin,
45
+ onLogout,
46
+ onClearLocalContent,
47
+ onServerLogout,
48
+ } = props;
49
+ const [isOpen, setOpen] = useState(true);
50
+
51
+ const { result, reload } = useCurrentUserResult({
52
+ // We only want to fetch the current user if they exist in local storage
53
+ performFetch: !!localUser,
54
+ });
55
+
56
+ if (result.isFailure && result.failure.type === "API_ERROR") {
57
+ // The user's session has expired. They should be prompted to
58
+ // re-authenticate, as any syncing attemps will fail.
59
+ if (result.failure.code === 401) {
60
+ return (
61
+ <>
62
+ <ModalDialog size="large" open>
63
+ <LoginView
64
+ currentUser={localUser}
65
+ onLogin={onLogin}
66
+ onLogout={onLogout}
67
+ description={undefined}
68
+ reload={reload}
69
+ />
70
+ </ModalDialog>
71
+ {children}
72
+ </>
73
+ );
74
+ }
75
+
76
+ if (result.failure.code === 404) {
77
+ // The user account is not found. The user might have been deleted.
78
+ // The user should be notified and instructed to log out, as many
79
+ // interactions acrosss the app will be broken.
80
+ return (
81
+ <>
82
+ <ModalDialog size="large" open>
83
+ <AccountIssueView onLogout={onLogout} reload={reload} />
84
+ </ModalDialog>
85
+
86
+ {children}
87
+ </>
88
+ );
89
+ }
90
+ }
91
+
92
+ if (localUser && result.isSuccess) {
93
+ const serverUser = result.value;
94
+
95
+ // The cookie (server) user and the user in local storage are a different
96
+ // user. The current user needs to decide which account to use.
97
+ if (serverUser.id !== localUser.id) {
98
+ return (
99
+ <>
100
+ <ModalDialog size="large" open>
101
+ <UserMismatchView
102
+ serverUser={result.value}
103
+ localUser={localUser}
104
+ onClearLocalContent={onClearLocalContent}
105
+ onServerLogout={onServerLogout}
106
+ reload={reload}
107
+ />
108
+ </ModalDialog>
109
+
110
+ {children}
111
+ </>
112
+ );
113
+ }
114
+
115
+ if (!serverUser.isVerified) {
116
+ return (
117
+ <>
118
+ <ModalDialog size="large" open={isOpen}>
119
+ <VerifyAccountView
120
+ currentUser={serverUser}
121
+ onClose={() => setOpen(false)}
122
+ />
123
+ </ModalDialog>
124
+
125
+ {children}
126
+ </>
127
+ );
128
+ }
129
+ }
130
+
131
+ // In all other cases we simply render the children
132
+ return <>{children}</>;
133
+ }
@@ -0,0 +1,36 @@
1
+ import { Button } from "@ariakit/react";
2
+ import { cx } from "../class-names.ts";
3
+ import { interactiveText } from "../common.css.ts";
4
+ import {
5
+ Letterhead,
6
+ LetterheadHeading,
7
+ LetterheadParagraph,
8
+ } from "../Letterhead/index.tsx";
9
+
10
+ export function FailureFallbackView() {
11
+ return (
12
+ <Letterhead>
13
+ <LetterheadHeading>Something went wrong</LetterheadHeading>
14
+
15
+ <LetterheadParagraph>
16
+ {"This is probably an issue on our side. Sorry about that!"}
17
+ </LetterheadParagraph>
18
+
19
+ <LetterheadParagraph>
20
+ {"You can try "}
21
+ <Button
22
+ {...cx(interactiveText)}
23
+ onClick={() => window.location.reload()}
24
+ >
25
+ reloading the app
26
+ </Button>
27
+ {". "}
28
+ {"If the issue persists, please get in touch at "}
29
+ <a {...cx(interactiveText)} href="mailto:support@indietabletop.club">
30
+ support@indietabletop.club
31
+ </a>
32
+ {"."}
33
+ </LetterheadParagraph>
34
+ </Letterhead>
35
+ );
36
+ }