detach-compile-context.js 12 KB

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