tooot/src/utils/slices/settingsSlice.ts

68 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-11-29 23:44:11 +01:00
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
2022-02-06 23:25:16 +01:00
import { LOCALES } from '@root/i18n/locales'
2021-03-09 01:17:07 +01:00
import { RootState } from '@root/store'
2022-11-29 23:44:11 +01:00
import { SettingsV3 } from '@utils/migrations/settings/v3'
2021-01-17 22:37:05 +01:00
import * as Localization from 'expo-localization'
2021-01-31 03:09:35 +01:00
import { pickBy } from 'lodash'
2022-11-29 23:44:11 +01:00
export type SettingsState = SettingsV3
2021-01-07 19:13:09 +01:00
export const settingsInitialState = {
2021-03-10 10:22:53 +01:00
fontsize: 0,
2021-02-20 19:12:44 +01:00
notification: {
enabled: false
},
2022-11-29 23:44:11 +01:00
language: Object.keys(pickBy(LOCALES, (_, key) => Localization.locale.startsWith(key)))
? Object.keys(pickBy(LOCALES, (_, key) => Localization.locale.startsWith(key)))[0]
2021-01-31 03:09:35 +01:00
: 'en',
2020-12-20 18:41:28 +01:00
theme: 'auto',
2022-02-12 14:51:01 +01:00
darkTheme: 'lighter',
2020-12-29 16:19:04 +01:00
browser: 'internal',
2022-11-29 23:44:11 +01:00
staticEmoji: false
}
const settingsSlice = createSlice({
name: 'settings',
2021-01-07 19:13:09 +01:00
initialState: settingsInitialState as SettingsState,
reducers: {
2022-11-29 23:44:11 +01:00
changeFontsize: (state, action: PayloadAction<SettingsState['fontsize']>) => {
2021-03-10 10:22:53 +01:00
state.fontsize = action.payload
},
2022-11-29 23:44:11 +01:00
changeLanguage: (state, action: PayloadAction<NonNullable<SettingsState['language']>>) => {
state.language = action.payload
2020-11-29 18:08:31 +01:00
},
2022-11-29 23:44:11 +01:00
changeTheme: (state, action: PayloadAction<NonNullable<SettingsState['theme']>>) => {
2020-11-29 18:08:31 +01:00
state.theme = action.payload
2020-12-20 18:41:28 +01:00
},
2022-11-29 23:44:11 +01:00
changeDarkTheme: (state, action: PayloadAction<NonNullable<SettingsState['darkTheme']>>) => {
2022-02-12 14:51:01 +01:00
state.darkTheme = action.payload
},
2022-11-29 23:44:11 +01:00
changeBrowser: (state, action: PayloadAction<NonNullable<SettingsState['browser']>>) => {
2020-12-20 18:41:28 +01:00
state.browser = action.payload
2022-05-13 00:04:15 +02:00
},
changeStaticEmoji: (
state,
action: PayloadAction<NonNullable<SettingsState['staticEmoji']>>
) => {
state.staticEmoji = action.payload
}
}
})
2022-11-29 23:44:11 +01:00
export const getSettingsFontsize = (state: RootState) => state.settings.fontsize || 0
export const getSettingsLanguage = (state: RootState) => state.settings.language
2020-11-29 18:08:31 +01:00
export const getSettingsTheme = (state: RootState) => state.settings.theme
2022-11-29 23:44:11 +01:00
export const getSettingsDarkTheme = (state: RootState) => state.settings.darkTheme
2020-12-20 18:41:28 +01:00
export const getSettingsBrowser = (state: RootState) => state.settings.browser
2022-11-29 23:44:11 +01:00
export const getSettingsStaticEmoji = (state: RootState) => state.settings.staticEmoji
2022-02-12 14:51:01 +01:00
export const {
changeFontsize,
changeLanguage,
changeTheme,
changeDarkTheme,
2022-05-13 00:04:15 +02:00
changeBrowser,
changeStaticEmoji
2022-02-12 14:51:01 +01:00
} = settingsSlice.actions
export default settingsSlice.reducer