import React, { type ReactNode, useCallback } from 'react'; import { Origin } from '@growi/core'; import { useTranslation } from 'next-i18next'; import { useCreatePageAndTransit } from '~/client/services/create-page'; import { toastError } from '~/client/util/toastr'; import { useIsNotFound } from '~/stores/page'; import { EditorMode, useEditorMode, useIsDeviceLargerThanMd } from '~/stores/ui'; import { shouldCreateWipPage } from '../../utils/should-create-wip-page'; import styles from './PageEditorModeManager.module.scss'; type PageEditorModeButtonProps = { currentEditorMode: EditorMode, editorMode: EditorMode, children?: ReactNode, isBtnDisabled?: boolean, onClick?: () => void, } const PageEditorModeButton = React.memo((props: PageEditorModeButtonProps) => { const { currentEditorMode, isBtnDisabled, editorMode, children, onClick, } = props; const classNames = ['btn py-1 px-2 d-flex align-items-center justify-content-center']; if (currentEditorMode === editorMode) { classNames.push('active'); } if (isBtnDisabled) { classNames.push('disabled'); } return ( ); }); type Props = { editorMode: EditorMode | undefined, isBtnDisabled: boolean, path?: string, } export const PageEditorModeManager = (props: Props): JSX.Element => { const { editorMode = EditorMode.View, isBtnDisabled, path, } = props; const { t } = useTranslation('commons'); const { data: isNotFound } = useIsNotFound(); const { mutate: mutateEditorMode } = useEditorMode(); const { data: isDeviceLargerThanMd } = useIsDeviceLargerThanMd(); const { isCreating, createAndTransit } = useCreatePageAndTransit(); const editButtonClickedHandler = useCallback(async() => { if (isNotFound == null || isNotFound === false) { mutateEditorMode(EditorMode.Editor); return; } try { await createAndTransit( { path, wip: shouldCreateWipPage(path), origin: Origin.View }, { shouldCheckPageExists: true }, ); } catch (err) { toastError(t('toaster.create_failed', { target: path })); } }, [createAndTransit, isNotFound, mutateEditorMode, path, t]); const _isBtnDisabled = isCreating || isBtnDisabled; return ( <>