unsaved-docs-alert.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. 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. <OLNotification
  32. type="warning"
  33. content={t('saving_notification_with_seconds', {
  34. docname: doc.entity.name,
  35. seconds,
  36. })}
  37. />
  38. )
  39. }