Skip to content

Commit

Permalink
Merge pull request #1 from notV3NOM/development
Browse files Browse the repository at this point in the history
Additionally sort the generated builds
  • Loading branch information
notV3NOM authored Jan 23, 2024
2 parents f684d05 + 9ad66e1 commit b5ed586
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import { FieldDisplayList, NodeFieldDisplay } from '../FieldDisplay'
import ImgIcon from '../Image/ImgIcon'
import SqBadge from '../SqBadge'

export default function StatDisplayComponent() {
export default function StatDisplayComponent({
index,
sortOptions,
}: {
index?: number
sortOptions?: { sortBase: string[]; ascending: boolean }
}) {
const { data } = useContext(DataContext)
const sections = useMemo(
() =>
Expand All @@ -24,6 +30,20 @@ export default function StatDisplayComponent() {
),
[data]
)
if (sortOptions) {
const sortValues = JSON.parse(localStorage.getItem('sortValues') || '{}')
for (const [sectionKey, displayNs] of sections) {
for (const [nodeKey, n] of Object.entries(displayNs)) {
if (
JSON.stringify(sortOptions.sortBase) ===
JSON.stringify([sectionKey, nodeKey])
) {
sortValues[index.toString()] = n.value
localStorage.setItem('sortValues', JSON.stringify(sortValues))
}
}
}
}
return (
<Box sx={{ mr: -1, mb: -1 }}>
<Masonry columns={{ xs: 1, sm: 2, md: 3, xl: 4 }} spacing={1}>
Expand Down
10 changes: 10 additions & 0 deletions apps/frontend/src/app/Database/DataManagers/BuildSettingData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export interface BuildSetting {
compareBuild: boolean
levelLow: number
levelHigh: number
sortBase?: string[]
sortAsc?: boolean
}

export class BuildSettingDataManager extends DataManager<
Expand Down Expand Up @@ -115,6 +117,8 @@ export class BuildSettingDataManager extends DataManager<
compareBuild,
levelLow,
levelHigh,
sortBase,
sortAsc,
} = obj as BuildSetting

if (typeof statFilters !== 'object') statFilters = {}
Expand Down Expand Up @@ -166,6 +170,8 @@ export class BuildSettingDataManager extends DataManager<
)
maxBuildsToShow = maxBuildsToShowDefault
if (!plotBase || !Array.isArray(plotBase)) plotBase = undefined
if (!sortBase || !Array.isArray(sortBase)) sortBase = undefined
if (sortAsc === undefined) sortAsc = false
if (compareBuild === undefined) compareBuild = false
if (levelLow === undefined) levelLow = 0
if (levelHigh === undefined) levelHigh = 20
Expand Down Expand Up @@ -193,6 +199,8 @@ export class BuildSettingDataManager extends DataManager<
compareBuild,
levelLow,
levelHigh,
sortBase,
sortAsc,
}
}
override get(key: CharacterKey) {
Expand Down Expand Up @@ -220,6 +228,8 @@ const initialBuildSettings: BuildSetting = deepFreeze({
compareBuild: true,
levelLow: 0,
levelHigh: 20,
sortBase: undefined,
sortAsc: false,
})

export function handleArtSetExclusion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type BuildDisplayItemProps = {
disabled?: boolean
extraButtonsRight?: JSX.Element
extraButtonsLeft?: JSX.Element
index?: number
sortOptions?: { sortBase: string[]; ascending: boolean }
}
//for displaying each artifact build
export default function BuildDisplayItem({
Expand All @@ -64,6 +66,8 @@ export default function BuildDisplayItem({
extraButtonsRight,
extraButtonsLeft,
disabled,
index,
sortOptions,
}: BuildDisplayItemProps) {
const {
character: { key: characterKey, equippedArtifacts },
Expand Down Expand Up @@ -213,7 +217,7 @@ export default function BuildDisplayItem({
{artNanos}
</Grid>
<DataContext.Provider value={statProviderContext}>
<StatDisplayComponent />
<StatDisplayComponent index={index} sortOptions={sortOptions} />
</DataContext.Provider>
</CardContent>
</Suspense>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ import BootstrapTooltip from '../../../../../../Components/BootstrapTooltip'
import CardLight from '../../../../../../Components/Card/CardLight'
import SortIcon from '@mui/icons-material/Sort'
import OptimizationTargetSelector from '../OptimizationTargetSelector'
import { useContext } from 'react'
import { CharacterContext } from '../../../../../../Context/CharacterContext'
import useBuildSetting from '../../useBuildSetting'

type SortCardProps = {
sortBase?: string[]
setSortBase: (path: string[] | undefined) => void
ascending: boolean
setAscending: (ascending: boolean) => void
optimizationTarget?: string[]
}

export default function SortCard({
sortBase,
setSortBase,
ascending,
setAscending,
optimizationTarget,
}: SortCardProps) {
export default function SortCard() {
const { t } = useTranslation(['page_character_optimize', 'ui'])
const {
character: { key: characterKey },
} = useContext(CharacterContext)
const { buildSetting, buildSettingDispatch } = useBuildSetting(characterKey)
const { optimizationTarget, sortBase, sortAsc } = buildSetting

return (
<CardLight>
Expand All @@ -34,22 +28,28 @@ export default function SortCard({
<span>
<OptimizationTargetSelector
optimizationTarget={sortBase}
setTarget={(target) => setSortBase(target)}
setTarget={(target) => {
buildSettingDispatch({ sortBase: target })
}}
defaultText={'Select a Sort Target'}
disabled={false}
/>
</span>
</Grid>
<Grid item>
<Button
onClick={() => setAscending(!ascending)}
onClick={() => {
buildSettingDispatch({ sortAsc: !sortAsc })
}}
startIcon={
<SortIcon
sx={{ transform: ascending ? 'scale(1, -1)' : 'scale(1)' }}
sx={{
transform: sortAsc ? 'scale(1, -1)' : 'scale(1)',
}}
/>
}
>
{ascending ? t('ui:Ascending') : t('ui:Descending')}
{sortAsc ? t('ui:Ascending') : t('ui:Descending')}
</Button>
</Grid>
<Grid item>
Expand All @@ -61,8 +61,11 @@ export default function SortCard({
<Button
color="error"
onClick={() => {
setSortBase(optimizationTarget)
setAscending(false)
localStorage.removeItem('sortValues')
buildSettingDispatch({
sortBase: optimizationTarget,
sortAsc: false,
})
}}
disabled={false}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,15 @@ export default function TabBuild() {
maxBuildsToShow,
levelLow,
levelHigh,
sortBase,
sortAsc,
} = buildSetting

const {
buildResult: { builds, buildDate },
buildResultDispatch,
} = useBuildResult(characterKey)

const teamData = useTeamData(characterKey, mainStatAssumptionLevel)
const { characterSheet, target: data } =
teamData?.[characterKey as CharacterKey] ?? {}
Expand All @@ -173,9 +177,6 @@ export default function TabBuild() {
[setArtsDirty, database]
)

const [sortBase, setSortBase] = useState<string[]>(optimizationTarget)
const [ascending, setAscending] = useState<boolean>(false)

const deferredArtsDirty = useDeferredValue(artsDirty)
const deferredBuildSetting = useDeferredValue(buildSetting)
const filteredArts = useMemo(() => {
Expand Down Expand Up @@ -298,7 +299,8 @@ export default function TabBuild() {
} = buildSetting
if (!characterKey || !optimizationTarget) return

setSortBase(optimizationTarget)
buildSettingDispatch({ sortBase: optimizationTarget, sortAsc: false })

const split = compactArtifacts(
filteredArts,
mainStatAssumptionLevel,
Expand Down Expand Up @@ -463,6 +465,7 @@ export default function TabBuild() {
}, [
buildSetting,
characterKey,
buildSettingDispatch,
filteredArts,
database,
gender,
Expand Down Expand Up @@ -751,13 +754,7 @@ export default function TabBuild() {
/>
</Box>
<Box>
<SortCard
sortBase={sortBase}
setSortBase={setSortBase}
ascending={ascending}
setAscending={setAscending}
optimizationTarget={optimizationTarget}
/>
<SortCard />
</Box>
<CardLight>
<CardContent>
Expand Down Expand Up @@ -789,6 +786,10 @@ export default function TabBuild() {
onClick={() => {
setGraphBuilds(undefined)
buildResultDispatch({ builds: [], buildDate: 0 })
buildSettingDispatch({
sortBase: optimizationTarget,
sortAsc: false,
})
}}
>
Clear Builds
Expand Down Expand Up @@ -831,8 +832,6 @@ export default function TabBuild() {
disabled={!!generatingBuilds}
getLabel={getGraphBuildLabel}
setBuilds={setGraphBuilds}
sortBase={sortBase}
ascending={ascending}
/>
)}
<BuildList
Expand All @@ -842,8 +841,7 @@ export default function TabBuild() {
compareData={compareData}
disabled={!!generatingBuilds}
getLabel={getNormBuildLabel}
sortBase={sortBase}
ascending={ascending}
sortOptions={{ sortBase: sortBase, ascending: sortAsc }}
/>
</OptimizationTargetContext.Provider>
</DataContext.Provider>
Expand All @@ -860,20 +858,17 @@ function BuildList({
compareData,
disabled,
getLabel,
sortBase,
ascending,
sortOptions,
}: {
builds: string[][]
setBuilds?: (builds: string[][] | undefined) => void
characterKey?: '' | CharacterKey
characterKey?: CharacterKey
data?: UIData
compareData: boolean
disabled: boolean
getLabel: (index: number) => Displayable
sortBase: string[]
ascending: boolean
sortOptions?: { sortBase: string[]; ascending: boolean }
}) {
if (ascending) builds = [...builds].reverse()
const deleteBuild = useCallback(
(index: number) => {
if (setBuilds) {
Expand All @@ -891,7 +886,6 @@ function BuildList({
fallback={<Skeleton variant="rectangular" width="100%" height={600} />}
>
{builds?.map((build, index) => {
index = ascending ? builds.length - 1 - index : index
return (
characterKey &&
data && (
Expand All @@ -908,6 +902,7 @@ function BuildList({
compareData={compareData}
disabled={disabled}
deleteBuild={setBuilds ? deleteBuild : undefined}
sortOptions={sortOptions}
/>
</DataContextWrapper>
)
Expand All @@ -919,15 +914,19 @@ function BuildList({
builds,
characterKey,
data,
getLabel,
compareData,
disabled,
getLabel,
deleteBuild,
setBuilds,
ascending,
deleteBuild,
sortOptions,
]
)
return list
const sortedList = useMemo(
() => SortList(list, sortOptions),
[list, sortOptions]
)
return sortedList
}
function BuildItemWrapper({
index,
Expand All @@ -936,13 +935,15 @@ function BuildItemWrapper({
compareData,
disabled,
deleteBuild,
sortOptions,
}: {
index: number
label: Displayable
build: string[]
compareData: boolean
disabled: boolean
deleteBuild?: (index: number) => void
sortOptions?: { sortBase: string[]; ascending: boolean }
}) {
const { t } = useTranslation('page_character_optimize')
const location = useLocation()
Expand Down Expand Up @@ -980,6 +981,8 @@ function BuildItemWrapper({
)}
</>
}
index={index}
sortOptions={sortOptions}
/>
)
}
Expand Down Expand Up @@ -1026,3 +1029,23 @@ function DataContextWrapper({ children, characterKey, build, oldData }: Prop) {
</DataContext.Provider>
)
}

function SortList(
list: React.JSX.Element,
sortOptions: { sortBase: string[]; ascending: boolean }
) {
const sortValues = JSON.parse(localStorage.getItem('sortValues') || '{}')

const sortedBuildsWithValues = {}
list.props.children.map((child: JSX.Element, index: number) => {
sortedBuildsWithValues[child.key] = sortValues[index.toString()]
})

list.props.children.sort((a, b) => {
const valueA = sortedBuildsWithValues[a.key] || 0
const valueB = sortedBuildsWithValues[b.key] || 0
return sortOptions.ascending ? valueA - valueB : valueB - valueA
})

return list
}

0 comments on commit b5ed586

Please sign in to comment.