import React, { useCallback } from 'react'; import { Card, TouchableTile, Text, Visible, Avatar } from 'components'; import { Container, Row, Col, StyleSystemThemedProps } from 'components/layout'; import { Add, At, ChatFill, ClockFill, CloseThick, Ellipse, ExclamationMark, Pencil, Reply, Settings, Smiling, } from 'icons'; import { metrics } from 'config'; import { MultipleNotificationsDocument, Notification, NotificationFragment, NotificationTypeEnum, User, useReadNotificationMutation, } from 'expo-schema'; import { useResponsiveLayout } from 'hooks/responsive-layout'; import { getMessageDateTime } from 'utils/dateTime'; const { STYLES } = metrics; export const Item: React.FC = ({ notification, type, isAlertNotification, ...props }) => { const { isMobile } = useResponsiveLayout(); const [readNotification] = useReadNotificationMutation({ update: (cache, { data }) => { const notificationId = data?.readNotification.notification?.id; const notifications = cache.readQuery({ query: MultipleNotificationsDocument, variables: { limit: metrics.LIMITS.notifications, offset: 0, }, }) as any; const MultipleNotifications = ( notifications?.MultipleNotifications as NotificationFragment[] ).map((notificationItem) => { return notificationItem.id === notificationId ? { ...notificationItem, readAt: new Date() } : notificationItem; }); cache.writeQuery({ query: MultipleNotificationsDocument, data: { MultipleNotifications }, variables: { limit: metrics.LIMITS.notifications, offset: 0, }, }); }, }); const iconBasis = 45; const messageBasis = isMobile ? '85%' : STYLES.happeningBarWidth - (45 + 20); const my = isAlertNotification ? '3xs' : 'xs'; const from: User = notification?.notificationFromUser?.user as User; const icons: Record = { CHAT_MESSAGE: , CHAT_REACTION: , CHAT_REPLY: , POST_MESSAGE: , POST_REACTION: , POST_REPLY: , MESSAGE_MENTION: , BLOCK_MENTION: , NEW_BLOCK: , BLOCK_MESSAGE_MENTION: , ADDED_TO_ROOM: , ADDED_TO_SPACE: , REMINDER_EVENT: , REMINDER_DUE_DATE: , BLOCK_EDITED: , SUBSCRIPTION_RENEWAL_FAILURE: , SUBSCRIPTION_RENEWAL_SOON: , SYSTEM: , }; const getIcon = useCallback(() => { const notificationType = type || notification?.type; if (notificationType) { return ( ); } return null; // eslint-disable-next-line react-hooks/exhaustive-deps }, [notification?.type, type]); const openNotification = useCallback( (notificationItem: NotificationFragment) => { if (!notificationItem.readAt) { readNotification({ variables: { id: notificationItem.id, }, optimisticResponse: { readNotification: { __typename: 'ReadNotificationPayload', notification: { __typename: 'Notification', ...notificationItem, readAt: new Date(), } as NotificationFragment, }, }, }); } }, [readNotification] ); return ( openNotification(notification as NotificationFragment)} variant="text" shapeVariant="square" underlayColorVariant="base" leftElement={ {getIcon()} {from.name.first} {''} {from.name.last} {' '} {notification?.bodyContentPreview} {getMessageDateTime(notification?.createdAt)} } /> ); }; type NotificationItemProps = StyleSystemThemedProps & ItemProps; export type RoomVariants = 'compact' | 'full'; export interface ItemProps { isAlertNotification?: boolean; type?: NotificationTypeEnum; notification?: Notification; }