mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
677ec173ed
This may happen if the project is being used by multiple people, and we want to avoid race condition on the update since it's possible for multiple people to update setting value at the same time. GitOrigin-RevId: cdad6a6456e2d9e4ef1812ebfd6f6ef59f23747f
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { useCallback } from 'react'
|
|
import { ProjectCompiler } from '../../../../../types/project-settings'
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
|
import { ProjectSettingsScope, saveProjectSettings } from '../utils/api'
|
|
import useSetRootDocId from './use-set-root-doc-id'
|
|
import useSetSpellCheckLanguage from './use-set-spell-check-language'
|
|
|
|
type UseSetProjectWideSettings = {
|
|
ignoreUpdates: boolean
|
|
}
|
|
|
|
export default function useSetProjectWideSettings({
|
|
ignoreUpdates,
|
|
}: UseSetProjectWideSettings) {
|
|
// The value will be undefined on mount
|
|
const [project, setProject] = useScopeValue<ProjectSettingsScope | undefined>(
|
|
'project',
|
|
true
|
|
)
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
const setCompiler = useCallback(
|
|
(compiler: ProjectCompiler) => {
|
|
const allowUpdate = !ignoreUpdates && project?.compiler
|
|
|
|
if (allowUpdate) {
|
|
setProject({ ...project, compiler })
|
|
saveProjectSettings({ projectId, compiler })
|
|
}
|
|
},
|
|
[projectId, project, setProject, ignoreUpdates]
|
|
)
|
|
|
|
const setImageName = useCallback(
|
|
(imageName: string) => {
|
|
const allowUpdate = !ignoreUpdates && project?.imageName
|
|
|
|
if (allowUpdate) {
|
|
setProject({ ...project, imageName })
|
|
saveProjectSettings({ projectId, imageName })
|
|
}
|
|
},
|
|
[projectId, project, setProject, ignoreUpdates]
|
|
)
|
|
|
|
const setRootDocId = useSetRootDocId({ ignoreUpdates })
|
|
const setSpellCheckLanguage = useSetSpellCheckLanguage({ ignoreUpdates })
|
|
|
|
return {
|
|
compiler: project?.compiler,
|
|
setCompiler,
|
|
imageName: project?.imageName,
|
|
setImageName,
|
|
rootDocId: project?.rootDoc_id,
|
|
setRootDocId,
|
|
spellCheckLanguage: project?.spellCheckLanguage,
|
|
setSpellCheckLanguage,
|
|
}
|
|
}
|