Header.tsx 3.9 KB

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