|
@@ -7,9 +7,10 @@ import type {
|
|
|
import useSWR, {
|
|
import useSWR, {
|
|
|
mutate, type SWRConfiguration, type SWRResponse, type Arguments,
|
|
mutate, type SWRConfiguration, type SWRResponse, type Arguments,
|
|
|
} from 'swr';
|
|
} from 'swr';
|
|
|
|
|
+import { cache } from 'swr/_internal';
|
|
|
import useSWRImmutable from 'swr/immutable';
|
|
import useSWRImmutable from 'swr/immutable';
|
|
|
import type { SWRInfiniteResponse } from 'swr/infinite';
|
|
import type { SWRInfiniteResponse } from 'swr/infinite';
|
|
|
-import useSWRInfinite from 'swr/infinite';
|
|
|
|
|
|
|
+import useSWRInfinite, { unstable_serialize } from 'swr/infinite'; // eslint-disable-line camelcase
|
|
|
|
|
|
|
|
import type { IPagingResult } from '~/interfaces/paging-result';
|
|
import type { IPagingResult } from '~/interfaces/paging-result';
|
|
|
|
|
|
|
@@ -33,27 +34,47 @@ type RecentApiResult = {
|
|
|
totalCount: number,
|
|
totalCount: number,
|
|
|
offset: number,
|
|
offset: number,
|
|
|
}
|
|
}
|
|
|
-export const useSWRINFxRecentlyUpdated = (limit: number, includeWipPage?: boolean, config?: SWRConfiguration) : SWRInfiniteResponse<RecentApiResult, Error> => {
|
|
|
|
|
- return useSWRInfinite(
|
|
|
|
|
- (pageIndex, previousPageData) => {
|
|
|
|
|
- if (previousPageData != null && previousPageData.pages.length === 0) return null;
|
|
|
|
|
|
|
|
|
|
- if (pageIndex === 0 || previousPageData == null) {
|
|
|
|
|
- return ['/pages/recent', undefined, limit, includeWipPage];
|
|
|
|
|
- }
|
|
|
|
|
|
|
+export const getRecentlyUpdatedKey = (
|
|
|
|
|
+ pageIndex: number,
|
|
|
|
|
+ previousPageData: RecentApiResult | null,
|
|
|
|
|
+ includeWipPage?: boolean,
|
|
|
|
|
+): [string, number | undefined, boolean | undefined] | null => {
|
|
|
|
|
+ if (previousPageData != null && previousPageData.pages.length === 0) return null;
|
|
|
|
|
|
|
|
- const offset = previousPageData.offset + limit;
|
|
|
|
|
- return ['/pages/recent', offset, limit, includeWipPage];
|
|
|
|
|
- },
|
|
|
|
|
- ([endpoint, offset, limit, includeWipPage]) => apiv3Get<RecentApiResult>(endpoint, { offset, limit, includeWipPage }).then(response => response.data),
|
|
|
|
|
|
|
+ if (pageIndex === 0 || previousPageData == null) {
|
|
|
|
|
+ return ['/pages/recent', undefined, includeWipPage];
|
|
|
|
|
+ }
|
|
|
|
|
+ const offset = previousPageData.offset + previousPageData.pages.length;
|
|
|
|
|
+ return ['/pages/recent', offset, includeWipPage];
|
|
|
|
|
+
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export const useSWRINFxRecentlyUpdated = (
|
|
|
|
|
+ includeWipPage?: boolean,
|
|
|
|
|
+ config?: SWRConfiguration,
|
|
|
|
|
+): SWRInfiniteResponse<RecentApiResult, Error> => {
|
|
|
|
|
+ const PER_PAGE = 20;
|
|
|
|
|
+ return useSWRInfinite(
|
|
|
|
|
+ (pageIndex, previousPageData) => getRecentlyUpdatedKey(pageIndex, previousPageData, includeWipPage),
|
|
|
|
|
+ ([endpoint, offset, includeWipPage]) => apiv3Get<RecentApiResult>(endpoint, { offset, limit: PER_PAGE, includeWipPage }).then(response => response.data),
|
|
|
{
|
|
{
|
|
|
...config,
|
|
...config,
|
|
|
revalidateFirstPage: false,
|
|
revalidateFirstPage: false,
|
|
|
- revalidateAll: false,
|
|
|
|
|
|
|
+ revalidateAll: true,
|
|
|
},
|
|
},
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+export const mutateRecentlyUpdated = async(): Promise<undefined> => {
|
|
|
|
|
+ [true, false].forEach(includeWipPage => mutate(
|
|
|
|
|
+ unstable_serialize(
|
|
|
|
|
+ (pageIndex, previousPageData) => getRecentlyUpdatedKey(pageIndex, previousPageData, includeWipPage),
|
|
|
|
|
+ ),
|
|
|
|
|
+ ));
|
|
|
|
|
+ return;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
export const mutatePageList = async(): Promise<void[]> => {
|
|
export const mutatePageList = async(): Promise<void[]> => {
|
|
|
return mutate(
|
|
return mutate(
|
|
|
key => Array.isArray(key) && key[0] === '/pages/list',
|
|
key => Array.isArray(key) && key[0] === '/pages/list',
|