Header.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {
  2. useCallback, useEffect, useState, type JSX,
  3. } from 'react';
  4. import { globalEventTarget } from '@growi/core/dist/utils';
  5. import type { Element } from 'hast';
  6. import { useRouter } from 'next/router';
  7. import { NextLink } from '~/components/ReactMarkdownComponents/NextLink';
  8. import { useCurrentPageYjsData, useCurrentPageYjsDataLoading } from '~/features/collaborative-editor/states';
  9. import { useIsGuestUser, useIsReadOnlyUser, useIsSharedUser } from '~/states/context';
  10. import { useShareLinkId } from '~/states/page/hooks';
  11. import type { ReservedNextCaretLineEventDetail } from '~/states/ui/editor/reserved-next-caret-line';
  12. import loggerFactory from '~/utils/logger';
  13. import styles from './Header.module.scss';
  14. const logger = loggerFactory('growi:components:Header');
  15. const moduleClass = styles['revision-head'] ?? '';
  16. function setCaretLine(lineNumber?: number): void {
  17. if (lineNumber != null) {
  18. globalEventTarget.dispatchEvent(new CustomEvent<ReservedNextCaretLineEventDetail>('reservedNextCaretLine', {
  19. detail: {
  20. lineNumber,
  21. },
  22. }));
  23. }
  24. }
  25. type EditLinkProps = {
  26. line?: number,
  27. }
  28. /**
  29. * Inner FC to display edit link icon
  30. */
  31. const EditLink = (props: EditLinkProps): JSX.Element => {
  32. const isDisabled = props.line == null;
  33. return (
  34. <span className="revision-head-edit-button">
  35. <a href="#edit" aria-disabled={isDisabled} onClick={() => setCaretLine(props.line)}>
  36. <span className="material-symbols-outlined">edit_square</span>
  37. </a>
  38. </span>
  39. );
  40. };
  41. type HeaderProps = {
  42. children: React.ReactNode,
  43. node: Element,
  44. id?: string,
  45. }
  46. export const Header = (props: HeaderProps): JSX.Element => {
  47. const {
  48. node, id, children,
  49. } = props;
  50. const isGuestUser = useIsGuestUser();
  51. const isReadOnlyUser = useIsReadOnlyUser();
  52. const isSharedUser = useIsSharedUser();
  53. const shareLinkId = useShareLinkId();
  54. const currentPageYjsData = useCurrentPageYjsData();
  55. const isLoadingCurrentPageYjsData = useCurrentPageYjsDataLoading();
  56. const router = useRouter();
  57. const [isActive, setActive] = useState(false);
  58. const CustomTag = node.tagName as keyof JSX.IntrinsicElements;
  59. const activateByHash = useCallback((url: string) => {
  60. try {
  61. const hash = (new URL(url, 'https://example.com')).hash.slice(1);
  62. setActive(decodeURIComponent(hash) === id);
  63. }
  64. catch (err) {
  65. logger.debug(err);
  66. setActive(false);
  67. }
  68. }, [id]);
  69. // init
  70. useEffect(() => {
  71. activateByHash(window.location.href);
  72. }, [activateByHash]);
  73. // update isActive when hash is changed by next router
  74. useEffect(() => {
  75. router.events.on('hashChangeComplete', activateByHash);
  76. return () => {
  77. router.events.off('hashChangeComplete', activateByHash);
  78. };
  79. }, [activateByHash, router.events]);
  80. // update isActive when hash is changed
  81. useEffect(() => {
  82. const activeByHashWrapper = (e: HashChangeEvent) => {
  83. activateByHash(e.newURL);
  84. };
  85. window.addEventListener('hashchange', activeByHashWrapper);
  86. return () => {
  87. window.removeEventListener('hashchange', activeByHashWrapper);
  88. };
  89. }, [activateByHash, router.events]);
  90. // TODO: currentPageYjsData?.hasYdocsNewerThanLatestRevision === false make to hide the edit button when a Yjs draft exists
  91. // This is because the current conditional logic cannot handle cases where the draft is an empty string.
  92. // It will be possible to address this TODO ySyncAnnotation become available for import.
  93. // Ref: https://github.com/yjs/y-codemirror.next/pull/30
  94. const showEditButton = !isGuestUser && !isReadOnlyUser && !isSharedUser && shareLinkId == null
  95. && (!isLoadingCurrentPageYjsData && !currentPageYjsData?.hasYdocsNewerThanLatestRevision);
  96. return (
  97. <>
  98. <CustomTag id={id} className={`position-relative ${moduleClass} ${isActive ? styles.blink : ''} `}>
  99. <NextLink href={`#${id}`} className="d-none d-md-inline revision-head-link position-absolute">
  100. #
  101. </NextLink>
  102. {children}
  103. { showEditButton && (
  104. <EditLink line={node.position?.start.line} />
  105. ) }
  106. </CustomTag>
  107. </>
  108. );
  109. };