diff-view.tsx 1.7 KB

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