|
|
@@ -7,10 +7,8 @@ import type {
|
|
|
IPageInfo, IPageInfoForOperation,
|
|
|
IRevision, IRevisionHasId,
|
|
|
} from '@growi/core';
|
|
|
-import { useSWRStatic } from '@growi/core/dist/swr';
|
|
|
-import { isClient, pagePathUtils } from '@growi/core/dist/utils';
|
|
|
import useSWR, {
|
|
|
- mutate, useSWRConfig, type SWRResponse, type SWRConfiguration,
|
|
|
+ mutate, type SWRResponse, type SWRConfiguration,
|
|
|
} from 'swr';
|
|
|
import useSWRImmutable from 'swr/immutable';
|
|
|
import useSWRInfinite, { type SWRInfiniteResponse } from 'swr/infinite';
|
|
|
@@ -20,98 +18,15 @@ import { apiGet } from '~/client/util/apiv1-client';
|
|
|
import { apiv3Get } from '~/client/util/apiv3-client';
|
|
|
import type { IPagePathWithDescendantCount } from '~/interfaces/page';
|
|
|
import type { IRecordApplicableGrant, IResCurrentGrantData } from '~/interfaces/page-grant';
|
|
|
+import { usePageNotFound } from '~/states/page';
|
|
|
import {
|
|
|
- useCurrentPathname, useShareLinkId, useIsGuestUser, useIsReadOnlyUser,
|
|
|
+ useShareLinkId, useIsGuestUser, useIsReadOnlyUser,
|
|
|
} from '~/stores-universal/context';
|
|
|
import type { AxiosResponse } from '~/utils/axios';
|
|
|
|
|
|
import type { IPageTagsInfo } from '../interfaces/tag';
|
|
|
|
|
|
|
|
|
-import { useRemoteRevisionId } from './remote-latest-page';
|
|
|
-
|
|
|
-
|
|
|
-const { isPermalink: _isPermalink } = pagePathUtils;
|
|
|
-
|
|
|
-
|
|
|
-export const useCurrentPageId = (initialData?: Nullable<string>): SWRResponse<Nullable<string>, Error> => {
|
|
|
- return useSWRStatic<Nullable<string>, Error>('currentPageId', initialData);
|
|
|
-};
|
|
|
-
|
|
|
-export const useIsLatestRevision = (initialData?: boolean): SWRResponse<boolean, any> => {
|
|
|
- return useSWRStatic('isLatestRevision', initialData);
|
|
|
-};
|
|
|
-
|
|
|
-export const useIsNotFound = (initialData?: boolean): SWRResponse<boolean, Error> => {
|
|
|
- return useSWRStatic<boolean, Error>('isNotFound', initialData, { fallbackData: false });
|
|
|
-};
|
|
|
-
|
|
|
-export const useTemplateTagData = (initialData?: string[]): SWRResponse<string[], Error> => {
|
|
|
- return useSWRStatic<string[], Error>('templateTagData', initialData);
|
|
|
-};
|
|
|
-
|
|
|
-export const useTemplateBodyData = (initialData?: string): SWRResponse<string, Error> => {
|
|
|
- return useSWRStatic<string, Error>('templateBodyData', initialData);
|
|
|
-};
|
|
|
-
|
|
|
-/** "useSWRxCurrentPage" is intended for initial data retrieval only. Use "useSWRMUTxCurrentPage" for revalidation */
|
|
|
-export const useSWRxCurrentPage = (initialData?: IPagePopulatedToShowRevision|null): SWRResponse<IPagePopulatedToShowRevision|null> => {
|
|
|
- const key = 'currentPage';
|
|
|
-
|
|
|
- const { data: isLatestRevision } = useIsLatestRevision();
|
|
|
-
|
|
|
- const { cache } = useSWRConfig();
|
|
|
-
|
|
|
- // Problem 1: https://github.com/weseek/growi/pull/7772/files#diff-4c1708c4f959974166c15435c6b35950ba01bbf35e7e4b8e99efeb125a8000a7
|
|
|
- // Problem 2: https://redmine.weseek.co.jp/issues/141027
|
|
|
- // Problem 3: https://redmine.weseek.co.jp/issues/153618
|
|
|
- // Problem 4: https://redmine.weseek.co.jp/issues/153759
|
|
|
- const shouldMutate = (() => {
|
|
|
- if (initialData === undefined) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // reset when null
|
|
|
- if (initialData == null) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- const cachedData = cache.get(key)?.data as IPagePopulatedToShowRevision|null;
|
|
|
- if (initialData._id !== cachedData?._id) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- // mutate when the empty page has updated
|
|
|
- if (cachedData?.revision == null && initialData.revision != null) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- // mutate when opening a previous revision.
|
|
|
- if (!isLatestRevision
|
|
|
- && cachedData.revision?._id != null && initialData.revision?._id != null
|
|
|
- && cachedData.revision._id !== initialData.revision._id
|
|
|
- ) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- })();
|
|
|
-
|
|
|
- useEffect(() => {
|
|
|
- if (shouldMutate) {
|
|
|
- mutate(key, initialData, {
|
|
|
- optimisticData: initialData,
|
|
|
- populateCache: true,
|
|
|
- revalidate: false,
|
|
|
- });
|
|
|
- }
|
|
|
- }, [initialData, key, shouldMutate]);
|
|
|
-
|
|
|
- return useSWR(key, null, {
|
|
|
- keepPreviousData: true,
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
const getPageApiErrorHandler = (errs: AxiosResponse[]) => {
|
|
|
if (!Array.isArray(errs)) { throw Error('error is not array') }
|
|
|
|
|
|
@@ -123,39 +38,6 @@ const getPageApiErrorHandler = (errs: AxiosResponse[]) => {
|
|
|
throw Error('failed to get page');
|
|
|
};
|
|
|
|
|
|
-export const useSWRMUTxCurrentPage = (): SWRMutationResponse<IPagePopulatedToShowRevision|null> => {
|
|
|
- const key = 'currentPage';
|
|
|
-
|
|
|
- const { data: currentPageId } = useCurrentPageId();
|
|
|
- const { data: shareLinkId } = useShareLinkId();
|
|
|
-
|
|
|
- // Get URL parameter for specific revisionId
|
|
|
- let revisionId: string|undefined;
|
|
|
- if (isClient()) {
|
|
|
- const urlParams = new URLSearchParams(window.location.search);
|
|
|
- const requestRevisionId = urlParams.get('revisionId');
|
|
|
- revisionId = requestRevisionId != null ? requestRevisionId : undefined;
|
|
|
- }
|
|
|
-
|
|
|
- return useSWRMutation(
|
|
|
- key,
|
|
|
- () => apiv3Get<{ page: IPagePopulatedToShowRevision }>('/page', { pageId: currentPageId, shareLinkId, revisionId })
|
|
|
- .then((result) => {
|
|
|
- const newData = result.data.page;
|
|
|
-
|
|
|
- // for the issue https://redmine.weseek.co.jp/issues/156150
|
|
|
- mutate('currentPage', newData, false);
|
|
|
-
|
|
|
- return newData;
|
|
|
- })
|
|
|
- .catch(getPageApiErrorHandler),
|
|
|
- {
|
|
|
- populateCache: true,
|
|
|
- revalidate: false,
|
|
|
- },
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
export const useSWRxPageByPath = (path?: string, config?: SWRConfiguration): SWRResponse<IPagePopulatedToShowRevision|null, Error> => {
|
|
|
return useSWR(
|
|
|
path != null ? ['/page', path] : null,
|
|
|
@@ -294,7 +176,7 @@ export const useSWRxCurrentGrantData = (
|
|
|
|
|
|
const { data: isGuestUser } = useIsGuestUser();
|
|
|
const { data: isReadOnlyUser } = useIsReadOnlyUser();
|
|
|
- const { data: isNotFound } = useIsNotFound();
|
|
|
+ const [isNotFound] = usePageNotFound();
|
|
|
|
|
|
const key = !isGuestUser && !isReadOnlyUser && !isNotFound && pageId != null
|
|
|
? ['/page/grant-data', pageId]
|
|
|
@@ -322,59 +204,6 @@ export const useSWRxApplicableGrant = (
|
|
|
* @deprecated Use enhanced versions from ~/states/page instead
|
|
|
*********************************************************** */
|
|
|
|
|
|
-/**
|
|
|
- * @deprecated Use useCurrentPagePathEnhanced from ~/states/page instead
|
|
|
- */
|
|
|
-export const useCurrentPagePath = (): SWRResponse<string | undefined, Error> => {
|
|
|
- const { data: currentPage } = useSWRxCurrentPage();
|
|
|
- const { data: currentPathname } = useCurrentPathname();
|
|
|
-
|
|
|
- return useSWRImmutable(
|
|
|
- ['currentPagePath', currentPage?.path, currentPathname],
|
|
|
- ([, , pathname]) => {
|
|
|
- if (currentPage?.path != null) {
|
|
|
- return currentPage.path;
|
|
|
- }
|
|
|
- if (pathname != null && !_isPermalink(pathname)) {
|
|
|
- return pathname;
|
|
|
- }
|
|
|
- return undefined;
|
|
|
- },
|
|
|
- // TODO: set fallbackData
|
|
|
- // { fallbackData: }
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * @deprecated Use useIsTrashPageEnhanced from ~/states/page instead
|
|
|
- */
|
|
|
-export const useIsTrashPage = (): SWRResponse<boolean, Error> => {
|
|
|
- const { data: pagePath } = useCurrentPagePath();
|
|
|
-
|
|
|
- return useSWRImmutable(
|
|
|
- pagePath == null ? null : ['isTrashPage', pagePath],
|
|
|
- ([, pagePath]) => pagePathUtils.isTrashPage(pagePath),
|
|
|
- // TODO: set fallbackData
|
|
|
- // { fallbackData: }
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * @deprecated Use useIsRevisionOutdatedEnhanced from ~/states/page instead
|
|
|
- */
|
|
|
-export const useIsRevisionOutdated = (): SWRResponse<boolean, Error> => {
|
|
|
- const { data: currentPage } = useSWRxCurrentPage();
|
|
|
- const { data: remoteRevisionId } = useRemoteRevisionId();
|
|
|
-
|
|
|
- const currentRevisionId = currentPage?.revision?._id;
|
|
|
-
|
|
|
- return useSWRImmutable(
|
|
|
- currentRevisionId != null && remoteRevisionId != null ? ['useIsRevisionOutdated', currentRevisionId, remoteRevisionId] : null,
|
|
|
- ([, remoteRevisionId, currentRevisionId]) => { return remoteRevisionId !== currentRevisionId },
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
export const useSWRxPagePathsWithDescendantCount = (
|
|
|
paths?: string[], userGroups?: string[], isIncludeEmpty?: boolean, includeAnyoneWithTheLink?: boolean,
|
|
|
): SWRResponse<IPagePathWithDescendantCount[], Error> => {
|