@haatlabs-core/templates 0.19.0 → 0.21.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.
- package/package.json +3 -3
- package/starter/apps/api/package.json.tmpl +1 -0
- package/starter/apps/api/src/app.module.ts +36 -0
- package/starter/apps/api/src/notifications.int.test.ts +210 -0
- package/starter/apps/api/src/security-sweep.int.test.ts +63 -10
- package/starter/apps/api/src/seed.ts +16 -3
- package/starter/apps/web/public/notifications-sw.js +47 -0
- package/starter/apps/worker/package.json.tmpl +2 -0
- package/starter/apps/worker/src/handlers/notifications.handler.ts +63 -0
- package/starter/apps/worker/src/runtime.ts +52 -26
- package/starter/dot-env.example +16 -0
- package/starter/packages/config/src/config.test.ts +26 -0
- package/starter/packages/config/src/config.ts +11 -2
- package/starter/packages/contracts/package.json.tmpl +3 -1
- package/starter/packages/contracts/src/notifications.ts +38 -0
- package/starter/packages/database/drizzle/0009_notifications.sql +87 -0
- package/starter/packages/database/drizzle/meta/0009_snapshot.json +2446 -0
- package/starter/packages/database/drizzle/meta/_journal.json +7 -0
- package/starter/packages/database/package.json.tmpl +1 -0
- package/starter/packages/database/src/index.ts +1 -0
- package/starter/packages/database/src/repositories/notification.repository.ts +65 -0
- package/starter/packages/database/src/schema/index.ts +1 -0
- package/starter/packages/database/src/schema/notifications.ts +37 -0
- package/starter/packages/database/src/schema-tables.int.test.ts +10 -0
- package/starter/packages/queue/src/topology.test.ts +1 -0
- package/starter/packages/queue/src/topology.ts +3 -0
- package/starter/packages/web-ui/package.json.tmpl +1 -0
- package/starter/packages/web-ui/src/App.test.tsx +51 -2
- package/starter/packages/web-ui/src/App.tsx +84 -8
- package/starter/packages/web-ui/src/api-client.test.ts +204 -1
- package/starter/packages/web-ui/src/api-client.ts +207 -1
- package/starter/packages/web-ui/src/components/Logo.tsx +5 -0
- package/starter/packages/web-ui/src/pages/NotificationSettingsPage.tsx +24 -0
- package/starter/packages/web-ui/src/pages/NotificationsPage.tsx +19 -0
- package/starter/packages/web-ui/src/styles.css +21 -0
- package/starter/packages/web-ui/src/testing/fake-client.ts +15 -0
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"@@@name@@/observability": "workspace:*",
|
|
20
20
|
"@haatlabs-core/db-core": "^@@platformVersion@@",
|
|
21
21
|
"@haatlabs-core/nest-billing": "^@@platformVersion@@",
|
|
22
|
+
"@haatlabs-core/notifications": "^@@platformVersion@@",
|
|
22
23
|
"drizzle-orm": "^0.45.3",
|
|
23
24
|
"pg": "^8.23.1",
|
|
24
25
|
"tsx": "^4.23.15"
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { notificationEvents } from '@@@name@@/contracts/notifications';
|
|
2
|
+
import {
|
|
3
|
+
NotificationRepository as SharedNotificationRepository,
|
|
4
|
+
SystemNotificationRepository as SharedSystemNotificationRepository,
|
|
5
|
+
createNotifier,
|
|
6
|
+
} from '@haatlabs-core/notifications';
|
|
7
|
+
import type { Recipient } from '@haatlabs-core/notifications';
|
|
8
|
+
import { and, eq } from 'drizzle-orm';
|
|
9
|
+
import type { Database } from '../client';
|
|
10
|
+
import {
|
|
11
|
+
memberships,
|
|
12
|
+
notificationDefaults,
|
|
13
|
+
notificationPreferences,
|
|
14
|
+
notifications,
|
|
15
|
+
pushSubscriptions,
|
|
16
|
+
users,
|
|
17
|
+
} from '../schema';
|
|
18
|
+
import { insertOutbox } from './shared';
|
|
19
|
+
|
|
20
|
+
// The platform's notifications (@haatlabs-core/notifications), bound to this app's tables and events.
|
|
21
|
+
|
|
22
|
+
export const notificationTables = {
|
|
23
|
+
notifications,
|
|
24
|
+
notificationPreferences,
|
|
25
|
+
notificationDefaults,
|
|
26
|
+
pushSubscriptions,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `notify(tx, …)`: writes the inbox rows and their delivery in the caller's transaction, so
|
|
33
|
+
* they commit with the change they tell of, or not at all.
|
|
34
|
+
*/
|
|
35
|
+
export const notifier = createNotifier({
|
|
36
|
+
tables: notificationTables,
|
|
37
|
+
events: notificationEvents,
|
|
38
|
+
outbox: insertOutbox,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/** One tenant's inbox, choices and devices, through the runtime role (row-level security). */
|
|
42
|
+
export class NotificationRepository extends SharedNotificationRepository {
|
|
43
|
+
constructor(appDb: Database) {
|
|
44
|
+
super(appDb, notificationTables);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** What the worker delivers and prunes with, on the system connection. */
|
|
49
|
+
export class SystemNotificationRepository extends SharedSystemNotificationRepository {
|
|
50
|
+
constructor(systemDb: Database) {
|
|
51
|
+
super(systemDb, notificationTables);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A person's email, while they are a member of the organization; null for anyone else. */
|
|
55
|
+
async emailOf(organizationId: string, recipient: Recipient): Promise<string | null> {
|
|
56
|
+
if (recipient.type !== 'user' || !UUID.test(recipient.id)) return null;
|
|
57
|
+
const [row] = await this.systemDb
|
|
58
|
+
.select({ email: users.email })
|
|
59
|
+
.from(users)
|
|
60
|
+
.innerJoin(memberships, eq(memberships.userId, users.id))
|
|
61
|
+
.where(and(eq(users.id, recipient.id), eq(memberships.organizationId, organizationId)))
|
|
62
|
+
.limit(1);
|
|
63
|
+
return row?.email ?? null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { pgTable } from 'drizzle-orm/pg-core';
|
|
2
|
+
import {
|
|
3
|
+
notificationColumns,
|
|
4
|
+
notificationDefaultColumns,
|
|
5
|
+
notificationDefaultIndexes,
|
|
6
|
+
notificationIndexes,
|
|
7
|
+
notificationPreferenceColumns,
|
|
8
|
+
notificationPreferenceIndexes,
|
|
9
|
+
pushSubscriptionColumns,
|
|
10
|
+
pushSubscriptionIndexes,
|
|
11
|
+
} from '@haatlabs-core/db-core';
|
|
12
|
+
import { organizations } from './identity';
|
|
13
|
+
|
|
14
|
+
// The inbox, each person's choices, the organization's defaults and the devices push goes to
|
|
15
|
+
// (@haatlabs-core/notifications). Every row is one organization's, behind row-level security.
|
|
16
|
+
|
|
17
|
+
export const notifications = pgTable('notifications', notificationColumns({ organizations }), (t) =>
|
|
18
|
+
notificationIndexes(t),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
export const notificationPreferences = pgTable(
|
|
22
|
+
'notification_preferences',
|
|
23
|
+
notificationPreferenceColumns({ organizations }),
|
|
24
|
+
(t) => notificationPreferenceIndexes(t),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
export const notificationDefaults = pgTable(
|
|
28
|
+
'notification_defaults',
|
|
29
|
+
notificationDefaultColumns({ organizations }),
|
|
30
|
+
(t) => notificationDefaultIndexes(t),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
export const pushSubscriptions = pgTable(
|
|
34
|
+
'push_subscriptions',
|
|
35
|
+
pushSubscriptionColumns({ organizations }),
|
|
36
|
+
(t) => pushSubscriptionIndexes(t),
|
|
37
|
+
);
|
|
@@ -8,8 +8,12 @@ const EXPECTED = [
|
|
|
8
8
|
'allowance_grants',
|
|
9
9
|
'api_keys',
|
|
10
10
|
'audit_logs',
|
|
11
|
+
'disposable_email_domains',
|
|
11
12
|
'extension_keys',
|
|
12
13
|
'memberships',
|
|
14
|
+
'notification_defaults',
|
|
15
|
+
'notification_preferences',
|
|
16
|
+
'notifications',
|
|
13
17
|
'organization_features',
|
|
14
18
|
'organizations',
|
|
15
19
|
'outbox_messages',
|
|
@@ -18,10 +22,12 @@ const EXPECTED = [
|
|
|
18
22
|
'plan_prices',
|
|
19
23
|
'plans',
|
|
20
24
|
'platform_admins',
|
|
25
|
+
'push_subscriptions',
|
|
21
26
|
'shopify_installations',
|
|
22
27
|
'stores',
|
|
23
28
|
'subscriptions',
|
|
24
29
|
'usage_records',
|
|
30
|
+
'user_identities',
|
|
25
31
|
'users',
|
|
26
32
|
'webhook_events',
|
|
27
33
|
];
|
|
@@ -88,7 +94,11 @@ describe('database tables', () => {
|
|
|
88
94
|
'api_keys',
|
|
89
95
|
'extension_keys',
|
|
90
96
|
'memberships',
|
|
97
|
+
'notification_defaults',
|
|
98
|
+
'notification_preferences',
|
|
99
|
+
'notifications',
|
|
91
100
|
'organization_features',
|
|
101
|
+
'push_subscriptions',
|
|
92
102
|
'stores',
|
|
93
103
|
'subscriptions',
|
|
94
104
|
'usage_records',
|
|
@@ -4,6 +4,7 @@ import { DEFAULT_KEY_PREFIX, QueueName, allQueueNames, queueFor } from './index'
|
|
|
4
4
|
describe('this project’s topology', () => {
|
|
5
5
|
it('routes Shopify work to its queue, and refuses a key no queue takes', () => {
|
|
6
6
|
expect(queueFor('shopify.product.upsert')).toBe(QueueName.ShopifySync);
|
|
7
|
+
expect(queueFor('notifications.deliver')).toBe(QueueName.Notifications);
|
|
7
8
|
expect(queueFor('nothing.here')).toBeNull();
|
|
8
9
|
});
|
|
9
10
|
|
|
@@ -8,12 +8,15 @@ import type { QueueConnection } from '@haatlabs-core/queue';
|
|
|
8
8
|
|
|
9
9
|
export const QueueName = {
|
|
10
10
|
ShopifySync: 'shopify-sync',
|
|
11
|
+
/** Delivering notifications: live, push and email (@haatlabs-core/notifications). */
|
|
12
|
+
Notifications: 'notifications',
|
|
11
13
|
} as const;
|
|
12
14
|
export type QueueName = (typeof QueueName)[keyof typeof QueueName];
|
|
13
15
|
|
|
14
16
|
/** What a routing key starts with, for each kind of work. */
|
|
15
17
|
const route = routeByPrefix<QueueName>({
|
|
16
18
|
'shopify-sync': ['shopify.'],
|
|
19
|
+
notifications: ['notifications.'],
|
|
17
20
|
});
|
|
18
21
|
|
|
19
22
|
/** The kind of work a routing key is, or null when nothing handles it. */
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"@fontsource/jetbrains-mono": "^5.3.0",
|
|
18
18
|
"@fontsource/instrument-sans": "^5.3.0",
|
|
19
19
|
"@haatlabs-core/auth-contracts": "^@@platformVersion@@",
|
|
20
|
+
"@haatlabs-core/notifications": "^@@platformVersion@@",
|
|
20
21
|
"@haatlabs-core/ui": "^@@platformVersion@@",
|
|
21
22
|
"@tanstack/react-query": "^5.104.1",
|
|
22
23
|
"react": "^19.3.0",
|
|
@@ -11,10 +11,15 @@ import type { AppMode } from './App';
|
|
|
11
11
|
import { ApiError } from './api-client';
|
|
12
12
|
import type { ApiClient } from './api-client';
|
|
13
13
|
#@if kindShopify
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
SHOP_STORE_ID,
|
|
16
|
+
fakeNotifications,
|
|
17
|
+
fakeClient as fullFakeClient,
|
|
18
|
+
shopStore,
|
|
19
|
+
} from './testing/fake-client';
|
|
15
20
|
#@end
|
|
16
21
|
#@if !kindShopify
|
|
17
|
-
import { fakeClient as fullFakeClient } from './testing/fake-client';
|
|
22
|
+
import { fakeNotifications, fakeClient as fullFakeClient } from './testing/fake-client';
|
|
18
23
|
#@end
|
|
19
24
|
|
|
20
25
|
const ORG_ID = '11111111-1111-4111-8111-111111111111';
|
|
@@ -188,6 +193,30 @@ describe('standalone app', () => {
|
|
|
188
193
|
expect(await screen.findByRole('button', { name: 'Send me a code' })).toBeTruthy();
|
|
189
194
|
});
|
|
190
195
|
|
|
196
|
+
it('turns push off on this device before Sign out, and signs out when that fails', async () => {
|
|
197
|
+
const meFn = vi.fn().mockResolvedValueOnce(me).mockRejectedValue(unauthorized());
|
|
198
|
+
const disable = vi.fn().mockRejectedValue(new Error('no service worker'));
|
|
199
|
+
const client = fakeClient({
|
|
200
|
+
me: meFn,
|
|
201
|
+
notifications: fakeNotifications({
|
|
202
|
+
push: {
|
|
203
|
+
support: () => 'supported',
|
|
204
|
+
enable: vi.fn().mockResolvedValue('subscribed'),
|
|
205
|
+
disable,
|
|
206
|
+
},
|
|
207
|
+
}),
|
|
208
|
+
});
|
|
209
|
+
renderApp(client);
|
|
210
|
+
const user = userEvent.setup();
|
|
211
|
+
await user.click(await screen.findByRole('button', { name: 'Sign out' }));
|
|
212
|
+
expect(await screen.findByRole('button', { name: 'Send me a code' })).toBeTruthy();
|
|
213
|
+
expect(disable).toHaveBeenCalledTimes(1);
|
|
214
|
+
expect(client.logout).toHaveBeenCalledTimes(1);
|
|
215
|
+
const [disabledAt] = disable.mock.invocationCallOrder;
|
|
216
|
+
const [loggedOutAt] = vi.mocked(client.logout).mock.invocationCallOrder;
|
|
217
|
+
expect(disabledAt).toBeLessThan(loggedOutAt ?? 0);
|
|
218
|
+
});
|
|
219
|
+
|
|
191
220
|
it('shows the sign-in form when the session expires mid-use', async () => {
|
|
192
221
|
const meFn = vi.fn().mockResolvedValueOnce(me).mockRejectedValue(unauthorized());
|
|
193
222
|
const client = fakeClient({
|
|
@@ -253,6 +282,24 @@ describe('standalone app', () => {
|
|
|
253
282
|
}
|
|
254
283
|
});
|
|
255
284
|
|
|
285
|
+
it('shows the unread count on the bell in the top bar, and the inbox and its settings on their pages', async () => {
|
|
286
|
+
const client = fakeClient();
|
|
287
|
+
vi.mocked(client.notifications.unreadCount).mockResolvedValue(3);
|
|
288
|
+
renderApp(client, '/home');
|
|
289
|
+
expect(await screen.findByRole('button', { name: 'Notifications, 3 unread' })).toBeTruthy();
|
|
290
|
+
cleanup();
|
|
291
|
+
renderApp(fakeClient(), '/notifications');
|
|
292
|
+
expect(await screen.findByRole('heading', { name: 'Notifications' })).toBeTruthy();
|
|
293
|
+
expect(screen.getByRole('link', { name: 'Choose what you get' }).getAttribute('href')).toBe(
|
|
294
|
+
'/settings/notifications',
|
|
295
|
+
);
|
|
296
|
+
cleanup();
|
|
297
|
+
const settings = fakeClient();
|
|
298
|
+
renderApp(settings, '/settings/notifications');
|
|
299
|
+
expect(await screen.findByRole('heading', { name: 'Notification settings' })).toBeTruthy();
|
|
300
|
+
expect(settings.notifications.preferences).toHaveBeenCalled();
|
|
301
|
+
});
|
|
302
|
+
|
|
256
303
|
it('sends an address of a removed page to Home', async () => {
|
|
257
304
|
renderApp(fakeClient(), '/stores/7f2c1d5e-8a43-4b6f-9c1d-2e3f4a5b6c7d/products');
|
|
258
305
|
expect(await screen.findByRole('heading', { name: 'Home' })).toBeTruthy();
|
|
@@ -305,6 +352,8 @@ describe('embedded app', () => {
|
|
|
305
352
|
expect(screen.queryByRole('banner')).toBeNull();
|
|
306
353
|
expect(screen.queryByText('Acme Org')).toBeNull();
|
|
307
354
|
expect(screen.queryByRole('button', { name: 'Sign out' })).toBeNull();
|
|
355
|
+
// The bell sits beside the navigation.
|
|
356
|
+
expect(await screen.findByRole('button', { name: 'Notifications' })).toBeTruthy();
|
|
308
357
|
});
|
|
309
358
|
|
|
310
359
|
it('marks the page so it is styled like Shopify admin, and the standalone one is not', async () => {
|
|
@@ -2,7 +2,15 @@
|
|
|
2
2
|
import { QueryClientProvider, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
import { useEffect, useState } from 'react';
|
|
4
4
|
import type { ReactElement } from 'react';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
BrowserRouter,
|
|
7
|
+
NavLink,
|
|
8
|
+
Navigate,
|
|
9
|
+
Route,
|
|
10
|
+
Routes,
|
|
11
|
+
useLocation,
|
|
12
|
+
useNavigate,
|
|
13
|
+
} from 'react-router-dom';
|
|
6
14
|
import '@fontsource/instrument-sans/400.css';
|
|
7
15
|
import '@fontsource/instrument-sans/500.css';
|
|
8
16
|
import '@fontsource/instrument-sans/600.css';
|
|
@@ -13,13 +21,20 @@ import '@fontsource/jetbrains-mono/500.css';
|
|
|
13
21
|
import './styles.css';
|
|
14
22
|
import { ApiError } from './api-client';
|
|
15
23
|
import type { ApiClient } from './api-client';
|
|
16
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
NOTIFICATIONS_CSS,
|
|
26
|
+
NotificationBell,
|
|
27
|
+
createQueryClient,
|
|
28
|
+
isUnauthorized,
|
|
29
|
+
} from '@haatlabs-core/ui/client';
|
|
17
30
|
import { AppShell, ThemeStyles } from '@haatlabs-core/ui';
|
|
18
31
|
import type { NavGroup } from '@haatlabs-core/ui';
|
|
19
|
-
import { Logo } from './components/Logo';
|
|
32
|
+
import { Logo, LogoMark } from './components/Logo';
|
|
20
33
|
import { LoginPage } from './pages/LoginPage';
|
|
21
34
|
import { ConnectExtensionPage } from './pages/ConnectExtensionPage';
|
|
22
35
|
import { HomePage } from './pages/HomePage';
|
|
36
|
+
import { NotificationSettingsPage } from './pages/NotificationSettingsPage';
|
|
37
|
+
import { NotificationsPage } from './pages/NotificationsPage';
|
|
23
38
|
import { StoresPage } from './pages/stores/StoresPage';
|
|
24
39
|
import { UsagePage } from './pages/UsagePage';
|
|
25
40
|
#@if standalone
|
|
@@ -33,12 +48,30 @@ export type AppMode = 'standalone' | 'embedded';
|
|
|
33
48
|
const BRAND = '@@Name@@';
|
|
34
49
|
const ME_KEY = ['me'];
|
|
35
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Turns push off on this device first, so a shared computer stops showing this person's
|
|
53
|
+
* notifications; a failure there (no service worker, the server already gone) does not stop
|
|
54
|
+
* the sign-out.
|
|
55
|
+
*/
|
|
56
|
+
async function signOut(client: ApiClient): Promise<void> {
|
|
57
|
+
await client.notifications.push?.disable().catch(() => undefined);
|
|
58
|
+
await client.logout();
|
|
59
|
+
}
|
|
60
|
+
|
|
36
61
|
/** Keeps the query string (Shopify's `host` and `shop`) when redirecting to the default page. */
|
|
37
62
|
function RedirectTo({ pathname }: { pathname: string }): ReactElement {
|
|
38
63
|
const { search } = useLocation();
|
|
39
64
|
return <Navigate to={{ pathname, search }} replace />;
|
|
40
65
|
}
|
|
41
66
|
|
|
67
|
+
/** The bell: the unread count, live, and the newest items; an item opens its page. */
|
|
68
|
+
function Bell({ client }: { client: ApiClient }): ReactElement {
|
|
69
|
+
const navigate = useNavigate();
|
|
70
|
+
return (
|
|
71
|
+
<NotificationBell client={client.notifications} onOpenItem={(link) => void navigate(link)} />
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
42
75
|
function Shell({
|
|
43
76
|
client,
|
|
44
77
|
mode,
|
|
@@ -53,7 +86,7 @@ function Shell({
|
|
|
53
86
|
const queryClient = useQueryClient();
|
|
54
87
|
const me = useQuery({ queryKey: ME_KEY, queryFn: () => client.me(), retry: false });
|
|
55
88
|
const logout = useMutation({
|
|
56
|
-
mutationFn: () => client
|
|
89
|
+
mutationFn: () => signOut(client),
|
|
57
90
|
onSuccess: () => queryClient.clear(),
|
|
58
91
|
});
|
|
59
92
|
|
|
@@ -133,6 +166,14 @@ function Shell({
|
|
|
133
166
|
/>
|
|
134
167
|
}
|
|
135
168
|
/>
|
|
169
|
+
<Route
|
|
170
|
+
path="/notifications"
|
|
171
|
+
element={<NotificationsPage client={client.notifications} />}
|
|
172
|
+
/>
|
|
173
|
+
<Route
|
|
174
|
+
path="/settings/notifications"
|
|
175
|
+
element={<NotificationSettingsPage client={client.notifications} />}
|
|
176
|
+
/>
|
|
136
177
|
<Route path="*" element={<RedirectTo pathname="/home" />} />
|
|
137
178
|
</Routes>
|
|
138
179
|
);
|
|
@@ -141,12 +182,15 @@ function Shell({
|
|
|
141
182
|
// sign out: one quiet navigation, as Shopify's own pages have.
|
|
142
183
|
return (
|
|
143
184
|
<>
|
|
185
|
+
{/* The dashboard's theme is not rendered here; the bell's own rules are. */}
|
|
186
|
+
<style>{NOTIFICATIONS_CSS}</style>
|
|
144
187
|
<div className="embedded-nav">
|
|
145
188
|
<nav aria-label="Main" className="store-nav">
|
|
146
189
|
<NavLink to="/home">Home</NavLink>
|
|
147
190
|
<NavLink to="/stores">Store</NavLink>
|
|
148
191
|
<NavLink to="/usage">Plan and usage</NavLink>
|
|
149
192
|
</nav>
|
|
193
|
+
<Bell client={client} />
|
|
150
194
|
</div>
|
|
151
195
|
{routes}
|
|
152
196
|
</>
|
|
@@ -154,7 +198,9 @@ function Shell({
|
|
|
154
198
|
return (
|
|
155
199
|
<AppShell
|
|
156
200
|
brand={<Logo />}
|
|
201
|
+
brandMark={<LogoMark />}
|
|
157
202
|
nav={nav}
|
|
203
|
+
notifications={<Bell client={client} />}
|
|
158
204
|
account={{
|
|
159
205
|
name: me.data.organization.name,
|
|
160
206
|
detail: me.data.user?.email ?? undefined,
|
|
@@ -227,7 +273,7 @@ export function App({
|
|
|
227
273
|
import { QueryClientProvider, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
228
274
|
import { useEffect, useState } from 'react';
|
|
229
275
|
import type { ReactElement } from 'react';
|
|
230
|
-
import { BrowserRouter, Navigate, Route, Routes, useLocation } from 'react-router-dom';
|
|
276
|
+
import { BrowserRouter, Navigate, Route, Routes, useLocation, useNavigate } from 'react-router-dom';
|
|
231
277
|
import '@fontsource/instrument-sans/400.css';
|
|
232
278
|
import '@fontsource/instrument-sans/500.css';
|
|
233
279
|
import '@fontsource/instrument-sans/600.css';
|
|
@@ -238,13 +284,15 @@ import '@fontsource/jetbrains-mono/500.css';
|
|
|
238
284
|
import './styles.css';
|
|
239
285
|
import { ApiError } from './api-client';
|
|
240
286
|
import type { ApiClient } from './api-client';
|
|
241
|
-
import { createQueryClient, isUnauthorized } from '@haatlabs-core/ui/client';
|
|
287
|
+
import { NotificationBell, createQueryClient, isUnauthorized } from '@haatlabs-core/ui/client';
|
|
242
288
|
import { AppShell, ThemeStyles } from '@haatlabs-core/ui';
|
|
243
289
|
import type { NavGroup } from '@haatlabs-core/ui';
|
|
244
|
-
import { Logo } from './components/Logo';
|
|
290
|
+
import { Logo, LogoMark } from './components/Logo';
|
|
245
291
|
import { LoginPage } from './pages/LoginPage';
|
|
246
292
|
import { ConnectExtensionPage } from './pages/ConnectExtensionPage';
|
|
247
293
|
import { HomePage } from './pages/HomePage';
|
|
294
|
+
import { NotificationSettingsPage } from './pages/NotificationSettingsPage';
|
|
295
|
+
import { NotificationsPage } from './pages/NotificationsPage';
|
|
248
296
|
import { UsagePage } from './pages/UsagePage';
|
|
249
297
|
#@if standalone
|
|
250
298
|
import { FindImagesPage } from './pages/FindImagesPage';
|
|
@@ -254,17 +302,35 @@ import { Loading } from '@haatlabs-core/ui';
|
|
|
254
302
|
const BRAND = '@@Name@@';
|
|
255
303
|
const ME_KEY = ['me'];
|
|
256
304
|
|
|
305
|
+
/**
|
|
306
|
+
* Turns push off on this device first, so a shared computer stops showing this person's
|
|
307
|
+
* notifications; a failure there (no service worker, the server already gone) does not stop
|
|
308
|
+
* the sign-out.
|
|
309
|
+
*/
|
|
310
|
+
async function signOut(client: ApiClient): Promise<void> {
|
|
311
|
+
await client.notifications.push?.disable().catch(() => undefined);
|
|
312
|
+
await client.logout();
|
|
313
|
+
}
|
|
314
|
+
|
|
257
315
|
/** Keeps the query string when redirecting to the default page. */
|
|
258
316
|
function RedirectTo({ pathname }: { pathname: string }): ReactElement {
|
|
259
317
|
const { search } = useLocation();
|
|
260
318
|
return <Navigate to={{ pathname, search }} replace />;
|
|
261
319
|
}
|
|
262
320
|
|
|
321
|
+
/** The bell: the unread count, live, and the newest items; an item opens its page. */
|
|
322
|
+
function Bell({ client }: { client: ApiClient }): ReactElement {
|
|
323
|
+
const navigate = useNavigate();
|
|
324
|
+
return (
|
|
325
|
+
<NotificationBell client={client.notifications} onOpenItem={(link) => void navigate(link)} />
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
263
329
|
function Shell({ client, reload }: { client: ApiClient; reload: () => void }): ReactElement {
|
|
264
330
|
const queryClient = useQueryClient();
|
|
265
331
|
const me = useQuery({ queryKey: ME_KEY, queryFn: () => client.me(), retry: false });
|
|
266
332
|
const logout = useMutation({
|
|
267
|
-
mutationFn: () => client
|
|
333
|
+
mutationFn: () => signOut(client),
|
|
268
334
|
onSuccess: () => queryClient.clear(),
|
|
269
335
|
});
|
|
270
336
|
|
|
@@ -303,7 +369,9 @@ function Shell({ client, reload }: { client: ApiClient; reload: () => void }): R
|
|
|
303
369
|
return (
|
|
304
370
|
<AppShell
|
|
305
371
|
brand={<Logo />}
|
|
372
|
+
brandMark={<LogoMark />}
|
|
306
373
|
nav={nav}
|
|
374
|
+
notifications={<Bell client={client} />}
|
|
307
375
|
account={{
|
|
308
376
|
name: me.data.organization.name,
|
|
309
377
|
detail: me.data.user?.email ?? undefined,
|
|
@@ -333,6 +401,14 @@ function Shell({ client, reload }: { client: ApiClient; reload: () => void }): R
|
|
|
333
401
|
/>
|
|
334
402
|
}
|
|
335
403
|
/>
|
|
404
|
+
<Route
|
|
405
|
+
path="/notifications"
|
|
406
|
+
element={<NotificationsPage client={client.notifications} />}
|
|
407
|
+
/>
|
|
408
|
+
<Route
|
|
409
|
+
path="/settings/notifications"
|
|
410
|
+
element={<NotificationSettingsPage client={client.notifications} />}
|
|
411
|
+
/>
|
|
336
412
|
<Route path="*" element={<RedirectTo pathname="/home" />} />
|
|
337
413
|
</Routes>
|
|
338
414
|
</AppShell>
|