|
|
@@ -4,7 +4,7 @@ import useSWRImmutable from 'swr/immutable';
|
|
|
import { apiv3Get } from '~/client/util/apiv3-client';
|
|
|
|
|
|
import {
|
|
|
- IPageInfo, IPageHasId, IPageInfoForOperation, IPageInfoForListing,
|
|
|
+ IPageInfo, IPageHasId, IPageInfoForOperation, IPageInfoForListing, IPageWithMeta, IPageWithAnyMeta,
|
|
|
} from '~/interfaces/page';
|
|
|
import { IPagingResult } from '~/interfaces/paging-result';
|
|
|
import { apiGet } from '../client/util/apiv1-client';
|
|
|
@@ -91,15 +91,41 @@ export const useSWRxPageInfo = (
|
|
|
);
|
|
|
};
|
|
|
|
|
|
+type PageInfoInjector = {
|
|
|
+ injectTo: (pages: (IPageHasId | IPageWithAnyMeta)[]) => IPageWithMeta[],
|
|
|
+}
|
|
|
+
|
|
|
+const isIPageWithMeta = (page: IPageHasId | IPageWithAnyMeta): page is IPageWithAnyMeta => {
|
|
|
+ return 'pageData' in page;
|
|
|
+};
|
|
|
+
|
|
|
export const useSWRxPageInfoForList = (
|
|
|
pageIds: string[] | null | undefined,
|
|
|
attachShortBody = false,
|
|
|
-): SWRResponse<Record<string, IPageInfo | IPageInfoForListing>, Error> => {
|
|
|
+): SWRResponse<Record<string, IPageInfo | IPageInfoForListing>, Error> & PageInfoInjector => {
|
|
|
|
|
|
const shouldFetch = pageIds != null && pageIds.length > 0;
|
|
|
|
|
|
- return useSWRImmutable(
|
|
|
+ const swrResult = useSWRImmutable<Record<string, IPageInfo | IPageInfoForListing>>(
|
|
|
shouldFetch ? ['/page-listing/info', pageIds, attachShortBody] : null,
|
|
|
(endpoint, pageIds, attachShortBody) => apiv3Get(endpoint, { pageIds, attachShortBody }).then(response => response.data),
|
|
|
);
|
|
|
+
|
|
|
+ return {
|
|
|
+ ...swrResult,
|
|
|
+ injectTo: (pages: (IPageHasId | IPageWithAnyMeta)[]) => {
|
|
|
+ return pages.map((item) => {
|
|
|
+ const page: IPageHasId = isIPageWithMeta(item) ? item.pageData : item;
|
|
|
+ const orgPageMeta = isIPageWithMeta(item) ? item.pageMeta : undefined;
|
|
|
+
|
|
|
+ // get an applicable IPageInfo
|
|
|
+ const applicablePageInfo = (swrResult.data ?? {})[page._id];
|
|
|
+
|
|
|
+ return {
|
|
|
+ pageData: page,
|
|
|
+ pageMeta: applicablePageInfo ?? orgPageMeta,
|
|
|
+ };
|
|
|
+ });
|
|
|
+ },
|
|
|
+ };
|
|
|
};
|