Skip to content

Commit

Permalink
fix: use maximum available per_page limit
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Apr 19, 2024
1 parent c0e50d9 commit bdef3b7
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions src/hooks/useAttachments.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
import { useSelect } from "@wordpress/data";
import { type Attachment, store as coreStore } from "@wordpress/core-data";
import { type Attachment, useEntityRecords } from "@wordpress/core-data";

export default function useAttachments(ids: number[]) {
const { attachments, hasResolved } = useSelect(
(select) => {
if (!ids.length) {
return {
attachments: [],
hasResolved: true,
};
}

const selectorArgs = [
"postType",
"attachment",
{ include: ids, context: "view" },
] as const;

return {
attachments:
select(coreStore).getEntityRecords<Attachment<"view">>(
...selectorArgs,
) ?? [],
// @ts-ignore - missing hasFinishedResolution types
hasResolved: select(coreStore).hasFinishedResolution(
"getEntityRecords",
selectorArgs,
),
};
const { records, hasResolved } = useEntityRecords<Attachment<"view">>(
"postType",
"attachment",
{
include: ids,
per_page: -1,
},
[ids],
);

return { attachments, hasResolved };
return { attachments: records ?? [], hasResolved };
}

0 comments on commit bdef3b7

Please sign in to comment.