overleaf/services/web/frontend/js/shared/context/editor-context.js
Miguel Serrano d55e46d3c0 Merge pull request #3642 from overleaf/msm-history-track-changes-toggles
[ReactNavToolbar] Track changes and History toggle buttons.

GitOrigin-RevId: a67a9a488c0960dba3f3d374cde4db0080ed2952
2021-02-24 03:04:17 +00:00

61 lines
1.7 KiB
JavaScript

import React, { createContext, useContext } from 'react'
import PropTypes from 'prop-types'
import useScopeValue from './util/scope-value-hook'
export const EditorContext = createContext()
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,
isProjectOwner: PropTypes.bool,
isRestrictedTokenMember: PropTypes.bool
})
}
export function EditorProvider({ children, $scope }) {
const cobranding = window.brandVariation
? {
logoImgUrl: window.brandVariation.logo_url,
brandVariationName: window.brandVariation.name,
brandVariationHomeUrl: window.brandVariation.home_url
}
: undefined
const ownerId =
window._ide.$scope.project && window._ide.$scope.project.owner
? window._ide.$scope.project.owner._id
: null
const [loading] = useScopeValue('state.loading', $scope)
const editorContextValue = {
cobranding,
loading,
projectId: window.project_id,
isProjectOwner: ownerId === window.user.id,
isRestrictedTokenMember: window.isRestrictedTokenMember
}
return (
<EditorContext.Provider value={editorContextValue}>
{children}
</EditorContext.Provider>
)
}
EditorProvider.propTypes = {
children: PropTypes.any,
$scope: PropTypes.any.isRequired
}
export function useEditorContext(propTypes) {
const data = useContext(EditorContext)
PropTypes.checkPropTypes(propTypes, data, 'data', 'EditorContext.Provider')
return data
}