PageStaleAlert.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import type { JSX } from 'react';
  2. import { isIPageInfoForEntity } from '@growi/core';
  3. import { useAtomValue } from 'jotai';
  4. import { useTranslation } from 'next-i18next';
  5. import { useCurrentPageData } from '~/states/page';
  6. import { isEnabledStaleNotificationAtom } from '~/states/server-configurations';
  7. import { useSWRxPageInfo } from '~/stores/page';
  8. export const PageStaleAlert = (): JSX.Element => {
  9. const { t } = useTranslation();
  10. const isEnabledStaleNotification = useAtomValue(
  11. isEnabledStaleNotificationAtom,
  12. );
  13. // Todo: determine if it should fetch or not like useSWRxPageInfo below after https://redmine.weseek.co.jp/issues/96788
  14. const pageData = useCurrentPageData();
  15. const { data: pageInfo } = useSWRxPageInfo(
  16. isEnabledStaleNotification ? pageData?._id : null,
  17. );
  18. const contentAge = isIPageInfoForEntity(pageInfo)
  19. ? pageInfo.contentAge
  20. : null;
  21. if (!isEnabledStaleNotification) {
  22. // biome-ignore lint/complexity/noUselessFragments: ignore
  23. return <></>;
  24. }
  25. if (pageInfo == null || contentAge == null || contentAge === 0) {
  26. // biome-ignore lint/complexity/noUselessFragments: ignore
  27. return <></>;
  28. }
  29. let alertClass: string;
  30. switch (contentAge) {
  31. case 1:
  32. alertClass = 'alert-info';
  33. break;
  34. case 2:
  35. alertClass = 'alert-warning';
  36. break;
  37. default:
  38. alertClass = 'alert-danger';
  39. }
  40. return (
  41. <div className={`alert ${alertClass}`}>
  42. <span className="material-symbols-outlined me-1">hourglass</span>
  43. <strong>{t('page_page.notice.stale', { count: contentAge })}</strong>
  44. </div>
  45. );
  46. };