|
1
|
import { XMark } from "@medusajs/icons" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { Avatar, IconButton, Skeleton, Text } from "@medusajs/ui" |
|
4
|
import { useCustomer } from "../../hooks/api/customers" |
|
5
|
|
|
6
|
interface CustomerCardProps { |
|
7
|
customerId: string |
|
8
|
onRemove?: () => void | Promise<void> |
|
9
|
} |
|
10
|
|
|
11
|
export const CustomerCard = ({ customerId, onRemove }: CustomerCardProps) => { |
|
12
|
const { customer, isPending, isError, error } = useCustomer(customerId) |
|
13
|
|
|
14
|
if (isError) { |
|
15
|
throw error |
|
16
|
} |
|
17
|
|
|
18
|
const isReady = !isPending && !!customer |
|
19
|
|
|
20
|
return ( |
|
21
|
<div className="px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center gap-4"> |
|
22
|
{!isReady ? <LoadingState /> : <CustomerInfo customer={customer} />} |
|
23
|
{onRemove && ( |
|
24
|
<IconButton |
|
25
|
className="shrink-0" |
|
26
|
variant="transparent" |
|
27
|
size="small" |
|
28
|
onClick={onRemove} |
|
29
|
type="button" |
|
30
|
> |
|
31
|
<XMark /> |
|
32
|
</IconButton> |
|
33
|
)} |
|
34
|
</div> |
|
35
|
) |
|
36
|
} |
|
37
|
|
|
38
|
const LoadingState = () => { |
|
39
|
return ( |
|
40
|
<div className="flex items-center gap-4"> |
|
41
|
<Skeleton className="w-7 h-7 rounded-full" /> |
|
42
|
<div className="flex flex-col gap-y-1"> |
|
43
|
<Skeleton className="w-20 h-5" /> |
|
44
|
<Skeleton className="w-16 h-5" /> |
|
45
|
</div> |
|
46
|
</div> |
|
47
|
) |
|
48
|
} |
|
49
|
|
|
50
|
const CustomerInfo = ({ customer }: { customer: HttpTypes.AdminCustomer }) => { |
|
51
|
const name = [customer.first_name, customer.last_name] |
|
52
|
.filter(Boolean) |
|
53
|
.join(" ") |
|
54
|
const fallback = name ? name[0] : customer.email?.[0] || "-" |
|
55
|
|
|
56
|
return ( |
|
57
|
<div className="flex items-center gap-4 flex-1"> |
|
58
|
<Avatar size="small" fallback={fallback} className="w-6 h-6" /> |
|
59
|
<div className="flex flex-col flex-1 overflow-hidden"> |
|
60
|
{name && ( |
|
61
|
<Text |
|
62
|
leading="compact" |
|
63
|
size="small" |
|
64
|
weight="plus" |
|
65
|
className="truncate" |
|
66
|
> |
|
67
|
{name} |
|
68
|
</Text> |
|
69
|
)} |
|
70
|
<Text |
|
71
|
leading="compact" |
|
72
|
size="small" |
|
73
|
className="text-ui-fg-subtle truncate" |
|
74
|
> |
|
75
|
{customer.email} |
|
76
|
</Text> |
|
77
|
</div> |
|
78
|
</div> |
|
79
|
) |
|
80
|
} |