PageEditorModeManager.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React, { type ReactNode, useCallback, useMemo } from 'react';
  2. import { Origin } from '@growi/core';
  3. import { normalizePath } from '@growi/core/dist/utils/path-utils';
  4. import { useTranslation } from 'next-i18next';
  5. import { useCreatePage } from '~/client/services/create-page';
  6. import { toastError } from '~/client/util/toastr';
  7. import { EditorMode, useEditorMode } from '~/stores-universal/ui';
  8. import { useIsNotFound } from '~/stores/page';
  9. import { useIsDeviceLargerThanMd } from '~/stores/ui';
  10. import { useCurrentPageYjsData } from '~/stores/yjs';
  11. import { shouldCreateWipPage } from '../../../utils/should-create-wip-page';
  12. import styles from './PageEditorModeManager.module.scss';
  13. type PageEditorModeButtonProps = {
  14. currentEditorMode: EditorMode,
  15. editorMode: EditorMode,
  16. children?: ReactNode,
  17. isBtnDisabled?: boolean,
  18. onClick?: () => void,
  19. }
  20. const PageEditorModeButton = React.memo((props: PageEditorModeButtonProps) => {
  21. const {
  22. currentEditorMode, isBtnDisabled, editorMode, children, onClick,
  23. } = props;
  24. const classNames = ['btn py-1 px-2 d-flex align-items-center justify-content-center'];
  25. if (currentEditorMode === editorMode) {
  26. classNames.push('active');
  27. }
  28. if (isBtnDisabled) {
  29. classNames.push('disabled');
  30. }
  31. return (
  32. <button
  33. type="button"
  34. className={classNames.join(' ')}
  35. onClick={onClick}
  36. data-testid={`${editorMode}-button`}
  37. >
  38. {children}
  39. </button>
  40. );
  41. });
  42. type Props = {
  43. editorMode: EditorMode | undefined,
  44. isBtnDisabled: boolean,
  45. path?: string,
  46. }
  47. export const PageEditorModeManager = (props: Props): JSX.Element => {
  48. const {
  49. editorMode = EditorMode.View,
  50. isBtnDisabled,
  51. path,
  52. } = props;
  53. const { t } = useTranslation('commons');
  54. const { data: isNotFound } = useIsNotFound();
  55. const { mutate: mutateEditorMode } = useEditorMode();
  56. const { data: isDeviceLargerThanMd } = useIsDeviceLargerThanMd();
  57. const { data: currentPageYjsData } = useCurrentPageYjsData();
  58. const { isCreating, create } = useCreatePage();
  59. const editButtonClickedHandler = useCallback(async() => {
  60. if (isNotFound == null || isNotFound === false) {
  61. mutateEditorMode(EditorMode.Editor);
  62. return;
  63. }
  64. try {
  65. const parentPath = path != null ? normalizePath(path.split('/').slice(0, -1).join('/')) : undefined; // does not have to exist
  66. await create(
  67. {
  68. path, parentPath, wip: shouldCreateWipPage(path), origin: Origin.View,
  69. },
  70. );
  71. }
  72. catch (err) {
  73. toastError(t('toaster.create_failed', { target: path }));
  74. }
  75. }, [create, isNotFound, mutateEditorMode, path, t]);
  76. const _isBtnDisabled = isCreating || isBtnDisabled;
  77. const circleColor = useMemo(() => {
  78. if ((currentPageYjsData?.awarenessStateSize ?? 0) > 0) {
  79. return 'bg-primary';
  80. }
  81. if (currentPageYjsData?.hasYdocsNewerThanLatestRevision ?? false) {
  82. return 'bg-secondary';
  83. }
  84. }, [currentPageYjsData]);
  85. return (
  86. <>
  87. <div
  88. className={`btn-group grw-page-editor-mode-manager ${styles['grw-page-editor-mode-manager']}`}
  89. role="group"
  90. aria-label="page-editor-mode-manager"
  91. id="grw-page-editor-mode-manager"
  92. data-testid="grw-page-editor-mode-manager"
  93. >
  94. {(isDeviceLargerThanMd || editorMode !== EditorMode.View) && (
  95. <PageEditorModeButton
  96. currentEditorMode={editorMode}
  97. editorMode={EditorMode.View}
  98. isBtnDisabled={_isBtnDisabled}
  99. onClick={() => mutateEditorMode(EditorMode.View)}
  100. >
  101. <span className="material-symbols-outlined fs-4">play_arrow</span>{t('View')}
  102. </PageEditorModeButton>
  103. )}
  104. {(isDeviceLargerThanMd || editorMode === EditorMode.View) && (
  105. <PageEditorModeButton
  106. currentEditorMode={editorMode}
  107. editorMode={EditorMode.Editor}
  108. isBtnDisabled={_isBtnDisabled}
  109. onClick={editButtonClickedHandler}
  110. >
  111. <span className="material-symbols-outlined me-1 fs-5">edit_square</span>{t('Edit')}
  112. { circleColor != null && <span className={`position-absolute top-0 start-100 translate-middle p-1 rounded-circle ${circleColor}`} />}
  113. </PageEditorModeButton>
  114. )}
  115. </div>
  116. </>
  117. );
  118. };