main.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { useHistoryContext } from '../../context/history-context'
  2. import { diffDoc } from '../../services/api'
  3. import { useEffect } from 'react'
  4. import { DocDiffResponse, Highlight } from '../../services/types/doc'
  5. import { highlightsFromDiffResponse } from '../../utils/highlights-from-diff-response'
  6. import DocumentDiffViewer from './document-diff-viewer'
  7. import useAsync from '../../../../shared/hooks/use-async'
  8. import { useTranslation } from 'react-i18next'
  9. type Diff = {
  10. binary: boolean
  11. docDiff?: {
  12. doc: string
  13. highlights: Highlight[]
  14. }
  15. }
  16. function Main() {
  17. const { t } = useTranslation()
  18. const { projectId, selection } = useHistoryContext()
  19. const { isLoading, runAsync, data } = useAsync<DocDiffResponse>()
  20. let diff: Diff | undefined
  21. if (data?.diff) {
  22. if ('binary' in data.diff) {
  23. diff = { binary: true }
  24. } else {
  25. diff = { binary: false, docDiff: highlightsFromDiffResponse(data.diff) }
  26. }
  27. }
  28. const { updateRange, pathname } = selection
  29. useEffect(() => {
  30. if (!updateRange || !pathname) {
  31. return
  32. }
  33. const { fromV, toV } = updateRange
  34. // TODO: Error handling
  35. runAsync(diffDoc(projectId, fromV, toV, pathname))
  36. }, [projectId, runAsync, pathname, updateRange])
  37. if (isLoading) {
  38. return (
  39. <div className="history-loading-panel">
  40. <i className="fa fa-spin fa-refresh" />
  41. &nbsp;&nbsp;
  42. {t('loading')}…
  43. </div>
  44. )
  45. }
  46. if (!diff) {
  47. return <div>No document</div>
  48. }
  49. if (diff.binary) {
  50. return <div>Binary file</div>
  51. }
  52. if (diff.docDiff) {
  53. const { doc, highlights } = diff.docDiff
  54. return <DocumentDiffViewer doc={doc} highlights={highlights} />
  55. }
  56. return <div>No document</div>
  57. }
  58. export default Main