diff-view.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { useEffect } from 'react'
  2. import Toolbar from './toolbar/toolbar'
  3. import Main from './main'
  4. import { Diff, DocDiffResponse } from '../../services/types/doc'
  5. import { useHistoryContext } from '../../context/history-context'
  6. import { diffDoc } from '../../services/api'
  7. import { highlightsFromDiffResponse } from '../../utils/highlights-from-diff-response'
  8. import useAsync from '../../../../shared/hooks/use-async'
  9. import ErrorMessage from '../error-message'
  10. import { useTranslation } from 'react-i18next'
  11. function DiffView() {
  12. const { selection, projectId, loadingFileDiffs } = useHistoryContext()
  13. const { isLoading, data, runAsync, error } = useAsync<DocDiffResponse>()
  14. const { t } = useTranslation()
  15. const { updateRange, selectedFile } = selection
  16. useEffect(() => {
  17. if (!updateRange || !selectedFile?.pathname || loadingFileDiffs) {
  18. return
  19. }
  20. const { fromV, toV } = updateRange
  21. runAsync(diffDoc(projectId, fromV, toV, selectedFile.pathname)).catch(
  22. console.error
  23. )
  24. }, [projectId, runAsync, updateRange, selectedFile, loadingFileDiffs])
  25. let diff: Diff | null
  26. if (!data?.diff) {
  27. diff = null
  28. } else if ('binary' in data.diff) {
  29. diff = { binary: true }
  30. } else {
  31. diff = {
  32. binary: false,
  33. docDiff: highlightsFromDiffResponse(data.diff, t),
  34. }
  35. }
  36. return (
  37. <div className="doc-panel">
  38. {error ? (
  39. <ErrorMessage />
  40. ) : (
  41. <>
  42. <div className="history-header toolbar-container">
  43. <Toolbar diff={diff} selection={selection} />
  44. </div>
  45. <div className="doc-container">
  46. <Main diff={diff} isLoading={isLoading || loadingFileDiffs} />
  47. </div>
  48. </>
  49. )}
  50. </div>
  51. )
  52. }
  53. export default DiffView