ranges-context.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import {
  2. createContext,
  3. FC,
  4. useCallback,
  5. useContext,
  6. useEffect,
  7. useMemo,
  8. useState,
  9. } from 'react'
  10. import { DocumentContainer } from '@/features/ide-react/editor/document-container'
  11. import {
  12. Change,
  13. CommentOperation,
  14. EditOperation,
  15. } from '../../../../../types/change'
  16. import RangesTracker from '@overleaf/ranges-tracker'
  17. import { rejectChanges } from '@/features/source-editor/extensions/changes/reject-changes'
  18. import { useCodeMirrorViewContext } from '@/features/source-editor/components/codemirror-context'
  19. import { postJSON } from '@/infrastructure/fetch-json'
  20. import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
  21. import { useConnectionContext } from '@/features/ide-react/context/connection-context'
  22. import useSocketListener from '@/features/ide-react/hooks/use-socket-listener'
  23. import { throttle } from 'lodash'
  24. import { useEditorOpenDocContext } from '@/features/ide-react/context/editor-open-doc-context'
  25. export type Ranges = {
  26. docId: string
  27. changes: Change<EditOperation>[]
  28. comments: Change<CommentOperation>[]
  29. }
  30. export const RangesContext = createContext<Ranges | undefined>(undefined)
  31. type RangesActions = {
  32. acceptChanges: (...ids: string[]) => Promise<void>
  33. rejectChanges: (...ids: string[]) => Promise<void>
  34. }
  35. const buildRanges = (currentDocument: DocumentContainer | null) => {
  36. const ranges = currentDocument?.ranges
  37. if (!ranges) {
  38. return undefined
  39. }
  40. const dirtyState = ranges.getDirtyState()
  41. ranges.resetDirtyState()
  42. const changed = {
  43. changes: new Set([
  44. ...Object.keys(dirtyState.change.added),
  45. ...Object.keys(dirtyState.change.moved),
  46. ...Object.keys(dirtyState.change.removed),
  47. ]),
  48. comments: new Set([
  49. ...Object.keys(dirtyState.comment.added),
  50. ...Object.keys(dirtyState.comment.moved),
  51. ...Object.keys(dirtyState.comment.removed),
  52. ]),
  53. }
  54. return {
  55. changes:
  56. changed.changes.size > 0
  57. ? ranges.changes.map(change =>
  58. changed.changes.has(change.id) ? { ...change } : change
  59. )
  60. : ranges.changes,
  61. comments:
  62. changed.comments.size > 0
  63. ? ranges.comments.map(comment =>
  64. changed.comments.has(comment.id) ? { ...comment } : comment
  65. )
  66. : ranges.comments,
  67. docId: currentDocument.doc_id,
  68. }
  69. }
  70. const RangesActionsContext = createContext<RangesActions | undefined>(undefined)
  71. export const RangesProvider: FC<React.PropsWithChildren> = ({ children }) => {
  72. const view = useCodeMirrorViewContext()
  73. const { projectId } = useIdeReactContext()
  74. const { currentDocument } = useEditorOpenDocContext()
  75. const { socket } = useConnectionContext()
  76. const [ranges, setRanges] = useState<Ranges | undefined>(() =>
  77. buildRanges(currentDocument)
  78. )
  79. // rebuild the ranges when the current doc changes
  80. useEffect(() => {
  81. setRanges(buildRanges(currentDocument))
  82. }, [currentDocument])
  83. useEffect(() => {
  84. if (currentDocument) {
  85. const listener = throttle(
  86. () => {
  87. window.setTimeout(() => {
  88. setRanges(buildRanges(currentDocument))
  89. })
  90. },
  91. 500,
  92. { leading: true, trailing: true }
  93. )
  94. // currentDocument.on('ranges:clear.cm6', listener)
  95. currentDocument.on('ranges:redraw.cm6', listener)
  96. currentDocument.on('ranges:dirty.cm6', listener)
  97. return () => {
  98. // currentDocument.off('ranges:clear.cm6')
  99. currentDocument.off('ranges:redraw.cm6')
  100. currentDocument.off('ranges:dirty.cm6')
  101. }
  102. }
  103. }, [currentDocument])
  104. // TODO: move this into DocumentContainer?
  105. useEffect(() => {
  106. if (currentDocument) {
  107. const regenerateTrackChangesId = (doc: DocumentContainer) => {
  108. if (doc.ranges) {
  109. const inflight = doc.ranges.getIdSeed()
  110. const pending = RangesTracker.generateIdSeed()
  111. doc.ranges.setIdSeed(pending)
  112. doc.setTrackChangesIdSeeds({ pending, inflight })
  113. }
  114. }
  115. currentDocument.on('flipped_pending_to_inflight', () =>
  116. regenerateTrackChangesId(currentDocument)
  117. )
  118. regenerateTrackChangesId(currentDocument)
  119. return () => {
  120. currentDocument.off('flipped_pending_to_inflight')
  121. }
  122. }
  123. }, [currentDocument])
  124. useSocketListener(
  125. socket,
  126. 'accept-changes',
  127. useCallback(
  128. (docId: string, entryIds: string[]) => {
  129. if (currentDocument?.ranges) {
  130. if (docId === currentDocument.doc_id) {
  131. currentDocument.ranges.removeChangeIds(entryIds)
  132. setRanges(buildRanges(currentDocument))
  133. }
  134. }
  135. },
  136. [currentDocument]
  137. )
  138. )
  139. const actions = useMemo(
  140. () => ({
  141. async acceptChanges(...ids: string[]) {
  142. if (currentDocument?.ranges) {
  143. const url = `/project/${projectId}/doc/${currentDocument.doc_id}/changes/accept`
  144. await postJSON(url, { body: { change_ids: ids } })
  145. currentDocument.ranges.removeChangeIds(ids)
  146. setRanges(buildRanges(currentDocument))
  147. }
  148. },
  149. async rejectChanges(...ids: string[]) {
  150. if (currentDocument?.ranges) {
  151. view.dispatch(rejectChanges(view.state, currentDocument.ranges, ids))
  152. }
  153. },
  154. }),
  155. [currentDocument, projectId, view]
  156. )
  157. return (
  158. <RangesActionsContext.Provider value={actions}>
  159. <RangesContext.Provider value={ranges}>{children}</RangesContext.Provider>
  160. </RangesActionsContext.Provider>
  161. )
  162. }
  163. export const useRangesContext = () => {
  164. return useContext(RangesContext)
  165. }
  166. export const useRangesActionsContext = () => {
  167. const context = useContext(RangesActionsContext)
  168. if (!context) {
  169. throw new Error(
  170. 'useRangesActionsContext is only available inside RangesProvider'
  171. )
  172. }
  173. return context
  174. }