PageSideContents.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React, { Suspense, useCallback, useRef } from 'react';
  2. import type { IPagePopulatedToShowRevision, IPageInfoForOperation } from '@growi/core';
  3. import { pagePathUtils } from '@growi/core/dist/utils';
  4. import { useTranslation } from 'next-i18next';
  5. import dynamic from 'next/dynamic';
  6. import { scroller } from 'react-scroll';
  7. import { useIsGuestUser, useIsReadOnlyUser } from '~/stores-universal/context';
  8. import { useDescendantsPageListModal, useTagEditModal } from '~/stores/modal';
  9. import { useSWRxPageInfo, useSWRxTagsInfo } from '~/stores/page';
  10. import { useIsAbleToShowTagLabel } from '~/stores/ui';
  11. import { ContentLinkButtons } from '../ContentLinkButtons';
  12. import { PageTagsSkeleton } from '../PageTags';
  13. import TableOfContents from '../TableOfContents';
  14. import { PageAccessoriesControl } from './PageAccessoriesControl';
  15. import styles from './PageSideContents.module.scss';
  16. const { isTopPage, isUsersHomepage, isTrashPage } = pagePathUtils;
  17. const PageTags = dynamic(() => import('../PageTags').then(mod => mod.PageTags), {
  18. ssr: false,
  19. loading: PageTagsSkeleton,
  20. });
  21. type TagsProps = {
  22. pageId: string,
  23. revisionId: string,
  24. }
  25. const Tags = (props: TagsProps): JSX.Element => {
  26. const { pageId, revisionId } = props;
  27. const { data: tagsInfoData } = useSWRxTagsInfo(pageId, { suspense: true });
  28. const { data: showTagLabel } = useIsAbleToShowTagLabel();
  29. const { data: isGuestUser } = useIsGuestUser();
  30. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  31. const { open: openTagEditModal } = useTagEditModal();
  32. const onClickEditTagsButton = useCallback(() => {
  33. if (tagsInfoData == null) {
  34. return;
  35. }
  36. openTagEditModal(tagsInfoData.tags, pageId, revisionId);
  37. }, [pageId, revisionId, tagsInfoData, openTagEditModal]);
  38. if (!showTagLabel || tagsInfoData == null) {
  39. return <></>;
  40. }
  41. const isTagLabelsDisabled = !!isGuestUser || !!isReadOnlyUser;
  42. return (
  43. <div className="grw-tag-labels-container">
  44. <PageTags
  45. tags={tagsInfoData.tags}
  46. isTagLabelsDisabled={isTagLabelsDisabled}
  47. onClickEditTagsButton={onClickEditTagsButton}
  48. />
  49. </div>
  50. );
  51. };
  52. type PageSideContentsProps = {
  53. page: IPagePopulatedToShowRevision,
  54. isSharedUser?: boolean,
  55. }
  56. export const PageSideContents = (props: PageSideContentsProps): JSX.Element => {
  57. const { t } = useTranslation();
  58. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  59. const { page, isSharedUser } = props;
  60. const tagsRef = useRef<HTMLDivElement>(null);
  61. const { data: pageInfo } = useSWRxPageInfo(page._id);
  62. const pagePath = page.path;
  63. const isTopPagePath = isTopPage(pagePath);
  64. const isUsersHomepagePath = isUsersHomepage(pagePath);
  65. const isTrash = isTrashPage(pagePath);
  66. return (
  67. <>
  68. {/* Tags */}
  69. { page.revision != null && (
  70. <div ref={tagsRef}>
  71. <Suspense fallback={<PageTagsSkeleton />}>
  72. <Tags pageId={page._id} revisionId={page.revision._id} />
  73. </Suspense>
  74. </div>
  75. ) }
  76. <div className={`${styles['grw-page-accessories-controls']} d-flex flex-column gap-2`}>
  77. {/* Page list */}
  78. {!isSharedUser && (
  79. <div className="d-flex" data-testid="pageListButton">
  80. <PageAccessoriesControl
  81. icon={<span className="material-symbols-outlined">subject</span>}
  82. label={t('page_list')}
  83. // Do not display CountBadge if '/trash/*': https://github.com/weseek/growi/pull/7600
  84. count={!isTrash && pageInfo != null ? (pageInfo as IPageInfoForOperation).descendantCount : undefined}
  85. offset={1}
  86. onClick={() => openDescendantPageListModal(pagePath)}
  87. />
  88. </div>
  89. )}
  90. {/* Comments */}
  91. {!isTopPagePath && (
  92. <div className="d-flex" data-testid="page-comment-button">
  93. <PageAccessoriesControl
  94. icon={<span className="material-symbols-outlined">chat</span>}
  95. label={t('comments')}
  96. count={pageInfo != null ? (pageInfo as IPageInfoForOperation).commentCount : undefined}
  97. onClick={() => scroller.scrollTo('comments-container', { smooth: false, offset: -120 })}
  98. />
  99. </div>
  100. )}
  101. </div>
  102. <div className="d-none d-xl-block">
  103. <TableOfContents tagsElementHeight={tagsRef.current?.clientHeight} />
  104. {isUsersHomepagePath && <ContentLinkButtons author={page?.creator} />}
  105. </div>
  106. </>
  107. );
  108. };