@wordpress/core-data 7.32.0 → 7.32.1-next.ff1cebbba.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/build/actions.js +375 -632
- package/build/actions.js.map +7 -1
- package/build/batch/create-batch.js +49 -62
- package/build/batch/create-batch.js.map +7 -1
- package/build/batch/default-processor.js +43 -39
- package/build/batch/default-processor.js.map +7 -1
- package/build/batch/index.js +38 -17
- package/build/batch/index.js.map +7 -1
- package/build/dynamic-entities.js +30 -32
- package/build/dynamic-entities.js.map +7 -1
- package/build/entities.js +298 -410
- package/build/entities.js.map +7 -1
- package/build/entity-context.js +29 -12
- package/build/entity-context.js.map +7 -1
- package/build/entity-provider.js +38 -46
- package/build/entity-provider.js.map +7 -1
- package/build/entity-types/attachment.js +16 -5
- package/build/entity-types/attachment.js.map +7 -1
- package/build/entity-types/base-entity-records.js +16 -42
- package/build/entity-types/base-entity-records.js.map +7 -1
- package/build/entity-types/base.js +16 -5
- package/build/entity-types/base.js.map +7 -1
- package/build/entity-types/comment.js +16 -5
- package/build/entity-types/comment.js.map +7 -1
- package/build/entity-types/global-styles-revision.js +16 -5
- package/build/entity-types/global-styles-revision.js.map +7 -1
- package/build/entity-types/helpers.js +16 -5
- package/build/entity-types/helpers.js.map +7 -1
- package/build/entity-types/index.js +16 -5
- package/build/entity-types/index.js.map +7 -1
- package/build/entity-types/menu-location.js +16 -5
- package/build/entity-types/menu-location.js.map +7 -1
- package/build/entity-types/nav-menu-item.js +16 -5
- package/build/entity-types/nav-menu-item.js.map +7 -1
- package/build/entity-types/nav-menu.js +16 -5
- package/build/entity-types/nav-menu.js.map +7 -1
- package/build/entity-types/page.js +16 -5
- package/build/entity-types/page.js.map +7 -1
- package/build/entity-types/plugin.js +16 -5
- package/build/entity-types/plugin.js.map +7 -1
- package/build/entity-types/post-revision.js +16 -5
- package/build/entity-types/post-revision.js.map +7 -1
- package/build/entity-types/post-status.js +16 -5
- package/build/entity-types/post-status.js.map +7 -1
- package/build/entity-types/post.js +16 -5
- package/build/entity-types/post.js.map +7 -1
- package/build/entity-types/settings.js +16 -5
- package/build/entity-types/settings.js.map +7 -1
- package/build/entity-types/sidebar.js +16 -5
- package/build/entity-types/sidebar.js.map +7 -1
- package/build/entity-types/taxonomy.js +16 -5
- package/build/entity-types/taxonomy.js.map +7 -1
- package/build/entity-types/term.js +16 -5
- package/build/entity-types/term.js.map +7 -1
- package/build/entity-types/theme.js +16 -5
- package/build/entity-types/theme.js.map +7 -1
- package/build/entity-types/type.js +16 -5
- package/build/entity-types/type.js.map +7 -1
- package/build/entity-types/user.js +16 -5
- package/build/entity-types/user.js.map +7 -1
- package/build/entity-types/widget-type.js +16 -5
- package/build/entity-types/widget-type.js.map +7 -1
- package/build/entity-types/widget.js +16 -5
- package/build/entity-types/widget.js.map +7 -1
- package/build/entity-types/wp-template-part.js +16 -5
- package/build/entity-types/wp-template-part.js.map +7 -1
- package/build/entity-types/wp-template.js +16 -5
- package/build/entity-types/wp-template.js.map +7 -1
- package/build/fetch/__experimental-fetch-link-suggestions.js +141 -154
- package/build/fetch/__experimental-fetch-link-suggestions.js.map +7 -1
- package/build/fetch/__experimental-fetch-url-data.js +47 -59
- package/build/fetch/__experimental-fetch-url-data.js.map +7 -1
- package/build/fetch/index.js +53 -32
- package/build/fetch/index.js.map +7 -1
- package/build/footnotes/get-footnotes-order.js +38 -25
- package/build/footnotes/get-footnotes-order.js.map +7 -1
- package/build/footnotes/get-rich-text-values-cached.js +26 -27
- package/build/footnotes/get-rich-text-values-cached.js.map +7 -1
- package/build/footnotes/index.js +68 -55
- package/build/footnotes/index.js.map +7 -1
- package/build/hooks/constants.js +33 -12
- package/build/hooks/constants.js.map +7 -1
- package/build/hooks/index.js +59 -68
- package/build/hooks/index.js.map +7 -1
- package/build/hooks/memoize.js +34 -12
- package/build/hooks/memoize.js.map +7 -1
- package/build/hooks/use-entity-block-editor.js +111 -119
- package/build/hooks/use-entity-block-editor.js.map +7 -1
- package/build/hooks/use-entity-id.js +25 -23
- package/build/hooks/use-entity-id.js.map +7 -1
- package/build/hooks/use-entity-prop.js +59 -60
- package/build/hooks/use-entity-prop.js.map +7 -1
- package/build/hooks/use-entity-record.js +95 -155
- package/build/hooks/use-entity-record.js.map +7 -1
- package/build/hooks/use-entity-records.js +131 -139
- package/build/hooks/use-entity-records.js.map +7 -1
- package/build/hooks/use-query-select.js +65 -84
- package/build/hooks/use-query-select.js.map +7 -1
- package/build/hooks/use-resource-permissions.js +92 -145
- package/build/hooks/use-resource-permissions.js.map +7 -1
- package/build/index.js +96 -153
- package/build/index.js.map +7 -1
- package/build/lock-unlock.js +31 -14
- package/build/lock-unlock.js.map +7 -1
- package/build/locks/actions.js +36 -19
- package/build/locks/actions.js.map +7 -1
- package/build/locks/engine.js +48 -47
- package/build/locks/engine.js.map +7 -1
- package/build/locks/reducer.js +54 -63
- package/build/locks/reducer.js.map +7 -1
- package/build/locks/selectors.js +35 -30
- package/build/locks/selectors.js.map +7 -1
- package/build/locks/utils.js +37 -16
- package/build/locks/utils.js.map +7 -1
- package/build/name.js +27 -12
- package/build/name.js.map +7 -1
- package/build/private-actions.js +67 -75
- package/build/private-actions.js.map +7 -1
- package/build/private-apis.js +33 -16
- package/build/private-apis.js.map +7 -1
- package/build/private-selectors.js +204 -184
- package/build/private-selectors.js.map +7 -1
- package/build/queried-data/actions.js +32 -41
- package/build/queried-data/actions.js.map +7 -1
- package/build/queried-data/get-query-parts.js +41 -79
- package/build/queried-data/get-query-parts.js.map +7 -1
- package/build/queried-data/index.js +39 -36
- package/build/queried-data/index.js.map +7 -1
- package/build/queried-data/reducer.js +162 -193
- package/build/queried-data/reducer.js.map +7 -1
- package/build/queried-data/selectors.js +57 -85
- package/build/queried-data/selectors.js.map +7 -1
- package/build/reducer.js +279 -404
- package/build/reducer.js.map +7 -1
- package/build/resolvers.js +553 -600
- package/build/resolvers.js.map +7 -1
- package/build/selectors.js +456 -981
- package/build/selectors.js.map +7 -1
- package/build/sync.js +34 -22
- package/build/sync.js.map +7 -1
- package/build/types.js +16 -5
- package/build/types.js.map +7 -1
- package/build/utils/conservative-map-item.js +34 -27
- package/build/utils/conservative-map-item.js.map +7 -1
- package/build/utils/crdt-blocks.js +289 -0
- package/build/utils/crdt-blocks.js.map +7 -0
- package/build/utils/crdt.js +202 -0
- package/build/utils/crdt.js.map +7 -0
- package/build/utils/forward-resolver.js +24 -16
- package/build/utils/forward-resolver.js.map +7 -1
- package/build/utils/get-nested-value.js +26 -21
- package/build/utils/get-nested-value.js.map +7 -1
- package/build/utils/get-normalized-comma-separable.js +25 -17
- package/build/utils/get-normalized-comma-separable.js.map +7 -1
- package/build/utils/if-matching-action.js +25 -19
- package/build/utils/if-matching-action.js.map +7 -1
- package/build/utils/index.js +77 -108
- package/build/utils/index.js.map +7 -1
- package/build/utils/is-numeric-id.js +22 -12
- package/build/utils/is-numeric-id.js.map +7 -1
- package/build/utils/is-raw-attribute.js +22 -13
- package/build/utils/is-raw-attribute.js.map +7 -1
- package/build/utils/log-entity-deprecation.js +37 -38
- package/build/utils/log-entity-deprecation.js.map +7 -1
- package/build/utils/on-sub-key.js +30 -24
- package/build/utils/on-sub-key.js.map +7 -1
- package/build/utils/receive-intermediate-results.js +29 -6
- package/build/utils/receive-intermediate-results.js.map +7 -1
- package/build/utils/replace-action.js +24 -17
- package/build/utils/replace-action.js.map +7 -1
- package/build/utils/set-nested-value.js +25 -30
- package/build/utils/set-nested-value.js.map +7 -1
- package/build/utils/user-permissions.js +41 -13
- package/build/utils/user-permissions.js.map +7 -1
- package/build/utils/with-weak-map-cache.js +26 -22
- package/build/utils/with-weak-map-cache.js.map +7 -1
- package/build-module/actions.js +322 -601
- package/build-module/actions.js.map +7 -1
- package/build-module/batch/create-batch.js +21 -57
- package/build-module/batch/create-batch.js.map +7 -1
- package/build-module/batch/default-processor.js +14 -33
- package/build-module/batch/default-processor.js.map +7 -1
- package/build-module/batch/index.js +7 -3
- package/build-module/batch/index.js.map +7 -1
- package/build-module/dynamic-entities.js +7 -28
- package/build-module/dynamic-entities.js.map +7 -1
- package/build-module/entities.js +263 -399
- package/build-module/entities.js.map +7 -1
- package/build-module/entity-context.js +7 -7
- package/build-module/entity-context.js.map +7 -1
- package/build-module/entity-provider.js +19 -42
- package/build-module/entity-provider.js.map +7 -1
- package/build-module/entity-types/attachment.js +1 -2
- package/build-module/entity-types/attachment.js.map +7 -1
- package/build-module/entity-types/base-entity-records.js +1 -37
- package/build-module/entity-types/base-entity-records.js.map +7 -1
- package/build-module/entity-types/base.js +1 -2
- package/build-module/entity-types/base.js.map +7 -1
- package/build-module/entity-types/comment.js +1 -2
- package/build-module/entity-types/comment.js.map +7 -1
- package/build-module/entity-types/global-styles-revision.js +1 -2
- package/build-module/entity-types/global-styles-revision.js.map +7 -1
- package/build-module/entity-types/helpers.js +1 -2
- package/build-module/entity-types/helpers.js.map +7 -1
- package/build-module/entity-types/index.js +1 -2
- package/build-module/entity-types/index.js.map +7 -1
- package/build-module/entity-types/menu-location.js +1 -2
- package/build-module/entity-types/menu-location.js.map +7 -1
- package/build-module/entity-types/nav-menu-item.js +1 -2
- package/build-module/entity-types/nav-menu-item.js.map +7 -1
- package/build-module/entity-types/nav-menu.js +1 -2
- package/build-module/entity-types/nav-menu.js.map +7 -1
- package/build-module/entity-types/page.js +1 -2
- package/build-module/entity-types/page.js.map +7 -1
- package/build-module/entity-types/plugin.js +1 -2
- package/build-module/entity-types/plugin.js.map +7 -1
- package/build-module/entity-types/post-revision.js +1 -2
- package/build-module/entity-types/post-revision.js.map +7 -1
- package/build-module/entity-types/post-status.js +1 -2
- package/build-module/entity-types/post-status.js.map +7 -1
- package/build-module/entity-types/post.js +1 -2
- package/build-module/entity-types/post.js.map +7 -1
- package/build-module/entity-types/settings.js +1 -2
- package/build-module/entity-types/settings.js.map +7 -1
- package/build-module/entity-types/sidebar.js +1 -2
- package/build-module/entity-types/sidebar.js.map +7 -1
- package/build-module/entity-types/taxonomy.js +1 -2
- package/build-module/entity-types/taxonomy.js.map +7 -1
- package/build-module/entity-types/term.js +1 -2
- package/build-module/entity-types/term.js.map +7 -1
- package/build-module/entity-types/theme.js +1 -2
- package/build-module/entity-types/theme.js.map +7 -1
- package/build-module/entity-types/type.js +1 -2
- package/build-module/entity-types/type.js.map +7 -1
- package/build-module/entity-types/user.js +1 -2
- package/build-module/entity-types/user.js.map +7 -1
- package/build-module/entity-types/widget-type.js +1 -2
- package/build-module/entity-types/widget-type.js.map +7 -1
- package/build-module/entity-types/widget.js +1 -2
- package/build-module/entity-types/widget.js.map +7 -1
- package/build-module/entity-types/wp-template-part.js +1 -2
- package/build-module/entity-types/wp-template-part.js.map +7 -1
- package/build-module/entity-types/wp-template.js +1 -2
- package/build-module/entity-types/wp-template.js.map +7 -1
- package/build-module/fetch/__experimental-fetch-link-suggestions.js +111 -149
- package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +7 -1
- package/build-module/fetch/__experimental-fetch-url-data.js +20 -49
- package/build-module/fetch/__experimental-fetch-url-data.js.map +7 -1
- package/build-module/fetch/index.js +20 -15
- package/build-module/fetch/index.js.map +7 -1
- package/build-module/footnotes/get-footnotes-order.js +10 -19
- package/build-module/footnotes/get-footnotes-order.js.map +7 -1
- package/build-module/footnotes/get-rich-text-values-cached.js +8 -23
- package/build-module/footnotes/get-rich-text-values-cached.js.map +7 -1
- package/build-module/footnotes/index.js +34 -47
- package/build-module/footnotes/index.js.map +7 -1
- package/build-module/hooks/constants.js +11 -8
- package/build-module/hooks/constants.js.map +7 -1
- package/build-module/hooks/index.js +27 -15
- package/build-module/hooks/index.js.map +7 -1
- package/build-module/hooks/memoize.js +6 -8
- package/build-module/hooks/memoize.js.map +7 -1
- package/build-module/hooks/use-entity-block-editor.js +80 -110
- package/build-module/hooks/use-entity-block-editor.js.map +7 -1
- package/build-module/hooks/use-entity-id.js +7 -19
- package/build-module/hooks/use-entity-id.js.map +7 -1
- package/build-module/hooks/use-entity-prop.js +31 -55
- package/build-module/hooks/use-entity-prop.js.map +7 -1
- package/build-module/hooks/use-entity-record.js +63 -148
- package/build-module/hooks/use-entity-record.js.map +7 -1
- package/build-module/hooks/use-entity-records.js +98 -131
- package/build-module/hooks/use-entity-records.js.map +7 -1
- package/build-module/hooks/use-query-select.js +27 -71
- package/build-module/hooks/use-query-select.js.map +7 -1
- package/build-module/hooks/use-resource-permissions.js +57 -136
- package/build-module/hooks/use-resource-permissions.js.map +7 -1
- package/build-module/index.js +49 -71
- package/build-module/index.js.map +7 -1
- package/build-module/lock-unlock.js +8 -7
- package/build-module/lock-unlock.js.map +7 -1
- package/build-module/locks/actions.js +8 -13
- package/build-module/locks/actions.js.map +7 -1
- package/build-module/locks/engine.js +17 -38
- package/build-module/locks/engine.js.map +7 -1
- package/build-module/locks/reducer.js +37 -59
- package/build-module/locks/reducer.js.map +7 -1
- package/build-module/locks/selectors.js +16 -23
- package/build-module/locks/selectors.js.map +7 -1
- package/build-module/locks/utils.js +15 -12
- package/build-module/locks/utils.js.map +7 -1
- package/build-module/name.js +5 -8
- package/build-module/name.js.map +7 -1
- package/build-module/private-actions.js +35 -69
- package/build-module/private-actions.js.map +7 -1
- package/build-module/private-apis.js +8 -8
- package/build-module/private-apis.js.map +7 -1
- package/build-module/private-selectors.js +167 -174
- package/build-module/private-selectors.js.map +7 -1
- package/build-module/queried-data/actions.js +11 -38
- package/build-module/queried-data/actions.js.map +7 -1
- package/build-module/queried-data/get-query-parts.js +20 -75
- package/build-module/queried-data/get-query-parts.js.map +7 -1
- package/build-module/queried-data/index.js +7 -4
- package/build-module/queried-data/index.js.map +7 -1
- package/build-module/queried-data/reducer.js +134 -185
- package/build-module/queried-data/reducer.js.map +7 -1
- package/build-module/queried-data/selectors.js +23 -78
- package/build-module/queried-data/selectors.js.map +7 -1
- package/build-module/reducer.js +243 -393
- package/build-module/reducer.js.map +7 -1
- package/build-module/resolvers.js +478 -549
- package/build-module/resolvers.js.map +7 -1
- package/build-module/selectors.js +410 -953
- package/build-module/selectors.js.map +7 -1
- package/build-module/sync.js +14 -17
- package/build-module/sync.js.map +7 -1
- package/build-module/types.js +1 -2
- package/build-module/types.js.map +7 -1
- package/build-module/utils/conservative-map-item.js +6 -22
- package/build-module/utils/conservative-map-item.js.map +7 -1
- package/build-module/utils/crdt-blocks.js +255 -0
- package/build-module/utils/crdt-blocks.js.map +7 -0
- package/build-module/utils/crdt.js +167 -0
- package/build-module/utils/crdt.js.map +7 -0
- package/build-module/utils/forward-resolver.js +6 -12
- package/build-module/utils/forward-resolver.js.map +7 -1
- package/build-module/utils/get-nested-value.js +9 -18
- package/build-module/utils/get-nested-value.js.map +7 -1
- package/build-module/utils/get-normalized-comma-separable.js +7 -13
- package/build-module/utils/get-normalized-comma-separable.js.map +7 -1
- package/build-module/utils/if-matching-action.js +7 -15
- package/build-module/utils/if-matching-action.js.map +7 -1
- package/build-module/utils/index.js +35 -14
- package/build-module/utils/index.js.map +7 -1
- package/build-module/utils/is-numeric-id.js +5 -9
- package/build-module/utils/is-numeric-id.js.map +7 -1
- package/build-module/utils/is-raw-attribute.js +5 -10
- package/build-module/utils/is-raw-attribute.js.map +7 -1
- package/build-module/utils/log-entity-deprecation.js +8 -31
- package/build-module/utils/log-entity-deprecation.js.map +7 -1
- package/build-module/utils/on-sub-key.js +8 -19
- package/build-module/utils/on-sub-key.js.map +7 -1
- package/build-module/utils/receive-intermediate-results.js +7 -2
- package/build-module/utils/receive-intermediate-results.js.map +7 -1
- package/build-module/utils/replace-action.js +6 -13
- package/build-module/utils/replace-action.js.map +7 -1
- package/build-module/utils/set-nested-value.js +8 -27
- package/build-module/utils/set-nested-value.js.map +7 -1
- package/build-module/utils/user-permissions.js +19 -9
- package/build-module/utils/user-permissions.js.map +7 -1
- package/build-module/utils/with-weak-map-cache.js +8 -18
- package/build-module/utils/with-weak-map-cache.js.map +7 -1
- package/build-types/actions.d.ts.map +1 -1
- package/build-types/entities.d.ts +0 -56
- package/build-types/entities.d.ts.map +1 -1
- package/build-types/index.d.ts.map +1 -1
- package/build-types/private-selectors.d.ts.map +1 -1
- package/build-types/resolvers.d.ts +3 -0
- package/build-types/resolvers.d.ts.map +1 -1
- package/build-types/selectors.d.ts.map +1 -1
- package/build-types/sync.d.ts +6 -1
- package/build-types/sync.d.ts.map +1 -1
- package/build-types/types.d.ts +9 -0
- package/build-types/types.d.ts.map +1 -1
- package/build-types/utils/crdt-blocks.d.ts +30 -0
- package/build-types/utils/crdt-blocks.d.ts.map +1 -0
- package/build-types/utils/crdt.d.ts +49 -0
- package/build-types/utils/crdt.d.ts.map +1 -0
- package/package.json +26 -19
- package/src/actions.js +56 -74
- package/src/entities.js +59 -113
- package/src/private-selectors.ts +32 -7
- package/src/resolvers.js +173 -120
- package/src/selectors.ts +0 -13
- package/src/sync.ts +12 -0
- package/src/test/resolvers.js +183 -0
- package/src/types.ts +12 -0
- package/src/utils/crdt-blocks.ts +503 -0
- package/src/utils/crdt.ts +310 -0
- package/src/utils/test/crdt-blocks.ts +375 -0
- package/src/utils/test/crdt.ts +254 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/src/sync.js +0 -27
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-entity-record.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport deprecated from '@wordpress/deprecated';\nimport { useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Status } from './constants';\n\nexport interface EntityRecordResolution< RecordType > {\n\t/** The requested entity record */\n\trecord: RecordType | null;\n\n\t/** The edited entity record */\n\teditedRecord: Partial< RecordType >;\n\n\t/** The edits to the edited entity record */\n\tedits: Partial< RecordType >;\n\n\t/** Apply local (in-browser) edits to the edited entity record */\n\tedit: ( diff: Partial< RecordType > ) => void;\n\n\t/** Persist the edits to the server */\n\tsave: () => Promise< void >;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Does the record have any local edits?\n\t */\n\thasEdits: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n}\n\nexport interface Options {\n\t/**\n\t * Whether to run the query or short-circuit and return null.\n\t *\n\t * @default true\n\t */\n\tenabled: boolean;\n}\n\nconst EMPTY_OBJECT = {};\n\n/**\n * Resolves the specified entity record.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.\n * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.\n * @param recordId ID of the requested entity record.\n * @param options Optional hook options.\n * @example\n * ```js\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageTitleDisplay( { id } ) {\n * const { record, isResolving } = useEntityRecord( 'postType', 'page', id );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return record.title;\n * }\n *\n * // Rendered in the application:\n * // <PageTitleDisplay id={ 1 } />\n * ```\n *\n * In the above example, when `PageTitleDisplay` is rendered into an\n * application, the page and the resolution details will be retrieved from\n * the store state using `getEntityRecord()`, or resolved if missing.\n *\n * @example\n * ```js\n * import { useCallback } from 'react';\n * import { useDispatch } from '@wordpress/data';\n * import { __ } from '@wordpress/i18n';\n * import { TextControl } from '@wordpress/components';\n * import { store as noticeStore } from '@wordpress/notices';\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageRenameForm( { id } ) {\n * \tconst page = useEntityRecord( 'postType', 'page', id );\n * \tconst { createSuccessNotice, createErrorNotice } =\n * \t\tuseDispatch( noticeStore );\n *\n * \tconst setTitle = useCallback( ( title ) => {\n * \t\tpage.edit( { title } );\n * \t}, [ page.edit ] );\n *\n * \tif ( page.isResolving ) {\n * \t\treturn 'Loading...';\n * \t}\n *\n * \tasync function onRename( event ) {\n * \t\tevent.preventDefault();\n * \t\ttry {\n * \t\t\tawait page.save();\n * \t\t\tcreateSuccessNotice( __( 'Page renamed.' ), {\n * \t\t\t\ttype: 'snackbar',\n * \t\t\t} );\n * \t\t} catch ( error ) {\n * \t\t\tcreateErrorNotice( error.message, { type: 'snackbar' } );\n * \t\t}\n * \t}\n *\n * \treturn (\n * \t\t<form onSubmit={ onRename }>\n * \t\t\t<TextControl\n *\t\t\t\t__nextHasNoMarginBottom\n *\t\t\t\t__next40pxDefaultSize\n * \t\t\t\tlabel={ __( 'Name' ) }\n * \t\t\t\tvalue={ page.editedRecord.title }\n * \t\t\t\tonChange={ setTitle }\n * \t\t\t/>\n * \t\t\t<button type=\"submit\">{ __( 'Save' ) }</button>\n * \t\t</form>\n * \t);\n * }\n *\n * // Rendered in the application:\n * // <PageRenameForm id={ 1 } />\n * ```\n *\n * In the above example, updating and saving the page title is handled\n * via the `edit()` and `save()` mutation helpers provided by\n * `useEntityRecord()`;\n *\n * @return Entity record data.\n * @template RecordType\n */\nexport default function useEntityRecord< RecordType >(\n\tkind: string,\n\tname: string,\n\trecordId: string | number,\n\toptions: Options = { enabled: true }\n): EntityRecordResolution< RecordType > {\n\tconst { editEntityRecord, saveEditedEntityRecord } =\n\t\tuseDispatch( coreStore );\n\n\tconst mutations = useMemo(\n\t\t() => ( {\n\t\t\tedit: ( record, editOptions: any = {} ) =>\n\t\t\t\teditEntityRecord( kind, name, recordId, record, editOptions ),\n\t\t\tsave: ( saveOptions: any = {} ) =>\n\t\t\t\tsaveEditedEntityRecord( kind, name, recordId, {\n\t\t\t\t\tthrowOnError: true,\n\t\t\t\t\t...saveOptions,\n\t\t\t\t} ),\n\t\t} ),\n\t\t[ editEntityRecord, kind, name, recordId, saveEditedEntityRecord ]\n\t);\n\n\tconst { editedRecord, hasEdits, edits } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\teditedRecord: EMPTY_OBJECT,\n\t\t\t\t\thasEdits: false,\n\t\t\t\t\tedits: EMPTY_OBJECT,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\teditedRecord: select( coreStore ).getEditedEntityRecord(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\trecordId\n\t\t\t\t),\n\t\t\t\thasEdits: select( coreStore ).hasEditsForEntityRecord(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\trecordId\n\t\t\t\t),\n\t\t\t\tedits: select( coreStore ).getEntityRecordNonTransientEdits(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\trecordId\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ kind, name, recordId, options.enabled ]\n\t);\n\n\tconst { data: record, ...querySelectRest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\tdata: null,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecord( kind, name, recordId );\n\t\t},\n\t\t[ kind, name, recordId, options.enabled ]\n\t);\n\n\treturn {\n\t\trecord,\n\t\teditedRecord,\n\t\thasEdits,\n\t\tedits,\n\t\t...querySelectRest,\n\t\t...mutations,\n\t};\n}\n\nexport function __experimentalUseEntityRecord(\n\tkind: string,\n\tname: string,\n\trecordId: any,\n\toptions: any\n) {\n\tdeprecated( `wp.data.__experimentalUseEntityRecord`, {\n\t\talternative: 'wp.data.useEntityRecord',\n\t\tsince: '6.1',\n\t} );\n\treturn useEntityRecord( kind, name, recordId, options );\n}\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,aAAa,iBAAiB;AACvC,OAAO,gBAAgB;AACvB,SAAS,eAAe;AAKxB,OAAO,oBAAoB;AAC3B,SAAS,SAAS,iBAAiB;AA+CnC,MAAM,eAAe,CAAC;AA4FP,SAAR,gBACN,MACA,MACA,UACA,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;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,GAAG;AAAA,IACH,GAAG;AAAA,EACJ;AACD;AAEO,SAAS,8BACf,MACA,MACA,UACA,SACC;AACD,aAAY,yCAAyC;AAAA,IACpD,aAAa;AAAA,IACb,OAAO;AAAA,EACR,CAAE;AACF,SAAO,gBAAiB,MAAM,MAAM,UAAU,OAAQ;AACvD;",
|
|
6
|
+
"names": ["record"]
|
|
7
|
+
}
|
|
@@ -1,95 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Internal dependencies
|
|
11
|
-
*/
|
|
12
|
-
import useQuerySelect from './use-query-select';
|
|
13
|
-
import { store as coreStore } from '../';
|
|
14
|
-
import { unlock } from '../lock-unlock';
|
|
15
|
-
import { getNormalizedCommaSeparable } from '../utils';
|
|
1
|
+
import { addQueryArgs } from "@wordpress/url";
|
|
2
|
+
import deprecated from "@wordpress/deprecated";
|
|
3
|
+
import { useSelect } from "@wordpress/data";
|
|
4
|
+
import { useMemo } from "@wordpress/element";
|
|
5
|
+
import useQuerySelect from "./use-query-select";
|
|
6
|
+
import { store as coreStore } from "../";
|
|
7
|
+
import { unlock } from "../lock-unlock";
|
|
8
|
+
import { getNormalizedCommaSeparable } from "../utils";
|
|
16
9
|
const EMPTY_ARRAY = [];
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* <ul>
|
|
40
|
-
* {records.map(( page ) => (
|
|
41
|
-
* <li>{ page.title }</li>
|
|
42
|
-
* ))}
|
|
43
|
-
* </ul>
|
|
44
|
-
* );
|
|
45
|
-
* }
|
|
46
|
-
*
|
|
47
|
-
* // Rendered in the application:
|
|
48
|
-
* // <PageTitlesList />
|
|
49
|
-
* ```
|
|
50
|
-
*
|
|
51
|
-
* In the above example, when `PageTitlesList` is rendered into an
|
|
52
|
-
* application, the list of records and the resolution details will be retrieved from
|
|
53
|
-
* the store state using `getEntityRecords()`, or resolved if missing.
|
|
54
|
-
*
|
|
55
|
-
* @return Entity records data.
|
|
56
|
-
* @template RecordType
|
|
57
|
-
*/
|
|
58
|
-
export default function useEntityRecords(kind, name, queryArgs = {}, options = {
|
|
59
|
-
enabled: true
|
|
60
|
-
}) {
|
|
61
|
-
// Serialize queryArgs to a string that can be safely used as a React dep.
|
|
62
|
-
// We can't just pass queryArgs as one of the deps, because if it is passed
|
|
63
|
-
// as an object literal, then it will be a different object on each call even
|
|
64
|
-
// if the values remain the same.
|
|
65
|
-
const queryAsString = addQueryArgs('', queryArgs);
|
|
66
|
-
const {
|
|
67
|
-
data: records,
|
|
68
|
-
...rest
|
|
69
|
-
} = useQuerySelect(query => {
|
|
70
|
-
if (!options.enabled) {
|
|
71
|
-
return {
|
|
72
|
-
// Avoiding returning a new reference on every execution.
|
|
73
|
-
data: EMPTY_ARRAY
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
return query(coreStore).getEntityRecords(kind, name, queryArgs);
|
|
77
|
-
}, [kind, name, queryAsString, options.enabled]);
|
|
78
|
-
const {
|
|
79
|
-
totalItems,
|
|
80
|
-
totalPages
|
|
81
|
-
} = useSelect(select => {
|
|
82
|
-
if (!options.enabled) {
|
|
10
|
+
function useEntityRecords(kind, name, queryArgs = {}, options = { enabled: true }) {
|
|
11
|
+
const queryAsString = addQueryArgs("", queryArgs);
|
|
12
|
+
const { data: records, ...rest } = useQuerySelect(
|
|
13
|
+
(query) => {
|
|
14
|
+
if (!options.enabled) {
|
|
15
|
+
return {
|
|
16
|
+
// Avoiding returning a new reference on every execution.
|
|
17
|
+
data: EMPTY_ARRAY
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return query(coreStore).getEntityRecords(kind, name, queryArgs);
|
|
21
|
+
},
|
|
22
|
+
[kind, name, queryAsString, options.enabled]
|
|
23
|
+
);
|
|
24
|
+
const { totalItems, totalPages } = useSelect(
|
|
25
|
+
(select) => {
|
|
26
|
+
if (!options.enabled) {
|
|
27
|
+
return {
|
|
28
|
+
totalItems: null,
|
|
29
|
+
totalPages: null
|
|
30
|
+
};
|
|
31
|
+
}
|
|
83
32
|
return {
|
|
84
|
-
totalItems:
|
|
85
|
-
|
|
33
|
+
totalItems: select(coreStore).getEntityRecordsTotalItems(
|
|
34
|
+
kind,
|
|
35
|
+
name,
|
|
36
|
+
queryArgs
|
|
37
|
+
),
|
|
38
|
+
totalPages: select(coreStore).getEntityRecordsTotalPages(
|
|
39
|
+
kind,
|
|
40
|
+
name,
|
|
41
|
+
queryArgs
|
|
42
|
+
)
|
|
86
43
|
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
totalPages: select(coreStore).getEntityRecordsTotalPages(kind, name, queryArgs)
|
|
91
|
-
};
|
|
92
|
-
}, [kind, name, queryAsString, options.enabled]);
|
|
44
|
+
},
|
|
45
|
+
[kind, name, queryAsString, options.enabled]
|
|
46
|
+
);
|
|
93
47
|
return {
|
|
94
48
|
records,
|
|
95
49
|
totalItems,
|
|
@@ -97,53 +51,66 @@ export default function useEntityRecords(kind, name, queryArgs = {}, options = {
|
|
|
97
51
|
...rest
|
|
98
52
|
};
|
|
99
53
|
}
|
|
100
|
-
|
|
54
|
+
function __experimentalUseEntityRecords(kind, name, queryArgs, options) {
|
|
101
55
|
deprecated(`wp.data.__experimentalUseEntityRecords`, {
|
|
102
|
-
alternative:
|
|
103
|
-
since:
|
|
56
|
+
alternative: "wp.data.useEntityRecords",
|
|
57
|
+
since: "6.1"
|
|
104
58
|
});
|
|
105
59
|
return useEntityRecords(kind, name, queryArgs, options);
|
|
106
60
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
61
|
+
function useEntityRecordsWithPermissions(kind, name, queryArgs = {}, options = { enabled: true }) {
|
|
62
|
+
const entityConfig = useSelect(
|
|
63
|
+
(select) => select(coreStore).getEntityConfig(kind, name),
|
|
64
|
+
[kind, name]
|
|
65
|
+
);
|
|
66
|
+
const { records: data, ...ret } = useEntityRecords(
|
|
67
|
+
kind,
|
|
68
|
+
name,
|
|
69
|
+
{
|
|
70
|
+
...queryArgs,
|
|
71
|
+
// If _fields is provided, we need to include _links in the request for permission caching to work.
|
|
72
|
+
...queryArgs._fields ? {
|
|
73
|
+
_fields: [
|
|
74
|
+
.../* @__PURE__ */ new Set([
|
|
75
|
+
...getNormalizedCommaSeparable(
|
|
76
|
+
queryArgs._fields
|
|
77
|
+
) || [],
|
|
78
|
+
"_links"
|
|
79
|
+
])
|
|
80
|
+
].join()
|
|
81
|
+
} : {}
|
|
82
|
+
},
|
|
83
|
+
options
|
|
84
|
+
);
|
|
85
|
+
const ids = useMemo(
|
|
86
|
+
() => data?.map(
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
(record) => record[entityConfig?.key ?? "id"]
|
|
89
|
+
) ?? [],
|
|
90
|
+
[data, entityConfig?.key]
|
|
91
|
+
);
|
|
92
|
+
const permissions = useSelect(
|
|
93
|
+
(select) => {
|
|
94
|
+
const { getEntityRecordsPermissions } = unlock(
|
|
95
|
+
select(coreStore)
|
|
96
|
+
);
|
|
97
|
+
return getEntityRecordsPermissions(kind, name, ids);
|
|
98
|
+
},
|
|
99
|
+
[ids, kind, name]
|
|
100
|
+
);
|
|
101
|
+
const dataWithPermissions = useMemo(
|
|
102
|
+
() => data?.map((record, index) => ({
|
|
139
103
|
// @ts-ignore
|
|
140
104
|
...record,
|
|
141
105
|
permissions: permissions[index]
|
|
142
|
-
}))
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
...ret
|
|
147
|
-
};
|
|
106
|
+
})) ?? [],
|
|
107
|
+
[data, permissions]
|
|
108
|
+
);
|
|
109
|
+
return { records: dataWithPermissions, ...ret };
|
|
148
110
|
}
|
|
149
|
-
|
|
111
|
+
export {
|
|
112
|
+
__experimentalUseEntityRecords,
|
|
113
|
+
useEntityRecords as default,
|
|
114
|
+
useEntityRecordsWithPermissions
|
|
115
|
+
};
|
|
116
|
+
//# sourceMappingURL=use-entity-records.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"names":["addQueryArgs","deprecated","useSelect","useMemo","useQuerySelect","store","coreStore","unlock","getNormalizedCommaSeparable","EMPTY_ARRAY","useEntityRecords","kind","name","queryArgs","options","enabled","queryAsString","data","records","rest","query","getEntityRecords","totalItems","totalPages","select","getEntityRecordsTotalItems","getEntityRecordsTotalPages","__experimentalUseEntityRecords","alternative","since","useEntityRecordsWithPermissions","entityConfig","getEntityConfig","ret","_fields","Set","join","ids","_data$map","map","record","_entityConfig$key","key","permissions","getEntityRecordsPermissions","dataWithPermissions","_data$map2","index"],"sources":["@wordpress/core-data/src/hooks/use-entity-records.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\nimport deprecated from '@wordpress/deprecated';\nimport { useSelect } from '@wordpress/data';\nimport { useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Options } from './use-entity-record';\nimport type { Status } from './constants';\nimport { unlock } from '../lock-unlock';\nimport { getNormalizedCommaSeparable } from '../utils';\n\ninterface EntityRecordsResolution< RecordType > {\n\t/** The requested entity records */\n\trecords: RecordType[] | null;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n\n\t/**\n\t * The total number of available items (if not paginated).\n\t */\n\ttotalItems: number | null;\n\n\t/**\n\t * The total number of pages.\n\t */\n\ttotalPages: number | null;\n}\n\nexport type WithPermissions< RecordType > = RecordType & {\n\tpermissions: { delete: boolean; update: boolean };\n};\n\ninterface EntityRecordsWithPermissionsResolution< RecordType >\n\textends Omit< EntityRecordsResolution< RecordType >, 'records' > {\n\t/** The requested entity records with permissions */\n\trecords: WithPermissions< RecordType >[] | null;\n}\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Resolves the specified entity records.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.\n * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.\n * @param queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.\n * @param options Optional hook options.\n * @example\n * ```js\n * import { useEntityRecords } from '@wordpress/core-data';\n *\n * function PageTitlesList() {\n * const { records, isResolving } = useEntityRecords( 'postType', 'page' );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return (\n * <ul>\n * {records.map(( page ) => (\n * <li>{ page.title }</li>\n * ))}\n * </ul>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PageTitlesList />\n * ```\n *\n * In the above example, when `PageTitlesList` is rendered into an\n * application, the list of records and the resolution details will be retrieved from\n * the store state using `getEntityRecords()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template RecordType\n */\nexport default function useEntityRecords< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsResolution< RecordType > {\n\t// Serialize queryArgs to a string that can be safely used as a React dep.\n\t// We can't just pass queryArgs as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst queryAsString = addQueryArgs( '', queryArgs );\n\n\tconst { data: records, ...rest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\t// Avoiding returning a new reference on every execution.\n\t\t\t\t\tdata: EMPTY_ARRAY,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecords( kind, name, queryArgs );\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\tconst { totalItems, totalPages } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\ttotalItems: null,\n\t\t\t\t\ttotalPages: null,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn {\n\t\t\t\ttotalItems: select( coreStore ).getEntityRecordsTotalItems(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tqueryArgs\n\t\t\t\t),\n\t\t\t\ttotalPages: select( coreStore ).getEntityRecordsTotalPages(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tqueryArgs\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\treturn {\n\t\trecords,\n\t\ttotalItems,\n\t\ttotalPages,\n\t\t...rest,\n\t};\n}\n\nexport function __experimentalUseEntityRecords(\n\tkind: string,\n\tname: string,\n\tqueryArgs: any,\n\toptions: any\n) {\n\tdeprecated( `wp.data.__experimentalUseEntityRecords`, {\n\t\talternative: 'wp.data.useEntityRecords',\n\t\tsince: '6.1',\n\t} );\n\treturn useEntityRecords( kind, name, queryArgs, options );\n}\n\nexport function useEntityRecordsWithPermissions< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsWithPermissionsResolution< RecordType > {\n\tconst entityConfig = useSelect(\n\t\t( select ) => select( coreStore ).getEntityConfig( kind, name ),\n\t\t[ kind, name ]\n\t);\n\tconst { records: data, ...ret } = useEntityRecords(\n\t\tkind,\n\t\tname,\n\t\t{\n\t\t\t...queryArgs,\n\t\t\t// If _fields is provided, we need to include _links in the request for permission caching to work.\n\t\t\t...( queryArgs._fields\n\t\t\t\t? {\n\t\t\t\t\t\t_fields: [\n\t\t\t\t\t\t\t...new Set( [\n\t\t\t\t\t\t\t\t...( getNormalizedCommaSeparable(\n\t\t\t\t\t\t\t\t\tqueryArgs._fields\n\t\t\t\t\t\t\t\t) || [] ),\n\t\t\t\t\t\t\t\t'_links',\n\t\t\t\t\t\t\t] ),\n\t\t\t\t\t\t].join(),\n\t\t\t\t }\n\t\t\t\t: {} ),\n\t\t},\n\t\toptions\n\t);\n\tconst ids = useMemo(\n\t\t() =>\n\t\t\tdata?.map(\n\t\t\t\t// @ts-ignore\n\t\t\t\t( record: RecordType ) => record[ entityConfig?.key ?? 'id' ]\n\t\t\t) ?? [],\n\t\t[ data, entityConfig?.key ]\n\t);\n\n\tconst permissions = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecordsPermissions } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\t\t\treturn getEntityRecordsPermissions( kind, name, ids );\n\t\t},\n\t\t[ ids, kind, name ]\n\t);\n\n\tconst dataWithPermissions = useMemo(\n\t\t() =>\n\t\t\tdata?.map( ( record, index ) => ( {\n\t\t\t\t// @ts-ignore\n\t\t\t\t...record,\n\t\t\t\tpermissions: permissions[ index ],\n\t\t\t} ) ) ?? [],\n\t\t[ data, permissions ]\n\t);\n\n\treturn { records: dataWithPermissions, ...ret };\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAY,QAAQ,gBAAgB;AAC7C,OAAOC,UAAU,MAAM,uBAAuB;AAC9C,SAASC,SAAS,QAAQ,iBAAiB;AAC3C,SAASC,OAAO,QAAQ,oBAAoB;;AAE5C;AACA;AACA;AACA,OAAOC,cAAc,MAAM,oBAAoB;AAC/C,SAASC,KAAK,IAAIC,SAAS,QAAQ,KAAK;AAGxC,SAASC,MAAM,QAAQ,gBAAgB;AACvC,SAASC,2BAA2B,QAAQ,UAAU;AAwCtD,MAAMC,WAAW,GAAG,EAAE;;AAEtB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,gBAAgBA,CACvCC,IAAY,EACZC,IAAY,EACZC,SAAoC,GAAG,CAAC,CAAC,EACzCC,OAAgB,GAAG;EAAEC,OAAO,EAAE;AAAK,CAAC,EACI;EACxC;EACA;EACA;EACA;EACA,MAAMC,aAAa,GAAGhB,YAAY,CAAE,EAAE,EAAEa,SAAU,CAAC;EAEnD,MAAM;IAAEI,IAAI,EAAEC,OAAO;IAAE,GAAGC;EAAK,CAAC,GAAGf,cAAc,CAC9CgB,KAAK,IAAM;IACZ,IAAK,CAAEN,OAAO,CAACC,OAAO,EAAG;MACxB,OAAO;QACN;QACAE,IAAI,EAAER;MACP,CAAC;IACF;IACA,OAAOW,KAAK,CAAEd,SAAU,CAAC,CAACe,gBAAgB,CAAEV,IAAI,EAAEC,IAAI,EAAEC,SAAU,CAAC;EACpE,CAAC,EACD,CAAEF,IAAI,EAAEC,IAAI,EAAEI,aAAa,EAAEF,OAAO,CAACC,OAAO,CAC7C,CAAC;EAED,M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,cAAAA,UAAA,GAAI,EAAE;EAAA,GACZ,CAAE7B,IAAI,EAAE0B,WAAW,CACpB,CAAC;EAED,OAAO;IAAEzB,OAAO,EAAE2B,mBAAmB;IAAE,GAAGZ;EAAI,CAAC;AAChD","ignoreList":[]}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-entity-records.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\nimport deprecated from '@wordpress/deprecated';\nimport { useSelect } from '@wordpress/data';\nimport { useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Options } from './use-entity-record';\nimport type { Status } from './constants';\nimport { unlock } from '../lock-unlock';\nimport { getNormalizedCommaSeparable } from '../utils';\n\ninterface EntityRecordsResolution< RecordType > {\n\t/** The requested entity records */\n\trecords: RecordType[] | null;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n\n\t/**\n\t * The total number of available items (if not paginated).\n\t */\n\ttotalItems: number | null;\n\n\t/**\n\t * The total number of pages.\n\t */\n\ttotalPages: number | null;\n}\n\nexport type WithPermissions< RecordType > = RecordType & {\n\tpermissions: { delete: boolean; update: boolean };\n};\n\ninterface EntityRecordsWithPermissionsResolution< RecordType >\n\textends Omit< EntityRecordsResolution< RecordType >, 'records' > {\n\t/** The requested entity records with permissions */\n\trecords: WithPermissions< RecordType >[] | null;\n}\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Resolves the specified entity records.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.\n * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.\n * @param queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.\n * @param options Optional hook options.\n * @example\n * ```js\n * import { useEntityRecords } from '@wordpress/core-data';\n *\n * function PageTitlesList() {\n * const { records, isResolving } = useEntityRecords( 'postType', 'page' );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return (\n * <ul>\n * {records.map(( page ) => (\n * <li>{ page.title }</li>\n * ))}\n * </ul>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PageTitlesList />\n * ```\n *\n * In the above example, when `PageTitlesList` is rendered into an\n * application, the list of records and the resolution details will be retrieved from\n * the store state using `getEntityRecords()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template RecordType\n */\nexport default function useEntityRecords< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsResolution< RecordType > {\n\t// Serialize queryArgs to a string that can be safely used as a React dep.\n\t// We can't just pass queryArgs as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst queryAsString = addQueryArgs( '', queryArgs );\n\n\tconst { data: records, ...rest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\t// Avoiding returning a new reference on every execution.\n\t\t\t\t\tdata: EMPTY_ARRAY,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecords( kind, name, queryArgs );\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\tconst { totalItems, totalPages } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\ttotalItems: null,\n\t\t\t\t\ttotalPages: null,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn {\n\t\t\t\ttotalItems: select( coreStore ).getEntityRecordsTotalItems(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tqueryArgs\n\t\t\t\t),\n\t\t\t\ttotalPages: select( coreStore ).getEntityRecordsTotalPages(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tqueryArgs\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\treturn {\n\t\trecords,\n\t\ttotalItems,\n\t\ttotalPages,\n\t\t...rest,\n\t};\n}\n\nexport function __experimentalUseEntityRecords(\n\tkind: string,\n\tname: string,\n\tqueryArgs: any,\n\toptions: any\n) {\n\tdeprecated( `wp.data.__experimentalUseEntityRecords`, {\n\t\talternative: 'wp.data.useEntityRecords',\n\t\tsince: '6.1',\n\t} );\n\treturn useEntityRecords( kind, name, queryArgs, options );\n}\n\nexport function useEntityRecordsWithPermissions< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsWithPermissionsResolution< RecordType > {\n\tconst entityConfig = useSelect(\n\t\t( select ) => select( coreStore ).getEntityConfig( kind, name ),\n\t\t[ kind, name ]\n\t);\n\tconst { records: data, ...ret } = useEntityRecords(\n\t\tkind,\n\t\tname,\n\t\t{\n\t\t\t...queryArgs,\n\t\t\t// If _fields is provided, we need to include _links in the request for permission caching to work.\n\t\t\t...( queryArgs._fields\n\t\t\t\t? {\n\t\t\t\t\t\t_fields: [\n\t\t\t\t\t\t\t...new Set( [\n\t\t\t\t\t\t\t\t...( getNormalizedCommaSeparable(\n\t\t\t\t\t\t\t\t\tqueryArgs._fields\n\t\t\t\t\t\t\t\t) || [] ),\n\t\t\t\t\t\t\t\t'_links',\n\t\t\t\t\t\t\t] ),\n\t\t\t\t\t\t].join(),\n\t\t\t\t }\n\t\t\t\t: {} ),\n\t\t},\n\t\toptions\n\t);\n\tconst ids = useMemo(\n\t\t() =>\n\t\t\tdata?.map(\n\t\t\t\t// @ts-ignore\n\t\t\t\t( record: RecordType ) => record[ entityConfig?.key ?? 'id' ]\n\t\t\t) ?? [],\n\t\t[ data, entityConfig?.key ]\n\t);\n\n\tconst permissions = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecordsPermissions } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\t\t\treturn getEntityRecordsPermissions( kind, name, ids );\n\t\t},\n\t\t[ ids, kind, name ]\n\t);\n\n\tconst dataWithPermissions = useMemo(\n\t\t() =>\n\t\t\tdata?.map( ( record, index ) => ( {\n\t\t\t\t// @ts-ignore\n\t\t\t\t...record,\n\t\t\t\tpermissions: permissions[ index ],\n\t\t\t} ) ) ?? [],\n\t\t[ data, permissions ]\n\t);\n\n\treturn { records: dataWithPermissions, ...ret };\n}\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,oBAAoB;AAC7B,OAAO,gBAAgB;AACvB,SAAS,iBAAiB;AAC1B,SAAS,eAAe;AAKxB,OAAO,oBAAoB;AAC3B,SAAS,SAAS,iBAAiB;AAGnC,SAAS,cAAc;AACvB,SAAS,mCAAmC;AAwC5C,MAAM,cAAc,CAAC;AA0CN,SAAR,iBACN,MACA,MACA,YAAuC,CAAC,GACxC,UAAmB,EAAE,SAAS,KAAK,GACK;AAKxC,QAAM,gBAAgB,aAAc,IAAI,SAAU;AAElD,QAAM,EAAE,MAAM,SAAS,GAAG,KAAK,IAAI;AAAA,IAClC,CAAE,UAAW;AACZ,UAAK,CAAE,QAAQ,SAAU;AACxB,eAAO;AAAA;AAAA,UAEN,MAAM;AAAA,QACP;AAAA,MACD;AACA,aAAO,MAAO,SAAU,EAAE,iBAAkB,MAAM,MAAM,SAAU;AAAA,IACnE;AAAA,IACA,CAAE,MAAM,MAAM,eAAe,QAAQ,OAAQ;AAAA,EAC9C;AAEA,QAAM,EAAE,YAAY,WAAW,IAAI;AAAA,IAClC,CAAE,WAAY;AACb,UAAK,CAAE,QAAQ,SAAU;AACxB,eAAO;AAAA,UACN,YAAY;AAAA,UACZ,YAAY;AAAA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QAAM,cAAc;AAAA,IACnB,CAAE,WAAY;AACb,YAAM,EAAE,4BAA4B,IAAI;AAAA,QACvC,OAAQ,SAAU;AAAA,MACnB;AACA,aAAO,4BAA6B,MAAM,MAAM,GAAI;AAAA,IACrD;AAAA,IACA,CAAE,KAAK,MAAM,IAAK;AAAA,EACnB;AAEA,QAAM,sBAAsB;AAAA,IAC3B,MACC,MAAM,IAAK,CAAE,QAAQ,WAAa;AAAA;AAAA,MAEjC,GAAG;AAAA,MACH,aAAa,YAAa,KAAM;AAAA,IACjC,EAAI,KAAK,CAAC;AAAA,IACX,CAAE,MAAM,WAAY;AAAA,EACrB;AAEA,SAAO,EAAE,SAAS,qBAAqB,GAAG,IAAI;AAC/C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,71 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Like useSelect, but the selectors return objects containing
|
|
14
|
-
* both the original data AND the resolution info.
|
|
15
|
-
*
|
|
16
|
-
* @since 6.1.0 Introduced in WordPress core.
|
|
17
|
-
* @private
|
|
18
|
-
*
|
|
19
|
-
* @param {Function} mapQuerySelect see useSelect
|
|
20
|
-
* @param {Array} deps see useSelect
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```js
|
|
24
|
-
* import { useQuerySelect } from '@wordpress/data';
|
|
25
|
-
* import { store as coreDataStore } from '@wordpress/core-data';
|
|
26
|
-
*
|
|
27
|
-
* function PageTitleDisplay( { id } ) {
|
|
28
|
-
* const { data: page, isResolving } = useQuerySelect( ( query ) => {
|
|
29
|
-
* return query( coreDataStore ).getEntityRecord( 'postType', 'page', id )
|
|
30
|
-
* }, [ id ] );
|
|
31
|
-
*
|
|
32
|
-
* if ( isResolving ) {
|
|
33
|
-
* return 'Loading...';
|
|
34
|
-
* }
|
|
35
|
-
*
|
|
36
|
-
* return page.title;
|
|
37
|
-
* }
|
|
38
|
-
*
|
|
39
|
-
* // Rendered in the application:
|
|
40
|
-
* // <PageTitleDisplay id={ 10 } />
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* In the above example, when `PageTitleDisplay` is rendered into an
|
|
44
|
-
* application, the page and the resolution details will be retrieved from
|
|
45
|
-
* the store state using the `mapSelect` callback on `useQuerySelect`.
|
|
46
|
-
*
|
|
47
|
-
* If the id prop changes then any page in the state for that id is
|
|
48
|
-
* retrieved. If the id prop doesn't change and other props are passed in
|
|
49
|
-
* that do change, the title will not change because the dependency is just
|
|
50
|
-
* the id.
|
|
51
|
-
* @see useSelect
|
|
52
|
-
*
|
|
53
|
-
* @return {QuerySelectResponse} Queried data.
|
|
54
|
-
*/
|
|
55
|
-
export default function useQuerySelect(mapQuerySelect, deps) {
|
|
1
|
+
import { useSelect } from "@wordpress/data";
|
|
2
|
+
import memoize from "./memoize";
|
|
3
|
+
import { Status } from "./constants";
|
|
4
|
+
const META_SELECTORS = [
|
|
5
|
+
"getIsResolving",
|
|
6
|
+
"hasStartedResolution",
|
|
7
|
+
"hasFinishedResolution",
|
|
8
|
+
"isResolving",
|
|
9
|
+
"getCachedResolvers"
|
|
10
|
+
];
|
|
11
|
+
function useQuerySelect(mapQuerySelect, deps) {
|
|
56
12
|
return useSelect((select, registry) => {
|
|
57
|
-
const resolve = store => enrichSelectors(select(store));
|
|
13
|
+
const resolve = (store) => enrichSelectors(select(store));
|
|
58
14
|
return mapQuerySelect(resolve, registry);
|
|
59
15
|
}, deps);
|
|
60
16
|
}
|
|
61
|
-
|
|
62
|
-
* Transform simple selectors into ones that return an object with the
|
|
63
|
-
* original return value AND the resolution info.
|
|
64
|
-
*
|
|
65
|
-
* @param {Object} selectors Selectors to enrich
|
|
66
|
-
* @return {EnrichedSelectors} Enriched selectors
|
|
67
|
-
*/
|
|
68
|
-
const enrichSelectors = memoize(selectors => {
|
|
17
|
+
const enrichSelectors = memoize(((selectors) => {
|
|
69
18
|
const resolvers = {};
|
|
70
19
|
for (const selectorName in selectors) {
|
|
71
20
|
if (META_SELECTORS.includes(selectorName)) {
|
|
@@ -74,19 +23,22 @@ const enrichSelectors = memoize(selectors => {
|
|
|
74
23
|
Object.defineProperty(resolvers, selectorName, {
|
|
75
24
|
get: () => (...args) => {
|
|
76
25
|
const data = selectors[selectorName](...args);
|
|
77
|
-
const resolutionStatus = selectors.getResolutionState(
|
|
26
|
+
const resolutionStatus = selectors.getResolutionState(
|
|
27
|
+
selectorName,
|
|
28
|
+
args
|
|
29
|
+
)?.status;
|
|
78
30
|
let status;
|
|
79
31
|
switch (resolutionStatus) {
|
|
80
|
-
case
|
|
32
|
+
case "resolving":
|
|
81
33
|
status = Status.Resolving;
|
|
82
34
|
break;
|
|
83
|
-
case
|
|
35
|
+
case "finished":
|
|
84
36
|
status = Status.Success;
|
|
85
37
|
break;
|
|
86
|
-
case
|
|
38
|
+
case "error":
|
|
87
39
|
status = Status.Error;
|
|
88
40
|
break;
|
|
89
|
-
case
|
|
41
|
+
case void 0:
|
|
90
42
|
status = Status.Idle;
|
|
91
43
|
break;
|
|
92
44
|
}
|
|
@@ -101,5 +53,9 @@ const enrichSelectors = memoize(selectors => {
|
|
|
101
53
|
});
|
|
102
54
|
}
|
|
103
55
|
return resolvers;
|
|
104
|
-
});
|
|
105
|
-
|
|
56
|
+
}));
|
|
57
|
+
export {
|
|
58
|
+
META_SELECTORS,
|
|
59
|
+
useQuerySelect as default
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=use-query-select.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-query-select.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport memoize from './memoize';\nimport { Status } from './constants';\n\nexport const META_SELECTORS = [\n\t'getIsResolving',\n\t'hasStartedResolution',\n\t'hasFinishedResolution',\n\t'isResolving',\n\t'getCachedResolvers',\n];\n\ninterface QuerySelectResponse< Data > {\n\t/** the requested selector return value */\n\tdata: Data;\n\n\t/** is the record still being resolved? Via the `isResolving` meta-selector */\n\tisResolving: boolean;\n\n\t/** was the resolution started? Via the `hasStartedResolution` meta-selector */\n\thasStarted: boolean;\n\n\t/** has the resolution finished? Via the `hasFinishedResolution` meta-selector. */\n\thasResolved: boolean;\n}\n\n/**\n * Like useSelect, but the selectors return objects containing\n * both the original data AND the resolution info.\n *\n * @since 6.1.0 Introduced in WordPress core.\n * @private\n *\n * @param {Function} mapQuerySelect see useSelect\n * @param {Array} deps see useSelect\n *\n * @example\n * ```js\n * import { useQuerySelect } from '@wordpress/data';\n * import { store as coreDataStore } from '@wordpress/core-data';\n *\n * function PageTitleDisplay( { id } ) {\n * const { data: page, isResolving } = useQuerySelect( ( query ) => {\n * return query( coreDataStore ).getEntityRecord( 'postType', 'page', id )\n * }, [ id ] );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return page.title;\n * }\n *\n * // Rendered in the application:\n * // <PageTitleDisplay id={ 10 } />\n * ```\n *\n * In the above example, when `PageTitleDisplay` is rendered into an\n * application, the page and the resolution details will be retrieved from\n * the store state using the `mapSelect` callback on `useQuerySelect`.\n *\n * If the id prop changes then any page in the state for that id is\n * retrieved. If the id prop doesn't change and other props are passed in\n * that do change, the title will not change because the dependency is just\n * the id.\n * @see useSelect\n *\n * @return {QuerySelectResponse} Queried data.\n */\nexport default function useQuerySelect( mapQuerySelect, deps ) {\n\treturn useSelect( ( select, registry ) => {\n\t\tconst resolve = ( store ) => enrichSelectors( select( store ) );\n\t\treturn mapQuerySelect( resolve, registry );\n\t}, deps );\n}\n\ninterface EnrichedSelectors {\n\t< Selectors extends Record< string, ( ...args: any[] ) => any > >(\n\t\tselectors: Selectors\n\t): {\n\t\t[ Selector in keyof Selectors ]: (\n\t\t\t...args: Parameters< Selectors[ Selector ] >\n\t\t) => QuerySelectResponse< ReturnType< Selectors[ Selector ] > >;\n\t};\n}\n\n/**\n * Transform simple selectors into ones that return an object with the\n * original return value AND the resolution info.\n *\n * @param {Object} selectors Selectors to enrich\n * @return {EnrichedSelectors} Enriched selectors\n */\nconst enrichSelectors = memoize( ( ( selectors ) => {\n\tconst resolvers = {};\n\tfor ( const selectorName in selectors ) {\n\t\tif ( META_SELECTORS.includes( selectorName ) ) {\n\t\t\tcontinue;\n\t\t}\n\t\tObject.defineProperty( resolvers, selectorName, {\n\t\t\tget:\n\t\t\t\t() =>\n\t\t\t\t( ...args: unknown[] ) => {\n\t\t\t\t\tconst data = selectors[ selectorName ]( ...args );\n\t\t\t\t\tconst resolutionStatus = selectors.getResolutionState(\n\t\t\t\t\t\tselectorName,\n\t\t\t\t\t\targs\n\t\t\t\t\t)?.status;\n\n\t\t\t\t\tlet status;\n\t\t\t\t\tswitch ( resolutionStatus ) {\n\t\t\t\t\t\tcase 'resolving':\n\t\t\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 'finished':\n\t\t\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 'error':\n\t\t\t\t\t\t\tstatus = Status.Error;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase undefined:\n\t\t\t\t\t\t\tstatus = Status.Idle;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata,\n\t\t\t\t\t\tstatus,\n\t\t\t\t\t\tisResolving: status === Status.Resolving,\n\t\t\t\t\t\thasStarted: status !== Status.Idle,\n\t\t\t\t\t\thasResolved:\n\t\t\t\t\t\t\tstatus === Status.Success ||\n\t\t\t\t\t\t\tstatus === Status.Error,\n\t\t\t\t\t};\n\t\t\t\t},\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