Fix notifications not refreshing and not clearing "new"

Still quite clunky, but let's see
This commit is contained in:
Lim Chee Aun 2023-04-30 21:59:14 +08:00
parent 18c08076dd
commit ee2e78228e

View file

@ -34,9 +34,6 @@ function Home() {
})(); })();
}, []); }, []);
const notificationLinkRef = useRef();
const [menuState, setMenuState] = useState(undefined);
return ( return (
<> <>
{(snapStates.settings.shortcutsColumnsMode || {(snapStates.settings.shortcutsColumnsMode ||
@ -49,34 +46,7 @@ function Home() {
path="/" path="/"
id="home" id="home"
headerStart={false} headerStart={false}
headerEnd={ headerEnd={<NotificationsLink />}
<>
<Link
ref={notificationLinkRef}
to="/notifications"
class={`button plain notifications-button ${
snapStates.notificationsShowNew ? 'has-badge' : ''
} ${menuState}`}
onClick={(e) => {
e.stopPropagation();
if (window.matchMedia('(min-width: calc(40em))').matches) {
e.preventDefault();
setMenuState((state) => {
console.log('state', state, !state ? 'open' : undefined);
return !state ? 'open' : undefined;
});
}
}}
>
<Icon icon="notification" size="l" alt="Notifications" />
</Link>
<NotificationsMenu
state={menuState}
anchorRef={notificationLinkRef}
onClose={() => setMenuState(undefined)}
/>
</>
}
/> />
)} )}
<button <button
@ -101,6 +71,37 @@ function Home() {
); );
} }
function NotificationsLink() {
const snapStates = useSnapshot(states);
const notificationLinkRef = useRef();
const [menuState, setMenuState] = useState(undefined);
return (
<>
<Link
ref={notificationLinkRef}
to="/notifications"
class={`button plain notifications-button ${
snapStates.notificationsShowNew ? 'has-badge' : ''
} ${menuState}`}
onClick={(e) => {
e.stopPropagation();
if (window.matchMedia('(min-width: calc(40em))').matches) {
e.preventDefault();
setMenuState((state) => (!state ? 'open' : undefined));
}
}}
>
<Icon icon="notification" size="l" alt="Notifications" />
</Link>
<NotificationsMenu
state={menuState}
anchorRef={notificationLinkRef}
onClose={() => setMenuState(undefined)}
/>
</>
);
}
const NOTIFICATIONS_LIMIT = 30; const NOTIFICATIONS_LIMIT = 30;
const NOTIFICATIONS_DISPLAY_LIMIT = 5; const NOTIFICATIONS_DISPLAY_LIMIT = 5;
function NotificationsMenu({ anchorRef, state, onClose }) { function NotificationsMenu({ anchorRef, state, onClose }) {
@ -147,8 +148,8 @@ function NotificationsMenu({ anchorRef, state, onClose }) {
} }
useEffect(() => { useEffect(() => {
loadNotifications(); if (state === 'open') loadNotifications();
}, []); }, [state]);
return ( return (
<ControlledMenu <ControlledMenu