overleaf/services/web/frontend/js/shared/context/layout-context.js
Miguel Serrano a555f0d309 [ReactNavToolbar] Project name + pdf and share project buttons (#3709)
* Added project name, pdf toggle and share project buttons to navigation toolbar

* Added PropTypes check to `useChatContext()`

* React context updates for project name/rename, pdf view and share moda

* Hide PDF button when pdfLayout != 'flat'

GitOrigin-RevId: 3f4a1b072259df7148d3417cd22116702bdd79ac
2021-03-11 03:05:33 +00:00

63 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 LayoutContext = createContext()
LayoutContext.Provider.propTypes = {
value: PropTypes.shape({
view: PropTypes.string,
setView: PropTypes.func.isRequired,
chatIsOpen: PropTypes.bool,
setChatIsOpen: PropTypes.func.isRequired,
reviewPanelOpen: PropTypes.bool,
setReviewPanelOpen: PropTypes.func.isRequired,
leftMenuShown: PropTypes.bool,
setLeftMenuShown: PropTypes.func.isRequired,
pdfLayout: PropTypes.oneOf(['sideBySide', 'flat', 'split']).isRequired
}).isRequired
}
export function LayoutProvider({ children, $scope }) {
const [view, setView] = useScopeValue('ui.view', $scope)
const [chatIsOpen, setChatIsOpen] = useScopeValue('ui.chatOpen', $scope)
const [reviewPanelOpen, setReviewPanelOpen] = useScopeValue(
'ui.reviewPanelOpen',
$scope
)
const [leftMenuShown, setLeftMenuShown] = useScopeValue(
'ui.leftMenuShown',
$scope
)
const [pdfLayout] = useScopeValue('ui.pdfLayout', $scope)
const layoutContextValue = {
view,
setView,
chatIsOpen,
setChatIsOpen,
reviewPanelOpen,
setReviewPanelOpen,
leftMenuShown,
setLeftMenuShown,
pdfLayout
}
return (
<LayoutContext.Provider value={layoutContextValue}>
{children}
</LayoutContext.Provider>
)
}
LayoutProvider.propTypes = {
children: PropTypes.any,
$scope: PropTypes.any.isRequired
}
export function useLayoutContext(propTypes) {
const data = useContext(LayoutContext)
PropTypes.checkPropTypes(propTypes, data, 'data', 'LayoutContext.Provider')
return data
}