From 5ac04a77344316bb553fc2a21ad14e4d43c9f009 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Mon, 13 Jun 2022 23:55:12 -0700 Subject: [PATCH] Copy over notifications onto user-page --- web/components/user-page.tsx | 76 +++++++++++++++++++++++++++++++++++- web/pages/notifications.tsx | 4 +- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/web/components/user-page.tsx b/web/components/user-page.tsx index 4c906540..fcb8a9e3 100644 --- a/web/components/user-page.tsx +++ b/web/components/user-page.tsx @@ -3,6 +3,7 @@ import { uniq } from 'lodash' import { LinkIcon } from '@heroicons/react/solid' import { PencilIcon } from '@heroicons/react/outline' +import { Notification } from 'common/notification' import { follow, unfollow, User } from 'web/lib/firebase/users' import { CreatorContractsList } from './contract/contracts-list' import { SEO } from './SEO' @@ -28,6 +29,15 @@ import { FollowersButton, FollowingButton } from './following-button' import { AlertBox } from './alert-box' import { useFollows } from 'web/hooks/use-follows' import { FollowButton } from './follow-button' +import { + groupNotifications, + NotificationGroup, + usePreferredGroupedNotifications, +} from 'web/hooks/use-notifications' +import { + NotificationGroupItem, + NotificationItem, +} from 'web/pages/notifications' export function UserLink(props: { name: string @@ -48,7 +58,7 @@ export function UserLink(props: { ) } -export const TAB_IDS = ['markets', 'comments', 'bets'] +export const TAB_IDS = ['markets', 'comments', 'bets', 'notifications'] const JUNE_1_2022 = new Date('2022-06-01T00:00:00.000Z').valueOf() export function UserPage(props: { @@ -106,6 +116,42 @@ export function UserPage(props: { unfollow(currentUser.id, user.id) } + // Notifs stuff. TODO: Only works on your own profile... Check Firebase permissions + const [unseenNotificationGroups, setUnseenNotificationGroups] = useState< + NotificationGroup[] | undefined + >(undefined) + const allNotificationGroups = usePreferredGroupedNotifications(user?.id, { + unseenOnly: false, + }) + console.log( + 'allNotificationGroups length', + (allNotificationGroups || []).length + ) + + useEffect(() => { + if (!allNotificationGroups) return + // Don't re-add notifications that are visible right now or have been seen already. + const currentlyVisibleUnseenNotificationIds = Object.values( + unseenNotificationGroups ?? [] + ) + .map((n) => n.notifications.map((n) => n.id)) + .flat() + const unseenGroupedNotifications = groupNotifications( + allNotificationGroups + .map((notification: NotificationGroup) => notification.notifications) + .flat() + .filter( + (notification: Notification) => + !notification.isSeen || + currentlyVisibleUnseenNotificationIds.includes(notification.id) + ) + ) + setUnseenNotificationGroups(unseenGroupedNotifications) + + // We don't want unseenNotificationsGroup to be in the dependencies as we update it here. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [allNotificationGroups]) + return ( {usersBets.length} ), }, + { + title: 'Notifications', + content: allNotificationGroups ? ( +
+ {allNotificationGroups.length === 0 && + "You don't have any notifications. Try changing your settings to see more."} + {allNotificationGroups.map((notification) => + notification.notifications.length === 1 ? ( + + ) : ( + + ) + )} +
+ ) : ( + + ), + tabIcon:
4
, + }, ]} /> ) : ( diff --git a/web/pages/notifications.tsx b/web/pages/notifications.tsx index 02fd9d73..7b054a0b 100644 --- a/web/pages/notifications.tsx +++ b/web/pages/notifications.tsx @@ -177,7 +177,7 @@ const setNotificationsAsSeen = (notifications: Notification[]) => { return notifications } -function NotificationGroupItem(props: { +export function NotificationGroupItem(props: { notificationGroup: NotificationGroup className?: string }) { @@ -510,7 +510,7 @@ function isNotificationAboutContractResolution( ) } -function NotificationItem(props: { +export function NotificationItem(props: { notification: Notification justSummary?: boolean }) {