2022-01-10 10:47:01 -05:00
|
|
|
import { createContext, useContext, useMemo } from 'react'
|
2021-06-25 04:13:17 -04:00
|
|
|
import PropTypes from 'prop-types'
|
2021-10-08 06:09:41 -04:00
|
|
|
import useScopeValue from '../hooks/use-scope-value'
|
2021-06-25 04:13:17 -04:00
|
|
|
|
|
|
|
const ProjectContext = createContext()
|
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
export const projectShape = {
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
rootDocId: PropTypes.string,
|
|
|
|
members: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
invites: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
2021-06-25 04:13:17 -04:00
|
|
|
_id: PropTypes.string.isRequired,
|
2022-01-10 10:47:10 -05:00
|
|
|
})
|
|
|
|
),
|
|
|
|
features: PropTypes.shape({
|
|
|
|
collaborators: PropTypes.number,
|
|
|
|
compileGroup: PropTypes.oneOf(['alpha', 'standard', 'priority']),
|
|
|
|
trackChangesVisible: PropTypes.bool,
|
|
|
|
references: PropTypes.bool,
|
|
|
|
mendeley: PropTypes.bool,
|
|
|
|
zotero: PropTypes.bool,
|
|
|
|
}),
|
|
|
|
publicAccessLevel: PropTypes.string,
|
|
|
|
tokens: PropTypes.shape({
|
|
|
|
readOnly: PropTypes.string,
|
|
|
|
readAndWrite: PropTypes.string,
|
|
|
|
}),
|
|
|
|
owner: PropTypes.shape({
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
email: PropTypes.string.isRequired,
|
2021-06-25 04:13:17 -04:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
ProjectContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape(projectShape),
|
|
|
|
}
|
|
|
|
|
2021-06-25 04:13:17 -04:00
|
|
|
export function useProjectContext(propTypes) {
|
|
|
|
const context = useContext(ProjectContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useProjectContext is only available inside ProjectProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
PropTypes.checkPropTypes(
|
|
|
|
propTypes,
|
|
|
|
context,
|
|
|
|
'data',
|
|
|
|
'ProjectContext.Provider'
|
|
|
|
)
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
// when the provider is created the project is still not added to the Angular
|
|
|
|
// scope. A few props are populated to prevent errors in existing React
|
|
|
|
// components
|
|
|
|
const projectFallback = {
|
|
|
|
_id: window.project_id,
|
|
|
|
name: '',
|
|
|
|
features: {},
|
|
|
|
}
|
|
|
|
|
2021-06-25 04:13:17 -04:00
|
|
|
export function ProjectProvider({ children }) {
|
|
|
|
const [project] = useScopeValue('project', true)
|
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
const {
|
|
|
|
_id,
|
|
|
|
name,
|
|
|
|
rootDoc_id: rootDocId,
|
|
|
|
members,
|
|
|
|
invites,
|
|
|
|
features,
|
|
|
|
publicAccesLevel: publicAccessLevel,
|
|
|
|
tokens,
|
|
|
|
owner,
|
|
|
|
} = project || projectFallback
|
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
const value = useMemo(() => {
|
|
|
|
return {
|
2022-01-10 10:47:10 -05:00
|
|
|
_id,
|
|
|
|
name,
|
|
|
|
rootDocId,
|
|
|
|
members,
|
|
|
|
invites,
|
|
|
|
features,
|
|
|
|
publicAccessLevel,
|
|
|
|
tokens,
|
|
|
|
owner,
|
2022-01-10 10:47:01 -05:00
|
|
|
}
|
2022-01-10 10:47:10 -05:00
|
|
|
}, [
|
|
|
|
_id,
|
|
|
|
name,
|
|
|
|
rootDocId,
|
|
|
|
members,
|
|
|
|
invites,
|
|
|
|
features,
|
|
|
|
publicAccessLevel,
|
|
|
|
tokens,
|
|
|
|
owner,
|
|
|
|
])
|
|
|
|
|
2021-06-25 04:13:17 -04:00
|
|
|
return (
|
|
|
|
<ProjectContext.Provider value={value}>{children}</ProjectContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectProvider.propTypes = {
|
|
|
|
children: PropTypes.any,
|
|
|
|
}
|