@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
8 lines (7 loc) • 8.64 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../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": ";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,IAAM,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,QACb;AAAA,MACD;AACA,aAAO;AAAA,QACN,YAAY,OAAQ,SAAU,EAAE;AAAA,UAC/B;AAAA,UACA;AAAA,UACA;AAAA,QACD;AAAA,QACA,YAAY,OAAQ,SAAU,EAAE;AAAA,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