overleaf/services/web/frontend/js/features/editor-left-menu/hooks/use-save-user-settings.tsx
M Fahru 4bf09e21de Fix tsc lint error by restricting the generic type
GitOrigin-RevId: 11ef7ab4111c7b312b283283829e9b28a80c475c
2023-01-11 09:07:09 +00:00

20 lines
673 B
TypeScript

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 })
}
}
}