detach-compile-context.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. } = 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 [hasShortCompileTimeout] = useDetachStateWatcher(
  126. 'hasShortCompileTimeout',
  127. _hasShortCompileTimeout,
  128. 'detacher',
  129. 'detached'
  130. )
  131. const [highlights] = useDetachStateWatcher(
  132. 'highlights',
  133. _highlights,
  134. 'detacher',
  135. 'detached'
  136. )
  137. const [isProjectOwner] = useDetachStateWatcher(
  138. 'isProjectOwner',
  139. _isProjectOwner,
  140. 'detacher',
  141. 'detached'
  142. )
  143. const [lastCompileOptions] = useDetachStateWatcher(
  144. 'lastCompileOptions',
  145. _lastCompileOptions,
  146. 'detacher',
  147. 'detached'
  148. )
  149. const [logEntries] = useDetachStateWatcher(
  150. 'logEntries',
  151. _logEntries,
  152. 'detacher',
  153. 'detached'
  154. )
  155. const [logEntryAnnotations] = useDetachStateWatcher(
  156. 'logEntryAnnotations',
  157. _logEntryAnnotations,
  158. 'detacher',
  159. 'detached'
  160. )
  161. const [pdfFile] = useDetachStateWatcher(
  162. 'pdfFile',
  163. _pdfFile,
  164. 'detacher',
  165. 'detached'
  166. )
  167. const [pdfViewer] = useDetachStateWatcher(
  168. 'pdfViewer',
  169. _pdfViewer,
  170. 'detacher',
  171. 'detached'
  172. )
  173. const [position] = useDetachStateWatcher(
  174. 'position',
  175. _position,
  176. 'detacher',
  177. 'detached'
  178. )
  179. const [rawLog] = useDetachStateWatcher(
  180. 'rawLog',
  181. _rawLog,
  182. 'detacher',
  183. 'detached'
  184. )
  185. const [showCompileTimeWarning] = useDetachStateWatcher(
  186. 'showCompileTimeWarning',
  187. _showCompileTimeWarning,
  188. 'detacher',
  189. 'detached'
  190. )
  191. const [showLogs] = useDetachStateWatcher(
  192. 'showLogs',
  193. _showLogs,
  194. 'detacher',
  195. 'detached'
  196. )
  197. const [stopOnFirstError] = useDetachStateWatcher(
  198. 'stopOnFirstError',
  199. _stopOnFirstError,
  200. 'detacher',
  201. 'detached'
  202. )
  203. const [stopOnValidationError] = useDetachStateWatcher(
  204. 'stopOnValidationError',
  205. _stopOnValidationError,
  206. 'detacher',
  207. 'detached'
  208. )
  209. const [stoppedOnFirstError] = useDetachStateWatcher(
  210. 'stoppedOnFirstError',
  211. _stoppedOnFirstError,
  212. 'detacher',
  213. 'detached'
  214. )
  215. const [uncompiled] = useDetachStateWatcher(
  216. 'uncompiled',
  217. _uncompiled,
  218. 'detacher',
  219. 'detached'
  220. )
  221. const [editedSinceCompileStarted] = useDetachStateWatcher(
  222. 'editedSinceCompileStarted',
  223. _editedSinceCompileStarted,
  224. 'detacher',
  225. 'detached'
  226. )
  227. const [validationIssues] = useDetachStateWatcher(
  228. 'validationIssues',
  229. _validationIssues,
  230. 'detacher',
  231. 'detached'
  232. )
  233. const setAnimateCompileDropdownArrow = useDetachAction(
  234. 'setAnimateCompileDropdownArrow',
  235. _setAnimateCompileDropdownArrow,
  236. 'detached',
  237. 'detacher'
  238. )
  239. const setAutoCompile = useDetachAction(
  240. 'setAutoCompile',
  241. _setAutoCompile,
  242. 'detached',
  243. 'detacher'
  244. )
  245. const setDraft = useDetachAction(
  246. 'setDraft',
  247. _setDraft,
  248. 'detached',
  249. 'detacher'
  250. )
  251. const setError = useDetachAction(
  252. 'setError',
  253. _setError,
  254. 'detacher',
  255. 'detached'
  256. )
  257. const setPosition = useDetachAction(
  258. 'setPosition',
  259. _setPosition,
  260. 'detached',
  261. 'detacher'
  262. )
  263. const firstRenderDone = useDetachAction(
  264. 'firstRenderDone',
  265. _firstRenderDone,
  266. 'detached',
  267. 'detacher'
  268. )
  269. const setHasLintingError = useDetachAction(
  270. 'setHasLintingError',
  271. _setHasLintingError,
  272. 'detacher',
  273. 'detached'
  274. )
  275. const setHighlights = useDetachAction(
  276. 'setHighlights',
  277. _setHighlights,
  278. 'detacher',
  279. 'detached'
  280. )
  281. const setShowCompileTimeWarning = useDetachAction(
  282. 'setShowCompileTimeWarning',
  283. _setShowCompileTimeWarning,
  284. 'detached',
  285. 'detacher'
  286. )
  287. const setShowLogs = useDetachAction(
  288. 'setShowLogs',
  289. _setShowLogs,
  290. 'detached',
  291. 'detacher'
  292. )
  293. const toggleLogs = useDetachAction(
  294. 'toggleLogs',
  295. _toggleLogs,
  296. 'detached',
  297. 'detacher'
  298. )
  299. const setStopOnFirstError = useDetachAction(
  300. 'setStopOnFirstError',
  301. _setStopOnFirstError,
  302. 'detached',
  303. 'detacher'
  304. )
  305. const setStopOnValidationError = useDetachAction(
  306. 'setStopOnValidationError',
  307. _setStopOnValidationError,
  308. 'detached',
  309. 'detacher'
  310. )
  311. const cleanupCompileResult = useDetachAction(
  312. 'cleanupCompileResult',
  313. _cleanupCompileResult,
  314. 'detached',
  315. 'detacher'
  316. )
  317. const recompileFromScratch = useDetachAction(
  318. 'recompileFromScratch',
  319. _recompileFromScratch,
  320. 'detached',
  321. 'detacher'
  322. )
  323. const setCompiling = useDetachAction(
  324. 'setCompiling',
  325. _setCompiling,
  326. 'detacher',
  327. 'detached'
  328. )
  329. const startCompile = useDetachAction(
  330. 'startCompile',
  331. _startCompile,
  332. 'detached',
  333. 'detacher'
  334. )
  335. const stopCompile = useDetachAction(
  336. 'stopCompile',
  337. _stopCompile,
  338. 'detached',
  339. 'detacher'
  340. )
  341. const setChangedAt = useDetachAction(
  342. 'setChangedAt',
  343. _setChangedAt,
  344. 'detached',
  345. 'detacher'
  346. )
  347. const clearCache = useDetachAction(
  348. 'clearCache',
  349. _clearCache,
  350. 'detached',
  351. 'detacher'
  352. )
  353. const syncToEntry = useDetachAction(
  354. 'sync-to-entry',
  355. _syncToEntry,
  356. 'detached',
  357. 'detacher'
  358. )
  359. useCompileTriggers(startCompile, setChangedAt)
  360. useLogEvents(setShowLogs)
  361. const value = useMemo(
  362. () => ({
  363. animateCompileDropdownArrow,
  364. autoCompile,
  365. clearCache,
  366. clearingCache,
  367. clsiServerId,
  368. codeCheckFailed,
  369. compiling,
  370. deliveryLatencies,
  371. draft,
  372. editedSinceCompileStarted,
  373. error,
  374. fileList,
  375. hasChanges,
  376. hasShortCompileTimeout,
  377. highlights,
  378. isProjectOwner,
  379. lastCompileOptions,
  380. logEntryAnnotations,
  381. logEntries,
  382. pdfDownloadUrl: pdfFile?.pdfDownloadUrl,
  383. pdfFile,
  384. pdfUrl: pdfFile?.pdfUrl,
  385. pdfViewer,
  386. position,
  387. rawLog,
  388. recompileFromScratch,
  389. setAnimateCompileDropdownArrow,
  390. setAutoCompile,
  391. setCompiling,
  392. setDraft,
  393. setError,
  394. setHasLintingError,
  395. setHighlights,
  396. setPosition,
  397. setShowCompileTimeWarning,
  398. setShowLogs,
  399. toggleLogs,
  400. setStopOnFirstError,
  401. setStopOnValidationError,
  402. showLogs,
  403. showCompileTimeWarning,
  404. startCompile,
  405. stopCompile,
  406. stopOnFirstError,
  407. stopOnValidationError,
  408. stoppedOnFirstError,
  409. uncompiled,
  410. validationIssues,
  411. firstRenderDone,
  412. setChangedAt,
  413. cleanupCompileResult,
  414. syncToEntry,
  415. }),
  416. [
  417. animateCompileDropdownArrow,
  418. autoCompile,
  419. clearCache,
  420. clearingCache,
  421. clsiServerId,
  422. codeCheckFailed,
  423. compiling,
  424. deliveryLatencies,
  425. draft,
  426. editedSinceCompileStarted,
  427. error,
  428. fileList,
  429. hasChanges,
  430. hasShortCompileTimeout,
  431. highlights,
  432. isProjectOwner,
  433. lastCompileOptions,
  434. logEntryAnnotations,
  435. logEntries,
  436. pdfFile,
  437. pdfViewer,
  438. position,
  439. rawLog,
  440. recompileFromScratch,
  441. setAnimateCompileDropdownArrow,
  442. setAutoCompile,
  443. setCompiling,
  444. setDraft,
  445. setError,
  446. setHasLintingError,
  447. setHighlights,
  448. setPosition,
  449. setShowCompileTimeWarning,
  450. setShowLogs,
  451. toggleLogs,
  452. setStopOnFirstError,
  453. setStopOnValidationError,
  454. showCompileTimeWarning,
  455. showLogs,
  456. startCompile,
  457. stopCompile,
  458. stopOnFirstError,
  459. stopOnValidationError,
  460. stoppedOnFirstError,
  461. uncompiled,
  462. validationIssues,
  463. firstRenderDone,
  464. setChangedAt,
  465. cleanupCompileResult,
  466. syncToEntry,
  467. ]
  468. )
  469. return (
  470. <DetachCompileContext.Provider value={value}>
  471. {children}
  472. </DetachCompileContext.Provider>
  473. )
  474. }
  475. export function useDetachCompileContext() {
  476. const context = useContext(DetachCompileContext)
  477. if (!context) {
  478. throw new Error(
  479. 'useDetachCompileContext is ony available inside DetachCompileProvider'
  480. )
  481. }
  482. return context
  483. }