detach-compile-context.js 11 KB

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