"use client" import { useRouter } from "next/navigation" import { BellIcon, CheckCheckIcon } from "lucide-react" import { toast } from "sonner" import { DashboardListPage } from "@/components/dashboard/list" import { useNotifications } from "@/components/notification-provider" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { fetchNotifications, markAllNotificationsRead, markNotificationRead, type NotificationItem, type NotificationReadStatus, } from "@/lib/api/notification" import { formatDateTime } from "@/lib/utils" import { useI18n } from "@/i18n/provider" export default function DashboardNotificationsPage() { const t = useI18n() const router = useRouter() const { refreshUnreadCount } = useNotifications() const readStatusOptions: Array<{ value: NotificationReadStatus; label: string }> = [ { value: "all", label: t("notification.all") }, { value: "unread", label: t("notification.unread") }, { value: "read", label: t("notification.read") }, ] async function openNotification(item: NotificationItem) { try { if (!item.readAt) { await markNotificationRead(item.id) await refreshUnreadCount() } if (item.actionUrl) { router.push(item.actionUrl) } } catch (error) { toast.error(error instanceof Error ? error.message : t("notification.openFailed")) } } async function markAllRead(reload: () => Promise) { try { await markAllNotificationsRead() await refreshUnreadCount() await reload() toast.success(t("notification.markAllReadSuccess")) } catch (error) { toast.error( error instanceof Error ? error.message : t("notification.markAllReadFailed") ) } } return ( filters={[ { name: "readStatus", label: t("notification.all"), type: "segment", defaultValue: "all", allValue: "all", options: readStatusOptions, }, ]} fetchList={fetchNotifications} renderToolbarActions={({ result, reload }) => ( )} renderContent={({ result, loading }) => result.results.length > 0 ? (
{result.results.map((item) => { const unread = !item.readAt return ( ) })}
) : (
{loading ? t("notification.loading") : t("notification.empty")}
) } labels={{ refresh: t("notification.refresh"), loading: t("notification.loading"), empty: t("notification.empty"), loadFailed: t("notification.loadFailed"), }} /> ) }