import {
BellAlert,
BellAlertDone,
InformationCircleSolid,
} from "@medusajs/icons"
import { HttpTypes } from "@medusajs/types"
import { clx, Drawer, Heading, IconButton, Text } from "@medusajs/ui"
import { formatDistance } from "date-fns"
import { TFunction } from "i18next"
import { useEffect, useState } from "react"
import { useTranslation } from "react-i18next"
import { notificationQueryKeys, useNotifications } from "../../../hooks/api"
import { sdk } from "../../../lib/client"
import { FilePreview } from "../../common/file-preview"
import { InfiniteList } from "../../common/infinite-list"
interface NotificationData {
title: string
description?: string
file?: {
filename?: string
url?: string
mimeType?: string
}
}
const LAST_READ_NOTIFICATION_KEY = "notificationsLastReadAt"
export const Notifications = () => {
const { t } = useTranslation()
const [open, setOpen] = useState(false)
const [hasUnread, setHasUnread] = useUnreadNotifications()
// This is used to show the unread icon on the notification when the drawer is open,
// so it should lag behind the local storage data and should only be reset on close
const [lastReadAt, setLastReadAt] = useState(
localStorage.getItem(LAST_READ_NOTIFICATION_KEY)
)
useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "n" && (e.metaKey || e.ctrlKey)) {
setOpen((prev) => !prev)
}
}
document.addEventListener("keydown", onKeyDown)
return () => {
document.removeEventListener("keydown", onKeyDown)
}
}, [])
const handleOnOpen = (shouldOpen: boolean) => {
if (shouldOpen) {
setHasUnread(false)
setOpen(true)
localStorage.setItem(LAST_READ_NOTIFICATION_KEY, new Date().toISOString())
} else {
setOpen(false)
setLastReadAt(localStorage.getItem(LAST_READ_NOTIFICATION_KEY))
}
}
return (