'use client'; import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; import useSWR from 'swr'; import { FC, useCallback, useState } from 'react'; import clsx from 'clsx'; import { useClickAway } from '@uidotdev/usehooks'; function replaceLinks(text: string) { const urlRegex = /(\bhttps?:\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/gi; return text.replace(urlRegex, '$1'); } export const ShowNotification: FC<{ notification: { createdAt: string; content: string }; lastReadNotification: string; }> = (props) => { const { notification } = props; const [newNotification] = useState( new Date(notification.createdAt) > new Date(props.lastReadNotification) ); return (
); }; export const NotificationOpenComponent = () => { const fetch = useFetch(); const loadNotifications = useCallback(async () => { return await (await fetch('/notifications/list')).json(); }, []); const { data, isLoading } = useSWR('notifications', loadNotifications); return (
Notifications
{!isLoading && !data.notifications.length && (
No notifications
)} {!isLoading && data.notifications.map( ( notification: { createdAt: string; content: string }, index: number ) => ( ) )}
); }; const NotificationComponent = () => { const fetch = useFetch(); const [show, setShow] = useState(false); const loadNotifications = useCallback(async () => { return await (await fetch('/notifications')).json(); }, []); const { data, mutate } = useSWR('notifications-list', loadNotifications); const changeShow = useCallback(() => { mutate( { ...data, total: 0 }, { revalidate: false, } ); setShow(!show); }, [show, data]); const ref = useClickAway(() => setShow(false)); return (
{data && data.total > 0 && (
{data.total}
)}
{show && }
); }; export default NotificationComponent;