local-compile-context.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 { openDocWithId, openDocs } = 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. openDocs,
  250. })
  251. })
  252. // keep currentDoc in sync with the compiler
  253. useEffect(() => {
  254. compiler.currentDoc = currentDoc
  255. }, [compiler, currentDoc])
  256. // keep the project rootDocId in sync with the compiler
  257. useEffect(() => {
  258. compiler.projectRootDocId = rootDocId
  259. }, [compiler, rootDocId])
  260. // keep draft setting in sync with the compiler
  261. useEffect(() => {
  262. compiler.setOption('draft', draft)
  263. }, [compiler, draft])
  264. // keep stop on first error setting in sync with the compiler
  265. useEffect(() => {
  266. compiler.setOption('stopOnFirstError', stopOnFirstError)
  267. }, [compiler, stopOnFirstError])
  268. useEffect(() => {
  269. setUncompiled(changedAt > 0)
  270. }, [setUncompiled, changedAt])
  271. useEffect(() => {
  272. setEditedSinceCompileStarted(changedAt > 0)
  273. }, [setEditedSinceCompileStarted, changedAt])
  274. // always compile the PDF once after opening the project, after the doc has loaded
  275. useEffect(() => {
  276. if (!compiledOnce && currentDoc) {
  277. setCompiledOnce(true)
  278. compiler.compile({ isAutoCompileOnLoad: true })
  279. }
  280. }, [compiledOnce, currentDoc, compiler])
  281. useEffect(() => {
  282. setHasShortCompileTimeout(
  283. features?.compileTimeout !== undefined && features.compileTimeout <= 60
  284. )
  285. }, [features])
  286. useEffect(() => {
  287. if (hasShortCompileTimeout && compiling && isProjectOwner) {
  288. const timeout = window.setTimeout(() => {
  289. setShowCompileTimeWarning(true)
  290. }, 30000)
  291. return () => {
  292. window.clearTimeout(timeout)
  293. }
  294. }
  295. }, [compiling, isProjectOwner, hasShortCompileTimeout])
  296. const hasCompileLogsEvents = useFeatureFlag('compile-log-events')
  297. // compare log entry counts with the previous compile, and record actions between compiles
  298. // these are refs rather than state so they don't trigger the effect to run
  299. const previousRuleCountsRef = useRef<{
  300. ruleCounts: Record<string, number>
  301. rootDocId: string
  302. } | null>(null)
  303. const recordedActionsRef = useRef<Record<string, boolean>>({})
  304. const recordAction = useCallback((action: string) => {
  305. recordedActionsRef.current[action] = true
  306. }, [])
  307. // handle the data returned from a compile request
  308. // note: this should _only_ run when `data` changes,
  309. // the other dependencies must all be static
  310. useEffect(() => {
  311. const abortController = new AbortController()
  312. const recordedActions = recordedActionsRef.current
  313. recordedActionsRef.current = {}
  314. if (data) {
  315. if (data.clsiServerId) {
  316. setClsiServerId(data.clsiServerId) // set in scope, for PdfSynctexController
  317. }
  318. if (data.outputFiles) {
  319. const outputFiles = new Map()
  320. for (const outputFile of data.outputFiles) {
  321. outputFiles.set(outputFile.path, outputFile)
  322. }
  323. // set the PDF context
  324. if (data.status === 'success') {
  325. setPdfFile(handleOutputFiles(outputFiles, projectId, data))
  326. }
  327. setFileList(
  328. buildFileList(
  329. outputFiles,
  330. data.clsiServerId,
  331. data.compileGroup,
  332. data.outputFilesArchive
  333. )
  334. )
  335. // handle log files
  336. // asynchronous (TODO: cancel on new compile?)
  337. setLogEntryAnnotations(null)
  338. setLogEntries(null)
  339. setRawLog(undefined)
  340. handleLogFiles(outputFiles, data, abortController.signal).then(
  341. (result: Record<string, any>) => {
  342. setRawLog(result.log)
  343. setLogEntries(result.logEntries)
  344. setLogEntryAnnotations(
  345. buildLogEntryAnnotationsRef.current(result.logEntries.all)
  346. )
  347. // sample compile stats for real users
  348. if (!alphaProgram) {
  349. if (['success', 'stopped-on-first-error'].includes(data.status)) {
  350. sendMBSampled(
  351. 'compile-result',
  352. {
  353. errors: result.logEntries.errors.length,
  354. warnings: result.logEntries.warnings.length,
  355. typesetting: result.logEntries.typesetting.length,
  356. newPdfPreview: true, // TODO: is this useful?
  357. stopOnFirstError: data.options.stopOnFirstError,
  358. },
  359. 0.01
  360. )
  361. }
  362. if (hasCompileLogsEvents || labsProgram) {
  363. const ruleCounts = buildRuleCounts(
  364. result.logEntries.all
  365. ) as Record<string, number>
  366. const rootDocId = data.rootDocId || compiler.projectRootDocId
  367. const previousRuleCounts = previousRuleCountsRef.current
  368. previousRuleCountsRef.current = { ruleCounts, rootDocId }
  369. const ruleDeltas =
  370. previousRuleCounts &&
  371. previousRuleCounts.rootDocId === rootDocId
  372. ? buildRuleDeltas(ruleCounts, previousRuleCounts.ruleCounts)
  373. : {}
  374. sendMB('compile-log-entries', {
  375. status: data.status,
  376. stopOnFirstError: data.options.stopOnFirstError,
  377. isAutoCompileOnLoad: !!data.options.isAutoCompileOnLoad,
  378. isAutoCompileOnChange: !!data.options.isAutoCompileOnChange,
  379. rootDocId,
  380. ...recordedActions,
  381. ...ruleCounts,
  382. ...ruleDeltas,
  383. })
  384. }
  385. }
  386. }
  387. )
  388. }
  389. switch (data.status) {
  390. case 'success':
  391. setError(undefined)
  392. setShowLogs(false)
  393. break
  394. case 'stopped-on-first-error':
  395. setError(undefined)
  396. setShowLogs(true)
  397. break
  398. case 'clsi-maintenance':
  399. case 'compile-in-progress':
  400. case 'exited':
  401. case 'failure':
  402. case 'project-too-large':
  403. case 'rate-limited':
  404. case 'terminated':
  405. case 'too-recently-compiled':
  406. setError(data.status)
  407. break
  408. case 'timedout':
  409. setError('timedout')
  410. if (!hasPremiumCompile && isProjectOwner) {
  411. send(
  412. 'subscription-funnel',
  413. 'editor-click-feature',
  414. 'compile-timeout'
  415. )
  416. }
  417. break
  418. case 'autocompile-backoff':
  419. if (!data.options.isAutoCompileOnLoad) {
  420. setError('autocompile-disabled')
  421. setAutoCompile(false)
  422. }
  423. break
  424. case 'unavailable':
  425. setError('clsi-unavailable')
  426. break
  427. case 'validation-problems':
  428. setError('validation-problems')
  429. setValidationIssues(data.validationProblems)
  430. break
  431. default:
  432. setError('error')
  433. break
  434. }
  435. setStoppedOnFirstError(data.status === 'stopped-on-first-error')
  436. }
  437. return () => {
  438. abortController.abort()
  439. }
  440. }, [
  441. data,
  442. ide,
  443. alphaProgram,
  444. labsProgram,
  445. features,
  446. hasCompileLogsEvents,
  447. hasPremiumCompile,
  448. isProjectOwner,
  449. projectId,
  450. setAutoCompile,
  451. setClsiServerId,
  452. setLogEntries,
  453. setLogEntryAnnotations,
  454. setPdfFile,
  455. compiler,
  456. ])
  457. // switch to logs if there's an error
  458. useEffect(() => {
  459. if (error) {
  460. setShowLogs(true)
  461. }
  462. }, [error])
  463. // whether there has been an autocompile linting error, if syntax validation is switched on
  464. const autoCompileLintingError = Boolean(
  465. autoCompile && syntaxValidation && hasLintingError
  466. )
  467. const codeCheckFailed = stopOnValidationError && autoCompileLintingError
  468. // the project is available for auto-compiling
  469. // (autocompile is enabled, the PDF preview is open, and the code check (if enabled) hasn't failed)
  470. const canAutoCompile = Boolean(
  471. autoCompile && pdfPreviewOpen && !codeCheckFailed
  472. )
  473. // show that the project has pending changes
  474. const hasChanges = Boolean(canAutoCompile && uncompiled && compiledOnce)
  475. // call the debounced autocompile function if the project is available for auto-compiling and it has changed
  476. useEffect(() => {
  477. if (canAutoCompile) {
  478. if (changedAt > 0) {
  479. compiler.debouncedAutoCompile()
  480. }
  481. } else {
  482. compiler.debouncedAutoCompile.cancel()
  483. }
  484. }, [compiler, canAutoCompile, changedAt])
  485. // cancel debounced recompile on unmount
  486. useEffect(() => {
  487. return () => {
  488. compiler.debouncedAutoCompile.cancel()
  489. }
  490. }, [compiler])
  491. // start a compile manually
  492. const startCompile = useCallback(
  493. options => {
  494. compiler.compile(options)
  495. },
  496. [compiler]
  497. )
  498. // stop a compile manually
  499. const stopCompile = useCallback(() => {
  500. compiler.stopCompile()
  501. }, [compiler])
  502. // clear the compile cache
  503. const clearCache = useCallback(() => {
  504. setClearingCache(true)
  505. return compiler
  506. .clearCache()
  507. .then(() => {
  508. setFileList(undefined)
  509. setPdfFile(undefined)
  510. })
  511. .finally(() => {
  512. setClearingCache(false)
  513. })
  514. }, [compiler])
  515. const syncToEntry = useCallback(
  516. (entry, keepCurrentView = false) => {
  517. const result = findEntityByPath(entry.file)
  518. if (result && result.type === 'doc') {
  519. openDocWithId(result.entity._id, {
  520. gotoLine: entry.line ?? undefined,
  521. gotoColumn: entry.column ?? undefined,
  522. keepCurrentView,
  523. })
  524. }
  525. },
  526. [findEntityByPath, openDocWithId]
  527. )
  528. // clear the cache then run a compile, triggered by a menu item
  529. const recompileFromScratch = useCallback(() => {
  530. clearCache().then(() => {
  531. compiler.compile()
  532. })
  533. }, [clearCache, compiler])
  534. // After a compile, the compiler sets `data.options` to the options that were
  535. // used for that compile.
  536. const lastCompileOptions = useMemo(() => data?.options || {}, [data])
  537. useEffect(() => {
  538. const listener = (event: Event) => {
  539. setShowLogs((event as CustomEvent<boolean>).detail as boolean)
  540. }
  541. window.addEventListener('editor:show-logs', listener)
  542. return () => {
  543. window.removeEventListener('editor:show-logs', listener)
  544. }
  545. }, [])
  546. const value = useMemo(
  547. () => ({
  548. animateCompileDropdownArrow,
  549. autoCompile,
  550. clearCache,
  551. clearingCache,
  552. clsiServerId,
  553. codeCheckFailed,
  554. compiling,
  555. deliveryLatencies,
  556. draft,
  557. editedSinceCompileStarted,
  558. error,
  559. fileList,
  560. hasChanges,
  561. hasShortCompileTimeout,
  562. highlights,
  563. isProjectOwner,
  564. lastCompileOptions,
  565. logEntryAnnotations,
  566. logEntries,
  567. pdfDownloadUrl: pdfFile?.pdfDownloadUrl,
  568. pdfFile,
  569. pdfUrl: pdfFile?.pdfUrl,
  570. pdfViewer,
  571. position,
  572. rawLog,
  573. recompileFromScratch,
  574. setAnimateCompileDropdownArrow,
  575. setAutoCompile,
  576. setCompiling,
  577. setDraft,
  578. setError,
  579. setHasLintingError, // only for stories
  580. setHighlights,
  581. setPosition,
  582. showCompileTimeWarning,
  583. setShowCompileTimeWarning,
  584. setShowLogs,
  585. toggleLogs,
  586. setStopOnFirstError,
  587. setStopOnValidationError,
  588. showLogs,
  589. startCompile,
  590. stopCompile,
  591. stopOnFirstError,
  592. stopOnValidationError,
  593. stoppedOnFirstError,
  594. uncompiled,
  595. validationIssues,
  596. firstRenderDone,
  597. setChangedAt,
  598. cleanupCompileResult,
  599. syncToEntry,
  600. recordAction,
  601. }),
  602. [
  603. animateCompileDropdownArrow,
  604. autoCompile,
  605. clearCache,
  606. clearingCache,
  607. clsiServerId,
  608. codeCheckFailed,
  609. compiling,
  610. deliveryLatencies,
  611. draft,
  612. editedSinceCompileStarted,
  613. error,
  614. fileList,
  615. hasChanges,
  616. hasShortCompileTimeout,
  617. highlights,
  618. isProjectOwner,
  619. lastCompileOptions,
  620. logEntries,
  621. logEntryAnnotations,
  622. position,
  623. pdfFile,
  624. pdfViewer,
  625. rawLog,
  626. recompileFromScratch,
  627. setAnimateCompileDropdownArrow,
  628. setAutoCompile,
  629. setDraft,
  630. setError,
  631. setHasLintingError, // only for stories
  632. setHighlights,
  633. setPosition,
  634. setShowCompileTimeWarning,
  635. setStopOnFirstError,
  636. setStopOnValidationError,
  637. showCompileTimeWarning,
  638. showLogs,
  639. startCompile,
  640. stopCompile,
  641. stopOnFirstError,
  642. stopOnValidationError,
  643. stoppedOnFirstError,
  644. uncompiled,
  645. validationIssues,
  646. firstRenderDone,
  647. setChangedAt,
  648. cleanupCompileResult,
  649. setShowLogs,
  650. toggleLogs,
  651. syncToEntry,
  652. recordAction,
  653. ]
  654. )
  655. return (
  656. <LocalCompileContext.Provider value={value}>
  657. {children}
  658. </LocalCompileContext.Provider>
  659. )
  660. }
  661. export function useLocalCompileContext() {
  662. const context = useContext(LocalCompileContext)
  663. if (!context) {
  664. throw new Error(
  665. 'useLocalCompileContext is only available inside LocalCompileProvider'
  666. )
  667. }
  668. return context
  669. }