Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/renderer/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export const useNotifications = (): NotificationsState => {
}
isFetchingRef.current = true;
setStatus('loading');
setGlobalError(null);
try {
const previousNotifications = notifications;
const fetchedNotifications = await getAllNotifications(state);
Expand Down Expand Up @@ -138,6 +137,7 @@ export const useNotifications = (): NotificationsState => {
}

setStatus('success');
setGlobalError(null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear global error only if we made it to the end successfully

} finally {
isFetchingRef.current = false;
}
Expand Down
47 changes: 38 additions & 9 deletions src/renderer/routes/Notifications.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type FC, useMemo } from 'react';
import { type FC, useMemo, useRef } from 'react';

import { useAppContext } from '../hooks/useAppContext';

Expand All @@ -14,28 +14,57 @@ 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'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use previous state while loading

? prevStateRef.current
: {
notifications,
status,
globalError,
hasNotifications,
};

const hasMultipleAccounts = useMemo(
() => notifications.length > 1,
[notifications],
() => displayState.notifications.length > 1,
[displayState.notifications],
);

const hasNoAccountErrors = useMemo(
() => notifications.every((account) => account.error === null),
[notifications],
() => displayState.notifications.every((account) => account.error === null),
[displayState.notifications],
);

if (status === 'error') {
return <Oops error={globalError} />;
if (displayState.status === 'error') {
return <Oops error={displayState.globalError} />;
}

if (!hasNotifications && hasNoAccountErrors) {
if (!displayState.hasNotifications && hasNoAccountErrors) {
return <AllRead />;
}

return (
<Page testId="notifications">
<Contents paddingHorizontal={false}>
{notifications.map((accountNotification) => {
{displayState.notifications.map((accountNotification) => {
return (
<AccountNotifications
account={accountNotification.account}
Expand Down