mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
9b59c0813c
* Replaced `application-context` with `user-context` * deleted `user` initialization with `window.user` * fixed tests and storybook GitOrigin-RevId: 0ed4b9070d7c6d370fee2112f310c4bcfea519e7
158 lines
4.5 KiB
JavaScript
158 lines
4.5 KiB
JavaScript
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
} from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import useScopeValue from './util/scope-value-hook'
|
|
import useBrowserWindow from '../hooks/use-browser-window'
|
|
import { useIdeContext } from './ide-context'
|
|
import { useProjectContext } from './project-context'
|
|
|
|
export const EditorContext = createContext()
|
|
|
|
EditorContext.Provider.propTypes = {
|
|
value: PropTypes.shape({
|
|
cobranding: PropTypes.shape({
|
|
logoImgUrl: PropTypes.string.isRequired,
|
|
brandVariationName: PropTypes.string.isRequired,
|
|
brandVariationId: PropTypes.number.isRequired,
|
|
brandId: PropTypes.number.isRequired,
|
|
brandVariationHomeUrl: PropTypes.string.isRequired,
|
|
publishGuideHtml: PropTypes.string,
|
|
partner: PropTypes.string,
|
|
brandedMenu: PropTypes.string,
|
|
submitBtnHtml: PropTypes.string,
|
|
}),
|
|
hasPremiumCompile: PropTypes.bool,
|
|
loading: PropTypes.bool,
|
|
renameProject: PropTypes.func.isRequired,
|
|
isProjectOwner: PropTypes.bool,
|
|
isRestrictedTokenMember: PropTypes.bool,
|
|
rootFolder: PropTypes.shape({
|
|
children: PropTypes.arrayOf(PropTypes.shape({ type: PropTypes.string })),
|
|
}),
|
|
permissionsLevel: PropTypes.oneOf(['readOnly', 'readAndWrite', 'owner']),
|
|
}),
|
|
}
|
|
|
|
export function EditorProvider({ children, settings }) {
|
|
const ide = useIdeContext()
|
|
|
|
const { owner, features } = useProjectContext({
|
|
owner: PropTypes.shape({
|
|
_id: PropTypes.string.isRequired,
|
|
}),
|
|
features: PropTypes.shape({
|
|
compileGroup: PropTypes.string,
|
|
}),
|
|
})
|
|
|
|
const cobranding = useMemo(() => {
|
|
if (window.brandVariation) {
|
|
return {
|
|
logoImgUrl: window.brandVariation.logo_url,
|
|
brandVariationName: window.brandVariation.name,
|
|
brandVariationId: window.brandVariation.id,
|
|
brandId: window.brandVariation.brand_id,
|
|
brandVariationHomeUrl: window.brandVariation.home_url,
|
|
publishGuideHtml: window.brandVariation.publish_guide_html,
|
|
partner: window.brandVariation.partner,
|
|
brandedMenu: window.brandVariation.branded_menu,
|
|
submitBtnHtml: window.brandVariation.submit_button_html,
|
|
}
|
|
} else {
|
|
return undefined
|
|
}
|
|
}, [])
|
|
|
|
const [loading] = useScopeValue('state.loading')
|
|
const [projectName, setProjectName] = useScopeValue('project.name')
|
|
const [rootFolder] = useScopeValue('rootFolder')
|
|
const [permissionsLevel] = useScopeValue('permissionsLevel')
|
|
|
|
useEffect(() => {
|
|
if (ide?.socket) {
|
|
ide.socket.on('projectNameUpdated', setProjectName)
|
|
return () =>
|
|
ide.socket.removeListener('projectNameUpdated', setProjectName)
|
|
}
|
|
}, [ide?.socket, setProjectName])
|
|
|
|
const renameProject = useCallback(
|
|
newName => {
|
|
setProjectName(oldName => {
|
|
if (oldName !== newName) {
|
|
settings.saveProjectSettings({ name: newName }).catch(response => {
|
|
setProjectName(oldName)
|
|
const { data, status } = response
|
|
if (status === 400) {
|
|
return ide.showGenericMessageModal('Error renaming project', data)
|
|
} else {
|
|
return ide.showGenericMessageModal(
|
|
'Error renaming project',
|
|
'Please try again in a moment'
|
|
)
|
|
}
|
|
})
|
|
}
|
|
return newName
|
|
})
|
|
},
|
|
[settings, ide, setProjectName]
|
|
)
|
|
|
|
const { setTitle } = useBrowserWindow()
|
|
useEffect(() => {
|
|
setTitle(
|
|
`${projectName ? projectName + ' - ' : ''}Online LaTeX Editor ${
|
|
window.ExposedSettings.appName
|
|
}`
|
|
)
|
|
}, [projectName, setTitle])
|
|
|
|
const value = useMemo(
|
|
() => ({
|
|
cobranding,
|
|
hasPremiumCompile: features?.compileGroup === 'priority',
|
|
loading,
|
|
renameProject,
|
|
permissionsLevel,
|
|
isProjectOwner: owner?._id === window.user.id,
|
|
isRestrictedTokenMember: window.isRestrictedTokenMember,
|
|
rootFolder,
|
|
}),
|
|
[
|
|
cobranding,
|
|
features?.compileGroup,
|
|
loading,
|
|
renameProject,
|
|
permissionsLevel,
|
|
owner?._id,
|
|
rootFolder,
|
|
]
|
|
)
|
|
|
|
return (
|
|
<EditorContext.Provider value={value}>{children}</EditorContext.Provider>
|
|
)
|
|
}
|
|
|
|
EditorProvider.propTypes = {
|
|
children: PropTypes.any,
|
|
settings: PropTypes.any.isRequired,
|
|
}
|
|
|
|
export function useEditorContext(propTypes) {
|
|
const context = useContext(EditorContext)
|
|
|
|
if (!context) {
|
|
throw new Error('useEditorContext is only available inside EditorProvider')
|
|
}
|
|
|
|
PropTypes.checkPropTypes(propTypes, context, 'data', 'EditorContext.Provider')
|
|
|
|
return context
|
|
}
|