@contentful/field-editor-shared 2.17.0 → 2.17.1-canary.59
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/dist/cjs/hooks/useContentTypes.js +94 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/queryClient.js +127 -0
- package/dist/cjs/queryKeys.js +94 -0
- package/dist/esm/hooks/useContentTypes.js +73 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/queryClient.js +65 -0
- package/dist/esm/queryKeys.js +64 -0
- package/dist/types/hooks/useContentTypes.d.ts +24 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/queryClient.d.ts +11 -0
- package/dist/types/queryKeys.d.ts +76 -0
- package/package.json +9 -2
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
fetchContentType: function() {
|
|
13
|
+
return fetchContentType;
|
|
14
|
+
},
|
|
15
|
+
useContentType: function() {
|
|
16
|
+
return useContentType;
|
|
17
|
+
},
|
|
18
|
+
useContentTypes: function() {
|
|
19
|
+
return useContentTypes;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const _react = require("react");
|
|
23
|
+
const _queryClient = require("../queryClient");
|
|
24
|
+
const _queryKeys = require("../queryKeys");
|
|
25
|
+
function useContentType(sdk, contentTypeId, options) {
|
|
26
|
+
const spaceId = sdk.ids.space;
|
|
27
|
+
const environmentId = sdk.ids.environmentAlias ?? sdk.ids.environment;
|
|
28
|
+
return (0, _queryClient.useQuery)((0, _queryKeys.createGetContentTypeKey)(spaceId, environmentId, contentTypeId), ()=>sdk.cma.contentType.get({
|
|
29
|
+
contentTypeId
|
|
30
|
+
}), {
|
|
31
|
+
staleTime: Infinity,
|
|
32
|
+
...options
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async function fetchContentType(sdk, contentTypeId) {
|
|
36
|
+
return sdk.cma.contentType.get({
|
|
37
|
+
contentTypeId
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function useContentTypes(source) {
|
|
41
|
+
const cma = 'cma' in source ? source.cma : source;
|
|
42
|
+
const navigator = 'navigator' in source ? source.navigator : undefined;
|
|
43
|
+
const queryClient = (0, _queryClient.useQueryClient)();
|
|
44
|
+
const spaceId = 'ids' in source ? source.ids.space : '';
|
|
45
|
+
const environmentId = 'ids' in source ? source.ids.environmentAlias ?? source.ids.environment : '';
|
|
46
|
+
const queryParams = (0, _react.useMemo)(()=>({
|
|
47
|
+
limit: 1000
|
|
48
|
+
}), []);
|
|
49
|
+
const { data: contentTypes = [] } = (0, _queryClient.useQuery)((0, _queryKeys.createGetManyContentTypesKey)(spaceId, environmentId, queryParams), async ()=>{
|
|
50
|
+
const allContentTypes = [];
|
|
51
|
+
const limit = 1000;
|
|
52
|
+
let skip = 0;
|
|
53
|
+
let total = 0;
|
|
54
|
+
do {
|
|
55
|
+
const response = await cma.contentType.getMany({
|
|
56
|
+
query: {
|
|
57
|
+
limit,
|
|
58
|
+
skip
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
allContentTypes.push(...response.items);
|
|
62
|
+
total = response.total;
|
|
63
|
+
skip += response.items.length;
|
|
64
|
+
}while (skip < total)
|
|
65
|
+
return allContentTypes;
|
|
66
|
+
}, {
|
|
67
|
+
staleTime: Infinity,
|
|
68
|
+
refetchOnMount: false
|
|
69
|
+
});
|
|
70
|
+
(0, _react.useEffect)(()=>{
|
|
71
|
+
if (!navigator?.onSlideInNavigation) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const unsubscribe = navigator.onSlideInNavigation(({ oldSlideLevel, newSlideLevel })=>{
|
|
75
|
+
if (oldSlideLevel > newSlideLevel) {
|
|
76
|
+
void queryClient.invalidateQueries((0, _queryKeys.createGetManyContentTypesKey)(spaceId, environmentId, queryParams));
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return unsubscribe;
|
|
80
|
+
}, [
|
|
81
|
+
navigator,
|
|
82
|
+
queryClient,
|
|
83
|
+
spaceId,
|
|
84
|
+
environmentId,
|
|
85
|
+
queryParams
|
|
86
|
+
]);
|
|
87
|
+
const invalidate = ()=>{
|
|
88
|
+
return queryClient.invalidateQueries((0, _queryKeys.createGetManyContentTypesKey)(spaceId, environmentId, queryParams));
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
contentTypes,
|
|
92
|
+
invalidate
|
|
93
|
+
};
|
|
94
|
+
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -75,6 +75,9 @@ _export(exports, {
|
|
|
75
75
|
PredefinedValuesError: function() {
|
|
76
76
|
return _PredefinedValuesError.PredefinedValuesError;
|
|
77
77
|
},
|
|
78
|
+
SharedQueryClientProvider: function() {
|
|
79
|
+
return _queryClient.SharedQueryClientProvider;
|
|
80
|
+
},
|
|
78
81
|
SpaceAPI: function() {
|
|
79
82
|
return _appsdk.SpaceAPI;
|
|
80
83
|
},
|
|
@@ -92,6 +95,12 @@ _export(exports, {
|
|
|
92
95
|
},
|
|
93
96
|
toLocaleString: function() {
|
|
94
97
|
return _shortenStorageUnit.toLocaleString;
|
|
98
|
+
},
|
|
99
|
+
useQuery: function() {
|
|
100
|
+
return _queryClient.useQuery;
|
|
101
|
+
},
|
|
102
|
+
useQueryClient: function() {
|
|
103
|
+
return _queryClient.useQueryClient;
|
|
95
104
|
}
|
|
96
105
|
});
|
|
97
106
|
const _ModalDialogLauncher = /*#__PURE__*/ _interop_require_wildcard(require("./ModalDialogLauncher"));
|
|
@@ -108,11 +117,14 @@ _export_star(require("./types"), exports);
|
|
|
108
117
|
_export_star(require("./hooks/useActiveLocales"), exports);
|
|
109
118
|
_export_star(require("./hooks/useReleaseStatus"), exports);
|
|
110
119
|
_export_star(require("./hooks/useLocalePublishStatus"), exports);
|
|
120
|
+
_export_star(require("./hooks/useContentTypes"), exports);
|
|
111
121
|
_export_star(require("./LocalePublishingEntityStatusBadge"), exports);
|
|
112
122
|
_export_star(require("./ReleaseEntityStatusBadge"), exports);
|
|
113
123
|
_export_star(require("./utils/determineReleaseAction"), exports);
|
|
114
124
|
_export_star(require("./utils/getEntityReleaseStatus"), exports);
|
|
115
125
|
_export_star(require("./utils/getReleaseStatusBadgeConfig"), exports);
|
|
126
|
+
const _queryClient = require("./queryClient");
|
|
127
|
+
_export_star(require("./queryKeys"), exports);
|
|
116
128
|
function _export_star(from, to) {
|
|
117
129
|
Object.keys(from).forEach(function(k) {
|
|
118
130
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
SharedQueryClientProvider: function() {
|
|
13
|
+
return SharedQueryClientProvider;
|
|
14
|
+
},
|
|
15
|
+
useQuery: function() {
|
|
16
|
+
return useQuery;
|
|
17
|
+
},
|
|
18
|
+
useQueryClient: function() {
|
|
19
|
+
return useQueryClient;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
|
|
23
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
24
|
+
if (typeof WeakMap !== "function") return null;
|
|
25
|
+
var cacheBabelInterop = new WeakMap();
|
|
26
|
+
var cacheNodeInterop = new WeakMap();
|
|
27
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
28
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
29
|
+
})(nodeInterop);
|
|
30
|
+
}
|
|
31
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
32
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
33
|
+
return obj;
|
|
34
|
+
}
|
|
35
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
36
|
+
return {
|
|
37
|
+
default: obj
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
41
|
+
if (cache && cache.has(obj)) {
|
|
42
|
+
return cache.get(obj);
|
|
43
|
+
}
|
|
44
|
+
var newObj = {
|
|
45
|
+
__proto__: null
|
|
46
|
+
};
|
|
47
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
48
|
+
for(var key in obj){
|
|
49
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
50
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
51
|
+
if (desc && (desc.get || desc.set)) {
|
|
52
|
+
Object.defineProperty(newObj, key, desc);
|
|
53
|
+
} else {
|
|
54
|
+
newObj[key] = obj[key];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
newObj.default = obj;
|
|
59
|
+
if (cache) {
|
|
60
|
+
cache.set(obj, newObj);
|
|
61
|
+
}
|
|
62
|
+
return newObj;
|
|
63
|
+
}
|
|
64
|
+
let RQQueryClient;
|
|
65
|
+
let useRQ;
|
|
66
|
+
let useHostQueryClient = ()=>undefined;
|
|
67
|
+
try {
|
|
68
|
+
const rq = require('@tanstack/react-query');
|
|
69
|
+
RQQueryClient = rq.QueryClient;
|
|
70
|
+
useRQ = rq.useQuery;
|
|
71
|
+
useHostQueryClient = rq.useQueryClient;
|
|
72
|
+
} catch {}
|
|
73
|
+
const clientContext = /*#__PURE__*/ _react.createContext(undefined);
|
|
74
|
+
function useMaybeHostQueryClient() {
|
|
75
|
+
try {
|
|
76
|
+
return useHostQueryClient();
|
|
77
|
+
} catch {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function useQueryClient() {
|
|
82
|
+
const client = _react.useContext(clientContext);
|
|
83
|
+
const hostClient = useMaybeHostQueryClient();
|
|
84
|
+
return _react.useMemo(()=>{
|
|
85
|
+
if (client) {
|
|
86
|
+
return client;
|
|
87
|
+
}
|
|
88
|
+
if (hostClient) return hostClient;
|
|
89
|
+
if (!RQQueryClient) {
|
|
90
|
+
throw new Error('@tanstack/react-query is required to use QueryClient. Please install it as a dependency: npm install @tanstack/react-query');
|
|
91
|
+
}
|
|
92
|
+
return new RQQueryClient({
|
|
93
|
+
defaultOptions: {
|
|
94
|
+
queries: {
|
|
95
|
+
useErrorBoundary: false,
|
|
96
|
+
refetchOnWindowFocus: false,
|
|
97
|
+
refetchOnReconnect: true,
|
|
98
|
+
refetchOnMount: false,
|
|
99
|
+
staleTime: Infinity,
|
|
100
|
+
retry: false
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}, [
|
|
105
|
+
client,
|
|
106
|
+
hostClient
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
function useQuery(queryKey, queryFn, options) {
|
|
110
|
+
if (!useRQ) {
|
|
111
|
+
throw new Error('@tanstack/react-query is required to use useQuery. Please install it as a dependency: npm install @tanstack/react-query');
|
|
112
|
+
}
|
|
113
|
+
return useRQ(queryKey, queryFn, {
|
|
114
|
+
...options,
|
|
115
|
+
context: clientContext
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function SharedQueryClientProvider({ children, client: providedClient }) {
|
|
119
|
+
const internalClient = useQueryClient();
|
|
120
|
+
const client = _react.useMemo(()=>providedClient ?? internalClient, [
|
|
121
|
+
providedClient,
|
|
122
|
+
internalClient
|
|
123
|
+
]);
|
|
124
|
+
return /*#__PURE__*/ _react.createElement(clientContext.Provider, {
|
|
125
|
+
value: client
|
|
126
|
+
}, children);
|
|
127
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
createGetAssetKey: function() {
|
|
13
|
+
return createGetAssetKey;
|
|
14
|
+
},
|
|
15
|
+
createGetContentTypeKey: function() {
|
|
16
|
+
return createGetContentTypeKey;
|
|
17
|
+
},
|
|
18
|
+
createGetEntryKey: function() {
|
|
19
|
+
return createGetEntryKey;
|
|
20
|
+
},
|
|
21
|
+
createGetManyContentTypesKey: function() {
|
|
22
|
+
return createGetManyContentTypesKey;
|
|
23
|
+
},
|
|
24
|
+
createGetManyLocalesKey: function() {
|
|
25
|
+
return createGetManyLocalesKey;
|
|
26
|
+
},
|
|
27
|
+
createGetSpaceKey: function() {
|
|
28
|
+
return createGetSpaceKey;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const createGetEntryKey = (spaceId, environmentId, entryId)=>{
|
|
32
|
+
return [
|
|
33
|
+
'spaces',
|
|
34
|
+
spaceId,
|
|
35
|
+
'environments',
|
|
36
|
+
environmentId,
|
|
37
|
+
'entries',
|
|
38
|
+
'get',
|
|
39
|
+
entryId
|
|
40
|
+
];
|
|
41
|
+
};
|
|
42
|
+
const createGetAssetKey = (spaceId, environmentId, assetId)=>{
|
|
43
|
+
return [
|
|
44
|
+
'spaces',
|
|
45
|
+
spaceId,
|
|
46
|
+
'environments',
|
|
47
|
+
environmentId,
|
|
48
|
+
'assets',
|
|
49
|
+
'get',
|
|
50
|
+
assetId
|
|
51
|
+
];
|
|
52
|
+
};
|
|
53
|
+
const createGetSpaceKey = (spaceId)=>{
|
|
54
|
+
return [
|
|
55
|
+
'spaces',
|
|
56
|
+
'get',
|
|
57
|
+
spaceId
|
|
58
|
+
];
|
|
59
|
+
};
|
|
60
|
+
const createGetManyLocalesKey = (spaceId, environmentId, params = {})=>{
|
|
61
|
+
return [
|
|
62
|
+
'spaces',
|
|
63
|
+
spaceId,
|
|
64
|
+
'environments',
|
|
65
|
+
environmentId,
|
|
66
|
+
'locales',
|
|
67
|
+
params
|
|
68
|
+
];
|
|
69
|
+
};
|
|
70
|
+
const createGetContentTypeKey = (spaceId, environmentId, contentTypeId)=>{
|
|
71
|
+
return [
|
|
72
|
+
'spaces',
|
|
73
|
+
spaceId,
|
|
74
|
+
'environments',
|
|
75
|
+
environmentId,
|
|
76
|
+
'content_types',
|
|
77
|
+
'get',
|
|
78
|
+
contentTypeId
|
|
79
|
+
];
|
|
80
|
+
};
|
|
81
|
+
const createGetManyContentTypesKey = (spaceId, environmentId, params = {})=>{
|
|
82
|
+
const prefix = [
|
|
83
|
+
'spaces',
|
|
84
|
+
spaceId,
|
|
85
|
+
'environments',
|
|
86
|
+
environmentId,
|
|
87
|
+
'content_types',
|
|
88
|
+
'getMany'
|
|
89
|
+
];
|
|
90
|
+
return Object.keys(params).length === 0 ? prefix : [
|
|
91
|
+
...prefix,
|
|
92
|
+
params
|
|
93
|
+
];
|
|
94
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { useEffect, useMemo } from 'react';
|
|
2
|
+
import { useQuery, useQueryClient } from '../queryClient';
|
|
3
|
+
import { createGetContentTypeKey, createGetManyContentTypesKey } from '../queryKeys';
|
|
4
|
+
export function useContentType(sdk, contentTypeId, options) {
|
|
5
|
+
const spaceId = sdk.ids.space;
|
|
6
|
+
const environmentId = sdk.ids.environmentAlias ?? sdk.ids.environment;
|
|
7
|
+
return useQuery(createGetContentTypeKey(spaceId, environmentId, contentTypeId), ()=>sdk.cma.contentType.get({
|
|
8
|
+
contentTypeId
|
|
9
|
+
}), {
|
|
10
|
+
staleTime: Infinity,
|
|
11
|
+
...options
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export async function fetchContentType(sdk, contentTypeId) {
|
|
15
|
+
return sdk.cma.contentType.get({
|
|
16
|
+
contentTypeId
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export function useContentTypes(source) {
|
|
20
|
+
const cma = 'cma' in source ? source.cma : source;
|
|
21
|
+
const navigator = 'navigator' in source ? source.navigator : undefined;
|
|
22
|
+
const queryClient = useQueryClient();
|
|
23
|
+
const spaceId = 'ids' in source ? source.ids.space : '';
|
|
24
|
+
const environmentId = 'ids' in source ? source.ids.environmentAlias ?? source.ids.environment : '';
|
|
25
|
+
const queryParams = useMemo(()=>({
|
|
26
|
+
limit: 1000
|
|
27
|
+
}), []);
|
|
28
|
+
const { data: contentTypes = [] } = useQuery(createGetManyContentTypesKey(spaceId, environmentId, queryParams), async ()=>{
|
|
29
|
+
const allContentTypes = [];
|
|
30
|
+
const limit = 1000;
|
|
31
|
+
let skip = 0;
|
|
32
|
+
let total = 0;
|
|
33
|
+
do {
|
|
34
|
+
const response = await cma.contentType.getMany({
|
|
35
|
+
query: {
|
|
36
|
+
limit,
|
|
37
|
+
skip
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
allContentTypes.push(...response.items);
|
|
41
|
+
total = response.total;
|
|
42
|
+
skip += response.items.length;
|
|
43
|
+
}while (skip < total)
|
|
44
|
+
return allContentTypes;
|
|
45
|
+
}, {
|
|
46
|
+
staleTime: Infinity,
|
|
47
|
+
refetchOnMount: false
|
|
48
|
+
});
|
|
49
|
+
useEffect(()=>{
|
|
50
|
+
if (!navigator?.onSlideInNavigation) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const unsubscribe = navigator.onSlideInNavigation(({ oldSlideLevel, newSlideLevel })=>{
|
|
54
|
+
if (oldSlideLevel > newSlideLevel) {
|
|
55
|
+
void queryClient.invalidateQueries(createGetManyContentTypesKey(spaceId, environmentId, queryParams));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return unsubscribe;
|
|
59
|
+
}, [
|
|
60
|
+
navigator,
|
|
61
|
+
queryClient,
|
|
62
|
+
spaceId,
|
|
63
|
+
environmentId,
|
|
64
|
+
queryParams
|
|
65
|
+
]);
|
|
66
|
+
const invalidate = ()=>{
|
|
67
|
+
return queryClient.invalidateQueries(createGetManyContentTypesKey(spaceId, environmentId, queryParams));
|
|
68
|
+
};
|
|
69
|
+
return {
|
|
70
|
+
contentTypes,
|
|
71
|
+
invalidate
|
|
72
|
+
};
|
|
73
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -12,8 +12,11 @@ export * from './types';
|
|
|
12
12
|
export * from './hooks/useActiveLocales';
|
|
13
13
|
export * from './hooks/useReleaseStatus';
|
|
14
14
|
export * from './hooks/useLocalePublishStatus';
|
|
15
|
+
export * from './hooks/useContentTypes';
|
|
15
16
|
export * from './LocalePublishingEntityStatusBadge';
|
|
16
17
|
export * from './ReleaseEntityStatusBadge';
|
|
17
18
|
export * from './utils/determineReleaseAction';
|
|
18
19
|
export * from './utils/getEntityReleaseStatus';
|
|
19
20
|
export * from './utils/getReleaseStatusBadgeConfig';
|
|
21
|
+
export { SharedQueryClientProvider, useQueryClient, useQuery } from './queryClient';
|
|
22
|
+
export * from './queryKeys';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
let RQQueryClient;
|
|
3
|
+
let useRQ;
|
|
4
|
+
let useHostQueryClient = ()=>undefined;
|
|
5
|
+
try {
|
|
6
|
+
const rq = require('@tanstack/react-query');
|
|
7
|
+
RQQueryClient = rq.QueryClient;
|
|
8
|
+
useRQ = rq.useQuery;
|
|
9
|
+
useHostQueryClient = rq.useQueryClient;
|
|
10
|
+
} catch {}
|
|
11
|
+
const clientContext = /*#__PURE__*/ React.createContext(undefined);
|
|
12
|
+
function useMaybeHostQueryClient() {
|
|
13
|
+
try {
|
|
14
|
+
return useHostQueryClient();
|
|
15
|
+
} catch {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function useQueryClient() {
|
|
20
|
+
const client = React.useContext(clientContext);
|
|
21
|
+
const hostClient = useMaybeHostQueryClient();
|
|
22
|
+
return React.useMemo(()=>{
|
|
23
|
+
if (client) {
|
|
24
|
+
return client;
|
|
25
|
+
}
|
|
26
|
+
if (hostClient) return hostClient;
|
|
27
|
+
if (!RQQueryClient) {
|
|
28
|
+
throw new Error('@tanstack/react-query is required to use QueryClient. Please install it as a dependency: npm install @tanstack/react-query');
|
|
29
|
+
}
|
|
30
|
+
return new RQQueryClient({
|
|
31
|
+
defaultOptions: {
|
|
32
|
+
queries: {
|
|
33
|
+
useErrorBoundary: false,
|
|
34
|
+
refetchOnWindowFocus: false,
|
|
35
|
+
refetchOnReconnect: true,
|
|
36
|
+
refetchOnMount: false,
|
|
37
|
+
staleTime: Infinity,
|
|
38
|
+
retry: false
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}, [
|
|
43
|
+
client,
|
|
44
|
+
hostClient
|
|
45
|
+
]);
|
|
46
|
+
}
|
|
47
|
+
export function useQuery(queryKey, queryFn, options) {
|
|
48
|
+
if (!useRQ) {
|
|
49
|
+
throw new Error('@tanstack/react-query is required to use useQuery. Please install it as a dependency: npm install @tanstack/react-query');
|
|
50
|
+
}
|
|
51
|
+
return useRQ(queryKey, queryFn, {
|
|
52
|
+
...options,
|
|
53
|
+
context: clientContext
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export function SharedQueryClientProvider({ children, client: providedClient }) {
|
|
57
|
+
const internalClient = useQueryClient();
|
|
58
|
+
const client = React.useMemo(()=>providedClient ?? internalClient, [
|
|
59
|
+
providedClient,
|
|
60
|
+
internalClient
|
|
61
|
+
]);
|
|
62
|
+
return /*#__PURE__*/ React.createElement(clientContext.Provider, {
|
|
63
|
+
value: client
|
|
64
|
+
}, children);
|
|
65
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export const createGetEntryKey = (spaceId, environmentId, entryId)=>{
|
|
2
|
+
return [
|
|
3
|
+
'spaces',
|
|
4
|
+
spaceId,
|
|
5
|
+
'environments',
|
|
6
|
+
environmentId,
|
|
7
|
+
'entries',
|
|
8
|
+
'get',
|
|
9
|
+
entryId
|
|
10
|
+
];
|
|
11
|
+
};
|
|
12
|
+
export const createGetAssetKey = (spaceId, environmentId, assetId)=>{
|
|
13
|
+
return [
|
|
14
|
+
'spaces',
|
|
15
|
+
spaceId,
|
|
16
|
+
'environments',
|
|
17
|
+
environmentId,
|
|
18
|
+
'assets',
|
|
19
|
+
'get',
|
|
20
|
+
assetId
|
|
21
|
+
];
|
|
22
|
+
};
|
|
23
|
+
export const createGetSpaceKey = (spaceId)=>{
|
|
24
|
+
return [
|
|
25
|
+
'spaces',
|
|
26
|
+
'get',
|
|
27
|
+
spaceId
|
|
28
|
+
];
|
|
29
|
+
};
|
|
30
|
+
export const createGetManyLocalesKey = (spaceId, environmentId, params = {})=>{
|
|
31
|
+
return [
|
|
32
|
+
'spaces',
|
|
33
|
+
spaceId,
|
|
34
|
+
'environments',
|
|
35
|
+
environmentId,
|
|
36
|
+
'locales',
|
|
37
|
+
params
|
|
38
|
+
];
|
|
39
|
+
};
|
|
40
|
+
export const createGetContentTypeKey = (spaceId, environmentId, contentTypeId)=>{
|
|
41
|
+
return [
|
|
42
|
+
'spaces',
|
|
43
|
+
spaceId,
|
|
44
|
+
'environments',
|
|
45
|
+
environmentId,
|
|
46
|
+
'content_types',
|
|
47
|
+
'get',
|
|
48
|
+
contentTypeId
|
|
49
|
+
];
|
|
50
|
+
};
|
|
51
|
+
export const createGetManyContentTypesKey = (spaceId, environmentId, params = {})=>{
|
|
52
|
+
const prefix = [
|
|
53
|
+
'spaces',
|
|
54
|
+
spaceId,
|
|
55
|
+
'environments',
|
|
56
|
+
environmentId,
|
|
57
|
+
'content_types',
|
|
58
|
+
'getMany'
|
|
59
|
+
];
|
|
60
|
+
return Object.keys(params).length === 0 ? prefix : [
|
|
61
|
+
...prefix,
|
|
62
|
+
params
|
|
63
|
+
];
|
|
64
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ContentType, FieldAppSDK } from '@contentful/app-sdk';
|
|
2
|
+
type SDKWithCMA = Pick<FieldAppSDK, 'cma'>;
|
|
3
|
+
type SDKWithCMAAndNavigator = Pick<FieldAppSDK, 'cma' | 'navigator'>;
|
|
4
|
+
type SDKWithIdsAndCMA = Pick<FieldAppSDK, 'cma' | 'ids'>;
|
|
5
|
+
type CMAClient = SDKWithCMA['cma'];
|
|
6
|
+
/**
|
|
7
|
+
* Hook to fetch a single content type by ID.
|
|
8
|
+
* Uses proper query key for cache sharing with user_interface.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useContentType(sdk: Pick<FieldAppSDK, 'cma' | 'ids'>, contentTypeId: string, options?: {
|
|
11
|
+
enabled?: boolean;
|
|
12
|
+
}): import("@tanstack/react-query").UseQueryResult<ContentType, unknown>;
|
|
13
|
+
/**
|
|
14
|
+
* Simple helper to fetch a single content type by ID.
|
|
15
|
+
* For use in async functions or one-off fetches.
|
|
16
|
+
* For repeated fetches with caching, use the useContentType hook instead.
|
|
17
|
+
*/
|
|
18
|
+
export declare function fetchContentType(sdk: Pick<FieldAppSDK, 'cma'>, contentTypeId: string): Promise<ContentType>;
|
|
19
|
+
export type UseContentTypesResult = {
|
|
20
|
+
contentTypes: ContentType[];
|
|
21
|
+
invalidate: () => void;
|
|
22
|
+
};
|
|
23
|
+
export declare function useContentTypes(source: SDKWithCMAAndNavigator | SDKWithCMA | SDKWithIdsAndCMA | CMAClient): UseContentTypesResult;
|
|
24
|
+
export {};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -14,8 +14,11 @@ export * from './types';
|
|
|
14
14
|
export * from './hooks/useActiveLocales';
|
|
15
15
|
export * from './hooks/useReleaseStatus';
|
|
16
16
|
export * from './hooks/useLocalePublishStatus';
|
|
17
|
+
export * from './hooks/useContentTypes';
|
|
17
18
|
export * from './LocalePublishingEntityStatusBadge';
|
|
18
19
|
export * from './ReleaseEntityStatusBadge';
|
|
19
20
|
export * from './utils/determineReleaseAction';
|
|
20
21
|
export * from './utils/getEntityReleaseStatus';
|
|
21
22
|
export * from './utils/getReleaseStatusBadgeConfig';
|
|
23
|
+
export { SharedQueryClientProvider, useQueryClient, useQuery } from './queryClient';
|
|
24
|
+
export * from './queryKeys';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { QueryClient, UseQueryOptions, UseQueryResult, QueryKey, QueryFunction } from '@tanstack/react-query';
|
|
3
|
+
export declare function useQueryClient(): QueryClient;
|
|
4
|
+
export declare function useQuery<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(queryKey: TQueryKey, queryFn: QueryFunction<TQueryFnData, TQueryKey>, options?: Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey' | 'queryFn'>): UseQueryResult<TData, TError>;
|
|
5
|
+
/**
|
|
6
|
+
* Provides access to a query client either by sharing an existing client or
|
|
7
|
+
* creating a new one.
|
|
8
|
+
*/
|
|
9
|
+
export declare function SharedQueryClientProvider({ children, client: providedClient, }: React.PropsWithChildren<{
|
|
10
|
+
client?: QueryClient;
|
|
11
|
+
}>): React.JSX.Element;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query key factories matching user_interface repository structure.
|
|
3
|
+
*
|
|
4
|
+
* IMPORTANT: These must stay in sync with user_interface to enable cache sharing.
|
|
5
|
+
* When updating, check the corresponding file in user_interface repository.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Creates a query key for fetching a single entry.
|
|
9
|
+
* Matches the structure used in user_interface for cache sharing.
|
|
10
|
+
*
|
|
11
|
+
* Source: https://github.com/contentful/user_interface/blob/main/src/javascripts/core/react-query/cma/entry/useGetEntry.ts
|
|
12
|
+
*
|
|
13
|
+
* @param spaceId - The space ID
|
|
14
|
+
* @param environmentId - The environment ID
|
|
15
|
+
* @param entryId - The entry ID to fetch
|
|
16
|
+
* @returns Query key array for React Query
|
|
17
|
+
*/
|
|
18
|
+
export declare const createGetEntryKey: (spaceId: string, environmentId: string, entryId: string) => string[];
|
|
19
|
+
/**
|
|
20
|
+
* Creates a query key for fetching a single asset.
|
|
21
|
+
* Matches the structure used in user_interface for cache sharing.
|
|
22
|
+
*
|
|
23
|
+
* Source: https://github.com/contentful/user_interface/blob/main/src/javascripts/core/react-query/cma/asset/useGetAsset.ts
|
|
24
|
+
*
|
|
25
|
+
* @param spaceId - The space ID
|
|
26
|
+
* @param environmentId - The environment ID
|
|
27
|
+
* @param assetId - The asset ID to fetch
|
|
28
|
+
* @returns Query key array for React Query
|
|
29
|
+
*/
|
|
30
|
+
export declare const createGetAssetKey: (spaceId: string, environmentId: string, assetId: string) => string[];
|
|
31
|
+
/**
|
|
32
|
+
* Creates a query key for fetching a single space.
|
|
33
|
+
* Matches the structure used in user_interface for cache sharing.
|
|
34
|
+
*
|
|
35
|
+
* Source: https://github.com/contentful/user_interface/blob/main/src/javascripts/core/react-query/cma/space/useGetSpace.ts
|
|
36
|
+
*
|
|
37
|
+
* @param spaceId - The space ID to fetch
|
|
38
|
+
* @returns Query key array for React Query
|
|
39
|
+
*/
|
|
40
|
+
export declare const createGetSpaceKey: (spaceId: string) => string[];
|
|
41
|
+
/**
|
|
42
|
+
* Creates a query key for fetching multiple locales.
|
|
43
|
+
* Matches the structure used in user_interface for cache sharing.
|
|
44
|
+
*
|
|
45
|
+
* Source: https://github.com/contentful/user_interface/blob/main/src/javascripts/core/react-query/cma/locale/useGetManyLocales.ts
|
|
46
|
+
*
|
|
47
|
+
* @param spaceId - The space ID
|
|
48
|
+
* @param environmentId - The environment ID
|
|
49
|
+
* @param params - Optional query parameters
|
|
50
|
+
* @returns Query key array for React Query
|
|
51
|
+
*/
|
|
52
|
+
export declare const createGetManyLocalesKey: (spaceId: string, environmentId: string, params?: Record<string, unknown>) => (string | Record<string, unknown>)[];
|
|
53
|
+
/**
|
|
54
|
+
* Creates a query key for fetching a single content type.
|
|
55
|
+
* Matches the structure used in user_interface for cache sharing.
|
|
56
|
+
*
|
|
57
|
+
* Source: https://github.com/contentful/user_interface/blob/main/src/javascripts/core/react-query/cma/contentType/useGetContentType.ts
|
|
58
|
+
*
|
|
59
|
+
* @param spaceId - The space ID
|
|
60
|
+
* @param environmentId - The environment ID
|
|
61
|
+
* @param contentTypeId - The content type ID to fetch
|
|
62
|
+
* @returns Query key array for React Query
|
|
63
|
+
*/
|
|
64
|
+
export declare const createGetContentTypeKey: (spaceId: string, environmentId: string, contentTypeId: string) => string[];
|
|
65
|
+
/**
|
|
66
|
+
* Creates a query key for fetching multiple content types.
|
|
67
|
+
* Matches the structure used in user_interface for cache sharing.
|
|
68
|
+
*
|
|
69
|
+
* Source: https://github.com/contentful/user_interface/blob/main/src/javascripts/core/react-query/cma/contentType/useGetManyContentTypes.ts
|
|
70
|
+
*
|
|
71
|
+
* @param spaceId - The space ID
|
|
72
|
+
* @param environmentId - The environment ID
|
|
73
|
+
* @param params - Optional query parameters (e.g., { limit: 1000 })
|
|
74
|
+
* @returns Query key array for React Query
|
|
75
|
+
*/
|
|
76
|
+
export declare const createGetManyContentTypesKey: (spaceId: string, environmentId: string, params?: Record<string, unknown>) => (string | Record<string, unknown>)[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-shared",
|
|
3
|
-
"version": "2.17.
|
|
3
|
+
"version": "2.17.1-canary.59+54b401aa",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"@contentful/app-sdk": "^4.42.0",
|
|
40
40
|
"@contentful/field-editor-test-utils": "^1.7.0",
|
|
41
41
|
"@lingui/core": "5.3.0",
|
|
42
|
+
"@tanstack/react-query": "^4.3.9",
|
|
42
43
|
"@testing-library/react": "16.3.0"
|
|
43
44
|
},
|
|
44
45
|
"dependencies": {
|
|
@@ -54,11 +55,17 @@
|
|
|
54
55
|
"peerDependencies": {
|
|
55
56
|
"@contentful/app-sdk": "^4.29.0",
|
|
56
57
|
"@lingui/core": "^5.3.0",
|
|
58
|
+
"@tanstack/react-query": "^4.3.9",
|
|
57
59
|
"react": ">=16.8.0",
|
|
58
60
|
"react-dom": ">=16.8.0"
|
|
59
61
|
},
|
|
62
|
+
"peerDependenciesMeta": {
|
|
63
|
+
"@tanstack/react-query": {
|
|
64
|
+
"optional": true
|
|
65
|
+
}
|
|
66
|
+
},
|
|
60
67
|
"publishConfig": {
|
|
61
68
|
"registry": "https://npm.pkg.github.com/"
|
|
62
69
|
},
|
|
63
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "54b401aa9908c8380c3801bf25fa38e23dff72d2"
|
|
64
71
|
}
|