compile-context.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import React, { 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, $scope }) {
  14. const [pdfUrl] = useScopeValue('pdf.url', $scope)
  15. const [pdfDownloadUrl] = useScopeValue('pdf.downloadUrl', $scope)
  16. const [logEntries] = useScopeValue('pdf.logEntries', $scope)
  17. const [uncompiled] = useScopeValue('pdf.uncompiled', $scope)
  18. const value = {
  19. pdfUrl,
  20. pdfDownloadUrl,
  21. logEntries,
  22. uncompiled,
  23. }
  24. return (
  25. <>
  26. <CompileContext.Provider value={value}>
  27. {children}
  28. </CompileContext.Provider>
  29. </>
  30. )
  31. }
  32. CompileProvider.propTypes = {
  33. children: PropTypes.any,
  34. $scope: PropTypes.any.isRequired,
  35. }
  36. export function useCompileContext(propTypes) {
  37. const data = useContext(CompileContext)
  38. PropTypes.checkPropTypes(propTypes, data, 'data', 'CompileContext.Provider')
  39. return data
  40. }