| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import type { FC } from 'react';
- import { useCallback } from 'react';
- import path from 'path';
- import type { IPageToDeleteWithMeta } from '@growi/core/dist/interfaces';
- import { getIdStringForRef } from '@growi/core/dist/interfaces';
- import { pathUtils } from '@growi/core/dist/utils';
- import { useTranslation } from 'next-i18next';
- import { useRouter } from 'next/router';
- import { toastSuccess } from '~/client/util/toastr';
- import { ROOT_PAGE_VIRTUAL_ID } from '~/constants/page-tree';
- import type { IPageForItem } from '~/interfaces/page';
- import type { OnDeletedFunction, OnDuplicatedFunction } from '~/interfaces/ui';
- import { useCurrentPagePath, useFetchCurrentPage } from '~/states/page';
- import { usePageTreeInformationUpdate } from '~/states/page-tree-update';
- import { usePageDeleteModalActions } from '~/states/ui/modal/page-delete';
- import type { IPageForPageDuplicateModal } from '~/states/ui/modal/page-duplicate';
- import { usePageDuplicateModalActions } from '~/states/ui/modal/page-duplicate';
- import { mutateAllPageInfo } from '~/stores/page';
- import { mutatePageTree, mutatePageList } from '~/stores/page-listing';
- import { mutateSearching } from '~/stores/search';
- import type { TreeItemProps } from '../../TreeItem';
- import { TreeItemLayout } from '../../TreeItem';
- import { CountBadgeForPageTreeItem } from './CountBadgeForPageTreeItem';
- import { usePageItemControl } from './use-page-item-control';
- import styles from './PageTreeItem.module.scss';
- const moduleClass = styles['page-tree-item'] ?? '';
- export const simplifiedPageTreeItemSize = 40; // in px
- export const SimplifiedPageTreeItem: FC<TreeItemProps> = ({
- item,
- targetPath,
- targetPathOrId,
- isWipPageShown,
- isEnableActions = false,
- isReadOnlyUser = false,
- onToggle,
- }) => {
- const { t } = useTranslation();
- const router = useRouter();
- const itemData = item.getItemData();
- const currentPagePath = useCurrentPagePath();
- const { fetchCurrentPage } = useFetchCurrentPage();
- const { open: openDuplicateModal } = usePageDuplicateModalActions();
- const { open: openDeleteModal } = usePageDeleteModalActions();
- const { notifyUpdateItems } = usePageTreeInformationUpdate();
- const onClickDuplicateMenuItem = useCallback((page: IPageForPageDuplicateModal) => {
- const duplicatedHandler: OnDuplicatedFunction = (fromPath) => {
- toastSuccess(t('duplicated_pages', { fromPath }));
- mutatePageTree();
- mutateSearching();
- mutatePageList();
- // Notify headless-tree update
- const parentId = itemData.parent != null ? getIdStringForRef(itemData.parent) : ROOT_PAGE_VIRTUAL_ID;
- notifyUpdateItems([parentId]);
- };
- openDuplicateModal(page, { onDuplicated: duplicatedHandler });
- }, [openDuplicateModal, t, notifyUpdateItems, itemData.parent]);
- const onClickDeleteMenuItem = useCallback((page: IPageToDeleteWithMeta) => {
- const onDeletedHandler: OnDeletedFunction = (pathOrPathsToDelete, isRecursively, isCompletely) => {
- if (typeof pathOrPathsToDelete !== 'string') {
- return;
- }
- if (isCompletely) {
- toastSuccess(t('deleted_pages_completely', { path: pathOrPathsToDelete }));
- }
- else {
- toastSuccess(t('deleted_pages', { path: pathOrPathsToDelete }));
- }
- mutatePageTree();
- mutateSearching();
- mutatePageList();
- mutateAllPageInfo();
- if (currentPagePath === pathOrPathsToDelete) {
- fetchCurrentPage({ force: true });
- router.push(isCompletely ? path.dirname(pathOrPathsToDelete) : `/trash${pathOrPathsToDelete}`);
- }
- // Notify headless-tree update
- const parentId = itemData.parent != null ? getIdStringForRef(itemData.parent) : ROOT_PAGE_VIRTUAL_ID;
- notifyUpdateItems([parentId]);
- };
- openDeleteModal([page], { onDeleted: onDeletedHandler });
- }, [openDeleteModal, t, currentPagePath, fetchCurrentPage, router, itemData.parent, notifyUpdateItems]);
- const { Control } = usePageItemControl();
- const itemSelectedHandler = useCallback((page: IPageForItem) => {
- if (page.path == null || page._id == null) return;
- const link = pathUtils.returnPathForURL(page.path, page._id);
- router.push(link);
- }, [router]);
- const itemSelectedByWheelClickHandler = useCallback((page: IPageForItem) => {
- if (page.path == null || page._id == null) return;
- const url = pathUtils.returnPathForURL(page.path, page._id);
- window.open(url, '_blank');
- }, []);
- return (
- <TreeItemLayout
- className={moduleClass}
- item={item}
- targetPath={targetPath}
- targetPathOrId={targetPathOrId ?? undefined}
- isWipPageShown={isWipPageShown}
- isEnableActions={isEnableActions}
- isReadOnlyUser={isReadOnlyUser}
- onClick={itemSelectedHandler}
- onWheelClick={itemSelectedByWheelClickHandler}
- onToggle={onToggle}
- onClickDuplicateMenuItem={onClickDuplicateMenuItem}
- onClickDeleteMenuItem={onClickDeleteMenuItem}
- customEndComponents={[CountBadgeForPageTreeItem]}
- customHoveredEndComponents={[Control]}
- />
- );
- };
|