11import type { PayloadAction } from '@reduxjs/toolkit' ;
22import { createSlice } from '@reduxjs/toolkit' ;
3+ import React from 'react' ;
4+ import { useTranslation } from 'react-i18next' ;
5+ import { useSelector } from 'react-redux' ;
36import { PARAMS , setQueryParm , useQueryParam } from '../../utils/queryParam' ;
7+ import { getLocalStorageObject } from '../../utils/usefulFunctions' ;
48import type { RootState } from '../store' ;
59import { changeAll } from './controlsSlice' ;
610
711export type GameMode = 'fractals' | 'raids' | 'wvw' ;
812
9- function getLocalStorageState ( ) : object {
10- try {
11- const stored = localStorage . getItem ( SETTINGS_STORAGE_KEY ) ;
12- if ( stored ) {
13- return JSON . parse ( stored ) as object ;
14- }
15- } catch {
16- return { } ;
17- }
18- return { } ;
19- }
20-
2113const SETTINGS_STORAGE_KEY = 'globalSettings' ;
2214
2315const defaultState = { expertMode : true , gameMode : 'fractals' , language : 'en' } ;
2416// eslint-disable-next-line react-hooks/rules-of-hooks
2517const gameModeParam = useQueryParam ( { key : PARAMS . GAMEMODE } ) ;
2618
19+ // language is included here for historical reasons; src/utils/i18n.js imports this
2720export const loadedSettings = {
2821 ...defaultState ,
29- ...getLocalStorageState ( ) , // override default state with potentially saved localStorage variables
22+ ...getLocalStorageObject ( SETTINGS_STORAGE_KEY ) , // override default state with potentially saved localStorage variables
3023 ...( gameModeParam && { gameMode : gameModeParam } ) , // gameMode from query param takes priority
3124} as {
3225 expertMode : boolean ;
@@ -37,18 +30,31 @@ export const loadedSettings = {
3730// append the gamemode to the query param if no query param is present
3831if ( ! gameModeParam ) setQueryParm ( { key : PARAMS . GAMEMODE , value : loadedSettings . gameMode } ) ;
3932
33+ // save user settings to localStorage (as a react hook, to access react-i18next language)
34+ const getSliceState = ( state : RootState ) => state . optimizer . userSettings ;
35+ export const useSaveUserSettingsOnChange = ( ) => {
36+ const { i18n } = useTranslation ( ) ;
37+ const { language } = i18n ;
38+ const sliceState = useSelector ( getSliceState ) ;
39+
40+ React . useEffect ( ( ) => {
41+ const settings = JSON . stringify ( { ...sliceState , language } ) ;
42+ console . log ( `saving... ${ settings } ` ) ;
43+
44+ localStorage . setItem ( SETTINGS_STORAGE_KEY , settings ) ;
45+ } , [ sliceState , language ] ) ;
46+ } ;
47+
4048export const userSettingsSlice = createSlice ( {
4149 name : 'userSettings' ,
4250
4351 initialState : {
4452 expertMode : loadedSettings . expertMode ,
4553 gameMode : loadedSettings . gameMode ,
54+ // language is excluded from this redux slice; react-i18next has its own mutable state
4655 } ,
4756
4857 reducers : {
49- changeAllUserSettings : ( state , action ) => {
50- return { ...state , ...action . payload ?. userSettings } ;
51- } ,
5258 changeExpertMode : ( state , action : PayloadAction < boolean > ) => {
5359 state . expertMode = action . payload ;
5460 } ,
@@ -74,7 +80,6 @@ export const userSettingsSlice = createSlice({
7480export const getExpertMode = ( state : RootState ) => state . optimizer . userSettings . expertMode ;
7581export const getGameMode = ( state : RootState ) => state . optimizer . userSettings . gameMode ;
7682
77- export const { changeAllUserSettings, changeExpertMode, changeGameMode } =
78- userSettingsSlice . actions ;
83+ export const { changeExpertMode, changeGameMode } = userSettingsSlice . actions ;
7984
8085export default userSettingsSlice . reducer ;
0 commit comments