overleaf/services/web/frontend/js/features/editor-left-menu/hooks/use-save-user-settings.tsx

21 lines
673 B
TypeScript
Raw Normal View History

import useScopeValue from '../../../shared/hooks/use-scope-value'
import { saveUserSettings } from '../utils/api'
import type { UserSettingsScope } from '../utils/api'
export default function useSaveUserSettings() {
const [userSettingsScope, setUserSettingsScope] =
useScopeValue<UserSettingsScope>('settings', true)
return <T extends UserSettingsScope[keyof UserSettingsScope]>(
key: keyof UserSettingsScope,
newSetting: T
) => {
const currentSetting = userSettingsScope[key]
if (currentSetting !== newSetting) {
setUserSettingsScope({ ...userSettingsScope, [key]: newSetting })
saveUserSettings({ [key]: newSetting })
}
}
}