1 import { HttpTypes } from "@medusajs/types"
2 import { Heading, Text, Tooltip, clx } from "@medusajs/ui"
3 import ReactCountryFlag from "react-country-flag"
4
5 import {
6 ExclamationCircle,
7 MapPin,
8 Plus,
9 Trash,
10 PencilSquare,
11 } from "@medusajs/icons"
12 import { ComponentPropsWithoutRef, ReactNode } from "react"
13 import { useTranslation } from "react-i18next"
14 import { Link } from "react-router-dom"
15 import {
16 Action,
17 ActionMenu,
18 } from "../../../../../components/common/action-menu"
19 import { IconAvatar } from "../../../../../components/common/icon-avatar"
20 import { getCountryByIso2 } from "../../../../../lib/data/countries"
21 import {
22 getProvinceByIso2,
23 isProvinceInCountry,
24 } from "../../../../../lib/data/country-states"
25 import { useDeleteTaxRegionAction } from "../../hooks"
26
27 interface TaxRegionCardProps extends ComponentPropsWithoutRef<"div"> {
28 taxRegion: HttpTypes.AdminTaxRegion
29 type?: "header" | "list"
30 variant?: "country" | "province"
31 asLink?: boolean
32 badge?: ReactNode
33 }
34
35 export const TaxRegionCard = ({
36 taxRegion,
37 type = "list",
38 variant = "country",
39 asLink = true,
40 badge,
41 }: TaxRegionCardProps) => {
42 const { t } = useTranslation()
43 const { id, country_code, province_code } = taxRegion
44
45 const country = getCountryByIso2(country_code)
46 const province = getProvinceByIso2(province_code)
47
48 let name = "N/A"
49 let misconfiguredSublevelTooltip: string | null = null
50
51 if (province || province_code) {
52 name = province ? province : province_code!.toUpperCase()
53 } else if (country || country_code) {
54 name = country ? country.display_name : country_code!.toUpperCase()
55 }
56
57 if (
58 country_code &&
59 province_code &&
60 !isProvinceInCountry(country_code, province_code)
61 ) {
62 name = province_code.toUpperCase()
63 misconfiguredSublevelTooltip = t(
64 "taxRegions.fields.sublevels.tooltips.notPartOfCountry",
65 {
66 country: country?.display_name,
67 province: province_code.toUpperCase(),
68 }
69 )
70 }
71
72 const showCreateDefaultTaxRate =
73 !taxRegion.tax_rates.filter((tr) => tr.is_default).length &&
74 type === "header"
75
76 const Component = (
77 <div
78 className={clx(
79 "group-data-[link=true]:hover:bg-ui-bg-base-hover transition-fg flex flex-col justify-between gap-y-4 px-6 md:flex-row md:items-center md:gap-y-0",
80 {
81 "py-4": type === "header",
82 "py-3": type === "list",
83 }
84 )}
85 >
86 <div className="flex items-center justify-between">
87 <div className="flex items-center gap-x-4">
88 <IconAvatar size={type === "list" ? "small" : "large"}>
89 {country_code && !province_code ? (
90 <div
91 className={clx(
92 "flex size-fit items-center justify-center overflow-hidden rounded-[1px]",
93 {
94 "rounded-sm": type === "header",
95 }
96 )}
97 >
98 <ReactCountryFlag
99 countryCode={country_code}
100 svg
101 style={
102 type === "list"
103 ? { width: "12px", height: "9px" }
104 : { width: "16px", height: "12px" }
105 }
106 aria-label={country?.display_name}
107 />
108 </div>
109 ) : (
110 <MapPin className="text-ui-fg-subtle" />
111 )}
112 </IconAvatar>
113 <div>
114 {type === "list" ? (
115 <Text size="small" weight="plus" leading="compact">
116 {name}
117 </Text>
118 ) : (
119 <div className="flex items-center gap-x-2">
120 <Heading>{name}</Heading>
121 </div>
122 )}
123 </div>
124 </div>
125 <div className="flex size-fit items-center gap-x-2 md:hidden">
126 {misconfiguredSublevelTooltip && (
127 <Tooltip content={misconfiguredSublevelTooltip}>
128 <ExclamationCircle className="text-ui-tag-orange-icon" />
129 </Tooltip>
130 )}
131 {badge}
132 <TaxRegionCardActions
133 taxRegion={taxRegion}
134 showCreateDefaultTaxRate={showCreateDefaultTaxRate}
135 />
136 </div>
137 </div>
138
139 <div className="hidden size-fit items-center gap-x-2 md:flex">
140 {misconfiguredSublevelTooltip && (
141 <Tooltip content={misconfiguredSublevelTooltip}>
142 <ExclamationCircle className="text-ui-tag-orange-icon" />
143 </Tooltip>
144 )}
145 {badge}
146 <TaxRegionCardActions
147 taxRegion={taxRegion}
148 showCreateDefaultTaxRate={showCreateDefaultTaxRate}
149 />
150 </div>
151 </div>
152 )
153
154 if (asLink) {
155 return (
156 <Link
157 to={variant === "country" ? `${id}` : `provinces/${id}`}
158 data-link="true"
159 className="group block"
160 >
161 {Component}
162 </Link>
163 )
164 }
165
166 return Component
167 }
168
169 const TaxRegionCardActions = ({
170 taxRegion,
171 showCreateDefaultTaxRate,
172 }: {
173 taxRegion: HttpTypes.AdminTaxRegion
174 showCreateDefaultTaxRate?: boolean
175 }) => {
176 const { t } = useTranslation()
177
178 const hasParent = !!taxRegion.parent_id
179
180 const to = hasParent
181 ? `/settings/tax-regions/${taxRegion.parent_id}`
182 : undefined
183 const handleDelete = useDeleteTaxRegionAction({ taxRegion, to })
184
185 return (
186 <ActionMenu
187 groups={[
188 ...(showCreateDefaultTaxRate
189 ? [
190 {
191 actions: [
192 {
193 icon: <Plus />,
194 label: t("taxRegions.fields.defaultTaxRate.action"),
195 to: `tax-rates/create`,
196 },
197 ],
198 },
199 ]
200 : []),
201 {
202 actions: [
203 !hasParent && {
204 icon: <PencilSquare />,
205 label: t("actions.edit"),
206 to: `/settings/tax-regions/${taxRegion.id}/edit`,
207 },
208 {
209 icon: <Trash />,
210 label: t("actions.delete"),
211 onClick: handleDelete,
212 },
213 ].filter(Boolean) as unknown as Action[],
214 },
215 ]}
216 />
217 )
218 }