local-compile-context.tsx 22 KB

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