@indietabletop/appkit 3.6.0-2 → 3.6.0-3

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 (44) hide show
  1. package/lib/AppConfig/AppConfig.tsx +54 -0
  2. package/lib/HistoryState.ts +21 -0
  3. package/lib/Letterhead/style.css.ts +2 -0
  4. package/lib/LetterheadForm/index.tsx +53 -0
  5. package/lib/LetterheadForm/style.css.ts +8 -0
  6. package/lib/QRCode/QRCode.stories.tsx +47 -0
  7. package/lib/QRCode/QRCode.tsx +49 -0
  8. package/lib/QRCode/style.css.ts +19 -0
  9. package/lib/ShareButton/ShareButton.tsx +141 -0
  10. package/lib/SubscribeCard/LetterheadInfoCard.tsx +23 -0
  11. package/lib/SubscribeCard/SubscribeByEmailCard.stories.tsx +83 -0
  12. package/lib/SubscribeCard/SubscribeByEmailCard.tsx +177 -0
  13. package/lib/SubscribeCard/SubscribeCard.stories.tsx +27 -23
  14. package/lib/SubscribeCard/SubscribeCard.tsx +17 -16
  15. package/lib/Title/index.tsx +7 -2
  16. package/lib/account/AccountIssueView.tsx +40 -0
  17. package/lib/account/AlreadyLoggedInView.tsx +44 -0
  18. package/lib/account/CurrentUserFetcher.stories.tsx +339 -0
  19. package/lib/account/CurrentUserFetcher.tsx +119 -0
  20. package/lib/account/FailureFallbackView.tsx +36 -0
  21. package/lib/account/JoinPage.stories.tsx +270 -0
  22. package/lib/account/JoinPage.tsx +288 -0
  23. package/lib/account/LoadingView.tsx +14 -0
  24. package/lib/account/LoginPage.stories.tsx +318 -0
  25. package/lib/account/LoginPage.tsx +138 -0
  26. package/lib/account/LoginView.tsx +136 -0
  27. package/lib/account/NoConnectionView.tsx +34 -0
  28. package/lib/account/PasswordResetPage.stories.tsx +250 -0
  29. package/lib/account/PasswordResetPage.tsx +291 -0
  30. package/lib/account/UserMismatchView.tsx +61 -0
  31. package/lib/account/VerifyPage.tsx +217 -0
  32. package/lib/account/style.css.ts +57 -0
  33. package/lib/account/types.ts +9 -0
  34. package/lib/account/useCurrentUserResult.tsx +38 -0
  35. package/lib/class-names.ts +1 -1
  36. package/lib/client.ts +54 -7
  37. package/lib/globals.css.ts +5 -0
  38. package/lib/index.ts +11 -1
  39. package/lib/useEnsureValue.ts +31 -0
  40. package/package.json +3 -2
  41. package/lib/ClientContext/ClientContext.tsx +0 -25
  42. package/lib/LoginPage/LoginPage.stories.tsx +0 -107
  43. package/lib/LoginPage/LoginPage.tsx +0 -204
  44. package/lib/LoginPage/style.css.ts +0 -17
@@ -0,0 +1,339 @@
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
+ /**
167
+ * Fetches fresh current user data if local data is provided.
168
+ *
169
+ * This component uses the Indie Tabletop Client under the hood, so if new
170
+ * data is successfully fetched, the onCurrentUser callback will be invoked,
171
+ * and it is up to the configuration of the client to store the data.
172
+ *
173
+ * Importantly, this component also handles the various user account issues
174
+ * that we could run into: expired session, user mismatch and account deletion.
175
+ *
176
+ * All other errors are ignored. This allows users to use the app in offline
177
+ * more, and doesn't interrupt their session if some unexpected error happens,
178
+ * which they cannot do anything about anyways.
179
+ */
180
+ const meta = {
181
+ title: "Account/Current User Fetcher",
182
+ component: CurrentUserFetcher,
183
+ tags: ["autodocs"],
184
+ args: {
185
+ localUser: null,
186
+ onLogout: fn(),
187
+ onClearLocalContent: fn(),
188
+ onLogin: fn(),
189
+ onServerLogout: fn(),
190
+ children: (
191
+ <div
192
+ style={{
193
+ minBlockSize: "20rem",
194
+ backgroundColor: "white",
195
+ display: "flex",
196
+ alignItems: "center",
197
+ justifyContent: "center",
198
+ borderRadius: "1rem",
199
+ }}
200
+ >
201
+ App content
202
+ </div>
203
+ ),
204
+ },
205
+ parameters: {
206
+ msw: {
207
+ handlers: {
208
+ refreshTokens: handlers.refreshTokens.failed(),
209
+ },
210
+ },
211
+ },
212
+ } satisfies Meta<typeof CurrentUserFetcher>;
213
+
214
+ export default meta;
215
+
216
+ type Story = StoryObj<typeof meta>;
217
+
218
+ /**
219
+ * The default case, in which a local user is provided (so they have previously
220
+ * logged in), and the current user request returns the same user as is
221
+ * the local user (determined by the user id).
222
+ */
223
+ export const Default: Story = {
224
+ args: {
225
+ localUser: data.john,
226
+ },
227
+ parameters: {
228
+ msw: {
229
+ handlers: {
230
+ getCurrentUser: handlers.getCurrentUser.success(data.john),
231
+ },
232
+ },
233
+ },
234
+ };
235
+
236
+ /**
237
+ * In this case, no local user is provided and the component simply renders
238
+ * its children. There should be no network request in this case.
239
+ */
240
+ export const NoLocalUser: Story = {};
241
+
242
+ /**
243
+ * In this case, the local user is provided and the current user request returns
244
+ * error 401. The user should supply their credentials again.
245
+ */
246
+ export const SessionExpired: Story = {
247
+ args: {
248
+ localUser: data.john,
249
+ },
250
+ parameters: {
251
+ msw: {
252
+ handlers: {
253
+ getCurrentUser: handlers.getCurrentUser.notAuthenticated(),
254
+ },
255
+ },
256
+ },
257
+ };
258
+
259
+ /**
260
+ * In this case, the local user is provided and the current user request returns
261
+ * a different user (determined by user ID).
262
+ *
263
+ * This case can happen when user A logs into app X, then user B logs into
264
+ * app Y. Returning to app X, user B will be logged into ITC with their account
265
+ * but local data belong to user A.
266
+ *
267
+ * In practice this should be a rare case, but with unpleasant circumstances
268
+ * if not handled correctly.
269
+ */
270
+ export const UserMismatch: Story = {
271
+ args: {
272
+ localUser: data.john,
273
+ },
274
+
275
+ parameters: {
276
+ msw: {
277
+ handlers: {
278
+ getCurrentUser: handlers.getCurrentUser.success(data.mary),
279
+ },
280
+ },
281
+ },
282
+ };
283
+
284
+ /**
285
+ */
286
+ export const UserUnverified: Story = {
287
+ args: {
288
+ localUser: data.vernon,
289
+ },
290
+
291
+ parameters: {
292
+ msw: {
293
+ handlers: {
294
+ getCurrentUser: handlers.getCurrentUser.success(data.vernon),
295
+ requestVerify: handlers.requestVerify.success(),
296
+ verify: handlers.verify.success(),
297
+ refreshTokens: handlers.refreshTokens.success(),
298
+ },
299
+ },
300
+ },
301
+ };
302
+
303
+ /**
304
+ * In this case, the local user is provided and the current user request returns
305
+ * 404, indicating that the current user no longer exists.
306
+ *
307
+ * This can happen if users delete their accounts but
308
+ */
309
+ export const UserNotFound: Story = {
310
+ args: {
311
+ localUser: data.john,
312
+ },
313
+
314
+ parameters: {
315
+ msw: {
316
+ handlers: {
317
+ getCurrentUser: handlers.getCurrentUser.notFound(),
318
+ },
319
+ },
320
+ },
321
+ };
322
+
323
+ /**
324
+ * The proactive user session check has failed due to an error that doesn't
325
+ * carry any special meaning.
326
+ */
327
+ export const UnknownFailure: Story = {
328
+ args: {
329
+ localUser: data.john,
330
+ },
331
+
332
+ parameters: {
333
+ msw: {
334
+ handlers: {
335
+ getCurrentUser: handlers.getCurrentUser.unknownFailure(),
336
+ },
337
+ },
338
+ },
339
+ };
@@ -0,0 +1,119 @@
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
+ export function CurrentUserFetcher(props: {
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
+
20
+ onLogin: EventHandlerWithReload;
21
+ onClearLocalContent: EventHandler;
22
+ onLogout: EventHandlerWithReload;
23
+ onServerLogout: EventHandlerWithReload;
24
+
25
+ children: ReactNode;
26
+ }) {
27
+ const {
28
+ localUser,
29
+ children,
30
+ onLogin,
31
+ onLogout,
32
+ onClearLocalContent,
33
+ onServerLogout,
34
+ } = props;
35
+ const [isOpen, setOpen] = useState(true);
36
+
37
+ const { result, reload } = useCurrentUserResult({
38
+ // We only want to fetch the current user if they exist in local storage
39
+ performFetch: !!localUser,
40
+ });
41
+
42
+ if (result.isFailure && result.failure.type === "API_ERROR") {
43
+ // The user's session has expired. They should be prompted to
44
+ // re-authenticate, as any syncing attemps will fail.
45
+ if (result.failure.code === 401) {
46
+ return (
47
+ <>
48
+ <ModalDialog size="large" open>
49
+ <LoginView
50
+ currentUser={localUser}
51
+ onLogin={onLogin}
52
+ onLogout={onLogout}
53
+ description={undefined}
54
+ reload={reload}
55
+ />
56
+ </ModalDialog>
57
+ {children}
58
+ </>
59
+ );
60
+ }
61
+
62
+ if (result.failure.code === 404) {
63
+ // The user account is not found. The user might have been deleted.
64
+ // The user should be notified and instructed to log out, as many
65
+ // interactions acrosss the app will be broken.
66
+ return (
67
+ <>
68
+ <ModalDialog size="large" open>
69
+ <AccountIssueView onLogout={onLogout} reload={reload} />
70
+ </ModalDialog>
71
+
72
+ {children}
73
+ </>
74
+ );
75
+ }
76
+ }
77
+
78
+ if (localUser && result.isSuccess) {
79
+ const serverUser = result.value;
80
+
81
+ // The cookie (server) user and the user in local storage are a different
82
+ // user. The current user needs to decide which account to use.
83
+ if (serverUser.id !== localUser.id) {
84
+ return (
85
+ <>
86
+ <ModalDialog size="large" open>
87
+ <UserMismatchView
88
+ serverUser={result.value}
89
+ localUser={localUser}
90
+ onClearLocalContent={onClearLocalContent}
91
+ onServerLogout={onServerLogout}
92
+ reload={reload}
93
+ />
94
+ </ModalDialog>
95
+
96
+ {children}
97
+ </>
98
+ );
99
+ }
100
+
101
+ if (!serverUser.isVerified) {
102
+ return (
103
+ <>
104
+ <ModalDialog size="large" open={isOpen}>
105
+ <VerifyAccountView
106
+ currentUser={serverUser}
107
+ onClose={() => setOpen(false)}
108
+ />
109
+ </ModalDialog>
110
+
111
+ {children}
112
+ </>
113
+ );
114
+ }
115
+ }
116
+
117
+ // In all other cases we simply render the children
118
+ return <>{children}</>;
119
+ }
@@ -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
+ }