tooot/src/utils/slices/settingsSlice.ts

107 lines
3.0 KiB
TypeScript
Raw Normal View History

import { createAsyncThunk, 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-05-13 00:04:15 +02:00
import { SettingsV2 } from '@utils/migrations/settings/v2'
2020-12-29 16:19:04 +01:00
import * as Analytics from 'expo-firebase-analytics'
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'
2021-02-20 19:12:44 +01:00
export const changeAnalytics = createAsyncThunk(
'settings/changeAnalytics',
async (newValue: SettingsState['analytics']) => {
await Analytics.setAnalyticsCollectionEnabled(newValue)
2021-08-29 15:25:38 +02:00
return { newValue }
2021-02-20 19:12:44 +01:00
}
)
2022-05-13 00:04:15 +02:00
export type SettingsState = SettingsV2
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
},
2021-01-31 03:09:35 +01:00
language: Object.keys(
2022-02-06 23:25:16 +01:00
pickBy(LOCALES, (_, key) => Localization.locale.startsWith(key))
2021-01-31 03:09:35 +01:00
)
? Object.keys(
2022-02-06 23:25:16 +01:00
pickBy(LOCALES, (_, key) => Localization.locale.startsWith(key))
2021-01-31 03:09:35 +01:00
)[0]
: '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-05-13 00:04:15 +02:00
staticEmoji: false,
2020-12-30 14:33:33 +01:00
analytics: true
}
const settingsSlice = createSlice({
name: 'settings',
2021-01-07 19:13:09 +01:00
initialState: settingsInitialState as SettingsState,
reducers: {
2021-03-10 10:22:53 +01:00
changeFontsize: (
state,
action: PayloadAction<SettingsState['fontsize']>
) => {
state.fontsize = action.payload
},
changeLanguage: (
state,
action: PayloadAction<NonNullable<SettingsState['language']>>
) => {
state.language = action.payload
2020-11-29 18:08:31 +01:00
},
changeTheme: (
state,
action: PayloadAction<NonNullable<SettingsState['theme']>>
) => {
state.theme = action.payload
2020-12-20 18:41:28 +01:00
},
2022-02-12 14:51:01 +01:00
changeDarkTheme: (
state,
action: PayloadAction<NonNullable<SettingsState['darkTheme']>>
) => {
state.darkTheme = action.payload
},
2020-12-20 18:41:28 +01:00
changeBrowser: (
state,
action: PayloadAction<NonNullable<SettingsState['browser']>>
) => {
state.browser = action.payload
2022-05-13 00:04:15 +02:00
},
changeStaticEmoji: (
state,
action: PayloadAction<NonNullable<SettingsState['staticEmoji']>>
) => {
state.staticEmoji = action.payload
}
2020-12-29 16:19:04 +01:00
},
extraReducers: builder => {
builder.addCase(changeAnalytics.fulfilled, (state, action) => {
2021-08-29 15:25:38 +02:00
state.analytics = action.payload.newValue
2020-12-29 16:19:04 +01:00
})
}
})
2021-03-10 10:22:53 +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-02-12 14:51:01 +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-05-13 00:04:15 +02:00
export const getSettingsStaticEmoji = (state: RootState) =>
state.settings.staticEmoji
2020-12-29 16:19:04 +01:00
export const getSettingsAnalytics = (state: RootState) =>
state.settings.analytics
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