PageStaleAlert.tsx 1.3 KB

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