editor-context.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React, { createContext, useContext } from 'react'
  2. import PropTypes from 'prop-types'
  3. import useScopeValue from './util/scope-value-hook'
  4. export const EditorContext = createContext()
  5. EditorContext.Provider.propTypes = {
  6. value: PropTypes.shape({
  7. cobranding: PropTypes.shape({
  8. logoImgUrl: PropTypes.string.isRequired,
  9. brandVariationName: PropTypes.string.isRequired,
  10. brandVariationHomeUrl: PropTypes.string.isRequired
  11. }),
  12. loading: PropTypes.bool,
  13. projectId: PropTypes.string.isRequired,
  14. isProjectOwner: PropTypes.bool,
  15. isRestrictedTokenMember: PropTypes.bool
  16. })
  17. }
  18. export function EditorProvider({ children, $scope }) {
  19. const cobranding = window.brandVariation
  20. ? {
  21. logoImgUrl: window.brandVariation.logo_url,
  22. brandVariationName: window.brandVariation.name,
  23. brandVariationHomeUrl: window.brandVariation.home_url
  24. }
  25. : undefined
  26. const ownerId =
  27. window._ide.$scope.project && window._ide.$scope.project.owner
  28. ? window._ide.$scope.project.owner._id
  29. : null
  30. const [loading] = useScopeValue('state.loading', $scope)
  31. const editorContextValue = {
  32. cobranding,
  33. loading,
  34. projectId: window.project_id,
  35. isProjectOwner: ownerId === window.user.id,
  36. isRestrictedTokenMember: window.isRestrictedTokenMember
  37. }
  38. return (
  39. <EditorContext.Provider value={editorContextValue}>
  40. {children}
  41. </EditorContext.Provider>
  42. )
  43. }
  44. EditorProvider.propTypes = {
  45. children: PropTypes.any,
  46. $scope: PropTypes.any.isRequired
  47. }
  48. export function useEditorContext(propTypes) {
  49. const data = useContext(EditorContext)
  50. PropTypes.checkPropTypes(propTypes, data, 'data', 'EditorContext.Provider')
  51. return data
  52. }