2022-12-22 13:42:28 -05:00
|
|
|
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'
|
2022-12-27 14:54:21 -05:00
|
|
|
import { ProjectSettingsScope, saveProjectSettings } from '../utils/api'
|
|
|
|
import useSetRootDocId from './use-set-root-doc-id'
|
2022-12-28 12:02:43 -05:00
|
|
|
import useSetSpellCheckLanguage from './use-set-spell-check-language'
|
2022-12-22 13:42:28 -05:00
|
|
|
|
2023-01-03 12:04:34 -05:00
|
|
|
export default function useSetProjectWideSettings() {
|
2022-12-22 13:42:28 -05:00
|
|
|
// The value will be undefined on mount
|
2022-12-27 14:54:21 -05:00
|
|
|
const [project, setProject] = useScopeValue<ProjectSettingsScope | undefined>(
|
2022-12-22 13:42:28 -05:00
|
|
|
'project',
|
|
|
|
true
|
|
|
|
)
|
2022-12-27 14:54:21 -05:00
|
|
|
const { _id: projectId } = useProjectContext()
|
2022-12-22 13:42:28 -05:00
|
|
|
|
|
|
|
const setCompiler = useCallback(
|
|
|
|
(compiler: ProjectCompiler) => {
|
2023-01-03 12:04:34 -05:00
|
|
|
const allowUpdate = project?.compiler
|
2022-12-28 15:58:14 -05:00
|
|
|
|
|
|
|
if (allowUpdate) {
|
2022-12-22 13:42:28 -05:00
|
|
|
setProject({ ...project, compiler })
|
2022-12-28 12:38:27 -05:00
|
|
|
saveProjectSettings({ projectId, compiler })
|
2022-12-22 13:42:28 -05:00
|
|
|
}
|
|
|
|
},
|
2023-01-03 12:04:34 -05:00
|
|
|
[projectId, project, setProject]
|
2022-12-22 13:42:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const setImageName = useCallback(
|
|
|
|
(imageName: string) => {
|
2023-01-03 12:04:34 -05:00
|
|
|
const allowUpdate = project?.imageName
|
2022-12-28 15:58:14 -05:00
|
|
|
|
|
|
|
if (allowUpdate) {
|
2022-12-22 13:42:28 -05:00
|
|
|
setProject({ ...project, imageName })
|
2022-12-28 12:38:27 -05:00
|
|
|
saveProjectSettings({ projectId, imageName })
|
2022-12-22 13:42:28 -05:00
|
|
|
}
|
|
|
|
},
|
2023-01-03 12:04:34 -05:00
|
|
|
[projectId, project, setProject]
|
2022-12-22 13:42:28 -05:00
|
|
|
)
|
|
|
|
|
2023-01-03 12:04:34 -05:00
|
|
|
const setRootDocId = useSetRootDocId()
|
|
|
|
const setSpellCheckLanguage = useSetSpellCheckLanguage()
|
2022-12-22 13:42:28 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
compiler: project?.compiler,
|
|
|
|
setCompiler,
|
|
|
|
imageName: project?.imageName,
|
|
|
|
setImageName,
|
|
|
|
rootDocId: project?.rootDoc_id,
|
|
|
|
setRootDocId,
|
|
|
|
spellCheckLanguage: project?.spellCheckLanguage,
|
|
|
|
setSpellCheckLanguage,
|
|
|
|
}
|
|
|
|
}
|