unsaved-docs-alert.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { FC, useMemo } from 'react'
  2. import { useFileTreePathContext } from '@/features/file-tree/contexts/file-tree-path'
  3. import { useTranslation } from 'react-i18next'
  4. import OLNotification from '@/features/ui/components/ol/ol-notification'
  5. const MAX_UNSAVED_ALERT_SECONDS = 15
  6. export const UnsavedDocsAlert: FC<{ unsavedDocs: Map<string, number> }> = ({
  7. unsavedDocs,
  8. }) => (
  9. <>
  10. {[...unsavedDocs.entries()].map(
  11. ([docId, seconds]) =>
  12. seconds >= MAX_UNSAVED_ALERT_SECONDS && (
  13. <UnsavedDocAlert key={docId} docId={docId} seconds={seconds} />
  14. )
  15. )}
  16. </>
  17. )
  18. const UnsavedDocAlert: FC<{ docId: string; seconds: number }> = ({
  19. docId,
  20. seconds,
  21. }) => {
  22. const { pathInFolder, findEntityByPath } = useFileTreePathContext()
  23. const { t } = useTranslation()
  24. const doc = useMemo(() => {
  25. const path = pathInFolder(docId)
  26. return path ? findEntityByPath(path) : null
  27. }, [docId, findEntityByPath, pathInFolder])
  28. if (!doc) {
  29. return null
  30. }
  31. return (
  32. <OLNotification
  33. type="warning"
  34. content={t('saving_notification_with_seconds', {
  35. docname: doc.entity.name,
  36. seconds,
  37. })}
  38. />
  39. )
  40. }