2022-12-23 18:43:26 -05:00
|
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
|
|
|
import { saveUserSettings } from '../utils/api'
|
2023-01-04 07:17:50 -05:00
|
|
|
import type { UserSettings } from '../utils/api'
|
2022-12-23 18:43:26 -05:00
|
|
|
|
|
|
|
export default function useSaveUserSettings() {
|
2023-01-04 07:17:50 -05:00
|
|
|
const [userSettings, setUserSettings] = useScopeValue<UserSettings>(
|
|
|
|
'settings',
|
|
|
|
true
|
|
|
|
)
|
2022-12-23 18:43:26 -05:00
|
|
|
|
2023-01-03 12:41:47 -05:00
|
|
|
return (
|
2023-01-04 07:17:50 -05:00
|
|
|
key: keyof UserSettings,
|
|
|
|
newSetting: UserSettings[keyof UserSettings]
|
2022-12-28 11:38:19 -05:00
|
|
|
) => {
|
2023-01-04 07:17:50 -05:00
|
|
|
const currentSetting = userSettings[key]
|
2022-12-23 18:43:26 -05:00
|
|
|
|
|
|
|
if (currentSetting !== newSetting) {
|
2023-01-04 07:17:50 -05:00
|
|
|
setUserSettings({ ...userSettings, [key]: newSetting })
|
2023-01-06 09:29:03 -05:00
|
|
|
saveUserSettings(key, newSetting)
|
2022-12-23 18:43:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|