detach-compile-context.js 11 KB

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