2024-01-26 04:23:48 -05:00
|
|
|
import { FC, createContext, useContext, useMemo } from 'react'
|
2021-10-08 06:09:41 -04:00
|
|
|
import useScopeValue from '../hooks/use-scope-value'
|
2023-09-28 06:12:18 -04:00
|
|
|
import getMeta from '@/utils/meta'
|
2024-06-25 02:08:24 -04:00
|
|
|
import { ProjectContextValue } from './types/project-context'
|
2021-06-25 04:13:17 -04:00
|
|
|
|
2024-06-25 02:08:24 -04:00
|
|
|
const ProjectContext = createContext<ProjectContextValue | undefined>(undefined)
|
2022-01-10 10:47:10 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export function useProjectContext() {
|
2021-06-25 04:13:17 -04:00
|
|
|
const context = useContext(ProjectContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useProjectContext is only available inside ProjectProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
2024-06-18 06:01:37 -04:00
|
|
|
_id: getMeta('ol-project_id'),
|
2022-01-10 10:47:01 -05:00
|
|
|
name: '',
|
|
|
|
features: {},
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export const ProjectProvider: FC = ({ children }) => {
|
2024-06-05 04:33:11 -04:00
|
|
|
const [project] = useScopeValue('project')
|
2021-06-25 04:13:17 -04:00
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
const {
|
|
|
|
_id,
|
2024-06-17 06:34:09 -04:00
|
|
|
compiler,
|
2022-01-10 10:47:10 -05:00
|
|
|
name,
|
|
|
|
rootDoc_id: rootDocId,
|
|
|
|
members,
|
|
|
|
invites,
|
|
|
|
features,
|
|
|
|
publicAccesLevel: publicAccessLevel,
|
|
|
|
owner,
|
2023-11-16 06:56:54 -05:00
|
|
|
trackChangesState,
|
2024-10-28 05:19:33 -04:00
|
|
|
mainBibliographyDoc_id: mainBibliographyDocId,
|
2022-01-10 10:47:10 -05:00
|
|
|
} = project || projectFallback
|
|
|
|
|
2023-09-28 06:12:18 -04:00
|
|
|
const tags = useMemo(
|
|
|
|
() =>
|
2024-06-18 06:01:37 -04:00
|
|
|
(getMeta('ol-projectTags') || [])
|
2023-09-28 06:12:18 -04:00
|
|
|
// `tag.name` data may be null for some old users
|
2024-01-26 04:23:48 -05:00
|
|
|
.map((tag: any) => ({ ...tag, name: tag.name ?? '' })),
|
2023-09-28 06:12:18 -04:00
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
const value = useMemo(() => {
|
|
|
|
return {
|
2022-01-10 10:47:10 -05:00
|
|
|
_id,
|
2024-06-17 06:34:09 -04:00
|
|
|
compiler,
|
2022-01-10 10:47:10 -05:00
|
|
|
name,
|
|
|
|
rootDocId,
|
|
|
|
members,
|
|
|
|
invites,
|
|
|
|
features,
|
|
|
|
publicAccessLevel,
|
|
|
|
owner,
|
2023-09-28 06:12:18 -04:00
|
|
|
tags,
|
2023-11-16 06:56:54 -05:00
|
|
|
trackChangesState,
|
2024-10-28 05:19:33 -04:00
|
|
|
mainBibliographyDocId,
|
2022-01-10 10:47:01 -05:00
|
|
|
}
|
2022-01-10 10:47:10 -05:00
|
|
|
}, [
|
|
|
|
_id,
|
2024-06-17 06:34:09 -04:00
|
|
|
compiler,
|
2022-01-10 10:47:10 -05:00
|
|
|
name,
|
|
|
|
rootDocId,
|
|
|
|
members,
|
|
|
|
invites,
|
|
|
|
features,
|
|
|
|
publicAccessLevel,
|
|
|
|
owner,
|
2023-09-28 06:12:18 -04:00
|
|
|
tags,
|
2023-11-16 06:56:54 -05:00
|
|
|
trackChangesState,
|
2024-10-28 05:19:33 -04:00
|
|
|
mainBibliographyDocId,
|
2022-01-10 10:47:10 -05:00
|
|
|
])
|
|
|
|
|
2021-06-25 04:13:17 -04:00
|
|
|
return (
|
|
|
|
<ProjectContext.Provider value={value}>{children}</ProjectContext.Provider>
|
|
|
|
)
|
|
|
|
}
|