NotFoundPage.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { useMemo, useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import urljoin from 'url-join';
  4. import { useCurrentPageId, useCurrentPagePath, useNotFoundTargetPathOrId } from '~/stores/context';
  5. import CustomNavAndContents from './CustomNavigation/CustomNavAndContents';
  6. import { DescendantsPageListForCurrentPath } from './DescendantsPageList';
  7. import PageListIcon from './Icons/PageListIcon';
  8. import TimeLineIcon from './Icons/TimeLineIcon';
  9. import PageTimeline from './PageTimeline';
  10. /**
  11. * Replace url in address bar with new path and query parameters
  12. */
  13. const replaceURLHistory = (path: string) => {
  14. const queryParameters = window.location.search;
  15. window.history.replaceState(null, '', urljoin(path, queryParameters));
  16. };
  17. const NotFoundPage = (): JSX.Element => {
  18. const { t } = useTranslation();
  19. const { data: pageId } = useCurrentPageId();
  20. const { data: path } = useCurrentPagePath();
  21. const { data: notFoundTargetPathOrId } = useNotFoundTargetPathOrId();
  22. // replace url in address bar with path when accessing empty page by permalink
  23. useEffect(() => {
  24. const isEmptyPage = pageId != null; // Todo: should be improved. https://redmine.weseek.co.jp/issues/98152
  25. const isPathExist = path != null;
  26. const isPathLink = notFoundTargetPathOrId?.includes('/');
  27. if (isEmptyPage && isPathExist && !isPathLink) {
  28. // The (as string) below is a workaround for the case. See the link. (Fixed in typescript version 4.4)
  29. // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-4.html#control-flow-analysis-of-aliased-conditions-and-discriminants
  30. replaceURLHistory(path as string);
  31. }
  32. }, [pageId, path, notFoundTargetPathOrId]);
  33. const navTabMapping = useMemo(() => {
  34. return {
  35. pagelist: {
  36. Icon: PageListIcon,
  37. Content: DescendantsPageListForCurrentPath,
  38. i18n: t('page_list'),
  39. index: 0,
  40. },
  41. timeLine: {
  42. Icon: TimeLineIcon,
  43. Content: PageTimeline,
  44. i18n: t('Timeline View'),
  45. index: 1,
  46. },
  47. };
  48. }, [t]);
  49. return (
  50. <div className="d-edit-none">
  51. <CustomNavAndContents navTabMapping={navTabMapping} tabContentClasses={['py-4']} />
  52. </div>
  53. );
  54. };
  55. export default NotFoundPage;