diff-view.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. function DiffView() {
  11. const { selection, projectId, loadingState } = useHistoryContext()
  12. const loadingFileDiffs = loadingState === 'loadingFileDiffs'
  13. const { isLoading, data, runAsync, error } = useAsync<DocDiffResponse>()
  14. const { updateRange, selectedFile } = selection
  15. useEffect(() => {
  16. if (!updateRange || !selectedFile?.pathname || loadingFileDiffs) {
  17. return
  18. }
  19. const { fromV, toV } = updateRange
  20. runAsync(diffDoc(projectId, fromV, toV, selectedFile.pathname)).catch(
  21. console.error
  22. )
  23. }, [projectId, runAsync, updateRange, selectedFile, loadingFileDiffs])
  24. let diff: Diff | null
  25. if (!data?.diff) {
  26. diff = null
  27. } else if ('binary' in data.diff) {
  28. diff = { binary: true }
  29. } else {
  30. diff = {
  31. binary: false,
  32. docDiff: highlightsFromDiffResponse(data.diff),
  33. }
  34. }
  35. return (
  36. <div className="doc-panel">
  37. {error ? (
  38. <ErrorMessage />
  39. ) : (
  40. <>
  41. <div className="history-header toolbar-container">
  42. <Toolbar diff={diff} selection={selection} />
  43. </div>
  44. <div className="doc-container">
  45. <Main diff={diff} isLoading={isLoading || loadingFileDiffs} />
  46. </div>
  47. </>
  48. )}
  49. </div>
  50. )
  51. }
  52. export default DiffView