PageStaleAlert.tsx 1.4 KB

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