overleaf/services/web/frontend/js/shared/context/project-context.tsx
roo hutton 64d9792fe3 Merge pull request #18861 from overleaf/rh-editor-limit-exceeded
[web]: Handle exceeded editor limit in share modal

GitOrigin-RevId: 23a15805ca98327ae4a7fc731bbca3982c90bad5
2024-06-25 08:04:46 +00:00

84 lines
1.8 KiB
TypeScript

import { FC, createContext, useContext, useMemo } from 'react'
import useScopeValue from '../hooks/use-scope-value'
import getMeta from '@/utils/meta'
import { ProjectContextValue } from './types/project-context'
const ProjectContext = createContext<ProjectContextValue | undefined>(undefined)
export function useProjectContext() {
const context = useContext(ProjectContext)
if (!context) {
throw new Error(
'useProjectContext is only available inside ProjectProvider'
)
}
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: getMeta('ol-project_id'),
name: '',
features: {},
}
export const ProjectProvider: FC = ({ children }) => {
const [project] = useScopeValue('project')
const {
_id,
compiler,
name,
rootDoc_id: rootDocId,
members,
invites,
features,
publicAccesLevel: publicAccessLevel,
owner,
trackChangesState,
} = project || projectFallback
const tags = useMemo(
() =>
(getMeta('ol-projectTags') || [])
// `tag.name` data may be null for some old users
.map((tag: any) => ({ ...tag, name: tag.name ?? '' })),
[]
)
const value = useMemo(() => {
return {
_id,
compiler,
name,
rootDocId,
members,
invites,
features,
publicAccessLevel,
owner,
tags,
trackChangesState,
}
}, [
_id,
compiler,
name,
rootDocId,
members,
invites,
features,
publicAccessLevel,
owner,
tags,
trackChangesState,
])
return (
<ProjectContext.Provider value={value}>{children}</ProjectContext.Provider>
)
}