unsaved-docs-alert.tsx 1.1 KB

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