detach-compile-context.tsx 12 KB

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