use-save-project-settings.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { type ProjectSettings, saveProjectSettings } from '../utils/api'
  2. import { useProjectContext } from '../../../shared/context/project-context'
  3. import useScopeValue from '../../../shared/hooks/use-scope-value'
  4. export default function useSaveProjectSettings() {
  5. // projectSettings value will be undefined on mount
  6. const [projectSettings, setProjectSettings] = useScopeValue<
  7. ProjectSettings | undefined
  8. >('project', true)
  9. const { _id: projectId } = useProjectContext()
  10. return async (
  11. key: keyof ProjectSettings,
  12. newSetting: ProjectSettings[keyof ProjectSettings]
  13. ) => {
  14. if (projectSettings) {
  15. const currentSetting = projectSettings[key]
  16. if (currentSetting !== newSetting) {
  17. await saveProjectSettings(projectId, {
  18. [key]: newSetting,
  19. })
  20. // rootDocId is used in our tsx and our endpoint, but rootDoc_id is used in our project $scope, etc
  21. // as we use both namings in many files, and convert back and forth,
  22. // its complicated to seperate and choose one name for all usages
  23. // todo: make rootDocId or rootDoc_id consistent, and remove need for this/ other conversions
  24. const settingsKey = key === 'rootDocId' ? 'rootDoc_id' : key
  25. setProjectSettings({ ...projectSettings, [settingsKey]: newSetting })
  26. }
  27. }
  28. }
  29. }