compile-context.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { createContext, useContext } from 'react'
  2. import PropTypes from 'prop-types'
  3. import useScopeValue from './util/scope-value-hook'
  4. export const CompileContext = createContext()
  5. CompileContext.Provider.propTypes = {
  6. value: PropTypes.shape({
  7. pdfUrl: PropTypes.string,
  8. pdfDownloadUrl: PropTypes.string,
  9. logEntries: PropTypes.object,
  10. uncompiled: PropTypes.bool,
  11. }),
  12. }
  13. export function CompileProvider({ children }) {
  14. const [pdfUrl] = useScopeValue('pdf.url')
  15. const [pdfDownloadUrl] = useScopeValue('pdf.downloadUrl')
  16. const [logEntries] = useScopeValue('pdf.logEntries')
  17. const [uncompiled] = useScopeValue('pdf.uncompiled')
  18. const value = {
  19. pdfUrl,
  20. pdfDownloadUrl,
  21. logEntries,
  22. uncompiled,
  23. }
  24. return (
  25. <CompileContext.Provider value={value}>{children}</CompileContext.Provider>
  26. )
  27. }
  28. CompileProvider.propTypes = {
  29. children: PropTypes.any,
  30. }
  31. export function useCompileContext(propTypes) {
  32. const data = useContext(CompileContext)
  33. PropTypes.checkPropTypes(propTypes, data, 'data', 'CompileContext.Provider')
  34. return data
  35. }