SubNavButtons.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. return (
  43. <>
  44. {isViewMode && (
  45. <PageReactionButtons
  46. pageId={pageId}
  47. onLikeClicked={likeClickhandler}
  48. >
  49. </PageReactionButtons>
  50. )}
  51. {/*
  52. TODO :
  53. once 80335 is done, merge 77543 branch(parent of 80335) into 77524.
  54. (pageContainer dependencies in bookmark, delete modal, rename etc are removed)
  55. then place PageManagement here.
  56. TASK: https://estoc.weseek.co.jp/redmine/issues/81076
  57. CONDITION :isAbleToShowPageManagement = !isNotFoundPage && !isTrashPage && !isSharedUser
  58. */}
  59. {/* if (CONDITION) then <PageManagement isCompactMode> */}
  60. </>
  61. );
  62. };
  63. /**
  64. * Wrapper component for using unstated
  65. */
  66. const SubNavButtonsUnstatedWrapper = withUnstatedContainers(SubNavButtons, [AppContainer, NavigationContainer]);
  67. // wrapping tsx component returned by withUnstatedContainers to avoid type error when this component used in other tsx components.
  68. const SubNavButtonsWrapper = (props) => {
  69. return <SubNavButtonsUnstatedWrapper {...props}></SubNavButtonsUnstatedWrapper>;
  70. };
  71. export default SubNavButtonsWrapper;