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-01-26 04:23:48 -05:00
|
|
|
import { UserId } from '../../../../types/user'
|
|
|
|
import { PublicAccessLevel } from '../../../../types/public-access-level'
|
2024-03-07 05:43:42 -05:00
|
|
|
import type * as ReviewPanel from '@/features/source-editor/context/review-panel/types/review-panel-state'
|
2021-06-25 04:13:17 -04:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const ProjectContext = createContext<
|
|
|
|
| {
|
|
|
|
_id: string
|
|
|
|
name: string
|
|
|
|
rootDocId?: string
|
|
|
|
members: { _id: UserId; email: string; privileges: string }[]
|
|
|
|
invites: { _id: UserId }[]
|
|
|
|
features: {
|
|
|
|
collaborators?: number
|
|
|
|
compileGroup?: 'alpha' | 'standard' | 'priority'
|
|
|
|
trackChanges?: boolean
|
|
|
|
trackChangesVisible?: boolean
|
|
|
|
references?: boolean
|
|
|
|
mendeley?: boolean
|
|
|
|
zotero?: boolean
|
|
|
|
versioning?: boolean
|
|
|
|
gitBridge?: boolean
|
|
|
|
referencesSearch?: boolean
|
2024-01-29 15:23:03 -05:00
|
|
|
github?: boolean
|
2024-01-26 04:23:48 -05:00
|
|
|
}
|
|
|
|
publicAccessLevel?: PublicAccessLevel
|
|
|
|
owner: {
|
|
|
|
_id: UserId
|
|
|
|
email: string
|
|
|
|
}
|
|
|
|
tags: {
|
|
|
|
_id: string
|
|
|
|
name: string
|
|
|
|
color?: string
|
|
|
|
}[]
|
|
|
|
trackChangesState: ReviewPanel.Value<'trackChangesState'>
|
|
|
|
}
|
|
|
|
| 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 = {
|
|
|
|
_id: window.project_id,
|
|
|
|
name: '',
|
|
|
|
features: {},
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export const ProjectProvider: FC = ({ children }) => {
|
2021-06-25 04:13:17 -04:00
|
|
|
const [project] = useScopeValue('project', true)
|
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
const {
|
|
|
|
_id,
|
|
|
|
name,
|
|
|
|
rootDoc_id: rootDocId,
|
|
|
|
members,
|
|
|
|
invites,
|
|
|
|
features,
|
|
|
|
publicAccesLevel: publicAccessLevel,
|
|
|
|
owner,
|
2023-11-16 06:56:54 -05:00
|
|
|
trackChangesState,
|
2022-01-10 10:47:10 -05:00
|
|
|
} = project || projectFallback
|
|
|
|
|
2023-09-28 06:12:18 -04:00
|
|
|
const tags = useMemo(
|
|
|
|
() =>
|
|
|
|
getMeta('ol-projectTags', [])
|
|
|
|
// `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,
|
|
|
|
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,
|
2022-01-10 10:47:01 -05:00
|
|
|
}
|
2022-01-10 10:47:10 -05:00
|
|
|
}, [
|
|
|
|
_id,
|
|
|
|
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,
|
2022-01-10 10:47:10 -05:00
|
|
|
])
|
|
|
|
|
2021-06-25 04:13:17 -04:00
|
|
|
return (
|
|
|
|
<ProjectContext.Provider value={value}>{children}</ProjectContext.Provider>
|
|
|
|
)
|
|
|
|
}
|