overleaf/services/web/frontend/js/shared/context/project-context.js
Tim Down 70bae34bd8 Add paywall to React history view (#12849)
* Implement history view paywall

* Add tests and some CSS fallbacks

* Make additional faded version above paywall non-clickable

* Change isFaded to faded for consistency

* Remove unused import

* Add missing attribute

* SHow all labels in free tier

* Address review comments

* Change Boolean conversion

Co-authored-by: ilkin-overleaf <100852799+ilkin-overleaf@users.noreply.github.com>

* Make adding or deleting a label show up in version list again

* Refactor to use visibleUpdateCount rather than maintaining two separate update arrays

* Removed unused import

* Use data-testid instead on class

* Round gradient values

* Correct test selector

---------

Co-authored-by: ilkin-overleaf <100852799+ilkin-overleaf@users.noreply.github.com>
GitOrigin-RevId: a2b021f3f4d3b9eb1358edb2ee4aa7db1bc7240e
2023-05-02 19:44:39 +00:00

119 lines
2.5 KiB
JavaScript

import { createContext, useContext, useMemo } from 'react'
import PropTypes from 'prop-types'
import useScopeValue from '../hooks/use-scope-value'
const ProjectContext = createContext()
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({
_id: PropTypes.string.isRequired,
})
),
features: PropTypes.shape({
collaborators: PropTypes.number,
compileGroup: PropTypes.oneOf(['alpha', 'standard', 'priority']),
trackChangesVisible: PropTypes.bool,
references: PropTypes.bool,
mendeley: PropTypes.bool,
zotero: PropTypes.bool,
versioning: 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,
}),
}
ProjectContext.Provider.propTypes = {
value: PropTypes.shape(projectShape),
}
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
}
// 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: {},
}
export function ProjectProvider({ children }) {
const [project] = useScopeValue('project', true)
const {
_id,
name,
rootDoc_id: rootDocId,
members,
invites,
features,
publicAccesLevel: publicAccessLevel,
tokens,
owner,
} = project || projectFallback
const value = useMemo(() => {
return {
_id,
name,
rootDocId,
members,
invites,
features,
publicAccessLevel,
tokens,
owner,
}
}, [
_id,
name,
rootDocId,
members,
invites,
features,
publicAccessLevel,
tokens,
owner,
])
return (
<ProjectContext.Provider value={value}>{children}</ProjectContext.Provider>
)
}
ProjectProvider.propTypes = {
children: PropTypes.any,
}