|
1
|
import { Avatar, Text } from "@medusajs/ui" |
|
2
|
import { Link } from "react-router-dom" |
|
3
|
import { useUser } from "../../../hooks/api/users" |
|
4
|
import { useCustomer } from "../../../hooks/api/customers" |
|
5
|
|
|
6
|
type UserLinkProps = { |
|
7
|
id: string |
|
8
|
first_name?: string | null |
|
9
|
last_name?: string | null |
|
10
|
email: string |
|
11
|
type?: "customer" | "user" |
|
12
|
} |
|
13
|
|
|
14
|
export const UserLink = ({ |
|
15
|
id, |
|
16
|
first_name, |
|
17
|
last_name, |
|
18
|
email, |
|
19
|
type = "user", |
|
20
|
}: UserLinkProps) => { |
|
21
|
const name = [first_name, last_name].filter(Boolean).join(" ") |
|
22
|
const fallback = name ? name.slice(0, 1) : email.slice(0, 1) |
|
23
|
const link = type === "user" ? `/settings/users/${id}` : `/customers/${id}` |
|
24
|
|
|
25
|
return ( |
|
26
|
<Link |
|
27
|
to={link} |
|
28
|
className="transition-fg hover:text-ui-fg-subtle focus-visible:shadow-borders-focus flex w-fit items-center gap-x-2 rounded-md outline-none" |
|
29
|
> |
|
30
|
<Avatar size="2xsmall" fallback={fallback.toUpperCase()} /> |
|
31
|
<Text size="small" leading="compact" weight="regular"> |
|
32
|
{name || email} |
|
33
|
</Text> |
|
34
|
</Link> |
|
35
|
) |
|
36
|
} |
|
37
|
|
|
38
|
export const By = ({ id }: { id: string }) => { |
|
39
|
const isUser = id.startsWith("user_") |
|
40
|
const isCustomer = id.startsWith("cus_") |
|
41
|
|
|
42
|
// Hooks must run unconditionally (Rules of Hooks); the `enabled` flags |
|
43
|
// already prevent fetching for the branch that does not apply. |
|
44
|
const { user } = useUser(id, undefined, { enabled: isUser }) // todo: extend to support customers |
|
45
|
const { customer } = useCustomer(id, undefined, { enabled: isCustomer }) |
|
46
|
|
|
47
|
if (!isUser && !isCustomer) { |
|
48
|
return null |
|
49
|
} |
|
50
|
|
|
51
|
const actor = isUser ? user : customer |
|
52
|
|
|
53
|
if (!actor) { |
|
54
|
return null |
|
55
|
} |
|
56
|
|
|
57
|
return <UserLink {...actor} /> |
|
58
|
} |