detach-compile-context.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. darkModePdf: _darkModePdf,
  72. setDarkModePdf: _setDarkModePdf,
  73. activeOverallTheme: _activeOverallTheme,
  74. } = localCompileContext
  75. const [animateCompileDropdownArrow] = useDetachStateWatcher(
  76. 'animateCompileDropdownArrow',
  77. _animateCompileDropdownArrow,
  78. 'detacher',
  79. 'detached'
  80. )
  81. const [autoCompile] = useDetachStateWatcher(
  82. 'autoCompile',
  83. _autoCompile,
  84. 'detacher',
  85. 'detached'
  86. )
  87. const [clearingCache] = useDetachStateWatcher(
  88. 'clearingCache',
  89. _clearingCache,
  90. 'detacher',
  91. 'detached'
  92. )
  93. const [clsiServerId] = useDetachStateWatcher(
  94. 'clsiServerId',
  95. _clsiServerId,
  96. 'detacher',
  97. 'detached'
  98. )
  99. const [codeCheckFailed] = useDetachStateWatcher(
  100. 'codeCheckFailed',
  101. _codeCheckFailed,
  102. 'detacher',
  103. 'detached'
  104. )
  105. const [compiling] = useDetachStateWatcher(
  106. 'compiling',
  107. _compiling,
  108. 'detacher',
  109. 'detached'
  110. )
  111. const [deliveryLatencies] = useDetachStateWatcher(
  112. 'deliveryLatencies',
  113. _deliveryLatencies,
  114. 'detacher',
  115. 'detached'
  116. )
  117. const [draft] = useDetachStateWatcher('draft', _draft, 'detacher', 'detached')
  118. const [error] = useDetachStateWatcher('error', _error, 'detacher', 'detached')
  119. const [fileList] = useDetachStateWatcher(
  120. 'fileList',
  121. _fileList,
  122. 'detacher',
  123. 'detached'
  124. )
  125. const [hasChanges] = useDetachStateWatcher(
  126. 'hasChanges',
  127. _hasChanges,
  128. 'detacher',
  129. 'detached'
  130. )
  131. const [hasShortCompileTimeout] = useDetachStateWatcher(
  132. 'hasShortCompileTimeout',
  133. _hasShortCompileTimeout,
  134. 'detacher',
  135. 'detached'
  136. )
  137. const [highlights] = useDetachStateWatcher(
  138. 'highlights',
  139. _highlights,
  140. 'detacher',
  141. 'detached'
  142. )
  143. const [isProjectOwner] = useDetachStateWatcher(
  144. 'isProjectOwner',
  145. _isProjectOwner,
  146. 'detacher',
  147. 'detached'
  148. )
  149. const [lastCompileOptions] = useDetachStateWatcher(
  150. 'lastCompileOptions',
  151. _lastCompileOptions,
  152. 'detacher',
  153. 'detached'
  154. )
  155. const [logEntries] = useDetachStateWatcher(
  156. 'logEntries',
  157. _logEntries,
  158. 'detacher',
  159. 'detached'
  160. )
  161. const [logEntryAnnotations] = useDetachStateWatcher(
  162. 'logEntryAnnotations',
  163. _logEntryAnnotations,
  164. 'detacher',
  165. 'detached'
  166. )
  167. const [pdfFile] = useDetachStateWatcher(
  168. 'pdfFile',
  169. _pdfFile,
  170. 'detacher',
  171. 'detached'
  172. )
  173. const [pdfViewer] = useDetachStateWatcher(
  174. 'pdfViewer',
  175. _pdfViewer,
  176. 'detacher',
  177. 'detached'
  178. )
  179. const [position] = useDetachStateWatcher(
  180. 'position',
  181. _position,
  182. 'detacher',
  183. 'detached'
  184. )
  185. const [rawLog] = useDetachStateWatcher(
  186. 'rawLog',
  187. _rawLog,
  188. 'detacher',
  189. 'detached'
  190. )
  191. const [showCompileTimeWarning] = useDetachStateWatcher(
  192. 'showCompileTimeWarning',
  193. _showCompileTimeWarning,
  194. 'detacher',
  195. 'detached'
  196. )
  197. const [showLogs] = useDetachStateWatcher(
  198. 'showLogs',
  199. _showLogs,
  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. const recordAction = useDetachAction(
  366. 'record-action',
  367. _recordAction,
  368. 'detached',
  369. 'detacher'
  370. )
  371. const [darkModePdf] = useDetachStateWatcher(
  372. 'darkModePdf',
  373. _darkModePdf,
  374. 'detacher',
  375. 'detached'
  376. )
  377. const setDarkModePdf = useDetachAction(
  378. 'setDarkModePdf',
  379. _setDarkModePdf,
  380. 'detached',
  381. 'detacher'
  382. )
  383. const [activeOverallTheme] = useDetachStateWatcher(
  384. 'activeOverallTheme',
  385. _activeOverallTheme,
  386. 'detacher',
  387. 'detached'
  388. )
  389. useCompileTriggers(startCompile, setChangedAt)
  390. useLogEvents(setShowLogs)
  391. const value = useMemo(
  392. () => ({
  393. animateCompileDropdownArrow,
  394. autoCompile,
  395. clearCache,
  396. clearingCache,
  397. clsiServerId,
  398. codeCheckFailed,
  399. compiling,
  400. deliveryLatencies,
  401. draft,
  402. editedSinceCompileStarted,
  403. error,
  404. fileList,
  405. hasChanges,
  406. hasShortCompileTimeout,
  407. highlights,
  408. isProjectOwner,
  409. lastCompileOptions,
  410. logEntryAnnotations,
  411. logEntries,
  412. pdfDownloadUrl: pdfFile?.pdfDownloadUrl,
  413. pdfFile,
  414. pdfUrl: pdfFile?.pdfUrl,
  415. pdfViewer,
  416. position,
  417. rawLog,
  418. recompileFromScratch,
  419. setAnimateCompileDropdownArrow,
  420. setAutoCompile,
  421. setCompiling,
  422. setDraft,
  423. setError,
  424. setHasLintingError,
  425. setHighlights,
  426. setPosition,
  427. setShowCompileTimeWarning,
  428. setShowLogs,
  429. toggleLogs,
  430. setStopOnFirstError,
  431. setStopOnValidationError,
  432. showLogs,
  433. showCompileTimeWarning,
  434. startCompile,
  435. stopCompile,
  436. stopOnFirstError,
  437. stopOnValidationError,
  438. stoppedOnFirstError,
  439. uncompiled,
  440. validationIssues,
  441. firstRenderDone,
  442. setChangedAt,
  443. cleanupCompileResult,
  444. syncToEntry,
  445. recordAction,
  446. darkModePdf,
  447. setDarkModePdf,
  448. activeOverallTheme,
  449. }),
  450. [
  451. animateCompileDropdownArrow,
  452. autoCompile,
  453. clearCache,
  454. clearingCache,
  455. clsiServerId,
  456. codeCheckFailed,
  457. compiling,
  458. deliveryLatencies,
  459. draft,
  460. editedSinceCompileStarted,
  461. error,
  462. fileList,
  463. hasChanges,
  464. hasShortCompileTimeout,
  465. highlights,
  466. isProjectOwner,
  467. lastCompileOptions,
  468. logEntryAnnotations,
  469. logEntries,
  470. pdfFile,
  471. pdfViewer,
  472. position,
  473. rawLog,
  474. recompileFromScratch,
  475. setAnimateCompileDropdownArrow,
  476. setAutoCompile,
  477. setCompiling,
  478. setDraft,
  479. setError,
  480. setHasLintingError,
  481. setHighlights,
  482. setPosition,
  483. setShowCompileTimeWarning,
  484. setShowLogs,
  485. toggleLogs,
  486. setStopOnFirstError,
  487. setStopOnValidationError,
  488. showCompileTimeWarning,
  489. showLogs,
  490. startCompile,
  491. stopCompile,
  492. stopOnFirstError,
  493. stopOnValidationError,
  494. stoppedOnFirstError,
  495. uncompiled,
  496. validationIssues,
  497. firstRenderDone,
  498. setChangedAt,
  499. cleanupCompileResult,
  500. syncToEntry,
  501. recordAction,
  502. darkModePdf,
  503. setDarkModePdf,
  504. activeOverallTheme,
  505. ]
  506. )
  507. return (
  508. <DetachCompileContext.Provider value={value}>
  509. {children}
  510. </DetachCompileContext.Provider>
  511. )
  512. }
  513. export function useDetachCompileContext() {
  514. const context = useContext(DetachCompileContext)
  515. if (!context) {
  516. throw new Error(
  517. 'useDetachCompileContext is ony available inside DetachCompileProvider'
  518. )
  519. }
  520. return context
  521. }