|
| 1 | +import NotificationItem from '@/components/NotificationItem' |
| 2 | +import PageHeader from '@/components/PageHeader' |
| 3 | +import { getAndFormatNotifications, pushClient } from '@/utils/WalletConnectUtil' |
| 4 | +import { Text, Collapse, Grid, Avatar, Button } from '@nextui-org/react' |
| 5 | +import { PushClientTypes } from '@walletconnect/push-client' |
| 6 | +import Image from 'next/image' |
| 7 | +import { Fragment, useCallback, useEffect, useState } from 'react' |
| 8 | + |
| 9 | +export default function NotificationsPage() { |
| 10 | + const [notifications, setNotifications] = useState< |
| 11 | + { |
| 12 | + subscription: PushClientTypes.PushSubscription |
| 13 | + messages: PushClientTypes.PushMessageRecord[] |
| 14 | + }[] |
| 15 | + >([]) |
| 16 | + |
| 17 | + const handleDeleteNotification = useCallback(async (messageId: number) => { |
| 18 | + pushClient.deletePushMessage({ id: messageId }) |
| 19 | + const formattedNotifications = getAndFormatNotifications() |
| 20 | + setNotifications(formattedNotifications) |
| 21 | + }, []) |
| 22 | + |
| 23 | + const handleDeleteSubscription = useCallback(async (topic: string) => { |
| 24 | + await pushClient.deleteSubscription({ topic }) |
| 25 | + const formattedNotifications = getAndFormatNotifications() |
| 26 | + setNotifications(formattedNotifications) |
| 27 | + }, []) |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + pushClient.on('push_message', () => { |
| 31 | + const formattedNotifications = getAndFormatNotifications() |
| 32 | + setNotifications(formattedNotifications) |
| 33 | + }) |
| 34 | + }, []) |
| 35 | + |
| 36 | + // Get notifications for the initial state |
| 37 | + useEffect(() => { |
| 38 | + const formattedNotifications = getAndFormatNotifications() |
| 39 | + setNotifications(formattedNotifications) |
| 40 | + }, []) |
| 41 | + |
| 42 | + return ( |
| 43 | + <Fragment> |
| 44 | + <PageHeader title="Notifications" /> |
| 45 | + |
| 46 | + <Grid.Container gap={2}> |
| 47 | + <Grid> |
| 48 | + <Collapse.Group shadow> |
| 49 | + {notifications.map(notification => ( |
| 50 | + <Collapse |
| 51 | + key={notification.subscription.topic} |
| 52 | + title={<Text h4>{notification.subscription.metadata.name}</Text>} |
| 53 | + subtitle={notification.subscription.metadata.description} |
| 54 | + contentLeft={ |
| 55 | + <Avatar |
| 56 | + size="lg" |
| 57 | + src={notification.subscription.metadata.icons[0]} |
| 58 | + color="secondary" |
| 59 | + bordered |
| 60 | + squared |
| 61 | + /> |
| 62 | + } |
| 63 | + > |
| 64 | + <Button |
| 65 | + bordered |
| 66 | + css={{ |
| 67 | + width: '100%', |
| 68 | + marginBottom: '$6' |
| 69 | + }} |
| 70 | + color="error" |
| 71 | + auto |
| 72 | + onClick={() => handleDeleteSubscription(notification.subscription.topic)} |
| 73 | + > |
| 74 | + <Image src={'/icons/delete-icon.svg'} width={15} height={15} alt="delete icon" /> |
| 75 | + Delete subscription |
| 76 | + </Button> |
| 77 | + {notification.messages.length ? ( |
| 78 | + notification.messages |
| 79 | + .sort((a, b) => b.publishedAt - a.publishedAt) |
| 80 | + .map(({ id, message, publishedAt }) => ( |
| 81 | + <NotificationItem |
| 82 | + key={id} |
| 83 | + logo={message.icon} |
| 84 | + url={message.url} |
| 85 | + title={message.title} |
| 86 | + body={message.body} |
| 87 | + publishedAt={publishedAt} |
| 88 | + onDelete={() => handleDeleteNotification(id)} |
| 89 | + /> |
| 90 | + )) |
| 91 | + ) : ( |
| 92 | + <Text css={{ opacity: '0.5', textAlign: 'center', marginTop: '$6' }}> |
| 93 | + No notifications |
| 94 | + </Text> |
| 95 | + )} |
| 96 | + </Collapse> |
| 97 | + ))} |
| 98 | + </Collapse.Group> |
| 99 | + </Grid> |
| 100 | + </Grid.Container> |
| 101 | + </Fragment> |
| 102 | + ) |
| 103 | +} |
0 commit comments