overleaf/services/web/frontend/js/features/editor-left-menu/hooks/use-save-user-settings.tsx
M Fahru 12f5c845b4 Simplify function by removing generic
GitOrigin-RevId: 7732b6782cd1e0130eb56f6b674c03548b8e8315
2023-01-11 09:08:35 +00:00

26 lines
828 B
TypeScript

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