Skip to content

Commit

Permalink
Merge pull request #632 from us3r-network/F-notificationCount-bufan
Browse files Browse the repository at this point in the history
add notification count
  • Loading branch information
sin-bufan authored Mar 19, 2024
2 parents 1367f1f + d3b26b3 commit 8df3a09
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 44 deletions.
28 changes: 23 additions & 5 deletions apps/u3/src/components/layout/LoginButtonV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
LensAccount,
} from '../profile/info/PlatformAccounts';
import LoginIcon from './nav-icons/LoginIcon';
import useUnreadNotificationsCount from '@/hooks/social/useUnreadNotificationsCount';

const CONTACT_LINKS = [
{
Expand Down Expand Up @@ -66,7 +67,7 @@ export default function LoginButtonV2() {
const [openMenu, setOpenMenu] = useState(false);
const [openLogoutConfirm, setOpenLogoutConfirm] = useState(false);
const navigate = useNavigate();

const { unreadCount, clearUnread } = useUnreadNotificationsCount();
if (!isLogin) {
return (
<ButtonWrapper onClick={login}>
Expand Down Expand Up @@ -99,7 +100,12 @@ export default function LoginButtonV2() {
setOpenMenu((pre) => !pre);
}}
>
<UserAvatar style={{ width: '40px', height: '40px' }} />
<div className="relative">
<UserAvatar style={{ width: '40px', height: '40px' }} />
{unreadCount > 0 && (
<div className="absolute top-0 right-0 size-2 rounded-full bg-[#F81775] " />
)}
</div>
</ButtonWrapper>
</DropdownMenuTrigger>
<DropdownMenuContent
Expand All @@ -124,10 +130,22 @@ export default function LoginButtonV2() {

<DropdownMenuItemWarper
className="max-sm:hidden"
onClick={() => navigate('/notification/activity')}
onClick={() => {
clearUnread();
navigate('/notification/activity');
}}
>
<NotificationIcon />
Notifications
<div className="w-full flex justify-between items-center">
<div className="flex items-center gap-2">
<NotificationIcon />
Notifications
</div>
{unreadCount > 0 && (
<div className="flex items-center justify-center size-4 rounded-full bg-[#F81775] text-[12px] p-0 text-white">
{unreadCount}
</div>
)}
</div>
</DropdownMenuItemWarper>

<DropdownMenuItemWarper
Expand Down
14 changes: 12 additions & 2 deletions apps/u3/src/components/layout/mobile/MobileMainNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import NotificationIcon from '../nav-icons/NotificationIcon';
import MessageIcon from '../nav-icons/MessageIcon';
import ExploreIcon from '../nav-icons/ExploreIcon';
import FavIcon from '../nav-icons/FavIcon';
import useUnreadNotificationsCount from '@/hooks/social/useUnreadNotificationsCount';

export default function MobileMainNav({
className,
Expand All @@ -41,6 +42,8 @@ export default function MobileMainNav({
const isMessageRoute = firstRouteKey === RouteKey.message;
const isNotificationRoute = firstRouteKey === RouteKey.notification;
const isFavRoute = firstRouteKey === RouteKey.fav;

const { unreadCount, clearUnread } = useUnreadNotificationsCount();
return (
<div
className={cn(
Expand All @@ -58,8 +61,15 @@ export default function MobileMainNav({
<CommunityIcon active={isCommunityRoute} />
Communities
</MobileNavItem>
<MobileNavItem href="/notification" active={isNotificationRoute}>
<NotificationIcon active={isNotificationRoute} />
<MobileNavItem
href="/notification"
active={isNotificationRoute}
onClick={() => clearUnread()}
>
<NotificationIcon
active={isNotificationRoute}
unread={unreadCount > 0}
/>
Notification
</MobileNavItem>
<MobileNavItem active={isMessageRoute} href="/message">
Expand Down
53 changes: 16 additions & 37 deletions apps/u3/src/contexts/notification/NotificationStoreCtx.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
/* eslint-disable no-plusplus */
import {
Notification as LensNotification,
LimitType,
useNotifications as useLensNotifications,
} from '@lens-protocol/react-web';
import {
ReactNode,
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
ReactNode,
} from 'react';

import {
useNotifications as useLensNotifications,
Notification as LensNotification,
LimitType,
} from '@lens-protocol/react-web';

import { NotificationType } from '@/services/notification/types/notifications';
import useFarcasterNotifications from '../../hooks/social/farcaster/useFarcasterNotifications';
import useUnreadFarcasterNotificationsCount from '../../hooks/social/farcaster/useUnreadFarcasterNotificationsCount';
import { FarcasterNotification } from '../../services/social/api/farcaster';
import { NotificationType } from '@/services/notification/types/notifications';

interface NotificationStoreCtxValue {
notifications: (LensNotification | FarcasterNotification)[] | undefined;
farcasterUserData: { [key: string]: { type: number; value: string }[] };
unreadCount: number;
clearUnread: () => void;
loading: boolean;
hasMore: boolean;
loadMore: () => void;
Expand All @@ -33,8 +26,6 @@ interface NotificationStoreCtxValue {
const defaultContextValue: NotificationStoreCtxValue = {
notifications: [],
farcasterUserData: {},
unreadCount: 0,
clearUnread: () => {},
loading: false,
hasMore: false,
loadMore: () => {},
Expand Down Expand Up @@ -152,10 +143,6 @@ export function NotificationStoreProvider({
children,
config,
}: NotificationStoreProviderProps) {
const [unreadCount, setUnreadCount] = useState(
defaultContextValue.unreadCount
);

let lensPageSize: LimitType;
if (config.pageSize > 50) {
lensPageSize = LimitType.Fifty;
Expand Down Expand Up @@ -192,11 +179,6 @@ export function NotificationStoreProvider({
[lensNotifications, farcasterNotifications]
);

const {
unreadNotificationCount: unreadFarcasterCount,
clear: clearFarcasterUnread,
} = useUnreadFarcasterNotificationsCount(config.fid);

const loadMore = useCallback(async () => {
if (lensNotificationsHasMore && !lensNotificationsLoading) {
await loadMoreLensNotifications();
Expand All @@ -217,15 +199,6 @@ export function NotificationStoreProvider({
farcasterNotificationsLoading,
]);

const clearUnread = useCallback(async () => {
await clearFarcasterUnread();
setUnreadCount(0);
}, [clearFarcasterUnread]);

useEffect(() => {
setUnreadCount(unreadFarcasterCount);
}, [unreadFarcasterCount, config]);

return (
<NotificationStoreCtx.Provider
value={useMemo(
Expand All @@ -235,10 +208,16 @@ export function NotificationStoreProvider({
loadMore,
notifications,
farcasterUserData,
unreadCount,
clearUnread,
}),
[loadMore, notifications, unreadCount, clearUnread]
[
loadMore,
notifications,
lensNotificationsLoading,
farcasterNotificationsLoading,
farcasterUserData,
lensNotificationsHasMore,
farcasterNotificationsHasMore,
]
)}
>
{children}
Expand Down
27 changes: 27 additions & 0 deletions apps/u3/src/hooks/social/useUnreadNotificationsCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useCallback, useEffect, useState } from 'react';
import useUnreadFarcasterNotificationsCount from './farcaster/useUnreadFarcasterNotificationsCount';
import useFarcasterCurrFid from './farcaster/useFarcasterCurrFid';
// todo: add lens notification count
export default function useUnreadNotificationsCount() {
const [unreadCount, setUnreadCount] = useState(0);

const fid = Number(useFarcasterCurrFid());
const {
unreadNotificationCount: unreadFarcasterCount,
clear: clearFarcasterUnread,
} = useUnreadFarcasterNotificationsCount(fid);

const clearUnread = useCallback(async () => {
await clearFarcasterUnread();
setUnreadCount(0);
}, [clearFarcasterUnread]);

useEffect(() => {
setUnreadCount(unreadFarcasterCount);
}, [unreadFarcasterCount]);

return {
unreadCount,
clearUnread,
};
}

0 comments on commit 8df3a09

Please sign in to comment.