local-compile-context.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. import {
  2. FC,
  3. createContext,
  4. useCallback,
  5. useContext,
  6. useEffect,
  7. useMemo,
  8. useRef,
  9. useState,
  10. Dispatch,
  11. SetStateAction,
  12. } from 'react'
  13. import useScopeValue from '../hooks/use-scope-value'
  14. import useScopeValueSetterOnly from '../hooks/use-scope-value-setter-only'
  15. import usePersistedState from '../hooks/use-persisted-state'
  16. import useAbortController from '../hooks/use-abort-controller'
  17. import DocumentCompiler from '../../features/pdf-preview/util/compiler'
  18. import {
  19. send,
  20. sendMB,
  21. sendMBOnce,
  22. sendMBSampled,
  23. } from '../../infrastructure/event-tracking'
  24. import {
  25. buildLogEntryAnnotations,
  26. buildRuleCounts,
  27. buildRuleDeltas,
  28. handleLogFiles,
  29. handleOutputFiles,
  30. } from '../../features/pdf-preview/util/output-files'
  31. import { useIdeContext } from './ide-context'
  32. import { useProjectContext } from './project-context'
  33. import { useEditorContext } from './editor-context'
  34. import { buildFileList } from '../../features/pdf-preview/util/file-list'
  35. import { useLayoutContext } from './layout-context'
  36. import { useUserContext } from './user-context'
  37. import { useFileTreeData } from '@/shared/context/file-tree-data-context'
  38. import { useFileTreePathContext } from '@/features/file-tree/contexts/file-tree-path'
  39. import { useUserSettingsContext } from '@/shared/context/user-settings-context'
  40. import { useFeatureFlag } from '@/shared/context/split-test-context'
  41. import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
  42. import {
  43. PdfScrollPosition,
  44. usePdfScrollPosition,
  45. } from '@/shared/hooks/use-pdf-scroll-position'
  46. type PdfFile = Record<string, any>
  47. export type CompileContext = {
  48. autoCompile: boolean
  49. clearingCache: boolean
  50. clsiServerId?: string
  51. codeCheckFailed: boolean
  52. compiling: boolean
  53. deliveryLatencies: Record<string, any>
  54. draft: boolean
  55. error?: string
  56. fileList?: Record<string, any>
  57. hasChanges: boolean
  58. hasShortCompileTimeout: boolean
  59. highlights?: Record<string, any>[]
  60. isProjectOwner: boolean
  61. logEntries?: Record<string, any>
  62. logEntryAnnotations?: Record<string, any>
  63. outputFilesArchive?: string
  64. pdfDownloadUrl?: string
  65. pdfFile?: PdfFile
  66. pdfUrl?: string
  67. pdfViewer?: string
  68. position?: PdfScrollPosition
  69. rawLog?: string
  70. setAutoCompile: (value: boolean) => void
  71. setDraft: (value: any) => void
  72. setError: (value: any) => void
  73. setHasLintingError: (value: any) => void // only for storybook
  74. setHighlights: (value: any) => void
  75. setPosition: Dispatch<SetStateAction<PdfScrollPosition>>
  76. setShowCompileTimeWarning: (value: any) => void
  77. setShowLogs: (value: boolean) => void
  78. toggleLogs: () => void
  79. setStopOnFirstError: (value: boolean) => void
  80. setStopOnValidationError: (value: boolean) => void
  81. showCompileTimeWarning: boolean
  82. showLogs: boolean
  83. stopOnFirstError: boolean
  84. stopOnValidationError: boolean
  85. stoppedOnFirstError: boolean
  86. uncompiled?: boolean
  87. validationIssues?: Record<string, any>
  88. firstRenderDone: (metrics: {
  89. latencyFetch: number
  90. latencyRender: number | undefined
  91. pdfCachingMetrics: { viewerId: string }
  92. }) => void
  93. cleanupCompileResult?: () => void
  94. animateCompileDropdownArrow: boolean
  95. editedSinceCompileStarted: boolean
  96. lastCompileOptions: any
  97. setAnimateCompileDropdownArrow: (value: boolean) => void
  98. recompileFromScratch: () => void
  99. setCompiling: (value: boolean) => void
  100. startCompile: (options?: any) => void
  101. stopCompile: () => void
  102. setChangedAt: (value: any) => void
  103. clearCache: () => void
  104. syncToEntry: (value: any, keepCurrentView?: boolean) => void
  105. recordAction: (action: string) => void
  106. }
  107. export const LocalCompileContext = createContext<CompileContext | undefined>(
  108. undefined
  109. )
  110. export const LocalCompileProvider: FC = ({ children }) => {
  111. const ide = useIdeContext()
  112. const { hasPremiumCompile, isProjectOwner } = useEditorContext()
  113. const { openDocId } = useEditorManagerContext()
  114. const { _id: projectId, rootDocId } = useProjectContext()
  115. const { pdfPreviewOpen } = useLayoutContext()
  116. const { features, alphaProgram, labsProgram } = useUserContext()
  117. const { fileTreeData } = useFileTreeData()
  118. const { findEntityByPath } = useFileTreePathContext()
  119. // whether a compile is in progress
  120. const [compiling, setCompiling] = useState(false)
  121. // whether to show the compile time warning
  122. const [showCompileTimeWarning, setShowCompileTimeWarning] = useState(false)
  123. const [hasShortCompileTimeout, setHasShortCompileTimeout] = useState(false)
  124. // the log entries parsed from the compile output log
  125. const [logEntries, setLogEntries] = useScopeValueSetterOnly('pdf.logEntries')
  126. // annotations for display in the editor, built from the log entries
  127. const [logEntryAnnotations, setLogEntryAnnotations] = useScopeValue(
  128. 'pdf.logEntryAnnotations'
  129. )
  130. // the PDF viewer and whether syntax validation is enabled globally
  131. const { userSettings } = useUserSettingsContext()
  132. const { pdfViewer, syntaxValidation } = userSettings
  133. // the URL for downloading the PDF
  134. const [, setPdfDownloadUrl] =
  135. useScopeValueSetterOnly<string>('pdf.downloadUrl')
  136. // the URL for loading the PDF in the preview pane
  137. const [, setPdfUrl] = useScopeValueSetterOnly<string>('pdf.url')
  138. // low level details for metrics
  139. const [pdfFile, setPdfFile] = useState<PdfFile | undefined>()
  140. useEffect(() => {
  141. setPdfDownloadUrl(pdfFile?.pdfDownloadUrl)
  142. setPdfUrl(pdfFile?.pdfUrl)
  143. }, [pdfFile, setPdfDownloadUrl, setPdfUrl])
  144. // the project is considered to be "uncompiled" if a doc has changed, or finished saving, since the last compile started.
  145. const [uncompiled, setUncompiled] = useScopeValue('pdf.uncompiled')
  146. // whether a doc has been edited since the last compile started
  147. const [editedSinceCompileStarted, setEditedSinceCompileStarted] =
  148. useState(false)
  149. // the id of the CLSI server which ran the compile
  150. const [clsiServerId, setClsiServerId] = useState()
  151. // data received in response to a compile request
  152. const [data, setData] = useState<Record<string, any>>()
  153. // the rootDocId used in the most recent compile request, which may not be the
  154. // same as the project rootDocId. This is used to calculate correct paths when
  155. // parsing the compile logs
  156. const lastCompileRootDocId = data ? (data.rootDocId ?? rootDocId) : null
  157. // callback to be invoked for PdfJsMetrics
  158. const [firstRenderDone, setFirstRenderDone] = useState(() => () => {})
  159. // latencies of compile/pdf download/rendering
  160. const [deliveryLatencies, setDeliveryLatencies] = useState({})
  161. // whether the project has been compiled yet
  162. const [compiledOnce, setCompiledOnce] = useState(false)
  163. // whether the cache is being cleared
  164. const [clearingCache, setClearingCache] = useState(false)
  165. // whether the logs should be visible
  166. const [showLogs, setShowLogs] = useState(false)
  167. // whether the compile dropdown arrow should be animated
  168. const [animateCompileDropdownArrow, setAnimateCompileDropdownArrow] =
  169. useState(false)
  170. const toggleLogs = useCallback(() => {
  171. setShowLogs(prev => {
  172. if (!prev) {
  173. sendMBOnce('ide-open-logs-once')
  174. }
  175. return !prev
  176. })
  177. }, [setShowLogs])
  178. // an error that occurred
  179. const [error, setError] = useState<string>()
  180. // the list of files that can be downloaded
  181. const [fileList, setFileList] = useState<Record<string, any[]>>()
  182. // the raw contents of the log file
  183. const [rawLog, setRawLog] = useState<string>()
  184. // validation issues from CLSI
  185. const [validationIssues, setValidationIssues] = useState()
  186. // areas to highlight on the PDF, from synctex
  187. const [highlights, setHighlights] = useState()
  188. const [position, setPosition] = usePdfScrollPosition(lastCompileRootDocId)
  189. // whether autocompile is switched on
  190. const [autoCompile, setAutoCompile] = usePersistedState(
  191. `autocompile_enabled:${projectId}`,
  192. false,
  193. true
  194. )
  195. // whether the compile should run in draft mode
  196. const [draft, setDraft] = usePersistedState(`draft:${projectId}`, false, true)
  197. // whether compiling should stop on first error
  198. const [stopOnFirstError, setStopOnFirstError] = usePersistedState(
  199. `stop_on_first_error:${projectId}`,
  200. false,
  201. true
  202. )
  203. // whether the last compiles stopped on first error
  204. const [stoppedOnFirstError, setStoppedOnFirstError] = useState(false)
  205. // whether compiling should be prevented if there are linting errors
  206. const [stopOnValidationError, setStopOnValidationError] = usePersistedState(
  207. `stop_on_validation_error:${projectId}`,
  208. true,
  209. true
  210. )
  211. // the Document currently open in the editor
  212. const [currentDoc] = useScopeValue('editor.sharejs_doc')
  213. // whether the editor linter found errors
  214. const [hasLintingError, setHasLintingError] = useScopeValue('hasLintingError')
  215. // the timestamp that a doc was last changed
  216. const [changedAt, setChangedAt] = useState(0)
  217. const { signal } = useAbortController()
  218. const cleanupCompileResult = useCallback(() => {
  219. setPdfFile(undefined)
  220. setLogEntries(null)
  221. setLogEntryAnnotations({})
  222. }, [setPdfFile, setLogEntries, setLogEntryAnnotations])
  223. const compilingRef = useRef(false)
  224. useEffect(() => {
  225. compilingRef.current = compiling
  226. }, [compiling])
  227. const _buildLogEntryAnnotations = useCallback(
  228. entries =>
  229. buildLogEntryAnnotations(entries, fileTreeData, lastCompileRootDocId),
  230. [fileTreeData, lastCompileRootDocId]
  231. )
  232. const buildLogEntryAnnotationsRef = useRef(_buildLogEntryAnnotations)
  233. useEffect(() => {
  234. buildLogEntryAnnotationsRef.current = _buildLogEntryAnnotations
  235. }, [_buildLogEntryAnnotations])
  236. // the document compiler
  237. const [compiler] = useState(() => {
  238. return new DocumentCompiler({
  239. projectId,
  240. setChangedAt,
  241. setCompiling,
  242. setData,
  243. setFirstRenderDone,
  244. setDeliveryLatencies,
  245. setError,
  246. cleanupCompileResult,
  247. compilingRef,
  248. signal,
  249. })
  250. })
  251. // keep currentDoc in sync with the compiler
  252. useEffect(() => {
  253. compiler.currentDoc = currentDoc
  254. }, [compiler, currentDoc])
  255. // keep the project rootDocId in sync with the compiler
  256. useEffect(() => {
  257. compiler.projectRootDocId = rootDocId
  258. }, [compiler, rootDocId])
  259. // keep draft setting in sync with the compiler
  260. useEffect(() => {
  261. compiler.setOption('draft', draft)
  262. }, [compiler, draft])
  263. // keep stop on first error setting in sync with the compiler
  264. useEffect(() => {
  265. compiler.setOption('stopOnFirstError', stopOnFirstError)
  266. }, [compiler, stopOnFirstError])
  267. useEffect(() => {
  268. setUncompiled(changedAt > 0)
  269. }, [setUncompiled, changedAt])
  270. useEffect(() => {
  271. setEditedSinceCompileStarted(changedAt > 0)
  272. }, [setEditedSinceCompileStarted, changedAt])
  273. // always compile the PDF once after opening the project, after the doc has loaded
  274. useEffect(() => {
  275. if (!compiledOnce && currentDoc) {
  276. setCompiledOnce(true)
  277. compiler.compile({ isAutoCompileOnLoad: true })
  278. }
  279. }, [compiledOnce, currentDoc, compiler])
  280. useEffect(() => {
  281. setHasShortCompileTimeout(
  282. features?.compileTimeout !== undefined && features.compileTimeout <= 60
  283. )
  284. }, [features])
  285. useEffect(() => {
  286. if (hasShortCompileTimeout && compiling && isProjectOwner) {
  287. const timeout = window.setTimeout(() => {
  288. setShowCompileTimeWarning(true)
  289. }, 30000)
  290. return () => {
  291. window.clearTimeout(timeout)
  292. }
  293. }
  294. }, [compiling, isProjectOwner, hasShortCompileTimeout])
  295. const hasCompileLogsEvents = useFeatureFlag('compile-log-events')
  296. // compare log entry counts with the previous compile, and record actions between compiles
  297. // these are refs rather than state so they don't trigger the effect to run
  298. const previousRuleCountsRef = useRef<{
  299. ruleCounts: Record<string, number>
  300. rootDocId: string
  301. } | null>(null)
  302. const recordedActionsRef = useRef<Record<string, boolean>>({})
  303. const recordAction = useCallback((action: string) => {
  304. recordedActionsRef.current[action] = true
  305. }, [])
  306. // handle the data returned from a compile request
  307. // note: this should _only_ run when `data` changes,
  308. // the other dependencies must all be static
  309. useEffect(() => {
  310. const abortController = new AbortController()
  311. const recordedActions = recordedActionsRef.current
  312. recordedActionsRef.current = {}
  313. if (data) {
  314. if (data.clsiServerId) {
  315. setClsiServerId(data.clsiServerId) // set in scope, for PdfSynctexController
  316. }
  317. if (data.outputFiles) {
  318. const outputFiles = new Map()
  319. for (const outputFile of data.outputFiles) {
  320. outputFiles.set(outputFile.path, outputFile)
  321. }
  322. // set the PDF context
  323. if (data.status === 'success') {
  324. setPdfFile(handleOutputFiles(outputFiles, projectId, data))
  325. }
  326. setFileList(
  327. buildFileList(
  328. outputFiles,
  329. data.clsiServerId,
  330. data.compileGroup,
  331. data.outputFilesArchive
  332. )
  333. )
  334. // handle log files
  335. // asynchronous (TODO: cancel on new compile?)
  336. setLogEntryAnnotations(null)
  337. setLogEntries(null)
  338. setRawLog(undefined)
  339. handleLogFiles(outputFiles, data, abortController.signal).then(
  340. (result: Record<string, any>) => {
  341. setRawLog(result.log)
  342. setLogEntries(result.logEntries)
  343. setLogEntryAnnotations(
  344. buildLogEntryAnnotationsRef.current(result.logEntries.all)
  345. )
  346. // sample compile stats for real users
  347. if (!alphaProgram) {
  348. if (['success', 'stopped-on-first-error'].includes(data.status)) {
  349. sendMBSampled(
  350. 'compile-result',
  351. {
  352. errors: result.logEntries.errors.length,
  353. warnings: result.logEntries.warnings.length,
  354. typesetting: result.logEntries.typesetting.length,
  355. newPdfPreview: true, // TODO: is this useful?
  356. stopOnFirstError: data.options.stopOnFirstError,
  357. },
  358. 0.01
  359. )
  360. }
  361. if (hasCompileLogsEvents || labsProgram) {
  362. const ruleCounts = buildRuleCounts(
  363. result.logEntries.all
  364. ) as Record<string, number>
  365. const rootDocId = data.rootDocId || compiler.projectRootDocId
  366. const previousRuleCounts = previousRuleCountsRef.current
  367. previousRuleCountsRef.current = { ruleCounts, rootDocId }
  368. const ruleDeltas =
  369. previousRuleCounts &&
  370. previousRuleCounts.rootDocId === rootDocId
  371. ? buildRuleDeltas(ruleCounts, previousRuleCounts.ruleCounts)
  372. : {}
  373. sendMB('compile-log-entries', {
  374. status: data.status,
  375. stopOnFirstError: data.options.stopOnFirstError,
  376. isAutoCompileOnLoad: !!data.options.isAutoCompileOnLoad,
  377. isAutoCompileOnChange: !!data.options.isAutoCompileOnChange,
  378. rootDocId,
  379. ...recordedActions,
  380. ...ruleCounts,
  381. ...ruleDeltas,
  382. })
  383. }
  384. }
  385. }
  386. )
  387. }
  388. switch (data.status) {
  389. case 'success':
  390. setError(undefined)
  391. setShowLogs(false)
  392. break
  393. case 'stopped-on-first-error':
  394. setError(undefined)
  395. setShowLogs(true)
  396. break
  397. case 'clsi-maintenance':
  398. case 'compile-in-progress':
  399. case 'exited':
  400. case 'failure':
  401. case 'project-too-large':
  402. case 'rate-limited':
  403. case 'terminated':
  404. case 'too-recently-compiled':
  405. setError(data.status)
  406. break
  407. case 'timedout':
  408. setError('timedout')
  409. if (!hasPremiumCompile && isProjectOwner) {
  410. send(
  411. 'subscription-funnel',
  412. 'editor-click-feature',
  413. 'compile-timeout'
  414. )
  415. }
  416. break
  417. case 'autocompile-backoff':
  418. if (!data.options.isAutoCompileOnLoad) {
  419. setError('autocompile-disabled')
  420. setAutoCompile(false)
  421. }
  422. break
  423. case 'unavailable':
  424. setError('clsi-unavailable')
  425. break
  426. case 'validation-problems':
  427. setError('validation-problems')
  428. setValidationIssues(data.validationProblems)
  429. break
  430. default:
  431. setError('error')
  432. break
  433. }
  434. setStoppedOnFirstError(data.status === 'stopped-on-first-error')
  435. }
  436. return () => {
  437. abortController.abort()
  438. }
  439. }, [
  440. data,
  441. ide,
  442. alphaProgram,
  443. labsProgram,
  444. features,
  445. hasCompileLogsEvents,
  446. hasPremiumCompile,
  447. isProjectOwner,
  448. projectId,
  449. setAutoCompile,
  450. setClsiServerId,
  451. setLogEntries,
  452. setLogEntryAnnotations,
  453. setPdfFile,
  454. compiler,
  455. ])
  456. // switch to logs if there's an error
  457. useEffect(() => {
  458. if (error) {
  459. setShowLogs(true)
  460. }
  461. }, [error])
  462. // whether there has been an autocompile linting error, if syntax validation is switched on
  463. const autoCompileLintingError = Boolean(
  464. autoCompile && syntaxValidation && hasLintingError
  465. )
  466. const codeCheckFailed = stopOnValidationError && autoCompileLintingError
  467. // the project is available for auto-compiling
  468. // (autocompile is enabled, the PDF preview is open, and the code check (if enabled) hasn't failed)
  469. const canAutoCompile = Boolean(
  470. autoCompile && pdfPreviewOpen && !codeCheckFailed
  471. )
  472. // show that the project has pending changes
  473. const hasChanges = Boolean(canAutoCompile && uncompiled && compiledOnce)
  474. // call the debounced autocompile function if the project is available for auto-compiling and it has changed
  475. useEffect(() => {
  476. if (canAutoCompile) {
  477. if (changedAt > 0) {
  478. compiler.debouncedAutoCompile()
  479. }
  480. } else {
  481. compiler.debouncedAutoCompile.cancel()
  482. }
  483. }, [compiler, canAutoCompile, changedAt])
  484. // cancel debounced recompile on unmount
  485. useEffect(() => {
  486. return () => {
  487. compiler.debouncedAutoCompile.cancel()
  488. }
  489. }, [compiler])
  490. // start a compile manually
  491. const startCompile = useCallback(
  492. options => {
  493. compiler.compile(options)
  494. },
  495. [compiler]
  496. )
  497. // stop a compile manually
  498. const stopCompile = useCallback(() => {
  499. compiler.stopCompile()
  500. }, [compiler])
  501. // clear the compile cache
  502. const clearCache = useCallback(() => {
  503. setClearingCache(true)
  504. return compiler
  505. .clearCache()
  506. .then(() => {
  507. setFileList(undefined)
  508. setPdfFile(undefined)
  509. })
  510. .finally(() => {
  511. setClearingCache(false)
  512. })
  513. }, [compiler])
  514. const syncToEntry = useCallback(
  515. (entry, keepCurrentView = false) => {
  516. const result = findEntityByPath(entry.file)
  517. if (result && result.type === 'doc') {
  518. openDocId(result.entity._id, {
  519. gotoLine: entry.line ?? undefined,
  520. gotoColumn: entry.column ?? undefined,
  521. keepCurrentView,
  522. })
  523. }
  524. },
  525. [findEntityByPath, openDocId]
  526. )
  527. // clear the cache then run a compile, triggered by a menu item
  528. const recompileFromScratch = useCallback(() => {
  529. clearCache().then(() => {
  530. compiler.compile()
  531. })
  532. }, [clearCache, compiler])
  533. // After a compile, the compiler sets `data.options` to the options that were
  534. // used for that compile.
  535. const lastCompileOptions = useMemo(() => data?.options || {}, [data])
  536. useEffect(() => {
  537. const listener = (event: Event) => {
  538. setShowLogs((event as CustomEvent<boolean>).detail as boolean)
  539. }
  540. window.addEventListener('editor:show-logs', listener)
  541. return () => {
  542. window.removeEventListener('editor:show-logs', listener)
  543. }
  544. }, [])
  545. const value = useMemo(
  546. () => ({
  547. animateCompileDropdownArrow,
  548. autoCompile,
  549. clearCache,
  550. clearingCache,
  551. clsiServerId,
  552. codeCheckFailed,
  553. compiling,
  554. deliveryLatencies,
  555. draft,
  556. editedSinceCompileStarted,
  557. error,
  558. fileList,
  559. hasChanges,
  560. hasShortCompileTimeout,
  561. highlights,
  562. isProjectOwner,
  563. lastCompileOptions,
  564. logEntryAnnotations,
  565. logEntries,
  566. pdfDownloadUrl: pdfFile?.pdfDownloadUrl,
  567. pdfFile,
  568. pdfUrl: pdfFile?.pdfUrl,
  569. pdfViewer,
  570. position,
  571. rawLog,
  572. recompileFromScratch,
  573. setAnimateCompileDropdownArrow,
  574. setAutoCompile,
  575. setCompiling,
  576. setDraft,
  577. setError,
  578. setHasLintingError, // only for stories
  579. setHighlights,
  580. setPosition,
  581. showCompileTimeWarning,
  582. setShowCompileTimeWarning,
  583. setShowLogs,
  584. toggleLogs,
  585. setStopOnFirstError,
  586. setStopOnValidationError,
  587. showLogs,
  588. startCompile,
  589. stopCompile,
  590. stopOnFirstError,
  591. stopOnValidationError,
  592. stoppedOnFirstError,
  593. uncompiled,
  594. validationIssues,
  595. firstRenderDone,
  596. setChangedAt,
  597. cleanupCompileResult,
  598. syncToEntry,
  599. recordAction,
  600. }),
  601. [
  602. animateCompileDropdownArrow,
  603. autoCompile,
  604. clearCache,
  605. clearingCache,
  606. clsiServerId,
  607. codeCheckFailed,
  608. compiling,
  609. deliveryLatencies,
  610. draft,
  611. editedSinceCompileStarted,
  612. error,
  613. fileList,
  614. hasChanges,
  615. hasShortCompileTimeout,
  616. highlights,
  617. isProjectOwner,
  618. lastCompileOptions,
  619. logEntries,
  620. logEntryAnnotations,
  621. position,
  622. pdfFile,
  623. pdfViewer,
  624. rawLog,
  625. recompileFromScratch,
  626. setAnimateCompileDropdownArrow,
  627. setAutoCompile,
  628. setDraft,
  629. setError,
  630. setHasLintingError, // only for stories
  631. setHighlights,
  632. setPosition,
  633. setShowCompileTimeWarning,
  634. setStopOnFirstError,
  635. setStopOnValidationError,
  636. showCompileTimeWarning,
  637. showLogs,
  638. startCompile,
  639. stopCompile,
  640. stopOnFirstError,
  641. stopOnValidationError,
  642. stoppedOnFirstError,
  643. uncompiled,
  644. validationIssues,
  645. firstRenderDone,
  646. setChangedAt,
  647. cleanupCompileResult,
  648. setShowLogs,
  649. toggleLogs,
  650. syncToEntry,
  651. recordAction,
  652. ]
  653. )
  654. return (
  655. <LocalCompileContext.Provider value={value}>
  656. {children}
  657. </LocalCompileContext.Provider>
  658. )
  659. }
  660. export function useLocalCompileContext() {
  661. const context = useContext(LocalCompileContext)
  662. if (!context) {
  663. throw new Error(
  664. 'useLocalCompileContext is only available inside LocalCompileProvider'
  665. )
  666. }
  667. return context
  668. }