detach-compile-context.js 12 KB

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