1 import { Text, clx } from "@medusajs/ui"
2 import { useTranslation } from "react-i18next"
3 import { useDate } from "../../../hooks/use-date"
4
5 type DateRangeDisplayProps = {
6 startsAt?: Date | string | null
7 endsAt?: Date | string | null
8 showTime?: boolean
9 }
10
11 export const DateRangeDisplay = ({
12 startsAt,
13 endsAt,
14 showTime = false,
15 }: DateRangeDisplayProps) => {
16 const startDate = startsAt ? new Date(startsAt) : null
17 const endDate = endsAt ? new Date(endsAt) : null
18
19 const { t } = useTranslation()
20 const { getFullDate } = useDate()
21
22 return (
23 <div className="grid gap-3 md:grid-cols-2">
24 <div className="shadow-elevation-card-rest bg-ui-bg-component text-ui-fg-subtle flex items-center gap-x-3 rounded-md px-3 py-1.5">
25 <Bar date={startDate} />
26 <div>
27 <Text weight="plus" size="small">
28 {t("fields.startDate")}
29 </Text>
30 <Text size="small" className="tabular-nums">
31 {startDate
32 ? getFullDate({
33 date: startDate,
34 includeTime: showTime,
35 })
36 : "-"}
37 </Text>
38 </div>
39 </div>
40
41 <div className="shadow-elevation-card-rest bg-ui-bg-component text-ui-fg-subtle flex items-center gap-x-3 rounded-md px-3 py-1.5">
42 <Bar date={endDate} />
43 <div>
44 <Text size="small" weight="plus">
45 {t("fields.endDate")}
46 </Text>
47 <Text size="small" className="tabular-nums">
48 {endDate
49 ? getFullDate({
50 date: endDate,
51 includeTime: showTime,
52 })
53 : "-"}
54 </Text>
55 </div>
56 </div>
57 </div>
58 )
59 }
60
61 const Bar = ({ date }: { date: Date | null }) => {
62 const now = new Date()
63
64 const isDateInFuture = date && date > now
65
66 return (
67 <div
68 className={clx("bg-ui-tag-neutral-icon h-8 w-1 rounded-full", {
69 "bg-ui-tag-orange-icon": isDateInFuture,
70 })}
71 />
72 )
73 }