2021-02-23 05:17:41 -05:00
|
|
|
import React, { createContext, useContext } from 'react'
|
2020-12-14 06:44:10 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-02-23 05:17:41 -05:00
|
|
|
import useScopeValue from './util/scope-value-hook'
|
2020-12-14 06:44:10 -05:00
|
|
|
|
|
|
|
export const EditorContext = createContext()
|
|
|
|
|
2021-02-10 05:36:41 -05:00
|
|
|
EditorContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
cobranding: PropTypes.shape({
|
|
|
|
logoImgUrl: PropTypes.string.isRequired,
|
|
|
|
brandVariationName: PropTypes.string.isRequired,
|
|
|
|
brandVariationHomeUrl: PropTypes.string.isRequired
|
|
|
|
}),
|
|
|
|
loading: PropTypes.bool,
|
|
|
|
projectId: PropTypes.string.isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
isProjectOwner: PropTypes.bool,
|
|
|
|
isRestrictedTokenMember: PropTypes.bool
|
2021-02-10 05:36:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-23 05:17:41 -05:00
|
|
|
export function EditorProvider({ children, $scope }) {
|
2021-01-27 05:30:55 -05:00
|
|
|
const cobranding = window.brandVariation
|
|
|
|
? {
|
|
|
|
logoImgUrl: window.brandVariation.logo_url,
|
|
|
|
brandVariationName: window.brandVariation.name,
|
|
|
|
brandVariationHomeUrl: window.brandVariation.home_url
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
|
2021-01-14 10:16:54 -05:00
|
|
|
const ownerId =
|
|
|
|
window._ide.$scope.project && window._ide.$scope.project.owner
|
|
|
|
? window._ide.$scope.project.owner._id
|
|
|
|
: null
|
|
|
|
|
2021-02-23 05:17:41 -05:00
|
|
|
const [loading] = useScopeValue('state.loading', $scope)
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2021-01-14 10:16:54 -05:00
|
|
|
const editorContextValue = {
|
2021-01-27 05:30:55 -05:00
|
|
|
cobranding,
|
|
|
|
loading,
|
2021-01-14 10:16:54 -05:00
|
|
|
projectId: window.project_id,
|
2021-02-09 10:37:48 -05:00
|
|
|
isProjectOwner: ownerId === window.user.id,
|
2021-02-23 05:17:41 -05:00
|
|
|
isRestrictedTokenMember: window.isRestrictedTokenMember
|
2021-01-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
|
2020-12-14 06:44:10 -05:00
|
|
|
return (
|
2021-01-14 10:16:54 -05:00
|
|
|
<EditorContext.Provider value={editorContextValue}>
|
2020-12-14 06:44:10 -05:00
|
|
|
{children}
|
|
|
|
</EditorContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorProvider.propTypes = {
|
2021-01-27 05:30:55 -05:00
|
|
|
children: PropTypes.any,
|
2021-02-23 05:17:41 -05:00
|
|
|
$scope: PropTypes.any.isRequired
|
2020-12-14 06:44:10 -05:00
|
|
|
}
|
|
|
|
|
2021-02-10 05:36:41 -05:00
|
|
|
export function useEditorContext(propTypes) {
|
|
|
|
const data = useContext(EditorContext)
|
|
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'EditorContext.Provider')
|
|
|
|
return data
|
2020-12-14 06:44:10 -05:00
|
|
|
}
|