2023-01-06 20:45:37 -05:00
|
|
|
import { type ProjectSettings, saveProjectSettings } from '../utils/api'
|
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
|
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
|
|
|
|
|
|
|
export default function useSaveProjectSettings() {
|
|
|
|
// projectSettings value will be undefined on mount
|
|
|
|
const [projectSettings, setProjectSettings] = useScopeValue<
|
|
|
|
ProjectSettings | undefined
|
2024-06-05 04:33:11 -04:00
|
|
|
>('project')
|
2023-01-06 20:45:37 -05:00
|
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
|
|
|
|
return async (
|
|
|
|
key: keyof ProjectSettings,
|
|
|
|
newSetting: ProjectSettings[keyof ProjectSettings]
|
|
|
|
) => {
|
|
|
|
if (projectSettings) {
|
|
|
|
const currentSetting = projectSettings[key]
|
|
|
|
if (currentSetting !== newSetting) {
|
|
|
|
await saveProjectSettings(projectId, {
|
|
|
|
[key]: newSetting,
|
|
|
|
})
|
|
|
|
|
2023-08-03 10:43:59 -04:00
|
|
|
// rootDocId is used in our tsx and our endpoint, but rootDoc_id is used in our project $scope, etc
|
|
|
|
// as we use both namings in many files, and convert back and forth,
|
|
|
|
// its complicated to seperate and choose one name for all usages
|
|
|
|
// todo: make rootDocId or rootDoc_id consistent, and remove need for this/ other conversions
|
|
|
|
const settingsKey = key === 'rootDocId' ? 'rootDoc_id' : key
|
|
|
|
setProjectSettings({ ...projectSettings, [settingsKey]: newSetting })
|
2023-01-06 20:45:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|