2022-12-23 18:43:26 -05:00
|
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
2022-12-28 12:33:12 -05:00
|
|
|
import { sendMB } from '../../../infrastructure/event-tracking'
|
2022-12-23 18:43:26 -05:00
|
|
|
import { saveUserSettings } from '../utils/api'
|
|
|
|
import type { UserSettingsScope } from '../utils/api'
|
|
|
|
|
|
|
|
export default function useSaveUserSettings() {
|
2022-12-27 14:53:37 -05:00
|
|
|
const [userSettingsScope, setUserSettingsScope] =
|
|
|
|
useScopeValue<UserSettingsScope>('settings', true)
|
2022-12-23 18:43:26 -05:00
|
|
|
|
2022-12-28 11:38:19 -05:00
|
|
|
return <T extends UserSettingsScope[keyof UserSettingsScope]>(
|
|
|
|
key: keyof UserSettingsScope,
|
|
|
|
newSetting: T
|
|
|
|
) => {
|
2022-12-23 18:43:26 -05:00
|
|
|
const currentSetting = userSettingsScope[key]
|
|
|
|
|
2022-12-28 12:33:12 -05:00
|
|
|
sendMB('setting-changed', {
|
|
|
|
changedSetting: key,
|
|
|
|
changedSettingVal: newSetting,
|
|
|
|
})
|
|
|
|
|
2022-12-23 18:43:26 -05:00
|
|
|
if (currentSetting !== newSetting) {
|
|
|
|
setUserSettingsScope({ ...userSettingsScope, [key]: newSetting })
|
|
|
|
saveUserSettings({ [key]: newSetting })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|