1 import { defineWidgetConfig } from "@medusajs/admin-sdk";
2 import { Button, Container } from "@medusajs/ui";
3 import { Link, useParams } from "react-router-dom";
4 import { Header } from "../components/header";
5 import { NoRecords } from "../components/no-records";
6 import { SidebarLink } from "../components/sidebar-link";
7 import { useStoreCreditAccounts } from "../hooks/api/store-credit-accounts";
8 import CreditCardIcon from "../routes/store-credit-accounts/[id]/components/credit-card-icon";
9 import { formatAmount } from "../utils/format-amount";
10
11 const CustomerStoreCreditWidget = () => {
12 const params = useParams();
13
14 const { store_credit_accounts: storeCreditAccounts, isPending } =
15 useStoreCreditAccounts({
16 customer_id: params.id!,
17 });
18
19 if (isPending) {
20 return null;
21 }
22
23 const createHref = `/store-credit-accounts/create?prefill_customer_id=${params.id}`;
24
25 return (
26 <Container className="p-0">
27 <Header
28 title="Store Credit Accounts"
29 actions={[
30 {
31 type: "custom",
32 children: (
33 <Link to={createHref}>
34 <Button variant="secondary" size="small">
35 Create
36 </Button>
37 </Link>
38 ),
39 },
40 ]}
41 />
42
43 {!storeCreditAccounts?.length && (
44 <NoRecords
45 className="border-t"
46 title="No store credit accounts"
47 message="Create an account to credit this customer"
48 icon={null}
49 />
50 )}
51
52 {storeCreditAccounts?.map((storeCreditAccount) => (
53 <SidebarLink
54 icon={<CreditCardIcon className="w-[32px] h-[24px]" />}
55 key={storeCreditAccount.id}
56 labelKey={`${storeCreditAccount.currency_code.toUpperCase()} Account`}
57 descriptionKey={formatAmount(
58 (storeCreditAccount.balance as number) ?? 0,
59 storeCreditAccount.currency_code
60 )}
61 to={`/store-credit-accounts/${storeCreditAccount.id}`}
62 />
63 ))}
64 </Container>
65 );
66 };
67
68 export const config = defineWidgetConfig({
69 zone: "customer.details.side.after",
70 id: "medusa:customer-store-credit-widget",
71 });
72
73 export default CustomerStoreCreditWidget;