compile-context.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { createContext, useContext, useMemo } from 'react'
  2. import PropTypes from 'prop-types'
  3. import useScopeValue from '../hooks/use-scope-value'
  4. export const CompileContext = createContext()
  5. CompileContext.Provider.propTypes = {
  6. value: PropTypes.shape({
  7. clsiServerId: PropTypes.string,
  8. logEntries: PropTypes.object,
  9. logEntryAnnotations: PropTypes.object,
  10. pdfDownloadUrl: PropTypes.string,
  11. pdfUrl: PropTypes.string,
  12. setClsiServerId: PropTypes.func.isRequired,
  13. setLogEntries: PropTypes.func.isRequired,
  14. setLogEntryAnnotations: PropTypes.func.isRequired,
  15. setPdfDownloadUrl: PropTypes.func.isRequired,
  16. setPdfUrl: PropTypes.func.isRequired,
  17. setUncompiled: PropTypes.func.isRequired,
  18. uncompiled: PropTypes.bool,
  19. }),
  20. }
  21. export function CompileProvider({ children }) {
  22. // the log entries parsed from the compile output log
  23. const [logEntries, setLogEntries] = useScopeValue('pdf.logEntries')
  24. // annotations for display in the editor, built from the log entries
  25. const [logEntryAnnotations, setLogEntryAnnotations] = useScopeValue(
  26. 'pdf.logEntryAnnotations'
  27. )
  28. // the URL for downloading the PDF
  29. const [pdfDownloadUrl, setPdfDownloadUrl] = useScopeValue('pdf.downloadUrl')
  30. // the URL for loading the PDF in the preview pane
  31. const [pdfUrl, setPdfUrl] = useScopeValue('pdf.url')
  32. // the project is considered to be "uncompiled" if a doc has changed since the last compile started
  33. const [uncompiled, setUncompiled] = useScopeValue('pdf.uncompiled')
  34. // the id of the CLSI server which ran the compile
  35. const [clsiServerId, setClsiServerId] = useScopeValue('pdf.clsiServerId')
  36. const value = useMemo(
  37. () => ({
  38. clsiServerId,
  39. logEntries,
  40. logEntryAnnotations,
  41. pdfDownloadUrl,
  42. pdfUrl,
  43. setClsiServerId,
  44. setLogEntries,
  45. setLogEntryAnnotations,
  46. setPdfDownloadUrl,
  47. setPdfUrl,
  48. setUncompiled,
  49. uncompiled,
  50. }),
  51. [
  52. clsiServerId,
  53. logEntries,
  54. logEntryAnnotations,
  55. pdfDownloadUrl,
  56. pdfUrl,
  57. setClsiServerId,
  58. setLogEntries,
  59. setLogEntryAnnotations,
  60. setPdfDownloadUrl,
  61. setPdfUrl,
  62. setUncompiled,
  63. uncompiled,
  64. ]
  65. )
  66. return (
  67. <CompileContext.Provider value={value}>{children}</CompileContext.Provider>
  68. )
  69. }
  70. CompileProvider.propTypes = {
  71. children: PropTypes.any,
  72. }
  73. export function useCompileContext(propTypes) {
  74. const data = useContext(CompileContext)
  75. PropTypes.checkPropTypes(propTypes, data, 'data', 'CompileContext.Provider')
  76. return data
  77. }