Skip to content

Commit

Permalink
fix: notifications marker
Browse files Browse the repository at this point in the history
  • Loading branch information
v0l committed Nov 23, 2023
1 parent 0a3e15d commit 68ebe5e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ build/
.github/
transifex.yml
dist/
src-tauri/
src-tauri/
target/
5 changes: 0 additions & 5 deletions packages/app/src/Login/LoginSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ export interface LoginSession {
*/
blocked: Newest<Array<HexKey>>;

/**
* Latest notification
*/
latestNotification: number;

/**
* Timestamp of last read notification
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/app/src/Pages/Layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ header {
border-radius: 100%;
width: 9px;
height: 9px;
position: absolute;
top: 8px;
right: 8px;
position: relative;
top: -4px;
right: 7px;
}

@media (max-width: 520px) {
Expand Down
33 changes: 24 additions & 9 deletions packages/app/src/Pages/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "./Layout.css";
import { useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useState, useSyncExternalStore } from "react";
import { Link, Outlet, useLocation, useNavigate } from "react-router-dom";
import { FormattedMessage } from "react-intl";
import { useUserProfile } from "@snort/system-react";
Expand All @@ -24,6 +24,7 @@ import { ProfileLink } from "@/Element/User/ProfileLink";
import SearchBox from "../Element/SearchBox";
import SnortApi from "@/External/SnortApi";
import useEventPublisher from "@/Hooks/useEventPublisher";
import { Notifications } from "@/Cache";

export default function Layout() {
const location = useLocation();
Expand Down Expand Up @@ -99,19 +100,13 @@ const AccountHeader = () => {
}
});

const { publicKey, latestNotification, readNotifications, readonly } = useLogin(s => ({
const { publicKey, readonly } = useLogin(s => ({
publicKey: s.publicKey,
latestNotification: s.latestNotification,
readNotifications: s.readNotifications,
readonly: s.readonly,
}));
const profile = useUserProfile(publicKey);
const { publisher } = useEventPublisher();

const hasNotifications = useMemo(
() => latestNotification > readNotifications,
[latestNotification, readNotifications],
);
const unreadDms = useMemo(() => (publicKey ? 0 : 0), [publicKey]);

async function goToNotifications() {
Expand Down Expand Up @@ -166,7 +161,7 @@ const AccountHeader = () => {
)}
<Link className="btn" to="/notifications" onClick={goToNotifications}>
<Icon name="bell-02" size={24} />
{hasNotifications && <span className="has-unread"></span>}
<HasNotificationsMarker />
</Link>
<ProfileLink pubkey={publicKey} user={profile}>
<Avatar pubkey={publicKey} user={profile} />
Expand Down Expand Up @@ -200,3 +195,23 @@ function LogoHeader() {
</Link>
);
}

function HasNotificationsMarker() {
const readNotifications = useLogin(s => s.readNotifications);
const notifications = useSyncExternalStore(
c => Notifications.hook(c, "*"),
() => Notifications.snapshot(),
);
const latestNotification = useMemo(
() => notifications.reduce((acc, v) => (v.created_at > acc ? v.created_at : acc), 0),
[notifications],
);
const hasNotifications = useMemo(
() => latestNotification * 1000 > readNotifications,
[notifications, readNotifications],
);

if (hasNotifications) {
return <span className="has-unread"></span>;
}
}

0 comments on commit 68ebe5e

Please sign in to comment.