1 import { useMemo } from "react";
2 import { Calendar } from "@medusajs/icons";
3 import {
4 Badge,
5 Container,
6 Copy,
7 Heading,
8 StatusBadge,
9 Text,
10 } from "@medusajs/ui";
11 import { Link } from "react-router-dom";
12 import { AdminGiftCard } from "../../../../../types";
13 import { ActionMenu } from "../../../../components/action-menu";
14 import { SectionRow } from "../../../../components/section-row";
15 import { Thumbnail } from "../../../../components/thumbnail";
16 import { getFullDate } from "../../../../utils/date-utils";
17 import { formatAmount } from "../../../../utils/format-amount";
18 import { formatDate, getRelativeDate } from "../../../../utils/format-date";
19
20 const GiftCardGeneralSection = ({ giftCard }: { giftCard: AdminGiftCard }) => {
21 const hasGiftCardExpired = useMemo(() => {
22 return giftCard.expires_at && new Date(giftCard.expires_at) < new Date();
23 }, [giftCard.expires_at]);
24
25 return (
26 <Container className="divide-y p-0">
27 <div className="flex items-center justify-between px-6 py-4">
28 <div>
29 <div className="flex items-center gap-x-1">
30 <Heading>#{giftCard.code}</Heading>
31 <Copy content={`${giftCard.code}`} className="text-ui-fg-muted" />
32 </div>
33
34 <Text size="small" className="text-ui-fg-subtle">
35 {getFullDate({
36 date: giftCard.created_at,
37 includeTime: true,
38 })}
39 </Text>
40 </div>
41
42 <div className="flex items-center gap-x-4">
43 {hasGiftCardExpired && (
44 <StatusBadge color="orange">Expired</StatusBadge>
45 )}
46 <ActionMenu
47 groups={[
48 {
49 actions: [
50 {
51 icon: <Calendar />,
52 label: "Edit expiration date",
53 to: "expiration",
54 },
55 ],
56 },
57 ]}
58 />
59 </div>
60 </div>
61
62 {giftCard.line_item?.product && (
63 <SectionRow
64 title="Gift card product"
65 value={
66 <Link
67 to={`/products/${giftCard.line_item?.product?.id}`}
68 className="text-ui-fg-base text-sm"
69 >
70 <div className="flex h-full w-full max-w-[250px] items-center gap-x-3 overflow-hidden">
71 <div className="w-fit flex-shrink-0">
72 <Thumbnail
73 size="small"
74 src={giftCard.line_item?.product?.thumbnail}
75 />
76 </div>
77 <span
78 title={giftCard.line_item?.product?.title}
79 className="truncate"
80 >
81 {giftCard.line_item?.product?.title}
82 </span>
83 </div>
84 </Link>
85 }
86 />
87 )}
88
89 <SectionRow
90 title="Currency"
91 value={
92 <Badge size="xsmall" className="text-ui-fg-base rounded-xl px-2">
93 {giftCard.currency_code.toUpperCase()}
94 </Badge>
95 }
96 />
97
98 <SectionRow
99 title="Value"
100 value={formatAmount(giftCard.value, giftCard.currency_code)}
101 />
102
103 <SectionRow
104 title="Expiration Date"
105 value={formatDate(giftCard.expires_at)}
106 />
107
108 <SectionRow
109 title="Creation Date"
110 value={getRelativeDate(giftCard.created_at)}
111 />
112 </Container>
113 );
114 };
115
116 export default GiftCardGeneralSection;