overleaf/services/web/frontend/js/features/editor-left-menu/hooks/use-save-user-settings.tsx
M Fahru ae5a08e680 Add 'setting-changed' tracking when user changes user-wide settings
GitOrigin-RevId: 04ed66dcdff7fa04e783fe4028505d981a39b37b
2023-01-11 09:07:45 +00:00

26 lines
841 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 <T extends UserSettingsScope[keyof UserSettingsScope]>(
key: keyof UserSettingsScope,
newSetting: T
) => {
const currentSetting = userSettingsScope[key]
sendMB('setting-changed', {
changedSetting: key,
changedSettingVal: newSetting,
})
if (currentSetting !== newSetting) {
setUserSettingsScope({ ...userSettingsScope, [key]: newSetting })
saveUserSettings({ [key]: newSetting })
}
}
}