LinkEditorModeHashChange.tsx 485 B

12345678910111213141516171819
  1. import { FC, useEffect } from 'react';
  2. import Link, { LinkProps } from 'next/link';
  3. import { isEditorModeHash } from '~/stores/ui';
  4. export const LinkEditorModeHashChange: FC<LinkProps> = (props: LinkProps) => {
  5. const url = new URL(props.href.toString(), 'http://example.com');
  6. const hash = url.hash;
  7. useEffect(() => {
  8. if (isEditorModeHash(hash) && window.location.hash !== hash) {
  9. window.location.hash = hash;
  10. }
  11. }, [hash]);
  12. return <Link {...props} />;
  13. };