PageSideContents.tsx 5.1 KB

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