-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathNotifications.tsx
More file actions
83 lines (71 loc) · 2.21 KB
/
Notifications.tsx
File metadata and controls
83 lines (71 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { type FC, useMemo, useRef } from 'react';
import { useAppContext } from '../hooks/useAppContext';
import { AllRead } from '../components/AllRead';
import { Contents } from '../components/layout/Contents';
import { Page } from '../components/layout/Page';
import { AccountNotifications } from '../components/notifications/AccountNotifications';
import { Oops } from '../components/Oops';
import { getAccountUUID } from '../utils/auth/utils';
export const NotificationsRoute: FC = () => {
const { notifications, status, globalError, settings, hasNotifications } =
useAppContext();
// Store previous successful state
const prevStateRef = useRef({
notifications,
status,
globalError,
hasNotifications,
});
// Update ref only if not loading
if (status !== 'loading') {
prevStateRef.current = {
notifications,
status,
globalError,
hasNotifications,
};
}
// Use previous state if loading
const displayState =
status === 'loading'
? prevStateRef.current
: {
notifications,
status,
globalError,
hasNotifications,
};
const hasMultipleAccounts = useMemo(
() => displayState.notifications.length > 1,
[displayState.notifications],
);
const hasNoAccountErrors = useMemo(
() => displayState.notifications.every((account) => account.error === null),
[displayState.notifications],
);
if (displayState.status === 'error') {
return <Oops error={displayState.globalError} />;
}
if (!displayState.hasNotifications && hasNoAccountErrors) {
return <AllRead />;
}
return (
<Page testId="notifications">
<Contents paddingHorizontal={false}>
{displayState.notifications.map((accountNotification) => {
return (
<AccountNotifications
account={accountNotification.account}
error={accountNotification.error}
key={getAccountUUID(accountNotification.account)}
notifications={accountNotification.notifications}
showAccountHeader={
hasMultipleAccounts || settings.showAccountHeader
}
/>
);
})}
</Contents>
</Page>
);
};