detach-compile-context.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. 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 [forceNewDomainVariant] = useDetachStateWatcher(
  124. 'forceNewDomainVariant',
  125. _forceNewDomainVariant,
  126. 'detacher',
  127. 'detached'
  128. )
  129. const [hasChanges] = useDetachStateWatcher(
  130. 'hasChanges',
  131. _hasChanges,
  132. 'detacher',
  133. 'detached'
  134. )
  135. const [highlights] = useDetachStateWatcher(
  136. 'highlights',
  137. _highlights,
  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 [showFasterCompilesFeedbackUI] = useDetachStateWatcher(
  196. 'showFasterCompilesFeedbackUI',
  197. _showFasterCompilesFeedbackUI,
  198. 'detacher',
  199. 'detached'
  200. )
  201. const [stopOnFirstError] = useDetachStateWatcher(
  202. 'stopOnFirstError',
  203. _stopOnFirstError,
  204. 'detacher',
  205. 'detached'
  206. )
  207. const [stopOnValidationError] = useDetachStateWatcher(
  208. 'stopOnValidationError',
  209. _stopOnValidationError,
  210. 'detacher',
  211. 'detached'
  212. )
  213. const [stoppedOnFirstError] = useDetachStateWatcher(
  214. 'stoppedOnFirstError',
  215. _stoppedOnFirstError,
  216. 'detacher',
  217. 'detached'
  218. )
  219. const [uncompiled] = useDetachStateWatcher(
  220. 'uncompiled',
  221. _uncompiled,
  222. 'detacher',
  223. 'detached'
  224. )
  225. const [editedSinceCompileStarted] = useDetachStateWatcher(
  226. 'editedSinceCompileStarted',
  227. _editedSinceCompileStarted,
  228. 'detacher',
  229. 'detached'
  230. )
  231. const [validationIssues] = useDetachStateWatcher(
  232. 'validationIssues',
  233. _validationIssues,
  234. 'detacher',
  235. 'detached'
  236. )
  237. const setAnimateCompileDropdownArrow = useDetachAction(
  238. 'setAnimateCompileDropdownArrow',
  239. _setAnimateCompileDropdownArrow,
  240. 'detached',
  241. 'detacher'
  242. )
  243. const setAutoCompile = useDetachAction(
  244. 'setAutoCompile',
  245. _setAutoCompile,
  246. 'detached',
  247. 'detacher'
  248. )
  249. const setDraft = useDetachAction(
  250. 'setDraft',
  251. _setDraft,
  252. 'detached',
  253. 'detacher'
  254. )
  255. const setError = useDetachAction(
  256. 'setError',
  257. _setError,
  258. 'detacher',
  259. 'detached'
  260. )
  261. const setPosition = useDetachAction(
  262. 'setPosition',
  263. _setPosition,
  264. 'detached',
  265. 'detacher'
  266. )
  267. const firstRenderDone = useDetachAction(
  268. 'firstRenderDone',
  269. _firstRenderDone,
  270. 'detached',
  271. 'detacher'
  272. )
  273. const setHasLintingError = useDetachAction(
  274. 'setHasLintingError',
  275. _setHasLintingError,
  276. 'detacher',
  277. 'detached'
  278. )
  279. const setHighlights = useDetachAction(
  280. 'setHighlights',
  281. _setHighlights,
  282. 'detacher',
  283. 'detached'
  284. )
  285. const setShowCompileTimeWarning = useDetachAction(
  286. 'setShowCompileTimeWarning',
  287. _setShowCompileTimeWarning,
  288. 'detached',
  289. 'detacher'
  290. )
  291. const setShowLogs = useDetachAction(
  292. 'setShowLogs',
  293. _setShowLogs,
  294. 'detached',
  295. 'detacher'
  296. )
  297. const toggleLogs = useDetachAction(
  298. 'toggleLogs',
  299. _toggleLogs,
  300. 'detached',
  301. 'detacher'
  302. )
  303. const setStopOnFirstError = useDetachAction(
  304. 'setStopOnFirstError',
  305. _setStopOnFirstError,
  306. 'detached',
  307. 'detacher'
  308. )
  309. const setStopOnValidationError = useDetachAction(
  310. 'setStopOnValidationError',
  311. _setStopOnValidationError,
  312. 'detached',
  313. 'detacher'
  314. )
  315. const cleanupCompileResult = useDetachAction(
  316. 'cleanupCompileResult',
  317. _cleanupCompileResult,
  318. 'detached',
  319. 'detacher'
  320. )
  321. const recompileFromScratch = useDetachAction(
  322. 'recompileFromScratch',
  323. _recompileFromScratch,
  324. 'detached',
  325. 'detacher'
  326. )
  327. const setCompiling = useDetachAction(
  328. 'setCompiling',
  329. _setCompiling,
  330. 'detacher',
  331. 'detached'
  332. )
  333. const startCompile = useDetachAction(
  334. 'startCompile',
  335. _startCompile,
  336. 'detached',
  337. 'detacher'
  338. )
  339. const stopCompile = useDetachAction(
  340. 'stopCompile',
  341. _stopCompile,
  342. 'detached',
  343. 'detacher'
  344. )
  345. const setChangedAt = useDetachAction(
  346. 'setChangedAt',
  347. _setChangedAt,
  348. 'detached',
  349. 'detacher'
  350. )
  351. const setSavedAt = useDetachAction(
  352. 'setSavedAt',
  353. _setSavedAt,
  354. 'detached',
  355. 'detacher'
  356. )
  357. const clearCache = useDetachAction(
  358. 'clearCache',
  359. _clearCache,
  360. 'detached',
  361. 'detacher'
  362. )
  363. const syncToEntry = useDetachAction(
  364. 'sync-to-entry',
  365. _syncToEntry,
  366. 'detached',
  367. 'detacher'
  368. )
  369. useCompileTriggers(startCompile, setChangedAt, setSavedAt)
  370. useEffect(() => {
  371. // Sync the split test variant across the editor and pdf-detach.
  372. const variants = getMeta('ol-splitTestVariants') || {}
  373. variants['force-new-compile-domain'] = forceNewDomainVariant
  374. }, [forceNewDomainVariant])
  375. const value = useMemo(
  376. () => ({
  377. animateCompileDropdownArrow,
  378. autoCompile,
  379. clearCache,
  380. clearingCache,
  381. clsiServerId,
  382. codeCheckFailed,
  383. compiling,
  384. deliveryLatencies,
  385. draft,
  386. editedSinceCompileStarted,
  387. error,
  388. fileList,
  389. forceNewDomainVariant,
  390. hasChanges,
  391. highlights,
  392. lastCompileOptions,
  393. logEntryAnnotations,
  394. logEntries,
  395. pdfDownloadUrl: pdfFile?.pdfDownloadUrl,
  396. pdfFile,
  397. pdfUrl: pdfFile?.pdfUrl,
  398. pdfViewer,
  399. position,
  400. rawLog,
  401. recompileFromScratch,
  402. setAnimateCompileDropdownArrow,
  403. setAutoCompile,
  404. setCompiling,
  405. setDraft,
  406. setError,
  407. setHasLintingError,
  408. setHighlights,
  409. setPosition,
  410. setShowCompileTimeWarning,
  411. setShowLogs,
  412. toggleLogs,
  413. setStopOnFirstError,
  414. setStopOnValidationError,
  415. showLogs,
  416. showCompileTimeWarning,
  417. showFasterCompilesFeedbackUI,
  418. startCompile,
  419. stopCompile,
  420. stopOnFirstError,
  421. stopOnValidationError,
  422. stoppedOnFirstError,
  423. uncompiled,
  424. validationIssues,
  425. firstRenderDone,
  426. setChangedAt,
  427. cleanupCompileResult,
  428. syncToEntry,
  429. }),
  430. [
  431. animateCompileDropdownArrow,
  432. autoCompile,
  433. clearCache,
  434. clearingCache,
  435. clsiServerId,
  436. codeCheckFailed,
  437. compiling,
  438. deliveryLatencies,
  439. draft,
  440. error,
  441. editedSinceCompileStarted,
  442. fileList,
  443. forceNewDomainVariant,
  444. hasChanges,
  445. highlights,
  446. lastCompileOptions,
  447. logEntryAnnotations,
  448. logEntries,
  449. pdfFile,
  450. pdfViewer,
  451. position,
  452. rawLog,
  453. recompileFromScratch,
  454. setAnimateCompileDropdownArrow,
  455. setAutoCompile,
  456. setCompiling,
  457. setDraft,
  458. setError,
  459. setHasLintingError,
  460. setHighlights,
  461. setPosition,
  462. setShowCompileTimeWarning,
  463. setShowLogs,
  464. toggleLogs,
  465. setStopOnFirstError,
  466. setStopOnValidationError,
  467. showCompileTimeWarning,
  468. showLogs,
  469. showFasterCompilesFeedbackUI,
  470. startCompile,
  471. stopCompile,
  472. stopOnFirstError,
  473. stopOnValidationError,
  474. stoppedOnFirstError,
  475. uncompiled,
  476. validationIssues,
  477. firstRenderDone,
  478. setChangedAt,
  479. cleanupCompileResult,
  480. syncToEntry,
  481. ]
  482. )
  483. return (
  484. <DetachCompileContext.Provider value={value}>
  485. {children}
  486. </DetachCompileContext.Provider>
  487. )
  488. }
  489. DetachCompileProvider.propTypes = {
  490. children: PropTypes.any,
  491. }
  492. export function useDetachCompileContext(propTypes) {
  493. const data = useContext(DetachCompileContext)
  494. PropTypes.checkPropTypes(
  495. propTypes,
  496. data,
  497. 'data',
  498. 'DetachCompileContext.Provider'
  499. )
  500. return data
  501. }