TableWithEditButton.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import React, { useCallback, type JSX } from 'react';
  2. import { globalEventTarget } from '@growi/core/dist/utils';
  3. import type { Element } from 'hast';
  4. import type { LaunchHandsonTableModalEventDetail } from '~/client/interfaces/handsontable-modal';
  5. import { useCurrentPageYjsData } from '~/features/collaborative-editor/states';
  6. import { useIsGuestUser, useIsReadOnlyUser, useIsSharedUser } from '~/states/context';
  7. import { useShareLinkId } from '~/states/page/hooks';
  8. import { useIsRevisionOutdated } from '~/stores/page';
  9. import styles from './TableWithEditButton.module.scss';
  10. type TableWithEditButtonProps = {
  11. children: React.ReactNode,
  12. node: Element,
  13. className?: string
  14. }
  15. const TableWithEditButtonNoMemorized = (props: TableWithEditButtonProps): JSX.Element => {
  16. const { children, node, className } = props;
  17. const isGuestUser = useIsGuestUser();
  18. const isReadOnlyUser = useIsReadOnlyUser();
  19. const isSharedUser = useIsSharedUser();
  20. const shareLinkId = useShareLinkId();
  21. const isRevisionOutdated = useIsRevisionOutdated();
  22. const currentPageYjsData = useCurrentPageYjsData();
  23. const bol = node.position?.start.line ?? 0;
  24. const eol = node.position?.end.line ?? 0;
  25. const editButtonClickHandler = useCallback(() => {
  26. globalEventTarget.dispatchEvent(new CustomEvent<LaunchHandsonTableModalEventDetail>('launchHandsonTableModal', {
  27. detail: {
  28. bol,
  29. eol,
  30. },
  31. }));
  32. }, [bol, eol]);
  33. const isNoEditingUsers = currentPageYjsData?.awarenessStateSize == null || currentPageYjsData?.awarenessStateSize === 0;
  34. const showEditButton = isNoEditingUsers
  35. && !isRevisionOutdated
  36. && !isGuestUser
  37. && !isReadOnlyUser
  38. && !isSharedUser
  39. && shareLinkId == null;
  40. return (
  41. <div className={`editable-with-handsontable ${styles['editable-with-handsontable']}`}>
  42. { showEditButton && (
  43. <button type="button" className="handsontable-modal-trigger" onClick={editButtonClickHandler}>
  44. <span className="material-symbols-outlined">edit_square</span>
  45. </button>
  46. )}
  47. <table className={className}>
  48. {children}
  49. </table>
  50. </div>
  51. );
  52. };
  53. TableWithEditButtonNoMemorized.displayName = 'TableWithEditButton';
  54. export const TableWithEditButton = React.memo(TableWithEditButtonNoMemorized) as typeof TableWithEditButtonNoMemorized;