Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Uniform behavior when creating pages from create new page button #8297

Merged
merged 8 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions apps/app/src/client/services/use-on-template-button-clicked.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useCallback, useState } from 'react';

import { isCreatablePage } from '@growi/core/dist/utils/page-path-utils';
import { useRouter } from 'next/router';

import { createPage, exist } from '~/client/services/page-operation';
import { LabelType } from '~/interfaces/template';

export const useOnTemplateButtonClicked = (
currentPagePath?: string,
isLoading?: boolean,
): {
onClickHandler: (label: LabelType) => Promise<void>,
isPageCreating: boolean
Expand All @@ -15,23 +17,27 @@ export const useOnTemplateButtonClicked = (
const [isPageCreating, setIsPageCreating] = useState(false);

const onClickHandler = useCallback(async(label: LabelType) => {
if (isLoading) return;

try {
setIsPageCreating(true);

const path = currentPagePath == null || currentPagePath === '/'
const targetPath = currentPagePath == null || currentPagePath === '/'
? `/${label}`
: `${currentPagePath}/${label}`;

const params = {
isSlackEnabled: false,
slackChannels: '',
grant: 4,
// grant: currentPage?.grant || 1,
// grantUserGroupId: currentPage?.grantedGroup?._id,
};
const path = isCreatablePage(targetPath) ? targetPath : `/${label}`;

const res = await exist(JSON.stringify([path]));
if (!res.pages[path]) {
const params = {
isSlackEnabled: false,
slackChannels: '',
grant: 4,
// grant: currentPage?.grant || 1,
// grantUserGroupId: currentPage?.grantedGroup?._id,
};

await createPage(path, '', params);
}

Expand All @@ -43,7 +49,7 @@ export const useOnTemplateButtonClicked = (
finally {
setIsPageCreating(false);
}
}, [currentPagePath, router]);
}, [currentPagePath, isLoading, router]);

return { onClickHandler, isPageCreating };
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useOnTemplateButtonClicked } from '~/client/services/use-on-template-bu
import { toastError } from '~/client/util/toastr';
import { LabelType } from '~/interfaces/template';
import { useCurrentUser } from '~/stores/context';
import { useSWRxCurrentPage } from '~/stores/page';
import { useCurrentPagePath } from '~/stores/page';

import { CreateButton } from './CreateButton';
import { DropendMenu } from './DropendMenu';
Expand All @@ -18,7 +18,7 @@ import { useOnNewButtonClicked, useOnTodaysButtonClicked } from './hooks';
export const PageCreateButton = React.memo((): JSX.Element => {
const { t } = useTranslation('commons');

const { data: currentPage, isLoading } = useSWRxCurrentPage();
const { data: currentPagePath, isLoading } = useCurrentPagePath();
const { data: currentUser } = useCurrentUser();

const [isHovered, setIsHovered] = useState(false);
Expand All @@ -27,9 +27,9 @@ export const PageCreateButton = React.memo((): JSX.Element => {
const userHomepagePath = pagePathUtils.userHomepagePath(currentUser);
const todaysPath = `${userHomepagePath}/${t('create_page_dropdown.todays.memo')}/${now}`;

const { onClickHandler: onClickNewButton, isPageCreating: isNewPageCreating } = useOnNewButtonClicked(isLoading, currentPage);
const { onClickHandler: onClickNewButton, isPageCreating: isNewPageCreating } = useOnNewButtonClicked(currentPagePath, isLoading);
const { onClickHandler: onClickTodaysButton, isPageCreating: isTodaysPageCreating } = useOnTodaysButtonClicked(todaysPath, currentUser);
const { onClickHandler: onClickTemplateButton, isPageCreating: isTemplatePageCreating } = useOnTemplateButtonClicked(currentPage?.path);
const { onClickHandler: onClickTemplateButton, isPageCreating: isTemplatePageCreating } = useOnTemplateButtonClicked(currentPagePath, isLoading);

const onClickTemplateButtonHandler = useCallback(async(label: LabelType) => {
try {
Expand Down
14 changes: 7 additions & 7 deletions apps/app/src/components/Sidebar/PageCreateButton/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useCallback, useState } from 'react';

import type { Nullable, IPagePopulatedToShowRevision, IUserHasId } from '@growi/core';
import type { Nullable, IUserHasId } from '@growi/core';
import { useRouter } from 'next/router';

import { createPage, exist } from '~/client/services/page-operation';
import { toastError } from '~/client/util/toastr';

export const useOnNewButtonClicked = (
isLoading: boolean,
currentPage?: IPagePopulatedToShowRevision | null,
currentPagePath?: string,
isLoading?: boolean,
): {
onClickHandler: () => Promise<void>,
isPageCreating: boolean
Expand All @@ -22,9 +22,9 @@ export const useOnNewButtonClicked = (
try {
setIsPageCreating(true);

const parentPath = currentPage == null
const parentPath = currentPagePath == null
? '/'
: currentPage.path;
: currentPagePath;

const params = {
isSlackEnabled: false,
Expand All @@ -37,15 +37,15 @@ export const useOnNewButtonClicked = (

const response = await createPage(parentPath, '', params);

router.push(`${response.page.id}#edit`);
router.push(`/${response.page.id}#edit`);
}
catch (err) {
toastError(err);
}
finally {
setIsPageCreating(false);
}
}, [currentPage, isLoading, router]);
}, [currentPagePath, isLoading, router]);

return { onClickHandler, isPageCreating };
};
Expand Down
5 changes: 5 additions & 0 deletions apps/app/src/pages/_private-legacy-pages.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useCsrfToken, useCurrentUser, useIsSearchPage, useIsSearchScopeChildrenAsDefault,
useIsSearchServiceConfigured, useIsSearchServiceReachable, useRendererConfig, useGrowiCloudUri, useIsEnabledMarp,
} from '~/stores/context';
import { useSWRxCurrentPage } from '~/stores/page';

import type { CommonProps } from './utils/commons';
import {
Expand Down Expand Up @@ -46,6 +47,10 @@ const PrivateLegacyPage: NextPage<Props> = (props: Props) => {

useCurrentUser(props.currentUser ?? null);

// clear the cache for the current page
const { mutate } = useSWRxCurrentPage();
mutate(undefined, { revalidate: false });

// Search
useIsSearchPage(true);
useIsSearchServiceConfigured(props.isSearchServiceConfigured);
Expand Down
5 changes: 5 additions & 0 deletions apps/app/src/pages/_search.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useCsrfToken, useCurrentUser, useIsContainerFluid, useIsSearchPage, useIsSearchScopeChildrenAsDefault,
useIsSearchServiceConfigured, useIsSearchServiceReachable, useRendererConfig, useShowPageLimitationL, useGrowiCloudUri,
} from '~/stores/context';
import { useSWRxCurrentPage } from '~/stores/page';

import { SearchPage } from '../components/SearchPage';

Expand Down Expand Up @@ -50,6 +51,10 @@ const SearchResultPage: NextPageWithLayout<Props> = (props: Props) => {

useCurrentUser(props.currentUser ?? null);

// clear the cache for the current page
const { mutate } = useSWRxCurrentPage();
mutate(undefined, { revalidate: false });

// Search
useIsSearchPage(true);
useIsSearchServiceConfigured(props.isSearchServiceConfigured);
Expand Down
9 changes: 7 additions & 2 deletions apps/app/src/pages/me/[[...path]].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
useCsrfToken, useIsSearchScopeChildrenAsDefault,
useRegistrationWhitelist, useShowPageLimitationXL, useRendererConfig, useIsEnabledMarp,
} from '~/stores/context';
import { useSWRxCurrentPage } from '~/stores/page';
import loggerFactory from '~/utils/logger';

import { NextPageWithLayout } from '../_app.page';
Expand Down Expand Up @@ -87,8 +88,6 @@ const MePage: NextPageWithLayout<Props> = (props: Props) => {

useIsSearchPage(false);

useCurrentUser(props.currentUser ?? null);

useRegistrationWhitelist(props.registrationWhitelist);

useShowPageLimitationXL(props.showPageLimitationXL);
Expand All @@ -97,6 +96,12 @@ const MePage: NextPageWithLayout<Props> = (props: Props) => {
useCsrfToken(props.csrfToken);
useGrowiCloudUri(props.growiCloudUri);

useCurrentUser(props.currentUser ?? null);

// clear the cache for the current page
const { mutate } = useSWRxCurrentPage();
mutate(undefined, { revalidate: false });

// init sidebar config with UserUISettings and sidebarConfig
useInitSidebarConfig(props.sidebarConfig, props.userUISettings);

Expand Down
6 changes: 6 additions & 0 deletions apps/app/src/pages/tags.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Head from 'next/head';
import type { CrowiRequest } from '~/interfaces/crowi-request';
import type { RendererConfig } from '~/interfaces/services/renderer';
import type { IDataTagCount } from '~/interfaces/tag';
import { useSWRxCurrentPage } from '~/stores/page';
import { useSWRxTagsList } from '~/stores/tag';

import { BasicLayout } from '../components/Layout/BasicLayout';
Expand Down Expand Up @@ -44,6 +45,11 @@ const TagPage: NextPageWithLayout<CommonProps> = (props: Props) => {
const [offset, setOffset] = useState<number>(0);

useCurrentUser(props.currentUser ?? null);

// clear the cache for the current page
const { mutate } = useSWRxCurrentPage();
mutate(undefined, { revalidate: false });

const { data: tagDataList, error } = useSWRxTagsList(PAGING_LIMIT, offset);
const { t } = useTranslation('');
const setOffsetByPageNumber = useCallback((selectedPageNumber: number) => {
Expand Down
6 changes: 5 additions & 1 deletion apps/app/src/pages/trash.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Head from 'next/head';
import { PagePathNavSticky } from '~/components/Common/PagePathNav';
import type { CrowiRequest } from '~/interfaces/crowi-request';
import type { RendererConfig } from '~/interfaces/services/renderer';
import { useCurrentPageId } from '~/stores/page';
import { useCurrentPageId, useSWRxCurrentPage } from '~/stores/page';

import { BasicLayout } from '../components/Layout/BasicLayout';
import {
Expand Down Expand Up @@ -41,6 +41,10 @@ type Props = CommonProps & {
const TrashPage: NextPageWithLayout<CommonProps> = (props: Props) => {
useCurrentUser(props.currentUser ?? null);

// clear the cache for the current page
const { mutate } = useSWRxCurrentPage();
mutate(undefined, { revalidate: false });

useGrowiCloudUri(props.growiCloudUri);

useIsSearchServiceConfigured(props.isSearchServiceConfigured);
Expand Down
Loading