detach-compile-context.tsx 11 KB

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