| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import type { FC } from 'react';
- import React, {
- useCallback, useState,
- } from 'react';
- import nodePath from 'path';
- import type { IPageInfoAll, IPageToDeleteWithMeta } from '@growi/core';
- import { pathUtils } from '@growi/core/dist/utils';
- import { useTranslation } from 'next-i18next';
- import { DropdownToggle } from 'reactstrap';
- import { bookmark, unbookmark, resumeRenameOperation } from '~/client/services/page-operation';
- import { apiv3Put } from '~/client/util/apiv3-client';
- import { ValidationTarget } from '~/client/util/input-validator';
- import { toastError, toastSuccess } from '~/client/util/toastr';
- import { NotAvailableForGuest } from '~/components/NotAvailableForGuest';
- import { useSWRMUTxCurrentUserBookmarks } from '~/stores/bookmark';
- import { useSWRMUTxPageInfo } from '~/stores/page';
- import ClosableTextInput from '../../Common/ClosableTextInput';
- import { PageItemControl } from '../../Common/Dropdown/PageItemControl';
- import {
- type TreeItemToolProps, NotDraggableForClosableTextInput, SimpleItemTool,
- } from '../../TreeItem';
- export const Ellipsis: FC<TreeItemToolProps> = (props) => {
- const [isRenameInputShown, setRenameInputShown] = useState(false);
- const { t } = useTranslation();
- const {
- itemNode, onRenamed, onClickDuplicateMenuItem,
- onClickDeleteMenuItem, isEnableActions, isReadOnlyUser,
- } = props;
- const { page } = itemNode;
- const { trigger: mutateCurrentUserBookmarks } = useSWRMUTxCurrentUserBookmarks();
- const { trigger: mutatePageInfo } = useSWRMUTxPageInfo(page._id ?? null);
- const bookmarkMenuItemClickHandler = async(_pageId: string, _newValue: boolean): Promise<void> => {
- const bookmarkOperation = _newValue ? bookmark : unbookmark;
- await bookmarkOperation(_pageId);
- mutateCurrentUserBookmarks();
- mutatePageInfo();
- };
- const duplicateMenuItemClickHandler = useCallback((): void => {
- if (onClickDuplicateMenuItem == null) {
- return;
- }
- const { _id: pageId, path } = page;
- if (pageId == null || path == null) {
- throw Error('Any of _id and path must not be null.');
- }
- const pageToDuplicate = { pageId, path };
- onClickDuplicateMenuItem(pageToDuplicate);
- }, [onClickDuplicateMenuItem, page]);
- const renameMenuItemClickHandler = useCallback(() => {
- setRenameInputShown(true);
- }, []);
- const onPressEnterForRenameHandler = async(inputText: string) => {
- const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(page.path ?? ''));
- const newPagePath = nodePath.resolve(parentPath, inputText);
- if (newPagePath === page.path) {
- setRenameInputShown(false);
- return;
- }
- try {
- setRenameInputShown(false);
- await apiv3Put('/pages/rename', {
- pageId: page._id,
- revisionId: page.revision,
- newPagePath,
- });
- if (onRenamed != null) {
- onRenamed(page.path, newPagePath);
- }
- toastSuccess(t('renamed_pages', { path: page.path }));
- }
- catch (err) {
- setRenameInputShown(true);
- toastError(err);
- }
- };
- const deleteMenuItemClickHandler = useCallback(async(_pageId: string, pageInfo: IPageInfoAll | undefined): Promise<void> => {
- if (onClickDeleteMenuItem == null) {
- return;
- }
- if (page._id == null || page.path == null) {
- throw Error('_id and path must not be null.');
- }
- const pageToDelete: IPageToDeleteWithMeta = {
- data: {
- _id: page._id,
- revision: page.revision as string,
- path: page.path,
- },
- meta: pageInfo,
- };
- onClickDeleteMenuItem(pageToDelete);
- }, [onClickDeleteMenuItem, page]);
- const pathRecoveryMenuItemClickHandler = async(pageId: string): Promise<void> => {
- try {
- await resumeRenameOperation(pageId);
- toastSuccess(t('page_operation.paths_recovered'));
- }
- catch {
- toastError(t('page_operation.path_recovery_failed'));
- }
- };
- const hasChildren = page.descendantCount ? page.descendantCount > 0 : false;
- return (
- <>
- {isRenameInputShown ? (
- <div className={`position-absolute ${hasChildren ? 'ms-5' : 'ms-4'}`}>
- <NotDraggableForClosableTextInput>
- <ClosableTextInput
- value={nodePath.basename(page.path ?? '')}
- placeholder={t('Input page name')}
- onClickOutside={() => { setRenameInputShown(false) }}
- onPressEnter={onPressEnterForRenameHandler}
- validationTarget={ValidationTarget.PAGE}
- />
- </NotDraggableForClosableTextInput>
- </div>
- ) : (
- <SimpleItemTool itemNode={itemNode} isEnableActions={false} isReadOnlyUser={false} />
- )}
- <NotAvailableForGuest>
- <div className="grw-pagetree-control d-flex">
- <PageItemControl
- pageId={page._id}
- isEnableActions={isEnableActions}
- isReadOnlyUser={isReadOnlyUser}
- onClickBookmarkMenuItem={bookmarkMenuItemClickHandler}
- onClickDuplicateMenuItem={duplicateMenuItemClickHandler}
- onClickRenameMenuItem={renameMenuItemClickHandler}
- onClickDeleteMenuItem={deleteMenuItemClickHandler}
- onClickPathRecoveryMenuItem={pathRecoveryMenuItemClickHandler}
- isInstantRename
- // Todo: It is wanted to find a better way to pass operationProcessData to PageItemControl
- operationProcessData={page.processData}
- >
- {/* pass the color property to reactstrap dropdownToggle props. https://6-4-0--reactstrap.netlify.app/components/dropdowns/ */}
- <DropdownToggle color="transparent" className="border-0 rounded btn-page-item-control p-0 grw-visible-on-hover mr-1">
- <span id="option-button-in-page-tree" className="material-symbols-outlined p-1">more_vert</span>
- </DropdownToggle>
- </PageItemControl>
- </div>
- </NotAvailableForGuest>
- </>
- );
- };
|