detach-compile-context.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. import { createContext, FC, useContext, useMemo } from 'react'
  2. import { CompileContext, useLocalCompileContext } from './local-compile-context'
  3. import useDetachStateWatcher from '../hooks/use-detach-state-watcher'
  4. import useDetachAction from '../hooks/use-detach-action'
  5. import useCompileTriggers from '../../features/pdf-preview/hooks/use-compile-triggers'
  6. export const DetachCompileContext = createContext<CompileContext | undefined>(
  7. undefined
  8. )
  9. export const DetachCompileProvider: FC = ({ children }) => {
  10. const localCompileContext = useLocalCompileContext()
  11. if (!localCompileContext) {
  12. throw new Error(
  13. 'DetachCompileProvider is only available inside LocalCompileProvider'
  14. )
  15. }
  16. const {
  17. animateCompileDropdownArrow: _animateCompileDropdownArrow,
  18. autoCompile: _autoCompile,
  19. clearingCache: _clearingCache,
  20. clsiServerId: _clsiServerId,
  21. codeCheckFailed: _codeCheckFailed,
  22. compiling: _compiling,
  23. deliveryLatencies: _deliveryLatencies,
  24. draft: _draft,
  25. editedSinceCompileStarted: _editedSinceCompileStarted,
  26. error: _error,
  27. fileList: _fileList,
  28. hasChanges: _hasChanges,
  29. highlights: _highlights,
  30. isProjectOwner: _isProjectOwner,
  31. lastCompileOptions: _lastCompileOptions,
  32. logEntries: _logEntries,
  33. logEntryAnnotations: _logEntryAnnotations,
  34. pdfFile: _pdfFile,
  35. pdfViewer: _pdfViewer,
  36. position: _position,
  37. rawLog: _rawLog,
  38. setAnimateCompileDropdownArrow: _setAnimateCompileDropdownArrow,
  39. setAutoCompile: _setAutoCompile,
  40. setDraft: _setDraft,
  41. setError: _setError,
  42. setHasLintingError: _setHasLintingError,
  43. setHighlights: _setHighlights,
  44. setPosition: _setPosition,
  45. setShowCompileTimeWarning: _setShowCompileTimeWarning,
  46. setShowLogs: _setShowLogs,
  47. toggleLogs: _toggleLogs,
  48. setStopOnFirstError: _setStopOnFirstError,
  49. setStopOnValidationError: _setStopOnValidationError,
  50. showLogs: _showLogs,
  51. showCompileTimeWarning: _showCompileTimeWarning,
  52. showNewCompileTimeoutUI: _showNewCompileTimeoutUI,
  53. showFasterCompilesFeedbackUI: _showFasterCompilesFeedbackUI,
  54. stopOnFirstError: _stopOnFirstError,
  55. stopOnValidationError: _stopOnValidationError,
  56. stoppedOnFirstError: _stoppedOnFirstError,
  57. uncompiled: _uncompiled,
  58. validationIssues: _validationIssues,
  59. firstRenderDone: _firstRenderDone,
  60. cleanupCompileResult: _cleanupCompileResult,
  61. recompileFromScratch: _recompileFromScratch,
  62. setCompiling: _setCompiling,
  63. startCompile: _startCompile,
  64. stopCompile: _stopCompile,
  65. setChangedAt: _setChangedAt,
  66. clearCache: _clearCache,
  67. syncToEntry: _syncToEntry,
  68. } = localCompileContext
  69. const [animateCompileDropdownArrow] = useDetachStateWatcher(
  70. 'animateCompileDropdownArrow',
  71. _animateCompileDropdownArrow,
  72. 'detacher',
  73. 'detached'
  74. )
  75. const [autoCompile] = useDetachStateWatcher(
  76. 'autoCompile',
  77. _autoCompile,
  78. 'detacher',
  79. 'detached'
  80. )
  81. const [clearingCache] = useDetachStateWatcher(
  82. 'clearingCache',
  83. _clearingCache,
  84. 'detacher',
  85. 'detached'
  86. )
  87. const [clsiServerId] = useDetachStateWatcher(
  88. 'clsiServerId',
  89. _clsiServerId,
  90. 'detacher',
  91. 'detached'
  92. )
  93. const [codeCheckFailed] = useDetachStateWatcher(
  94. 'codeCheckFailed',
  95. _codeCheckFailed,
  96. 'detacher',
  97. 'detached'
  98. )
  99. const [compiling] = useDetachStateWatcher(
  100. 'compiling',
  101. _compiling,
  102. 'detacher',
  103. 'detached'
  104. )
  105. const [deliveryLatencies] = useDetachStateWatcher(
  106. 'deliveryLatencies',
  107. _deliveryLatencies,
  108. 'detacher',
  109. 'detached'
  110. )
  111. const [draft] = useDetachStateWatcher('draft', _draft, 'detacher', 'detached')
  112. const [error] = useDetachStateWatcher('error', _error, 'detacher', 'detached')
  113. const [fileList] = useDetachStateWatcher(
  114. 'fileList',
  115. _fileList,
  116. 'detacher',
  117. 'detached'
  118. )
  119. const [hasChanges] = useDetachStateWatcher(
  120. 'hasChanges',
  121. _hasChanges,
  122. 'detacher',
  123. 'detached'
  124. )
  125. const [highlights] = useDetachStateWatcher(
  126. 'highlights',
  127. _highlights,
  128. 'detacher',
  129. 'detached'
  130. )
  131. const [isProjectOwner] = useDetachStateWatcher(
  132. 'isProjectOwner',
  133. _isProjectOwner,
  134. 'detacher',
  135. 'detached'
  136. )
  137. const [lastCompileOptions] = useDetachStateWatcher(
  138. 'lastCompileOptions',
  139. _lastCompileOptions,
  140. 'detacher',
  141. 'detached'
  142. )
  143. const [logEntries] = useDetachStateWatcher(
  144. 'logEntries',
  145. _logEntries,
  146. 'detacher',
  147. 'detached'
  148. )
  149. const [logEntryAnnotations] = useDetachStateWatcher(
  150. 'logEntryAnnotations',
  151. _logEntryAnnotations,
  152. 'detacher',
  153. 'detached'
  154. )
  155. const [pdfFile] = useDetachStateWatcher(
  156. 'pdfFile',
  157. _pdfFile,
  158. 'detacher',
  159. 'detached'
  160. )
  161. const [pdfViewer] = useDetachStateWatcher(
  162. 'pdfViewer',
  163. _pdfViewer,
  164. 'detacher',
  165. 'detached'
  166. )
  167. const [position] = useDetachStateWatcher(
  168. 'position',
  169. _position,
  170. 'detacher',
  171. 'detached'
  172. )
  173. const [rawLog] = useDetachStateWatcher(
  174. 'rawLog',
  175. _rawLog,
  176. 'detacher',
  177. 'detached'
  178. )
  179. const [showCompileTimeWarning] = useDetachStateWatcher(
  180. 'showCompileTimeWarning',
  181. _showCompileTimeWarning,
  182. 'detacher',
  183. 'detached'
  184. )
  185. const [showLogs] = useDetachStateWatcher(
  186. 'showLogs',
  187. _showLogs,
  188. 'detacher',
  189. 'detached'
  190. )
  191. const [showNewCompileTimeoutUI] = useDetachStateWatcher(
  192. 'showNewCompileTimeoutUI',
  193. _showNewCompileTimeoutUI,
  194. 'detacher',
  195. 'detached'
  196. )
  197. const [showFasterCompilesFeedbackUI] = useDetachStateWatcher(
  198. 'showFasterCompilesFeedbackUI',
  199. _showFasterCompilesFeedbackUI,
  200. 'detacher',
  201. 'detached'
  202. )
  203. const [stopOnFirstError] = useDetachStateWatcher(
  204. 'stopOnFirstError',
  205. _stopOnFirstError,
  206. 'detacher',
  207. 'detached'
  208. )
  209. const [stopOnValidationError] = useDetachStateWatcher(
  210. 'stopOnValidationError',
  211. _stopOnValidationError,
  212. 'detacher',
  213. 'detached'
  214. )
  215. const [stoppedOnFirstError] = useDetachStateWatcher(
  216. 'stoppedOnFirstError',
  217. _stoppedOnFirstError,
  218. 'detacher',
  219. 'detached'
  220. )
  221. const [uncompiled] = useDetachStateWatcher(
  222. 'uncompiled',
  223. _uncompiled,
  224. 'detacher',
  225. 'detached'
  226. )
  227. const [editedSinceCompileStarted] = useDetachStateWatcher(
  228. 'editedSinceCompileStarted',
  229. _editedSinceCompileStarted,
  230. 'detacher',
  231. 'detached'
  232. )
  233. const [validationIssues] = useDetachStateWatcher(
  234. 'validationIssues',
  235. _validationIssues,
  236. 'detacher',
  237. 'detached'
  238. )
  239. const setAnimateCompileDropdownArrow = useDetachAction(
  240. 'setAnimateCompileDropdownArrow',
  241. _setAnimateCompileDropdownArrow,
  242. 'detached',
  243. 'detacher'
  244. )
  245. const setAutoCompile = useDetachAction(
  246. 'setAutoCompile',
  247. _setAutoCompile,
  248. 'detached',
  249. 'detacher'
  250. )
  251. const setDraft = useDetachAction(
  252. 'setDraft',
  253. _setDraft,
  254. 'detached',
  255. 'detacher'
  256. )
  257. const setError = useDetachAction(
  258. 'setError',
  259. _setError,
  260. 'detacher',
  261. 'detached'
  262. )
  263. const setPosition = useDetachAction(
  264. 'setPosition',
  265. _setPosition,
  266. 'detached',
  267. 'detacher'
  268. )
  269. const firstRenderDone = useDetachAction(
  270. 'firstRenderDone',
  271. _firstRenderDone,
  272. 'detached',
  273. 'detacher'
  274. )
  275. const setHasLintingError = useDetachAction(
  276. 'setHasLintingError',
  277. _setHasLintingError,
  278. 'detacher',
  279. 'detached'
  280. )
  281. const setHighlights = useDetachAction(
  282. 'setHighlights',
  283. _setHighlights,
  284. 'detacher',
  285. 'detached'
  286. )
  287. const setShowCompileTimeWarning = useDetachAction(
  288. 'setShowCompileTimeWarning',
  289. _setShowCompileTimeWarning,
  290. 'detached',
  291. 'detacher'
  292. )
  293. const setShowLogs = useDetachAction(
  294. 'setShowLogs',
  295. _setShowLogs,
  296. 'detached',
  297. 'detacher'
  298. )
  299. const toggleLogs = useDetachAction(
  300. 'toggleLogs',
  301. _toggleLogs,
  302. 'detached',
  303. 'detacher'
  304. )
  305. const setStopOnFirstError = useDetachAction(
  306. 'setStopOnFirstError',
  307. _setStopOnFirstError,
  308. 'detached',
  309. 'detacher'
  310. )
  311. const setStopOnValidationError = useDetachAction(
  312. 'setStopOnValidationError',
  313. _setStopOnValidationError,
  314. 'detached',
  315. 'detacher'
  316. )
  317. const cleanupCompileResult = useDetachAction(
  318. 'cleanupCompileResult',
  319. _cleanupCompileResult,
  320. 'detached',
  321. 'detacher'
  322. )
  323. const recompileFromScratch = useDetachAction(
  324. 'recompileFromScratch',
  325. _recompileFromScratch,
  326. 'detached',
  327. 'detacher'
  328. )
  329. const setCompiling = useDetachAction(
  330. 'setCompiling',
  331. _setCompiling,
  332. 'detacher',
  333. 'detached'
  334. )
  335. const startCompile = useDetachAction(
  336. 'startCompile',
  337. _startCompile,
  338. 'detached',
  339. 'detacher'
  340. )
  341. const stopCompile = useDetachAction(
  342. 'stopCompile',
  343. _stopCompile,
  344. 'detached',
  345. 'detacher'
  346. )
  347. const setChangedAt = useDetachAction(
  348. 'setChangedAt',
  349. _setChangedAt,
  350. 'detached',
  351. 'detacher'
  352. )
  353. const clearCache = useDetachAction(
  354. 'clearCache',
  355. _clearCache,
  356. 'detached',
  357. 'detacher'
  358. )
  359. const syncToEntry = useDetachAction(
  360. 'sync-to-entry',
  361. _syncToEntry,
  362. 'detached',
  363. 'detacher'
  364. )
  365. useCompileTriggers(startCompile, setChangedAt)
  366. const value = useMemo(
  367. () => ({
  368. animateCompileDropdownArrow,
  369. autoCompile,
  370. clearCache,
  371. clearingCache,
  372. clsiServerId,
  373. codeCheckFailed,
  374. compiling,
  375. deliveryLatencies,
  376. draft,
  377. editedSinceCompileStarted,
  378. error,
  379. fileList,
  380. hasChanges,
  381. highlights,
  382. isProjectOwner,
  383. lastCompileOptions,
  384. logEntryAnnotations,
  385. logEntries,
  386. pdfDownloadUrl: pdfFile?.pdfDownloadUrl,
  387. pdfFile,
  388. pdfUrl: pdfFile?.pdfUrl,
  389. pdfViewer,
  390. position,
  391. rawLog,
  392. recompileFromScratch,
  393. setAnimateCompileDropdownArrow,
  394. setAutoCompile,
  395. setCompiling,
  396. setDraft,
  397. setError,
  398. setHasLintingError,
  399. setHighlights,
  400. setPosition,
  401. setShowCompileTimeWarning,
  402. setShowLogs,
  403. toggleLogs,
  404. setStopOnFirstError,
  405. setStopOnValidationError,
  406. showLogs,
  407. showCompileTimeWarning,
  408. showNewCompileTimeoutUI,
  409. showFasterCompilesFeedbackUI,
  410. startCompile,
  411. stopCompile,
  412. stopOnFirstError,
  413. stopOnValidationError,
  414. stoppedOnFirstError,
  415. uncompiled,
  416. validationIssues,
  417. firstRenderDone,
  418. setChangedAt,
  419. cleanupCompileResult,
  420. syncToEntry,
  421. }),
  422. [
  423. animateCompileDropdownArrow,
  424. autoCompile,
  425. clearCache,
  426. clearingCache,
  427. clsiServerId,
  428. codeCheckFailed,
  429. compiling,
  430. deliveryLatencies,
  431. draft,
  432. error,
  433. editedSinceCompileStarted,
  434. fileList,
  435. hasChanges,
  436. highlights,
  437. isProjectOwner,
  438. lastCompileOptions,
  439. logEntryAnnotations,
  440. logEntries,
  441. pdfFile,
  442. pdfViewer,
  443. position,
  444. rawLog,
  445. recompileFromScratch,
  446. setAnimateCompileDropdownArrow,
  447. setAutoCompile,
  448. setCompiling,
  449. setDraft,
  450. setError,
  451. setHasLintingError,
  452. setHighlights,
  453. setPosition,
  454. setShowCompileTimeWarning,
  455. setShowLogs,
  456. toggleLogs,
  457. setStopOnFirstError,
  458. setStopOnValidationError,
  459. showCompileTimeWarning,
  460. showLogs,
  461. showNewCompileTimeoutUI,
  462. showFasterCompilesFeedbackUI,
  463. startCompile,
  464. stopCompile,
  465. stopOnFirstError,
  466. stopOnValidationError,
  467. stoppedOnFirstError,
  468. uncompiled,
  469. validationIssues,
  470. firstRenderDone,
  471. setChangedAt,
  472. cleanupCompileResult,
  473. syncToEntry,
  474. ]
  475. )
  476. return (
  477. <DetachCompileContext.Provider value={value}>
  478. {children}
  479. </DetachCompileContext.Provider>
  480. )
  481. }
  482. export function useDetachCompileContext() {
  483. const context = useContext(DetachCompileContext)
  484. if (!context) {
  485. throw new Error(
  486. 'useDetachCompileContext is ony available inside DetachCompileProvider'
  487. )
  488. }
  489. return context
  490. }