@conloca/content-api-client 0.1.15 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +190 -105
- package/dist/client.d.ts +92 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +435 -0
- package/dist/client.js.map +1 -0
- package/dist/data-bindings.d.ts +13 -0
- package/dist/data-bindings.d.ts.map +1 -0
- package/dist/data-bindings.js +71 -0
- package/dist/data-bindings.js.map +1 -0
- package/dist/hooks.d.ts +142 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +761 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/content-api-client.d.ts +2 -0
- package/dist/lib/content-api-client.d.ts.map +1 -0
- package/dist/lib/content-api-client.js +4 -0
- package/dist/lib/content-api-client.js.map +1 -0
- package/dist/test-helpers.d.ts +89 -0
- package/dist/test-helpers.d.ts.map +1 -0
- package/dist/test-helpers.js +236 -0
- package/dist/test-helpers.js.map +1 -0
- package/dist/testing.d.ts +8 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +8 -0
- package/dist/testing.js.map +1 -0
- package/package.json +64 -19
- package/src/client.ts +513 -0
- package/src/data-bindings.ts +104 -0
- package/src/hooks.tsx +959 -0
- package/src/index.ts +50 -0
- package/src/lib/content-api-client.ts +3 -0
- package/src/test-helpers.ts +282 -0
- package/src/testing.ts +8 -0
- package/dist/index.d.mts +0 -240
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -1105
- package/dist/index.mjs.map +0 -1
- package/dist/test-helpers-BJxZXOMi.d.mts +0 -94
- package/dist/test-helpers-BJxZXOMi.d.mts.map +0 -1
- package/dist/test-helpers-CRvDHjs2.mjs +0 -220
- package/dist/test-helpers-CRvDHjs2.mjs.map +0 -1
- package/dist/testing.d.mts +0 -2
- package/dist/testing.mjs +0 -3
package/src/hooks.tsx
ADDED
|
@@ -0,0 +1,959 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AssetEntry,
|
|
3
|
+
AssetMetadataPatch,
|
|
4
|
+
AssetUsage,
|
|
5
|
+
ContentEntry,
|
|
6
|
+
ContentListResult,
|
|
7
|
+
ContentManifest,
|
|
8
|
+
CreateContentInput,
|
|
9
|
+
DataContext,
|
|
10
|
+
EditorDataBindings,
|
|
11
|
+
FolderListing,
|
|
12
|
+
FolderTreeNode,
|
|
13
|
+
GitCommitResult,
|
|
14
|
+
GitPullResult,
|
|
15
|
+
GitPushResult,
|
|
16
|
+
GitStatus,
|
|
17
|
+
GlobalFilters,
|
|
18
|
+
LocalizedEntry,
|
|
19
|
+
MDXCompileResponse,
|
|
20
|
+
PageReference,
|
|
21
|
+
SitesConfig,
|
|
22
|
+
UpdateLocaleInput,
|
|
23
|
+
} from '@conloca/content-api';
|
|
24
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
25
|
+
import { useCallback } from 'react';
|
|
26
|
+
import { APIClientError, ContentAPIClient, StaleWriteError } from './client.js';
|
|
27
|
+
import { buildRouteData, type RouteData } from './data-bindings.js';
|
|
28
|
+
|
|
29
|
+
// Global client instance
|
|
30
|
+
let globalClient: ContentAPIClient | null = null;
|
|
31
|
+
|
|
32
|
+
export function getContentAPIClient(): ContentAPIClient {
|
|
33
|
+
if (!globalClient) {
|
|
34
|
+
globalClient = new ContentAPIClient();
|
|
35
|
+
}
|
|
36
|
+
return globalClient;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function setContentAPIClient(client: ContentAPIClient): void {
|
|
40
|
+
globalClient = client;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ===== Type Guards =====
|
|
44
|
+
function isContentListResult(value: unknown): value is ContentListResult {
|
|
45
|
+
return (
|
|
46
|
+
typeof value === 'object' &&
|
|
47
|
+
value !== null &&
|
|
48
|
+
'items' in value &&
|
|
49
|
+
'total' in value &&
|
|
50
|
+
Array.isArray((value as { items: unknown }).items) &&
|
|
51
|
+
typeof (value as { total: unknown }).total === 'number'
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function hashString(value: string): string {
|
|
56
|
+
let hash = 0;
|
|
57
|
+
for (let index = 0; index < value.length; index++) {
|
|
58
|
+
const char = value.charCodeAt(index);
|
|
59
|
+
hash = (hash << 5) - hash + char;
|
|
60
|
+
hash |= 0;
|
|
61
|
+
}
|
|
62
|
+
return hash.toString(36);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ===== Query Keys =====
|
|
66
|
+
const queryKeys = {
|
|
67
|
+
currentUser: () => ['auth', 'user'] as const,
|
|
68
|
+
gitStatus: () => ['git', 'status'] as const,
|
|
69
|
+
content: (id: string) => ['content', id] as const,
|
|
70
|
+
localized: (id: string, locale: string) => ['content', id, locale] as const,
|
|
71
|
+
sitePages: (site: string, locale?: string) => ['sites', site, 'pages', locale] as const,
|
|
72
|
+
pageByPathname: (site: string, pathname: string, locale?: string) =>
|
|
73
|
+
['sites', site, 'pathname', pathname, locale] as const,
|
|
74
|
+
pathnameAvailability: (site: string, pathname: string, excludeId?: string, locale?: string) =>
|
|
75
|
+
['sites', site, 'pathname-availability', pathname, excludeId, locale] as const,
|
|
76
|
+
fragments: (collection?: string, locale?: string) => ['fragments', collection, locale] as const,
|
|
77
|
+
fragmentByName: (name: string, collection?: string, locale?: string) =>
|
|
78
|
+
['fragments', name, collection, locale] as const,
|
|
79
|
+
data: (collection?: string, locale?: string) => ['data', collection, locale] as const,
|
|
80
|
+
dataByName: (name: string, collection: string, locale?: string) => ['data', name, collection, locale] as const,
|
|
81
|
+
dataNameAvailability: (name: string, collection: string, excludeId?: string) =>
|
|
82
|
+
['data', 'name-availability', name, collection, excludeId] as const,
|
|
83
|
+
dataCollections: () => ['data', 'collections'] as const,
|
|
84
|
+
dataContext: (collection?: string, bindings?: EditorDataBindings) =>
|
|
85
|
+
['data', 'context', collection, bindings] as const,
|
|
86
|
+
allContent: (filters?: GlobalFilters) => ['content', 'all', filters] as const,
|
|
87
|
+
untranslatedContent: (targetLocale: string, excludeSites?: string[], includeUnpublished?: boolean) =>
|
|
88
|
+
['content', 'untranslated', targetLocale, excludeSites, includeUnpublished] as const,
|
|
89
|
+
sitesConfig: () => ['sites', 'config'] as const,
|
|
90
|
+
assets: () => ['assets'] as const,
|
|
91
|
+
asset: (path: string) => ['assets', path] as const,
|
|
92
|
+
assetFolders: (path?: string) => ['asset-folders', path] as const,
|
|
93
|
+
assetUsage: (path: string | null) => ['asset-usage', path] as const,
|
|
94
|
+
folderTree: () => ['folder-tree'] as const,
|
|
95
|
+
mdxCompile: (cacheKey: string) => ['mdx', 'compile', cacheKey] as const,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// ===== Core Content Hooks =====
|
|
99
|
+
|
|
100
|
+
export function useContent(id: string) {
|
|
101
|
+
const client = getContentAPIClient();
|
|
102
|
+
|
|
103
|
+
return useQuery<ContentEntry | null>({
|
|
104
|
+
queryKey: queryKeys.content(id),
|
|
105
|
+
queryFn: () => client.getContent(id),
|
|
106
|
+
enabled: !!id,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function useLocalizedContent(id: string, locale: string) {
|
|
111
|
+
const client = getContentAPIClient();
|
|
112
|
+
|
|
113
|
+
return useQuery<LocalizedEntry | null>({
|
|
114
|
+
queryKey: queryKeys.localized(id, locale),
|
|
115
|
+
queryFn: () => client.getLocalized(id, locale),
|
|
116
|
+
enabled: !!id && !!locale,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface UseCompileMDXOptions {
|
|
121
|
+
mdxContent: string | null;
|
|
122
|
+
cacheKey?: string;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function useCompileMDX({ mdxContent, cacheKey }: UseCompileMDXOptions) {
|
|
126
|
+
const client = getContentAPIClient();
|
|
127
|
+
const resolvedCacheKey = cacheKey || (mdxContent ? `mdx-${hashString(mdxContent)}` : 'mdx-empty');
|
|
128
|
+
|
|
129
|
+
return useQuery<MDXCompileResponse>({
|
|
130
|
+
queryKey: queryKeys.mdxCompile(resolvedCacheKey),
|
|
131
|
+
queryFn: () => client.compileMDX(mdxContent!),
|
|
132
|
+
enabled: !!mdxContent,
|
|
133
|
+
retry: false,
|
|
134
|
+
staleTime: Number.POSITIVE_INFINITY,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ===== Mutation Hooks =====
|
|
139
|
+
|
|
140
|
+
export function useCreateContent() {
|
|
141
|
+
const client = getContentAPIClient();
|
|
142
|
+
const queryClient = useQueryClient();
|
|
143
|
+
|
|
144
|
+
return useMutation({
|
|
145
|
+
mutationFn: async (data: CreateContentInput) => {
|
|
146
|
+
const result = await client.createContent(data);
|
|
147
|
+
if (result.success && result.id) {
|
|
148
|
+
// Fetch the full content to get server-generated data
|
|
149
|
+
const fullContent = await client.getContent(result.id);
|
|
150
|
+
return { ...result, fullContent };
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
},
|
|
154
|
+
onSuccess: (result, variables) => {
|
|
155
|
+
if (result.success && result.id && 'fullContent' in result && result.fullContent) {
|
|
156
|
+
// Cache the server's response
|
|
157
|
+
queryClient.setQueryData(queryKeys.content(result.id), result.fullContent);
|
|
158
|
+
|
|
159
|
+
// Update list queries by adding the new item from server data
|
|
160
|
+
const newManifest: ContentManifest = {
|
|
161
|
+
id: result.fullContent.id,
|
|
162
|
+
type: result.fullContent.type,
|
|
163
|
+
kind: result.fullContent.kind,
|
|
164
|
+
site: result.fullContent.site,
|
|
165
|
+
collection: result.fullContent.collection,
|
|
166
|
+
locales: Object.entries(result.fullContent.locales).reduce(
|
|
167
|
+
(acc, [locale, localeData]) => {
|
|
168
|
+
const { content, ...manifestData } = localeData;
|
|
169
|
+
acc[locale] = manifestData;
|
|
170
|
+
return acc;
|
|
171
|
+
},
|
|
172
|
+
{} as ContentManifest['locales'],
|
|
173
|
+
),
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
if (variables.kind === 'page' && variables.site) {
|
|
177
|
+
queryClient.setQueriesData(
|
|
178
|
+
{ queryKey: queryKeys.sitePages(variables.site) },
|
|
179
|
+
(old: ContentListResult | undefined) => {
|
|
180
|
+
if (!isContentListResult(old)) return old;
|
|
181
|
+
return {
|
|
182
|
+
...old,
|
|
183
|
+
items: [...old.items, newManifest],
|
|
184
|
+
total: old.total + 1,
|
|
185
|
+
};
|
|
186
|
+
},
|
|
187
|
+
);
|
|
188
|
+
} else if (variables.kind === 'fragment') {
|
|
189
|
+
// Invalidate all fragment queries to ensure the new fragment appears everywhere
|
|
190
|
+
queryClient.invalidateQueries({ queryKey: ['fragments'] });
|
|
191
|
+
} else if (variables.kind === 'data') {
|
|
192
|
+
// Invalidate all data queries to ensure the new data entry appears everywhere
|
|
193
|
+
queryClient.invalidateQueries({ queryKey: ['data'] });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function useUpdateLocalized() {
|
|
201
|
+
const client = getContentAPIClient();
|
|
202
|
+
const queryClient = useQueryClient();
|
|
203
|
+
|
|
204
|
+
return useMutation({
|
|
205
|
+
mutationFn: async (input: UpdateLocaleInput) => {
|
|
206
|
+
const result = await client.updateLocalized(input);
|
|
207
|
+
if (result.success) {
|
|
208
|
+
// Fetch the updated localized content to get server-processed data
|
|
209
|
+
const updatedContent = await client.getLocalized(input.id, input.locale);
|
|
210
|
+
return { ...result, updatedContent };
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
},
|
|
214
|
+
onSuccess: (result, variables) => {
|
|
215
|
+
if (result.success && 'updatedContent' in result && result.updatedContent) {
|
|
216
|
+
// Update the localized content query with server data
|
|
217
|
+
queryClient.setQueryData(queryKeys.localized(variables.id, variables.locale), result.updatedContent);
|
|
218
|
+
|
|
219
|
+
// Update the full content entry
|
|
220
|
+
queryClient.setQueryData(queryKeys.content(variables.id), (old: ContentEntry | null | undefined) => {
|
|
221
|
+
if (!old) return old;
|
|
222
|
+
return {
|
|
223
|
+
...old,
|
|
224
|
+
locales: {
|
|
225
|
+
...old.locales,
|
|
226
|
+
[variables.locale]: result.updatedContent!.localized,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// Update list queries to reflect the server data
|
|
232
|
+
const updateListQueries = (old: ContentListResult | undefined) => {
|
|
233
|
+
if (!isContentListResult(old)) return old;
|
|
234
|
+
return {
|
|
235
|
+
...old,
|
|
236
|
+
items: old.items.map((item) =>
|
|
237
|
+
item.id === variables.id
|
|
238
|
+
? {
|
|
239
|
+
...item,
|
|
240
|
+
locales: {
|
|
241
|
+
...item.locales,
|
|
242
|
+
[variables.locale]: {
|
|
243
|
+
locale: result.updatedContent!.localized.locale,
|
|
244
|
+
etag: result.updatedContent!.localized.etag,
|
|
245
|
+
created: result.updatedContent!.localized.created,
|
|
246
|
+
modified: result.updatedContent!.localized.modified,
|
|
247
|
+
pathname: result.updatedContent!.localized.pathname,
|
|
248
|
+
previousPathnames: result.updatedContent!.localized.previousPathnames,
|
|
249
|
+
name: result.updatedContent!.localized.name,
|
|
250
|
+
publishAt: result.updatedContent!.localized.publishAt,
|
|
251
|
+
unpublishAt: result.updatedContent!.localized.unpublishAt,
|
|
252
|
+
meta: result.updatedContent!.localized.meta,
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
}
|
|
256
|
+
: item,
|
|
257
|
+
),
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
queryClient.setQueriesData({ queryKey: ['sites'], exact: false }, updateListQueries);
|
|
262
|
+
|
|
263
|
+
queryClient.setQueriesData({ queryKey: ['fragments'], exact: false }, updateListQueries);
|
|
264
|
+
|
|
265
|
+
queryClient.setQueriesData({ queryKey: ['data'], exact: false }, updateListQueries);
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
interface DeleteContentArgs {
|
|
272
|
+
id: string;
|
|
273
|
+
etag: string;
|
|
274
|
+
locale?: string;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function useDeleteContent() {
|
|
278
|
+
const client = getContentAPIClient();
|
|
279
|
+
const queryClient = useQueryClient();
|
|
280
|
+
|
|
281
|
+
return useMutation({
|
|
282
|
+
mutationFn: async ({ id, etag, locale }: DeleteContentArgs) => {
|
|
283
|
+
// If locale is provided, it's a locale-specific delete
|
|
284
|
+
if (locale) {
|
|
285
|
+
return client.deleteLocalized({ id, locale, etag });
|
|
286
|
+
}
|
|
287
|
+
// Otherwise, delete entire content
|
|
288
|
+
return client.deleteContent(id, etag);
|
|
289
|
+
},
|
|
290
|
+
onSuccess: (result, variables) => {
|
|
291
|
+
if (result.success) {
|
|
292
|
+
if (variables.locale) {
|
|
293
|
+
// For locale-specific deletion, update the content entry to remove the locale
|
|
294
|
+
queryClient.setQueryData(queryKeys.content(variables.id), (old: ContentEntry | null | undefined) => {
|
|
295
|
+
if (!old) return old;
|
|
296
|
+
const locale = variables.locale!;
|
|
297
|
+
const { [locale]: _, ...remainingLocales } = old.locales;
|
|
298
|
+
return {
|
|
299
|
+
...old,
|
|
300
|
+
locales: remainingLocales,
|
|
301
|
+
};
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// Update list queries to remove the locale from the item
|
|
305
|
+
const updateListQueries = (old: ContentListResult | undefined) => {
|
|
306
|
+
if (!isContentListResult(old)) return old;
|
|
307
|
+
const locale = variables.locale!;
|
|
308
|
+
return {
|
|
309
|
+
...old,
|
|
310
|
+
items: old.items
|
|
311
|
+
.map((item) => {
|
|
312
|
+
if (item.id === variables.id) {
|
|
313
|
+
const { [locale]: _, ...remainingLocales } = item.locales;
|
|
314
|
+
return {
|
|
315
|
+
...item,
|
|
316
|
+
locales: remainingLocales,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
return item;
|
|
320
|
+
})
|
|
321
|
+
.filter((item) => Object.keys(item.locales).length > 0), // Remove items with no locales
|
|
322
|
+
};
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
queryClient.setQueriesData({ queryKey: ['sites'], exact: false }, updateListQueries);
|
|
326
|
+
queryClient.setQueriesData({ queryKey: ['fragments'], exact: false }, updateListQueries);
|
|
327
|
+
queryClient.setQueriesData({ queryKey: ['data'], exact: false }, updateListQueries);
|
|
328
|
+
queryClient.setQueriesData({ queryKey: ['content', 'all'], exact: false }, updateListQueries);
|
|
329
|
+
} else {
|
|
330
|
+
// For full deletion, remove from cache
|
|
331
|
+
queryClient.removeQueries({ queryKey: queryKeys.content(variables.id) });
|
|
332
|
+
queryClient.removeQueries({ queryKey: ['content', variables.id], exact: false });
|
|
333
|
+
|
|
334
|
+
// Update list queries by removing the deleted item
|
|
335
|
+
queryClient.setQueriesData({ queryKey: ['sites'], exact: false }, (old: ContentListResult | undefined) => {
|
|
336
|
+
if (!isContentListResult(old)) return old;
|
|
337
|
+
const newItems = old.items.filter((item) => item.id !== variables.id);
|
|
338
|
+
return {
|
|
339
|
+
...old,
|
|
340
|
+
items: newItems,
|
|
341
|
+
total: newItems.length < old.items.length ? old.total - 1 : old.total,
|
|
342
|
+
};
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
// Update fragment lists
|
|
346
|
+
queryClient.setQueriesData(
|
|
347
|
+
{ queryKey: ['fragments'], exact: false },
|
|
348
|
+
(old: ContentListResult | undefined) => {
|
|
349
|
+
if (!isContentListResult(old)) return old;
|
|
350
|
+
const newItems = old.items.filter((item) => item.id !== variables.id);
|
|
351
|
+
return {
|
|
352
|
+
...old,
|
|
353
|
+
items: newItems,
|
|
354
|
+
total: newItems.length < old.items.length ? old.total - 1 : old.total,
|
|
355
|
+
};
|
|
356
|
+
},
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
// Update data lists
|
|
360
|
+
queryClient.setQueriesData({ queryKey: ['data'], exact: false }, (old: ContentListResult | undefined) => {
|
|
361
|
+
if (!isContentListResult(old)) return old;
|
|
362
|
+
const newItems = old.items.filter((item) => item.id !== variables.id);
|
|
363
|
+
return {
|
|
364
|
+
...old,
|
|
365
|
+
items: newItems,
|
|
366
|
+
total: newItems.length < old.items.length ? old.total - 1 : old.total,
|
|
367
|
+
};
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
// Update all content queries
|
|
371
|
+
queryClient.setQueriesData(
|
|
372
|
+
{ queryKey: ['content', 'all'], exact: false },
|
|
373
|
+
(old: ContentListResult | undefined) => {
|
|
374
|
+
if (!isContentListResult(old)) return old;
|
|
375
|
+
const newItems = old.items.filter((item) => item.id !== variables.id);
|
|
376
|
+
return {
|
|
377
|
+
...old,
|
|
378
|
+
items: newItems,
|
|
379
|
+
total: newItems.length < old.items.length ? old.total - 1 : old.total,
|
|
380
|
+
};
|
|
381
|
+
},
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
interface DeleteLocalizedArgs {
|
|
390
|
+
id: string;
|
|
391
|
+
locale: string;
|
|
392
|
+
etag: string;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function useDeleteLocalized() {
|
|
396
|
+
const client = getContentAPIClient();
|
|
397
|
+
const queryClient = useQueryClient();
|
|
398
|
+
|
|
399
|
+
return useMutation({
|
|
400
|
+
mutationFn: ({ id, locale, etag }: DeleteLocalizedArgs) => client.deleteLocalized({ id, locale, etag }),
|
|
401
|
+
onSuccess: (result, variables) => {
|
|
402
|
+
if (result.success) {
|
|
403
|
+
// Invalidate specific locale query
|
|
404
|
+
queryClient.invalidateQueries({
|
|
405
|
+
queryKey: queryKeys.localized(variables.id, variables.locale),
|
|
406
|
+
});
|
|
407
|
+
// Invalidate the full content query
|
|
408
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.content(variables.id) });
|
|
409
|
+
}
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// ===== Site Hooks =====
|
|
415
|
+
|
|
416
|
+
export function useSitePages(site: string, locale?: string) {
|
|
417
|
+
const client = getContentAPIClient();
|
|
418
|
+
|
|
419
|
+
return useQuery<ContentListResult>({
|
|
420
|
+
queryKey: queryKeys.sitePages(site, locale),
|
|
421
|
+
queryFn: () => client.getSitePages(site, locale),
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export function usePageByPathname(site: string, pathname: string, locale?: string) {
|
|
426
|
+
const client = getContentAPIClient();
|
|
427
|
+
|
|
428
|
+
return useQuery<ContentManifest | null>({
|
|
429
|
+
queryKey: queryKeys.pageByPathname(site, pathname, locale),
|
|
430
|
+
queryFn: () => client.getPageByPathname(site, pathname, locale),
|
|
431
|
+
enabled: !!pathname,
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export function usePathnameAvailability(site: string, pathname: string, excludeId?: string, locale?: string) {
|
|
436
|
+
const client = getContentAPIClient();
|
|
437
|
+
|
|
438
|
+
return useQuery({
|
|
439
|
+
queryKey: queryKeys.pathnameAvailability(site, pathname, excludeId, locale),
|
|
440
|
+
queryFn: async () => {
|
|
441
|
+
const available = await client.isPathnameAvailable(site, pathname, excludeId, locale);
|
|
442
|
+
return { available };
|
|
443
|
+
},
|
|
444
|
+
enabled: !!site && !!pathname,
|
|
445
|
+
// For pathname validation, we want immediate feedback
|
|
446
|
+
staleTime: 0,
|
|
447
|
+
gcTime: 0,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
interface MovePageArgs {
|
|
452
|
+
site: string;
|
|
453
|
+
id: string;
|
|
454
|
+
pathname: string;
|
|
455
|
+
locale?: string;
|
|
456
|
+
etag: string;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export function useMovePage() {
|
|
460
|
+
const client = getContentAPIClient();
|
|
461
|
+
const queryClient = useQueryClient();
|
|
462
|
+
|
|
463
|
+
return useMutation({
|
|
464
|
+
mutationFn: ({ site, id, pathname, locale = 'en', etag }: MovePageArgs) =>
|
|
465
|
+
client.movePage(site, id, pathname, locale, etag),
|
|
466
|
+
onSuccess: (result, variables) => {
|
|
467
|
+
if (result.moved) {
|
|
468
|
+
// Invalidate site pages and content queries
|
|
469
|
+
queryClient.invalidateQueries({ queryKey: ['sites', variables.site] });
|
|
470
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.content(variables.id) });
|
|
471
|
+
}
|
|
472
|
+
},
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// ===== Fragment Hooks =====
|
|
477
|
+
|
|
478
|
+
export function useFragments(collection?: string, locale?: string) {
|
|
479
|
+
const client = getContentAPIClient();
|
|
480
|
+
|
|
481
|
+
return useQuery<ContentListResult>({
|
|
482
|
+
queryKey: queryKeys.fragments(collection, locale),
|
|
483
|
+
queryFn: () => client.getFragments(collection, locale),
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export function useFragmentByName(name: string, collection?: string, locale?: string) {
|
|
488
|
+
const client = getContentAPIClient();
|
|
489
|
+
|
|
490
|
+
return useQuery<ContentManifest | null>({
|
|
491
|
+
queryKey: queryKeys.fragmentByName(name, collection, locale),
|
|
492
|
+
queryFn: () => client.getFragmentByName(name, collection, locale),
|
|
493
|
+
enabled: !!name,
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// ===== Data Hooks =====
|
|
498
|
+
|
|
499
|
+
export function useData(collection?: string, locale?: string) {
|
|
500
|
+
const client = getContentAPIClient();
|
|
501
|
+
|
|
502
|
+
return useQuery<ContentListResult>({
|
|
503
|
+
queryKey: queryKeys.data(collection, locale),
|
|
504
|
+
queryFn: () => client.getData(collection, locale),
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
export function useDataByName(name: string, collection: string, locale?: string) {
|
|
509
|
+
const client = getContentAPIClient();
|
|
510
|
+
|
|
511
|
+
return useQuery<ContentManifest | null>({
|
|
512
|
+
queryKey: queryKeys.dataByName(name, collection, locale),
|
|
513
|
+
queryFn: () => client.getDataByName(name, collection, locale),
|
|
514
|
+
enabled: !!name && !!collection,
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export function useDataNameAvailability(name: string, collection: string, excludeId?: string) {
|
|
519
|
+
const client = getContentAPIClient();
|
|
520
|
+
|
|
521
|
+
return useQuery({
|
|
522
|
+
queryKey: queryKeys.dataNameAvailability(name, collection, excludeId),
|
|
523
|
+
queryFn: async () => {
|
|
524
|
+
const available = await client.isDataNameAvailable(name, collection, excludeId);
|
|
525
|
+
return { available };
|
|
526
|
+
},
|
|
527
|
+
enabled: !!name && !!collection,
|
|
528
|
+
staleTime: 0,
|
|
529
|
+
gcTime: 0,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
export function useDataCollections() {
|
|
534
|
+
const client = getContentAPIClient();
|
|
535
|
+
|
|
536
|
+
return useQuery<string[]>({
|
|
537
|
+
queryKey: queryKeys.dataCollections(),
|
|
538
|
+
queryFn: () => client.getDataCollections(),
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// ===== Data Context Hooks =====
|
|
543
|
+
|
|
544
|
+
/** Whether two values are equal by value, Dates compared by their time. */
|
|
545
|
+
function isSameValue<T>(a: T, b: T): boolean {
|
|
546
|
+
if (a instanceof Date || b instanceof Date) {
|
|
547
|
+
return a instanceof Date && b instanceof Date && a.getTime() === b.getTime();
|
|
548
|
+
}
|
|
549
|
+
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
|
|
550
|
+
return Object.is(a, b);
|
|
551
|
+
}
|
|
552
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
553
|
+
const entries = Object.entries(a);
|
|
554
|
+
return (
|
|
555
|
+
entries.length === Object.keys(b).length &&
|
|
556
|
+
entries.every(([key, value]) => {
|
|
557
|
+
const descriptor = Object.getOwnPropertyDescriptor(b, key);
|
|
558
|
+
return descriptor !== undefined && isSameValue(value, descriptor.value);
|
|
559
|
+
})
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* The data context the published page gets for `page`, built in the browser
|
|
565
|
+
* from the site's routing bindings. Waits while either is unknown; null when
|
|
566
|
+
* no route serves the page's collection, since a query cannot hold undefined.
|
|
567
|
+
* A refetch that finds nothing changed hands back the context already held,
|
|
568
|
+
* so work keyed on it, such as resolving the page's blocks, does not run again.
|
|
569
|
+
*/
|
|
570
|
+
export function useDataContext(page: PageReference | undefined, bindings: EditorDataBindings | undefined) {
|
|
571
|
+
const client = getContentAPIClient();
|
|
572
|
+
const collection = page?.collection;
|
|
573
|
+
// The page is joined on read, not fetched: a Page settings save changes the
|
|
574
|
+
// page but not what its route binds, so it must not fetch the feed again.
|
|
575
|
+
const withPage = useCallback(
|
|
576
|
+
(routeData: RouteData | null): DataContext | null => (routeData && page ? { ...routeData, page } : null),
|
|
577
|
+
[page],
|
|
578
|
+
);
|
|
579
|
+
|
|
580
|
+
return useQuery<RouteData | null, Error, DataContext | null>({
|
|
581
|
+
queryKey: queryKeys.dataContext(collection, bindings),
|
|
582
|
+
queryFn: async () => {
|
|
583
|
+
// A manual refetch can run a disabled query before its inputs arrive.
|
|
584
|
+
if (!collection || !bindings) return null;
|
|
585
|
+
return (await buildRouteData(client, collection, bindings)) ?? null;
|
|
586
|
+
},
|
|
587
|
+
enabled: !!collection && !!bindings,
|
|
588
|
+
select: withPage,
|
|
589
|
+
// The cache's own sharing keeps unchanged plain objects and arrays only,
|
|
590
|
+
// so it would take the page timestamps (Dates) for a change every time.
|
|
591
|
+
structuralSharing: (held, fetched) => (isSameValue(held, fetched) ? held : fetched),
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// ===== Global Hooks =====
|
|
596
|
+
|
|
597
|
+
export function useAllContent(filters?: GlobalFilters) {
|
|
598
|
+
const client = getContentAPIClient();
|
|
599
|
+
|
|
600
|
+
return useQuery<ContentListResult>({
|
|
601
|
+
queryKey: queryKeys.allContent(filters),
|
|
602
|
+
queryFn: () => client.listAllContent(filters),
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
export function useUntranslatedContent(targetLocale: string, excludeSites?: string[], includeUnpublished?: boolean) {
|
|
607
|
+
const client = getContentAPIClient();
|
|
608
|
+
|
|
609
|
+
return useQuery<ContentListResult>({
|
|
610
|
+
queryKey: queryKeys.untranslatedContent(targetLocale, excludeSites, includeUnpublished),
|
|
611
|
+
queryFn: () => client.findUntranslatedContent(targetLocale, excludeSites, includeUnpublished),
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
export function useSitesConfig() {
|
|
616
|
+
const client = getContentAPIClient();
|
|
617
|
+
|
|
618
|
+
return useQuery<SitesConfig>({
|
|
619
|
+
queryKey: queryKeys.sitesConfig(),
|
|
620
|
+
queryFn: () => client.getSitesConfig(),
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// ===== Auth Hooks =====
|
|
625
|
+
|
|
626
|
+
export function useCurrentUser() {
|
|
627
|
+
const client = getContentAPIClient();
|
|
628
|
+
|
|
629
|
+
return useQuery({
|
|
630
|
+
queryKey: queryKeys.currentUser(),
|
|
631
|
+
queryFn: () => client.getCurrentUser(),
|
|
632
|
+
staleTime: 60000, // Cache for 1 minute (user doesn't change often)
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// ===== Batch Operations =====
|
|
637
|
+
|
|
638
|
+
export function useBatchUpdate() {
|
|
639
|
+
const client = getContentAPIClient();
|
|
640
|
+
const queryClient = useQueryClient();
|
|
641
|
+
|
|
642
|
+
return useMutation({
|
|
643
|
+
mutationFn: async (operations: UpdateLocaleInput[]) => {
|
|
644
|
+
const result = await client.batchUpdate(operations);
|
|
645
|
+
|
|
646
|
+
// Fetch updated content for successful operations
|
|
647
|
+
if (result.operations && result.updated > 0) {
|
|
648
|
+
const updatedContents = await Promise.all(
|
|
649
|
+
result.operations.map(async (opResult, index) => {
|
|
650
|
+
if (opResult.updated) {
|
|
651
|
+
const op = operations[index];
|
|
652
|
+
const updated = await client.getLocalized(op.id, op.locale);
|
|
653
|
+
return { index, content: updated };
|
|
654
|
+
}
|
|
655
|
+
return null;
|
|
656
|
+
}),
|
|
657
|
+
);
|
|
658
|
+
|
|
659
|
+
return {
|
|
660
|
+
...result,
|
|
661
|
+
updatedContents: updatedContents.filter(
|
|
662
|
+
(item): item is { index: number; content: LocalizedEntry } => item !== null,
|
|
663
|
+
),
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
return result;
|
|
668
|
+
},
|
|
669
|
+
onSuccess: (result, variables) => {
|
|
670
|
+
if ('updatedContents' in result && result.updatedContents) {
|
|
671
|
+
// Update caches with the fetched server data
|
|
672
|
+
result.updatedContents.forEach(({ index, content }) => {
|
|
673
|
+
if (!content) return;
|
|
674
|
+
|
|
675
|
+
const operation = variables[index];
|
|
676
|
+
|
|
677
|
+
// Update localized content
|
|
678
|
+
queryClient.setQueryData(queryKeys.localized(operation.id, operation.locale), content);
|
|
679
|
+
|
|
680
|
+
// Update full content entry
|
|
681
|
+
queryClient.setQueryData(queryKeys.content(operation.id), (old: ContentEntry | null | undefined) => {
|
|
682
|
+
if (!old) return old;
|
|
683
|
+
return {
|
|
684
|
+
...old,
|
|
685
|
+
locales: {
|
|
686
|
+
...old.locales,
|
|
687
|
+
[operation.locale]: content.localized,
|
|
688
|
+
},
|
|
689
|
+
};
|
|
690
|
+
});
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
// Update list queries with server data
|
|
694
|
+
const updatedMap = new Map(
|
|
695
|
+
result.updatedContents.map(({ index, content }) => {
|
|
696
|
+
const op = variables[index];
|
|
697
|
+
return [`${op.id}:${op.locale}`, content];
|
|
698
|
+
}),
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
const updateListQueries = (old: ContentListResult | undefined) => {
|
|
702
|
+
if (!isContentListResult(old)) return old;
|
|
703
|
+
return {
|
|
704
|
+
...old,
|
|
705
|
+
items: old.items.map((item) => {
|
|
706
|
+
const hasUpdate = Object.keys(item.locales).some((locale) => updatedMap.has(`${item.id}:${locale}`));
|
|
707
|
+
|
|
708
|
+
if (hasUpdate) {
|
|
709
|
+
const newLocales = { ...item.locales };
|
|
710
|
+
Object.keys(newLocales).forEach((locale) => {
|
|
711
|
+
const updated = updatedMap.get(`${item.id}:${locale}`);
|
|
712
|
+
if (updated) {
|
|
713
|
+
const { content, ...manifestData } = updated.localized;
|
|
714
|
+
newLocales[locale] = manifestData;
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
return { ...item, locales: newLocales };
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
return item;
|
|
721
|
+
}),
|
|
722
|
+
};
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
queryClient.setQueriesData({ queryKey: ['sites'], exact: false }, updateListQueries);
|
|
726
|
+
|
|
727
|
+
queryClient.setQueriesData({ queryKey: ['fragments'], exact: false }, updateListQueries);
|
|
728
|
+
|
|
729
|
+
queryClient.setQueriesData({ queryKey: ['data'], exact: false }, updateListQueries);
|
|
730
|
+
}
|
|
731
|
+
},
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// ===== Git Hooks =====
|
|
736
|
+
|
|
737
|
+
export function useGitStatus() {
|
|
738
|
+
const client = getContentAPIClient();
|
|
739
|
+
|
|
740
|
+
return useQuery<GitStatus>({
|
|
741
|
+
queryKey: queryKeys.gitStatus(),
|
|
742
|
+
queryFn: () => client.getGitStatus(),
|
|
743
|
+
refetchInterval: 10000,
|
|
744
|
+
staleTime: 5000,
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
export function useCommitChanges() {
|
|
749
|
+
const queryClient = useQueryClient();
|
|
750
|
+
const client = getContentAPIClient();
|
|
751
|
+
|
|
752
|
+
return useMutation({
|
|
753
|
+
mutationFn: (message: string | undefined) => client.commitChanges(message),
|
|
754
|
+
onSuccess: () => {
|
|
755
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.gitStatus() });
|
|
756
|
+
},
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
export function usePushChanges() {
|
|
761
|
+
const queryClient = useQueryClient();
|
|
762
|
+
const client = getContentAPIClient();
|
|
763
|
+
|
|
764
|
+
return useMutation({
|
|
765
|
+
mutationFn: () => client.pushChanges(),
|
|
766
|
+
onSuccess: () => {
|
|
767
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.gitStatus() });
|
|
768
|
+
},
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
export function usePullChanges() {
|
|
773
|
+
const queryClient = useQueryClient();
|
|
774
|
+
const client = getContentAPIClient();
|
|
775
|
+
|
|
776
|
+
return useMutation({
|
|
777
|
+
mutationFn: () => client.pullChanges(),
|
|
778
|
+
onSuccess: () => {
|
|
779
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.gitStatus() });
|
|
780
|
+
},
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
// ===== Asset Hooks =====
|
|
785
|
+
|
|
786
|
+
export function useAssets() {
|
|
787
|
+
const client = getContentAPIClient();
|
|
788
|
+
|
|
789
|
+
return useQuery<{ assets: AssetEntry[] }>({
|
|
790
|
+
queryKey: queryKeys.assets(),
|
|
791
|
+
queryFn: () => client.listAssets(),
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
export function useAsset(path: string) {
|
|
796
|
+
const client = getContentAPIClient();
|
|
797
|
+
|
|
798
|
+
return useQuery<AssetEntry | null>({
|
|
799
|
+
queryKey: queryKeys.asset(path),
|
|
800
|
+
queryFn: () => client.getAsset(path),
|
|
801
|
+
enabled: !!path,
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
export function useUploadAsset() {
|
|
806
|
+
const client = getContentAPIClient();
|
|
807
|
+
const queryClient = useQueryClient();
|
|
808
|
+
|
|
809
|
+
return useMutation({
|
|
810
|
+
mutationFn: (formData: FormData) => client.uploadAsset(formData),
|
|
811
|
+
onSuccess: () => {
|
|
812
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
813
|
+
queryClient.invalidateQueries({ queryKey: ['asset-folders'] });
|
|
814
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.folderTree() });
|
|
815
|
+
},
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
export function useImportAssetUrl() {
|
|
820
|
+
const client = getContentAPIClient();
|
|
821
|
+
const queryClient = useQueryClient();
|
|
822
|
+
|
|
823
|
+
return useMutation({
|
|
824
|
+
mutationFn: ({ url, alt, folder }: { url: string; alt?: string; folder?: string }) =>
|
|
825
|
+
client.importAssetUrl(url, alt, folder),
|
|
826
|
+
onSuccess: () => {
|
|
827
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
828
|
+
queryClient.invalidateQueries({ queryKey: ['asset-folders'] });
|
|
829
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.folderTree() });
|
|
830
|
+
},
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
export function useDeleteAsset() {
|
|
835
|
+
const client = getContentAPIClient();
|
|
836
|
+
const queryClient = useQueryClient();
|
|
837
|
+
|
|
838
|
+
return useMutation({
|
|
839
|
+
mutationFn: (path: string) => client.deleteAsset(path),
|
|
840
|
+
onSuccess: () => {
|
|
841
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
842
|
+
queryClient.invalidateQueries({ queryKey: ['asset-folders'] });
|
|
843
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.folderTree() });
|
|
844
|
+
},
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
export function useBulkDeleteAssets() {
|
|
849
|
+
const client = getContentAPIClient();
|
|
850
|
+
const queryClient = useQueryClient();
|
|
851
|
+
|
|
852
|
+
return useMutation({
|
|
853
|
+
mutationFn: async (paths: string[]) => {
|
|
854
|
+
let successCount = 0;
|
|
855
|
+
let failCount = 0;
|
|
856
|
+
for (const path of paths) {
|
|
857
|
+
try {
|
|
858
|
+
await client.deleteAsset(path);
|
|
859
|
+
successCount++;
|
|
860
|
+
} catch {
|
|
861
|
+
failCount++;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
return { successCount, failCount };
|
|
865
|
+
},
|
|
866
|
+
onSettled: () => {
|
|
867
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
868
|
+
queryClient.invalidateQueries({ queryKey: ['asset-folders'] });
|
|
869
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.folderTree() });
|
|
870
|
+
},
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// ===== Media Library Hooks =====
|
|
875
|
+
|
|
876
|
+
export function useAssetFolders(path?: string) {
|
|
877
|
+
const client = getContentAPIClient();
|
|
878
|
+
|
|
879
|
+
return useQuery<FolderListing>({
|
|
880
|
+
queryKey: queryKeys.assetFolders(path),
|
|
881
|
+
queryFn: () => client.listFolder(path),
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
export function useCreateFolder() {
|
|
886
|
+
const client = getContentAPIClient();
|
|
887
|
+
const queryClient = useQueryClient();
|
|
888
|
+
|
|
889
|
+
return useMutation({
|
|
890
|
+
mutationFn: (path: string) => client.createFolder(path),
|
|
891
|
+
onSuccess: () => {
|
|
892
|
+
queryClient.invalidateQueries({ queryKey: ['asset-folders'] });
|
|
893
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.folderTree() });
|
|
894
|
+
},
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
export function useUpdateAssetMetadata() {
|
|
899
|
+
const client = getContentAPIClient();
|
|
900
|
+
const queryClient = useQueryClient();
|
|
901
|
+
|
|
902
|
+
return useMutation({
|
|
903
|
+
mutationFn: ({ path, updates }: { path: string; updates: AssetMetadataPatch }) =>
|
|
904
|
+
client.updateAssetMetadata(path, updates),
|
|
905
|
+
onSuccess: () => {
|
|
906
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
907
|
+
queryClient.invalidateQueries({ queryKey: ['asset-folders'] });
|
|
908
|
+
},
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
export function useAssetUsage(path: string | null) {
|
|
913
|
+
const client = getContentAPIClient();
|
|
914
|
+
|
|
915
|
+
return useQuery<AssetUsage[]>({
|
|
916
|
+
queryKey: queryKeys.assetUsage(path),
|
|
917
|
+
queryFn: () => client.getAssetUsage(path!),
|
|
918
|
+
enabled: !!path,
|
|
919
|
+
// Asked again each time: a page saved since the last answer may link the asset now
|
|
920
|
+
staleTime: 0,
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
export function useFolderTree() {
|
|
925
|
+
const client = getContentAPIClient();
|
|
926
|
+
|
|
927
|
+
return useQuery<{ tree: FolderTreeNode[] }>({
|
|
928
|
+
queryKey: queryKeys.folderTree(),
|
|
929
|
+
queryFn: () => client.getFolderTree(),
|
|
930
|
+
staleTime: 30_000, // Cache for 30 seconds
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
export function useMoveAssets() {
|
|
935
|
+
const client = getContentAPIClient();
|
|
936
|
+
const queryClient = useQueryClient();
|
|
937
|
+
|
|
938
|
+
return useMutation({
|
|
939
|
+
mutationFn: ({
|
|
940
|
+
filenames,
|
|
941
|
+
sourceFolder,
|
|
942
|
+
targetFolder,
|
|
943
|
+
}: {
|
|
944
|
+
filenames: string[];
|
|
945
|
+
sourceFolder: string;
|
|
946
|
+
targetFolder: string;
|
|
947
|
+
}) => client.moveAssets(filenames, sourceFolder, targetFolder),
|
|
948
|
+
onSuccess: () => {
|
|
949
|
+
// Invalidate all folder-related queries to refresh counts and listings
|
|
950
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
951
|
+
queryClient.invalidateQueries({ queryKey: ['asset-folders'] });
|
|
952
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.folderTree() });
|
|
953
|
+
},
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
export type { GitCommitResult, GitPullResult, GitPushResult, GitStatus };
|
|
958
|
+
// Re-export for convenience
|
|
959
|
+
export { APIClientError, ContentAPIClient, StaleWriteError };
|