@spree/dashboard-core 1.0.0-rc.5 → 1.0.0-rc.6

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.
@@ -0,0 +1,66 @@
1
+ import { getApiClient, i18n, useResourceKey, useResourceMutation } from '@spree/dashboard-core'
2
+ import { useQuery } from '@tanstack/react-query'
3
+ import type { PanelDigitalAssetParams } from '../api-client'
4
+
5
+ // The logical invalidation key (tenant id auto-injected by useResourceMutation).
6
+ const digitalAssetsKey = (productId: string) => ['products', productId, 'digital-assets']
7
+
8
+ function digitalAssetsClient() {
9
+ const client = getApiClient().digitalAssets
10
+ if (!client) throw new Error('This panel cannot manage digital files.')
11
+ return client
12
+ }
13
+
14
+ export function useDigitalAssets(productId: string, page = 1, enabled = true) {
15
+ return useQuery({
16
+ queryKey: useResourceKey('products', productId, 'digital-assets', page),
17
+ queryFn: () => digitalAssetsClient().list(productId, { page, limit: 25 }),
18
+ enabled: enabled && Boolean(productId) && Boolean(getApiClient().digitalAssets),
19
+ })
20
+ }
21
+
22
+ // The selectable sources for a new asset. Almost always just the File default,
23
+ // so a host with no extra provider gets a one-entry list and the card keeps its
24
+ // plain "Add file" button.
25
+ export function useDigitalAssetProviders(productId: string, enabled = true) {
26
+ const providers = getApiClient().digitalAssets?.providers
27
+ return useQuery({
28
+ // Not keyed by product: the registry is the same for all of them.
29
+ queryKey: useResourceKey('digital-asset-providers'),
30
+ queryFn: () => providers!(productId),
31
+ enabled: enabled && Boolean(productId) && Boolean(providers),
32
+ staleTime: Number.POSITIVE_INFINITY, // the registry doesn't change at runtime
33
+ })
34
+ }
35
+
36
+ /**
37
+ * `errorMessage: false` hands every failure to the caller — the card names
38
+ * the file in its own toast, and two toasts for one refusal is noise.
39
+ */
40
+ export function useCreateDigitalAsset(productId: string, options: { errorMessage?: false } = {}) {
41
+ return useResourceMutation({
42
+ mutationFn: (params: PanelDigitalAssetParams) =>
43
+ digitalAssetsClient().create(productId, params),
44
+ invalidate: [digitalAssetsKey(productId)],
45
+ successMessage: i18n.t('admin.messages.digital_asset_saved'),
46
+ ...options,
47
+ })
48
+ }
49
+
50
+ export function useUpdateDigitalAsset(productId: string, options: { errorMessage?: false } = {}) {
51
+ return useResourceMutation({
52
+ mutationFn: ({ id, ...params }: PanelDigitalAssetParams & { id: string }) =>
53
+ digitalAssetsClient().update(productId, id, params),
54
+ invalidate: [digitalAssetsKey(productId)],
55
+ successMessage: i18n.t('admin.messages.digital_asset_saved'),
56
+ ...options,
57
+ })
58
+ }
59
+
60
+ export function useDeleteDigitalAsset(productId: string) {
61
+ return useResourceMutation({
62
+ mutationFn: (id: string) => digitalAssetsClient().delete(productId, id),
63
+ invalidate: [digitalAssetsKey(productId)],
64
+ successMessage: i18n.t('admin.messages.digital_asset_removed'),
65
+ })
66
+ }