|
|
@@ -1,12 +1,13 @@
|
|
|
import React, { useState, useCallback } from 'react';
|
|
|
|
|
|
+import type { Nullable, IUserHasId } from '@growi/core';
|
|
|
import { pagePathUtils } from '@growi/core/dist/utils';
|
|
|
import { format } from 'date-fns';
|
|
|
|
|
|
import { useOnTemplateButtonClicked } from '~/client/services/use-on-template-button-clicked';
|
|
|
import { toastError } from '~/client/util/toastr';
|
|
|
import { LabelType } from '~/interfaces/template';
|
|
|
-import { useCurrentUser } from '~/stores/context';
|
|
|
+import { useCurrentUser, useIsGuestUser } from '~/stores/context';
|
|
|
import { useSWRxCurrentPage } from '~/stores/page';
|
|
|
|
|
|
import { CreateButton } from './CreateButton';
|
|
|
@@ -14,18 +15,27 @@ import { DropendMenu } from './DropendMenu';
|
|
|
import { DropendToggle } from './DropendToggle';
|
|
|
import { useOnNewButtonClicked, useOnTodaysButtonClicked } from './hooks';
|
|
|
|
|
|
+const generateTodaysPath = (currentUser?: Nullable<IUserHasId>, isGuestUser?: boolean) => {
|
|
|
+ if (isGuestUser) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ const now = format(new Date(), 'yyyy/MM/dd');
|
|
|
+ const userHomepagePath = pagePathUtils.userHomepagePath(currentUser);
|
|
|
+ return `${userHomepagePath}/memo/${now}`;
|
|
|
+};
|
|
|
+
|
|
|
export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
const { data: currentPage, isLoading } = useSWRxCurrentPage();
|
|
|
const { data: currentUser } = useCurrentUser();
|
|
|
+ const { data: isGuestUser } = useIsGuestUser();
|
|
|
|
|
|
const [isHovered, setIsHovered] = useState(false);
|
|
|
|
|
|
- const now = format(new Date(), 'yyyy/MM/dd');
|
|
|
- const userHomepagePath = pagePathUtils.userHomepagePath(currentUser);
|
|
|
- const todaysPath = `${userHomepagePath}/memo/${now}`;
|
|
|
+ const todaysPath = generateTodaysPath(currentUser, isGuestUser);
|
|
|
|
|
|
const { onClickHandler: onClickNewButton, isPageCreating: isNewPageCreating } = useOnNewButtonClicked(isLoading, currentPage);
|
|
|
- const { onClickHandler: onClickTodaysButton, isPageCreating: isTodaysPageCreating } = useOnTodaysButtonClicked(todaysPath, currentUser);
|
|
|
+ const { onClickHandler: onClickTodaysButton, isPageCreating: isTodaysPageCreating } = useOnTodaysButtonClicked(todaysPath);
|
|
|
const { onClickHandler: onClickTemplateButton, isPageCreating: isTemplatePageCreating } = useOnTemplateButtonClicked(currentPage?.path);
|
|
|
|
|
|
const onClickTemplateButtonHandler = useCallback(async(label: LabelType) => {
|
|
|
@@ -66,10 +76,10 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
aria-expanded="false"
|
|
|
/>
|
|
|
<DropendMenu
|
|
|
- todaysPath={todaysPath}
|
|
|
onClickCreateNewPageButtonHandler={onClickNewButton}
|
|
|
onClickCreateTodaysButtonHandler={onClickTodaysButton}
|
|
|
onClickTemplateButtonHandler={onClickTemplateButtonHandler}
|
|
|
+ todaysPath={todaysPath}
|
|
|
/>
|
|
|
</div>
|
|
|
)}
|