|
1
|
import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared" |
|
2
|
import { Alert, Button, Container, Heading, Text } from "@medusajs/ui" |
|
3
|
import { useCallback, useMemo } from "react" |
|
4
|
import { useTranslation } from "react-i18next" |
|
5
|
import { useNavigate } from "react-router-dom" |
|
6
|
import { TwoColumnPageSkeleton } from "../../../components/common/skeleton" |
|
7
|
import { LayoutComposer } from "../../../components/layout-composer" |
|
8
|
import { |
|
9
|
useStore, |
|
10
|
useTranslationSettings, |
|
11
|
useTranslationStatistics, |
|
12
|
} from "../../../hooks/api" |
|
13
|
import { ActiveLocalesSection } from "./components/active-locales-section/active-locales-section" |
|
14
|
import { TranslationListSection } from "./components/translation-list-section/translation-list-section" |
|
15
|
import { TranslationsCompletionSection } from "./components/translations-completion-section/translations-completion-section" |
|
16
|
import { ListCheckbox } from "@medusajs/icons" |
|
17
|
|
|
18
|
export type TranslatableEntity = { |
|
19
|
label: string |
|
20
|
reference: string |
|
21
|
translatableFields: string[] |
|
22
|
translatedCount?: number |
|
23
|
totalCount?: number |
|
24
|
} |
|
25
|
|
|
26
|
export const TranslationList = () => { |
|
27
|
const { t } = useTranslation() |
|
28
|
const navigate = useNavigate() |
|
29
|
|
|
30
|
const { store, isPending, isError, error } = useStore() |
|
31
|
const { |
|
32
|
translation_settings, |
|
33
|
isPending: isTranslationSettingsPending, |
|
34
|
isError: isTranslationSettingsError, |
|
35
|
error: translationSettingsError, |
|
36
|
} = useTranslationSettings({ |
|
37
|
is_active: true, |
|
38
|
}) |
|
39
|
const { |
|
40
|
statistics, |
|
41
|
isPending: isTranslationStatisticsPending, |
|
42
|
isError: isTranslationStatisticsError, |
|
43
|
error: translationStatisticsError, |
|
44
|
} = useTranslationStatistics( |
|
45
|
{ |
|
46
|
locales: |
|
47
|
store?.supported_locales?.map( |
|
48
|
(suportedLocale) => suportedLocale.locale_code |
|
49
|
) ?? [], |
|
50
|
entity_types: Object.keys(translation_settings ?? {}), |
|
51
|
}, |
|
52
|
{ |
|
53
|
enabled: |
|
54
|
!!translation_settings && |
|
55
|
!!store && |
|
56
|
store.supported_locales?.length > 0, |
|
57
|
} |
|
58
|
) |
|
59
|
|
|
60
|
if (isError || isTranslationSettingsError || isTranslationStatisticsError) { |
|
61
|
throw error || translationSettingsError || translationStatisticsError |
|
62
|
} |
|
63
|
|
|
64
|
const hasLocales = (store?.supported_locales ?? []).length > 0 |
|
65
|
|
|
66
|
const translatableEntities: TranslatableEntity[] = useMemo(() => { |
|
67
|
if (!translation_settings) { |
|
68
|
return [] |
|
69
|
} |
|
70
|
|
|
71
|
return ( |
|
72
|
Object.entries(translation_settings) |
|
73
|
.map(([entity, setting]) => { |
|
74
|
const entityStatistics = statistics?.[entity] ?? { |
|
75
|
translated: 0, |
|
76
|
expected: 0, |
|
77
|
} |
|
78
|
|
|
79
|
return { |
|
80
|
label: entity |
|
81
|
.split("_") |
|
82
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
|
83
|
.join(" "), |
|
84
|
reference: entity, |
|
85
|
translatableFields: setting.fields, |
|
86
|
translatedCount: entityStatistics.translated, |
|
87
|
totalCount: entityStatistics.expected, |
|
88
|
} |
|
89
|
}) |
|
90
|
// sort by label alphabetically |
|
91
|
.sort((a, b) => a.label.localeCompare(b.label)) |
|
92
|
) |
|
93
|
}, [translation_settings, statistics]) |
|
94
|
|
|
95
|
const handleManageLocales = useCallback(() => { |
|
96
|
navigate("/settings/translations/add-locales") |
|
97
|
}, [navigate]) |
|
98
|
|
|
99
|
const handleManageEntities = useCallback(() => { |
|
100
|
navigate("/settings/translations/settings") |
|
101
|
}, [navigate]) |
|
102
|
|
|
103
|
const isReady = |
|
104
|
!!store && |
|
105
|
!isPending && |
|
106
|
!isTranslationSettingsPending && |
|
107
|
!!translation_settings && |
|
108
|
((!!statistics && !isTranslationStatisticsPending) || !hasLocales) |
|
109
|
|
|
110
|
if (!isReady) { |
|
111
|
return <TwoColumnPageSkeleton sidebarSections={2} /> |
|
112
|
} |
|
113
|
|
|
114
|
return ( |
|
115
|
<LayoutComposer |
|
116
|
widgetsZonePrefix="translation.list" |
|
117
|
preferredLayoutId={CORE_LAYOUT_IDS.TWO_COLUMN} |
|
118
|
sections={{ |
|
119
|
main: ( |
|
120
|
<> |
|
121
|
<LayoutComposer.Entry id="translations-header"> |
|
122
|
<Container className="flex items-center justify-between px-6 py-4"> |
|
123
|
<div className="flex flex-col"> |
|
124
|
<Heading>Manage {t("translations.domain")}</Heading> |
|
125
|
<Text className="text-ui-fg-subtle" size="small"> |
|
126
|
{t("translations.subtitle")} |
|
127
|
</Text> |
|
128
|
</div> |
|
129
|
<Button |
|
130
|
size="small" |
|
131
|
variant="secondary" |
|
132
|
onClick={handleManageEntities} |
|
133
|
> |
|
134
|
<ListCheckbox className="text-ui-fg-subtle" /> |
|
135
|
<Text className="txt-compact-small-plus text-ui-fg-base"> |
|
136
|
{t("translations.actions.manageEntities")} |
|
137
|
</Text> |
|
138
|
</Button> |
|
139
|
</Container> |
|
140
|
</LayoutComposer.Entry> |
|
141
|
|
|
142
|
{!hasLocales && ( |
|
143
|
<LayoutComposer.Entry id="no-locales-alert"> |
|
144
|
<Alert |
|
145
|
variant="info" |
|
146
|
className="bg-ui-bg-base flex items-center px-6 py-4" |
|
147
|
> |
|
148
|
<div className="flex items-center justify-between gap-x-2"> |
|
149
|
<p>{t("translations.activeLocales.noLocalesTip")}.</p> |
|
150
|
<Button |
|
151
|
onClick={handleManageLocales} |
|
152
|
size="small" |
|
153
|
variant="secondary" |
|
154
|
> |
|
155
|
{t( |
|
156
|
"translations.activeLocales.noLocalesTipConfigureAction" |
|
157
|
)} |
|
158
|
</Button> |
|
159
|
</div> |
|
160
|
</Alert> |
|
161
|
</LayoutComposer.Entry> |
|
162
|
)} |
|
163
|
|
|
164
|
<LayoutComposer.Entry id="TranslationListSection"> |
|
165
|
<TranslationListSection |
|
166
|
entities={translatableEntities} |
|
167
|
hasLocales={hasLocales} |
|
168
|
/> |
|
169
|
</LayoutComposer.Entry> |
|
170
|
</> |
|
171
|
), |
|
172
|
side: ( |
|
173
|
<> |
|
174
|
<LayoutComposer.Entry id="ActiveLocalesSection"> |
|
175
|
<ActiveLocalesSection |
|
176
|
locales={ |
|
177
|
store?.supported_locales?.map( |
|
178
|
(suportedLocale) => suportedLocale.locale |
|
179
|
) ?? [] |
|
180
|
} |
|
181
|
></ActiveLocalesSection> |
|
182
|
</LayoutComposer.Entry> |
|
183
|
<LayoutComposer.Entry id="TranslationsCompletionSection"> |
|
184
|
<TranslationsCompletionSection |
|
185
|
statistics={statistics ?? {}} |
|
186
|
locales={ |
|
187
|
store?.supported_locales?.map( |
|
188
|
(supportedLocale) => supportedLocale.locale |
|
189
|
) ?? [] |
|
190
|
} |
|
191
|
/> |
|
192
|
</LayoutComposer.Entry> |
|
193
|
</> |
|
194
|
), |
|
195
|
}} |
|
196
|
/> |
|
197
|
) |
|
198
|
} |