detach-compile-context.js 11 KB

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