detach-compile-context.tsx 12 KB

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