PageSideContents.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { useCallback } from 'react';
  2. import { getIdForRef, type IPageHasId, type 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/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);
  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) {
  39. return <></>;
  40. }
  41. const isTagLabelsDisabled = !!isGuestUser || !!isReadOnlyUser;
  42. return (
  43. <div className="grw-taglabels-container">
  44. { tagsInfoData?.tags != null
  45. ? (
  46. <PageTags
  47. tags={tagsInfoData.tags}
  48. isTagLabelsDisabled={isTagLabelsDisabled}
  49. onClickEditTagsButton={onClickEditTagsButton}
  50. />
  51. )
  52. : <PageTagsSkeleton />
  53. }
  54. </div>
  55. );
  56. };
  57. export type PageSideContentsProps = {
  58. page: IPageHasId,
  59. isSharedUser?: boolean,
  60. }
  61. export const PageSideContents = (props: PageSideContentsProps): JSX.Element => {
  62. const { t } = useTranslation();
  63. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  64. const { page, isSharedUser } = props;
  65. const { data: pageInfo } = useSWRxPageInfo(page._id);
  66. const pagePath = page.path;
  67. const isTopPagePath = isTopPage(pagePath);
  68. const isUsersHomepagePath = isUsersHomepage(pagePath);
  69. const isTrash = isTrashPage(pagePath);
  70. return (
  71. <>
  72. {/* Tags */}
  73. <Tags pageId={page._id} revisionId={getIdForRef(page.revision)} />
  74. <div className={`${styles['grw-page-accessories-controls']} d-flex flex-column gap-2`}>
  75. {/* Page list */}
  76. {!isSharedUser && (
  77. <div className="d-flex" data-testid="pageListButton">
  78. <PageAccessoriesControl
  79. icon={<span className="material-symbols-outlined">subject</span>}
  80. label={t('page_list')}
  81. // Do not display CountBadge if '/trash/*': https://github.com/weseek/growi/pull/7600
  82. count={!isTrash && pageInfo != null ? (pageInfo as IPageInfoForOperation).descendantCount : undefined}
  83. onClick={() => openDescendantPageListModal(pagePath)}
  84. />
  85. </div>
  86. )}
  87. {/* Comments */}
  88. {!isTopPagePath && (
  89. <div className="d-flex" data-testid="page-comment-button">
  90. <PageAccessoriesControl
  91. icon={<span className="material-symbols-outlined">chat</span>}
  92. label="Comments"
  93. count={pageInfo != null ? (pageInfo as IPageInfoForOperation).commentCount : undefined}
  94. onClick={() => scroller.scrollTo('comments-container', { smooth: false, offset: -120 })}
  95. />
  96. </div>
  97. )}
  98. </div>
  99. <div className="d-none d-xl-block">
  100. <TableOfContents />
  101. {isUsersHomepagePath && <ContentLinkButtons author={page?.creator} />}
  102. </div>
  103. </>
  104. );
  105. };