|
1 |
| -import { BellAlert } from "@medusajs/icons" |
2 |
| -import { Drawer, Heading, IconButton } from "@medusajs/ui" |
| 1 | +import { |
| 2 | + ArrowDownTray, |
| 3 | + BellAlert, |
| 4 | + InformationCircleSolid, |
| 5 | +} from "@medusajs/icons" |
| 6 | +import { Drawer, Heading, IconButton, Label, Text } from "@medusajs/ui" |
3 | 7 | import { useEffect, useState } from "react"
|
| 8 | +import { useTranslation } from "react-i18next" |
| 9 | +import { HttpTypes } from "@medusajs/types" |
| 10 | +import { formatDistance } from "date-fns" |
| 11 | +import { Divider } from "../../common/divider" |
| 12 | +import { InfiniteList } from "../../common/infinite-list" |
| 13 | +import { sdk } from "../../../lib/client" |
| 14 | +import { notificationQueryKeys } from "../../../hooks/api" |
| 15 | + |
| 16 | +interface NotificationData { |
| 17 | + title: string |
| 18 | + description?: string |
| 19 | + file?: { |
| 20 | + filename?: string |
| 21 | + url?: string |
| 22 | + mimeType?: string |
| 23 | + } |
| 24 | +} |
4 | 25 |
|
5 | 26 | export const Notifications = () => {
|
6 | 27 | const [open, setOpen] = useState(false)
|
| 28 | + const { t } = useTranslation() |
7 | 29 |
|
8 | 30 | useEffect(() => {
|
9 | 31 | const onKeyDown = (e: KeyboardEvent) => {
|
@@ -31,10 +53,107 @@ export const Notifications = () => {
|
31 | 53 | </Drawer.Trigger>
|
32 | 54 | <Drawer.Content>
|
33 | 55 | <Drawer.Header>
|
34 |
| - <Heading>Notifications</Heading> |
| 56 | + <Drawer.Title asChild> |
| 57 | + <Heading>{t("notifications.domain")}</Heading> |
| 58 | + </Drawer.Title> |
| 59 | + <Drawer.Description className="sr-only"> |
| 60 | + {t("notifications.accessibility.description")} |
| 61 | + </Drawer.Description> |
35 | 62 | </Drawer.Header>
|
36 |
| - <Drawer.Body>Notifications will go here</Drawer.Body> |
| 63 | + <Drawer.Body className="overflow-y-auto px-0"> |
| 64 | + <InfiniteList< |
| 65 | + HttpTypes.AdminNotificationListResponse, |
| 66 | + HttpTypes.AdminNotification, |
| 67 | + HttpTypes.AdminNotificationListParams |
| 68 | + > |
| 69 | + responseKey="notifications" |
| 70 | + queryKey={notificationQueryKeys.all} |
| 71 | + queryFn={(params) => sdk.admin.notification.list(params)} |
| 72 | + queryOptions={{ enabled: open }} |
| 73 | + renderItem={(notification) => { |
| 74 | + return ( |
| 75 | + <Notification |
| 76 | + key={notification.id} |
| 77 | + notification={notification} |
| 78 | + /> |
| 79 | + ) |
| 80 | + }} |
| 81 | + /> |
| 82 | + </Drawer.Body> |
37 | 83 | </Drawer.Content>
|
38 | 84 | </Drawer>
|
39 | 85 | )
|
40 | 86 | }
|
| 87 | + |
| 88 | +const Notification = ({ |
| 89 | + notification, |
| 90 | +}: { |
| 91 | + notification: HttpTypes.AdminNotification |
| 92 | +}) => { |
| 93 | + const data = notification.data as unknown as NotificationData | undefined |
| 94 | + |
| 95 | + // We need at least the title to render a notification in the feed |
| 96 | + if (!data?.title) { |
| 97 | + return null |
| 98 | + } |
| 99 | + |
| 100 | + return ( |
| 101 | + <> |
| 102 | + <div className="flex items-start justify-center gap-3 border-b p-6"> |
| 103 | + <div className="text-ui-fg-muted flex size-5 items-center justify-center"> |
| 104 | + <InformationCircleSolid /> |
| 105 | + </div> |
| 106 | + <div className="flex w-full flex-col gap-y-3"> |
| 107 | + <div> |
| 108 | + <div className="align-center flex flex-row justify-between"> |
| 109 | + <Text size="small" leading="compact" weight="plus"> |
| 110 | + {data.title} |
| 111 | + </Text> |
| 112 | + <Text |
| 113 | + as={"span"} |
| 114 | + className="text-ui-fg-subtle" |
| 115 | + size="small" |
| 116 | + leading="compact" |
| 117 | + weight="plus" |
| 118 | + > |
| 119 | + {formatDistance(notification.created_at, new Date(), { |
| 120 | + addSuffix: true, |
| 121 | + })} |
| 122 | + </Text> |
| 123 | + </div> |
| 124 | + {!!data.description && ( |
| 125 | + <Text |
| 126 | + className="text-ui-fg-subtle whitespace-pre-line" |
| 127 | + size="small" |
| 128 | + > |
| 129 | + {data.description} |
| 130 | + </Text> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + <NotificationFile file={data.file} /> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + </> |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +const NotificationFile = ({ file }: { file: NotificationData["file"] }) => { |
| 141 | + if (!file?.url) { |
| 142 | + return null |
| 143 | + } |
| 144 | + |
| 145 | + return ( |
| 146 | + <div className="shadow-elevation-card-rest bg-ui-bg-component transition-fg rounded-md px-3 py-2"> |
| 147 | + <div className="flex w-full flex-row items-center justify-between gap-2"> |
| 148 | + <Text size="small" leading="compact"> |
| 149 | + {file?.filename ?? file.url} |
| 150 | + </Text> |
| 151 | + <IconButton variant="transparent" asChild> |
| 152 | + <a href={file.url} download={file.filename ?? `${Date.now()}`}> |
| 153 | + <ArrowDownTray /> |
| 154 | + </a> |
| 155 | + </IconButton> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + ) |
| 159 | +} |
0 commit comments