Skip to content

Commit

Permalink
feat(#275): default optimizer character on new sessions to the last s…
Browse files Browse the repository at this point in the history
…elected character (#285)

* feat: default optimizer character to the last selected character (#275)

On optimizer character change, save character id into
state.savedSession.optimizerCharacterId, then read react mount.

* use saved session key constant

* feat: add the saved session character to character tab doubleclick

---------

Co-authored-by: Fribbels <fribbelsgithub@gmail.com>
  • Loading branch information
khisaki and fribbels committed Apr 14, 2024
1 parent 48c8b67 commit 15f07a5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/components/optimizerTab/OptimizerForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Form } from 'antd'
import React, { useEffect } from 'react'
import { Optimizer } from 'lib/optimizer/optimizer'
import { Constants } from 'lib/constants.ts'
import { SavedSessionKeys } from 'lib/constantsSession'
import { FormRow, OptimizerMenuIds, TeammateFormRow } from 'components/optimizerTab/FormRow.tsx'
import FormCard from 'components/optimizerTab/FormCard'
import OptimizerOptionsDisplay from 'components/optimizerTab/optimizerForm/OptimizerOptionsDisplay.tsx'
Expand All @@ -28,10 +29,11 @@ export default function OptimizerForm() {
const [optimizerForm] = Form.useForm()
window.optimizerForm = optimizerForm

// On first load, display the first character from the roster
// On first load, load from last session, else display the first character from the roster
useEffect(() => {
const characters = DB.getCharacters() || []
OptimizerTabController.updateCharacter(characters[0]?.id)
const savedSessionCharacterId = window.store.getState().savedSession[SavedSessionKeys.optimizerCharacterId]
OptimizerTabController.updateCharacter(savedSessionCharacterId || characters[0]?.id)
}, [])

const onValuesChange = (changedValues, allValues, bypass) => {
Expand All @@ -56,6 +58,11 @@ export default function OptimizerForm() {
const request = allValues
console.log('@onValuesChange', request, changedValues)

if (keys[0] === 'characterId') {
window.store.getState().setSavedSessionKey(SavedSessionKeys.optimizerCharacterId, changedValues.characterId)
SaveState.save()
}

// Add any new characters to the list only if the user changed any value other than the characterId
if (!DB.getCharacterById(allValues.characterId) && keys[0] != 'characterId') {
DB.addFromForm(allValues)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/constantsSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum SavedSessionKeys {
optimizerCharacterId = 'optimizerCharacterId',
}
13 changes: 13 additions & 0 deletions src/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import objectHash from 'object-hash'
import { OptimizerTabController } from 'lib/optimizerTabController'
import { RelicAugmenter } from 'lib/relicAugmenter'
import { Constants, DEFAULT_STAT_DISPLAY, RelicSetFilterOptions } from 'lib/constants.ts'
import { SavedSessionKeys } from 'lib/constantsSession';
import { getDefaultForm } from 'lib/defaultForm'
import { Utils } from 'lib/utils'
import { SaveState } from 'lib/saveState'
Expand Down Expand Up @@ -115,6 +116,10 @@ window.store = create((set) => ({
[OptimizerMenuIds.teammates]: true,
},

savedSession: {
[SavedSessionKeys.optimizerCharacterId]: null,
},

setActiveKey: (x) => set(() => ({ activeKey: x })),
setCharacters: (x) => set(() => ({ characters: x })),
setCharactersById: (x) => set(() => ({ charactersById: x })),
Expand Down Expand Up @@ -142,6 +147,10 @@ window.store = create((set) => ({
setZeroPermutationsModalOpen: (x) => set(() => ({ zeroPermutationModalOpen: x })),
setExcludedRelicPotentialCharacters: (x) => set(() => ({ excludedRelicPotentialCharacters: x })),
setMenuSidebarOpen: (x) => set(() => ({ menuSidebarOpen: x })),
setSavedSession: (x) => set(() => ({ savedSession: x })),
setSavedSessionKey: (key, x) => set((state) => ({
savedSession: { ...state.savedSession, [key]: x },
})),
}))

export const DB = {
Expand Down Expand Up @@ -332,6 +341,10 @@ export const DB = {
window.store.getState().setOptimizerMenuState(menuState)
}

if (x.savedSession) {
window.store.getState().setSavedSession(x.savedSession)
}

window.store.getState().setExcludedRelicPotentialCharacters(x.excludedRelicPotentialCharacters || [])

assignRanks(x.characters)
Expand Down
4 changes: 4 additions & 0 deletions src/lib/optimizerTabController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CharacterConditionals } from './characterConditionals'
import { CharacterStats } from './characterStats'
import { StatCalculator } from './statCalculator'
import { defaultSetConditionals, defaultTeammate, getDefaultForm } from 'lib/defaultForm'
import { SavedSessionKeys } from 'lib/constantsSession'

let relics
let consts
Expand Down Expand Up @@ -572,6 +573,9 @@ export const OptimizerTabController = {
setCharacter: (id) => {
window.store.getState().setOptimizerTabFocusCharacter(id)
window.optimizerForm.setFieldValue('characterId', id)

window.store.getState().setSavedSessionKey(SavedSessionKeys.optimizerCharacterId, id)
SaveState.save()
},

// Update form values with the character
Expand Down
1 change: 1 addition & 0 deletions src/lib/saveState.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const SaveState = {
scoringMetadataOverrides: window.store.getState().scoringMetadataOverrides,
optimizerMenuState: window.store.getState().optimizerMenuState,
excludedRelicPotentialCharacters: window.store.getState().excludedRelicPotentialCharacters,
savedSession: window.store.getState().savedSession,
}

console.log('Saved state', state)
Expand Down

0 comments on commit 15f07a5

Please sign in to comment.