Files
overleaf-cep/services/web/frontend/js/shared/context/project-context.tsx
Jakob Ackermann b831a0b3f7 [clsi-cache] frontend (#24389)
* [clsi-lb] forward ?clsiserverid=cache requests to clsi-cache

* [web] use clsi-cache in frontend

* [web] upgrade compile from cache to full compile when triggered inflight

* [web] fix pdf-js-viewer.spec.tsx tests -- add ?clsiserverid=foo to url

* [web] fix renamed reference after merge

* [web] fix download of other output files and use specific build

* [web] consolidate validation of filename into ClsiCacheHandler

* [web] remove unused projectName from getLatestBuildFromCache

* [web] avoid hitting the same clsi-cache instance first all the time

* [web] update documentation

GitOrigin-RevId: d48265a7ba89d6731092640e1492bc9f103f5c33
2025-04-10 08:05:22 +00:00

96 lines
2.1 KiB
TypeScript

import { FC, createContext, useContext, useMemo, useState } from 'react'
import useScopeValue from '../hooks/use-scope-value'
import getMeta from '@/utils/meta'
import { ProjectContextValue } from './types/project-context'
import { ProjectSnapshot } from '@/infrastructure/project-snapshot'
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 joinedOnce = !!project
const {
_id,
compiler,
name,
rootDoc_id: rootDocId,
members,
invites,
features,
publicAccesLevel: publicAccessLevel,
owner,
trackChangesState,
mainBibliographyDoc_id: mainBibliographyDocId,
} = project || projectFallback
const [projectSnapshot] = useState(() => new ProjectSnapshot(_id))
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,
mainBibliographyDocId,
projectSnapshot,
joinedOnce,
}
}, [
_id,
compiler,
name,
rootDocId,
members,
invites,
features,
publicAccessLevel,
owner,
tags,
trackChangesState,
mainBibliographyDocId,
projectSnapshot,
joinedOnce,
])
return (
<ProjectContext.Provider value={value}>{children}</ProjectContext.Provider>
)
}