SubNavButtons.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React, {
  2. FC, useCallback,
  3. } from 'react';
  4. import AppContainer from '../../client/services/AppContainer';
  5. import NavigationContainer from '../../client/services/NavigationContainer';
  6. import { withUnstatedContainers } from '../UnstatedUtils';
  7. import PageReactionButtons from '../PageReactionButtons';
  8. import PageManagement from '../Page/PageManagement';
  9. import { useSWRPageInfo } from '../../stores/page';
  10. import { toastError } from '../../client/util/apiNotification';
  11. import { apiv3Put } from '../../client/util/apiv3-client';
  12. type SubNavButtonsProps= {
  13. appContainer: AppContainer,
  14. navigationContainer: NavigationContainer,
  15. isCompactMode?: boolean,
  16. pageId: string,
  17. }
  18. const SubNavButtons: FC<SubNavButtonsProps> = (props: SubNavButtonsProps) => {
  19. const {
  20. appContainer, navigationContainer, isCompactMode, pageId,
  21. } = props;
  22. const { editorMode } = navigationContainer.state;
  23. const isViewMode = editorMode === 'view';
  24. const { data: pageInfo, error: pageInfoError, mutate: mutatePageInfo } = useSWRPageInfo(pageId);
  25. const likeClickhandler = useCallback(async() => {
  26. const { isGuestUser } = appContainer;
  27. if (isGuestUser) {
  28. return;
  29. }
  30. try {
  31. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  32. await apiv3Put('/page/likes', { pageId, bool: !pageInfo!.isLiked });
  33. mutatePageInfo();
  34. }
  35. catch (err) {
  36. toastError(err);
  37. }
  38. }, [pageInfo]);
  39. if (pageInfoError != null || pageInfo == null) {
  40. return <></>;
  41. }
  42. const { sumOfLikers, likerIds, isLiked } = pageInfo;
  43. return (
  44. <>
  45. {isViewMode && (
  46. <PageReactionButtons
  47. pageId={pageId}
  48. sumOfLikers={sumOfLikers}
  49. likerIds={likerIds || []}
  50. isLiked={isLiked}
  51. onLikeClicked={likeClickhandler}
  52. >
  53. </PageReactionButtons>
  54. )}
  55. {/*
  56. TODO :
  57. once 80335 is done, merge 77543 branch(parent of 80335) into 77524.
  58. (pageContainer dependencies in bookmark, delete modal, rename etc are removed)
  59. then place PageManagement here.
  60. TASK: https://estoc.weseek.co.jp/redmine/issues/81076
  61. CONDITION :isAbleToShowPageManagement = !isNotFoundPage && !isTrashPage && !isSharedUser
  62. */}
  63. {/* if (CONDITION) then <PageManagement isCompactMode> */}
  64. </>
  65. );
  66. };
  67. /**
  68. * Wrapper component for using unstated
  69. */
  70. const SubNavButtonsUnstatedWrapper = withUnstatedContainers(SubNavButtons, [AppContainer, NavigationContainer]);
  71. // wrapping tsx component returned by withUnstatedContainers to avoid type error when this component used in other tsx components.
  72. const SubNavButtonsWrapper = (props) => {
  73. return <SubNavButtonsUnstatedWrapper {...props}></SubNavButtonsUnstatedWrapper>;
  74. };
  75. export default SubNavButtonsWrapper;