|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { |
|
4
|
QueryKey, |
|
5
|
useInfiniteQuery, |
|
6
|
UseInfiniteQueryOptions, |
|
7
|
useMutation, |
|
8
|
UseMutationOptions, |
|
9
|
useQuery, |
|
10
|
UseQueryOptions, |
|
11
|
} from "@tanstack/react-query" |
|
12
|
import { sdk } from "../../lib/client" |
|
13
|
import { queryClient } from "../../lib/query-client" |
|
14
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
15
|
|
|
16
|
const TRANSLATIONS_QUERY_KEY = "translations" as const |
|
17
|
export const translationsQueryKeys = queryKeysFactory(TRANSLATIONS_QUERY_KEY) |
|
18
|
|
|
19
|
const TRANSLATION_SETTINGS_QUERY_KEY = "translation_settings" as const |
|
20
|
export const translationSettingsQueryKeys = queryKeysFactory( |
|
21
|
TRANSLATION_SETTINGS_QUERY_KEY |
|
22
|
) |
|
23
|
|
|
24
|
const TRANSLATION_STATISTICS_QUERY_KEY = "translation_statistics" as const |
|
25
|
export const translationStatisticsQueryKeys = queryKeysFactory( |
|
26
|
TRANSLATION_STATISTICS_QUERY_KEY |
|
27
|
) |
|
28
|
|
|
29
|
const TRANSLATION_ENTITIES_QUERY_KEY = "translation_entities" as const |
|
30
|
export const translationEntitiesQueryKeys = queryKeysFactory( |
|
31
|
TRANSLATION_ENTITIES_QUERY_KEY |
|
32
|
) |
|
33
|
|
|
34
|
const DEFAULT_PAGE_SIZE = 20 |
|
35
|
|
|
36
|
/** |
|
37
|
* Hook to fetch entities with their translatable fields and all translations. |
|
38
|
* Uses the /admin/translations/entities endpoint which returns entities |
|
39
|
* with all their translations for all locales. |
|
40
|
* |
|
41
|
* @param reference - The entity type (e.g., "product", "product_variant") |
|
42
|
* @param referenceId - Optional ID(s) to filter specific entities |
|
43
|
* @param options - React Query options |
|
44
|
*/ |
|
45
|
export const useReferenceTranslations = ( |
|
46
|
reference: string, |
|
47
|
referenceId?: string | string[], |
|
48
|
options?: Omit< |
|
49
|
UseInfiniteQueryOptions< |
|
50
|
HttpTypes.AdminTranslationEntitiesResponse, |
|
51
|
FetchError, |
|
52
|
{ |
|
53
|
pages: HttpTypes.AdminTranslationEntitiesResponse[] |
|
54
|
pageParams: number[] |
|
55
|
}, |
|
56
|
HttpTypes.AdminTranslationEntitiesResponse, |
|
57
|
QueryKey, |
|
58
|
number |
|
59
|
>, |
|
60
|
"queryFn" | "queryKey" | "initialPageParam" | "getNextPageParam" |
|
61
|
> |
|
62
|
) => { |
|
63
|
const { data, ...rest } = useInfiniteQuery({ |
|
64
|
queryKey: translationEntitiesQueryKeys.list({ |
|
65
|
type: reference, |
|
66
|
id: referenceId, |
|
67
|
}), |
|
68
|
queryFn: async ({ pageParam = 0 }) => { |
|
69
|
return sdk.admin.translation.entities({ |
|
70
|
type: reference, |
|
71
|
id: referenceId, |
|
72
|
limit: DEFAULT_PAGE_SIZE, |
|
73
|
offset: pageParam, |
|
74
|
}) |
|
75
|
}, |
|
76
|
initialPageParam: 0, |
|
77
|
getNextPageParam: (lastPage) => { |
|
78
|
const nextOffset = lastPage.offset + lastPage.limit |
|
79
|
return nextOffset < lastPage.count ? nextOffset : undefined |
|
80
|
}, |
|
81
|
...options, |
|
82
|
}) |
|
83
|
|
|
84
|
const entitiesWithTranslations = |
|
85
|
data?.pages.flatMap((page) => page.data) ?? [] |
|
86
|
const translations = entitiesWithTranslations.flatMap( |
|
87
|
(entity) => entity.translations ?? [] |
|
88
|
) |
|
89
|
const references = entitiesWithTranslations.map( |
|
90
|
({ translations: _, ...entity }) => entity |
|
91
|
) |
|
92
|
const count = data?.pages[0]?.count ?? 0 |
|
93
|
|
|
94
|
return { |
|
95
|
references, |
|
96
|
translations, |
|
97
|
count, |
|
98
|
...rest, |
|
99
|
} |
|
100
|
} |
|
101
|
|
|
102
|
export const useTranslations = ( |
|
103
|
query?: HttpTypes.AdminTranslationsListParams, |
|
104
|
options?: Omit< |
|
105
|
UseQueryOptions< |
|
106
|
HttpTypes.AdminTranslationsListResponse, |
|
107
|
FetchError, |
|
108
|
HttpTypes.AdminTranslationsListResponse, |
|
109
|
QueryKey |
|
110
|
>, |
|
111
|
"queryFn" | "queryKey" |
|
112
|
> |
|
113
|
) => { |
|
114
|
const { data, ...rest } = useQuery({ |
|
115
|
queryKey: translationsQueryKeys.list(query), |
|
116
|
queryFn: () => sdk.admin.translation.list(query), |
|
117
|
...options, |
|
118
|
}) |
|
119
|
|
|
120
|
return { ...data, ...rest } |
|
121
|
} |
|
122
|
|
|
123
|
export const useBatchTranslations = ( |
|
124
|
reference: string, |
|
125
|
options?: UseMutationOptions< |
|
126
|
HttpTypes.AdminTranslationsBatchResponse, |
|
127
|
FetchError, |
|
128
|
HttpTypes.AdminBatchTranslations |
|
129
|
> |
|
130
|
) => { |
|
131
|
const mutation = useMutation({ |
|
132
|
mutationFn: (payload: HttpTypes.AdminBatchTranslations) => |
|
133
|
sdk.admin.translation.batch(payload), |
|
134
|
...options, |
|
135
|
}) |
|
136
|
|
|
137
|
/** |
|
138
|
* Useful to call the invalidation separately from the batch request and await the refetch finishes. |
|
139
|
*/ |
|
140
|
const invalidateQueries = async () => { |
|
141
|
await Promise.all([ |
|
142
|
queryClient.invalidateQueries({ |
|
143
|
queryKey: translationEntitiesQueryKeys.list({ type: reference }), |
|
144
|
}), |
|
145
|
queryClient.invalidateQueries({ |
|
146
|
queryKey: translationStatisticsQueryKeys.lists(), |
|
147
|
}), |
|
148
|
]) |
|
149
|
} |
|
150
|
|
|
151
|
return { |
|
152
|
...mutation, |
|
153
|
invalidateQueries, |
|
154
|
} |
|
155
|
} |
|
156
|
|
|
157
|
export const useTranslationSettings = ( |
|
158
|
query?: HttpTypes.AdminTranslationSettingsParams, |
|
159
|
options?: Omit< |
|
160
|
UseQueryOptions< |
|
161
|
HttpTypes.AdminTranslationSettingsResponse, |
|
162
|
FetchError, |
|
163
|
HttpTypes.AdminTranslationSettingsResponse, |
|
164
|
QueryKey |
|
165
|
>, |
|
166
|
"queryFn" | "queryKey" |
|
167
|
> |
|
168
|
) => { |
|
169
|
const { data, ...rest } = useQuery({ |
|
170
|
queryKey: translationSettingsQueryKeys.list(query), |
|
171
|
queryFn: () => sdk.admin.translation.settings(query), |
|
172
|
...options, |
|
173
|
}) |
|
174
|
|
|
175
|
return { ...data, ...rest } |
|
176
|
} |
|
177
|
|
|
178
|
export const useBatchTranslationSettings = ( |
|
179
|
options?: UseMutationOptions< |
|
180
|
HttpTypes.AdminBatchTranslationSettingsResponse, |
|
181
|
FetchError, |
|
182
|
HttpTypes.AdminBatchTranslationSettings |
|
183
|
> |
|
184
|
) => { |
|
185
|
return useMutation({ |
|
186
|
mutationFn: (payload: HttpTypes.AdminBatchTranslationSettings) => |
|
187
|
sdk.admin.translation.batchSettings(payload), |
|
188
|
onSuccess: (data, variables, context) => { |
|
189
|
queryClient.invalidateQueries({ |
|
190
|
queryKey: translationSettingsQueryKeys.lists(), |
|
191
|
}) |
|
192
|
queryClient.invalidateQueries({ |
|
193
|
queryKey: translationStatisticsQueryKeys.lists(), |
|
194
|
}) |
|
195
|
options?.onSuccess?.(data, variables, context) |
|
196
|
}, |
|
197
|
...options, |
|
198
|
}) |
|
199
|
} |
|
200
|
|
|
201
|
export const useTranslationEntities = ( |
|
202
|
query: HttpTypes.AdminTranslationEntitiesParams, |
|
203
|
options?: Omit< |
|
204
|
UseQueryOptions< |
|
205
|
HttpTypes.AdminTranslationEntitiesResponse, |
|
206
|
FetchError, |
|
207
|
HttpTypes.AdminTranslationEntitiesResponse, |
|
208
|
QueryKey |
|
209
|
>, |
|
210
|
"queryFn" | "queryKey" |
|
211
|
> |
|
212
|
) => { |
|
213
|
const { data, ...rest } = useQuery({ |
|
214
|
queryKey: translationEntitiesQueryKeys.list(query), |
|
215
|
queryFn: () => sdk.admin.translation.entities(query), |
|
216
|
...options, |
|
217
|
}) |
|
218
|
|
|
219
|
return { ...data, ...rest } |
|
220
|
} |
|
221
|
|
|
222
|
export const useTranslationStatistics = ( |
|
223
|
query?: HttpTypes.AdminTranslationStatisticsParams, |
|
224
|
options?: Omit< |
|
225
|
UseQueryOptions< |
|
226
|
HttpTypes.AdminTranslationStatisticsResponse, |
|
227
|
FetchError, |
|
228
|
HttpTypes.AdminTranslationStatisticsResponse, |
|
229
|
QueryKey |
|
230
|
>, |
|
231
|
"queryFn" | "queryKey" |
|
232
|
> |
|
233
|
) => { |
|
234
|
const { data, ...rest } = useQuery({ |
|
235
|
queryKey: translationStatisticsQueryKeys.list(query), |
|
236
|
queryFn: () => sdk.admin.translation.statistics(query), |
|
237
|
...options, |
|
238
|
}) |
|
239
|
|
|
240
|
return { ...data, ...rest } |
|
241
|
} |