Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
frzyc committed Nov 12, 2024
1 parent 8fb6453 commit 9150390
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 72 deletions.
4 changes: 2 additions & 2 deletions libs/common/ui/src/hooks/useRefOverflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export function useRefOverflow() {
setIsOverflowY(ele ? isOverflowedY(ele) : false)
}
handleResize() // Check on mount and whenever the window resizes
if (ref.current) window.addEventListener('resize', handleResize)
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
}, [])
}, []) // Safe to keep empty as we only want mount/unmount behavior
return { isOverflowX, isOverflowY, ref }
}
function isOverflowedX(ref: HTMLElement) {
Expand Down
4 changes: 2 additions & 2 deletions libs/common/ui/src/hooks/useRefSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export function useRefSize() {
setHeight(ref.current?.clientHeight ?? 0)
}
handleResize() // Check on mount and whenever the window resizes
if (ref.current) window.addEventListener('resize', handleResize)
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
}, [])
}, []) // Safe to keep empty as we only want mount/unmount behavior
return { width, height, ref }
}
7 changes: 7 additions & 0 deletions libs/sr/consts/src/relic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ export type RelicMainStatKey = (typeof allRelicMainStatKeys)[number]
export const allRelicRarityKeys = [2, 3, 4, 5] as const
export type RelicRarityKey = (typeof allRelicRarityKeys)[number]

export function isRelicRarityKey(rarity: unknown): rarity is RelicRarityKey {
return (
typeof rarity === 'number' &&
allRelicRarityKeys.includes(rarity as RelicRarityKey)
)
}

export const allRelicSetCountKeys = [2, 4] as const
export type RelicSetCountKey = (typeof allRelicSetCountKeys)[number]

Expand Down
15 changes: 9 additions & 6 deletions libs/sr/db/src/Database/DataManagers/BuildTcDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { clamp, objKeyMap, objMap } from '@genshin-optimizer/common/util'
import type {
CharacterKey,
RelicMainStatKey,
RelicRarityKey,
RelicSetKey,
RelicSlotKey,
} from '@genshin-optimizer/sr/consts'
import {
allCharacterKeys,
allLightConeKeys,
allRelicRarityKeys,
allRelicSlotKeys,
allRelicSubStatKeys,
isRelicRarityKey,
relicMaxLevel,
relicSubstatTypeKeys,
} from '@genshin-optimizer/sr/consts'
Expand Down Expand Up @@ -155,14 +156,17 @@ function validateCharTCRelic(relic: unknown): IBuildTc['relic'] | undefined {
stats = objKeyMap(allRelicSubStatKeys, (k) =>
typeof stats[k] === 'number' ? stats[k] : 0
)
if (typeof rarity !== 'number' || !allRelicRarityKeys.includes(rarity))
rarity = 5
rarity = validateRelicRarity(rarity)

if (typeof sets !== 'object') sets = {}
// TODO: validate sets

return { slots, substats: { type, stats, rarity }, sets }
}

function validateRelicRarity(rarity: unknown): RelicRarityKey {
return isRelicRarityKey(rarity) ? rarity : 5
}
function validateCharTCRelicSlots(
slots: unknown
): IBuildTc['relic']['slots'] | undefined {
Expand All @@ -179,10 +183,9 @@ function validateCharTCRelicSlots(
return objMap(
slots as IBuildTc['relic']['slots'],
({ level, rarity, ...rest }) => {
if (typeof rarity !== 'number' || !allRelicRarityKeys.includes(rarity))
rarity = 5
rarity = validateRelicRarity(rarity)
return {
level: clamp(level, 0, relicMaxLevel[5]),
level: clamp(level, 0, relicMaxLevel[rarity]),
rarity,
...rest,
}
Expand Down
23 changes: 12 additions & 11 deletions libs/sr/page-team/src/BuildsDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ export function BuildsDisplay({ onClose }: { onClose?: () => void }) {
() => dbTCDirty && database.buildTcs.getBuildTcIds(characterKey),
[dbTCDirty, database, characterKey]
)
useEffect(() => {
database.builds.followAny(setDbDirty)
}, [database.builds, setDbDirty])
useEffect(() => {
database.buildTcs.followAny(setDbTCDirty)
}, [database.buildTcs, setDbTCDirty])
useEffect(
() => database.builds.followAny(setDbDirty),
[database.builds, setDbDirty]
)
useEffect(
() => database.buildTcs.followAny(setDbTCDirty),
[database.buildTcs, setDbTCDirty]
)

return (
<CardThemed bgt="dark">
Expand Down Expand Up @@ -168,9 +170,9 @@ function useActiveBuildSwap(
return
}
teammateDatum.buildType = newBuildType
if (newBuildId === 'real' && newBuildId)
if (newBuildType === 'real' && newBuildId)
teammateDatum.buildId = newBuildId
if (newBuildType === 'tc' && newBuildId)
else if (newBuildType === 'tc' && newBuildId)
teammateDatum.buildTcId = newBuildId
})
},
Expand Down Expand Up @@ -309,7 +311,6 @@ export function EquipRow({
gridTemplateRows: `repeat(${rows}, ${COMPACT_ELE_HEIGHT})`,
maxWidth: '100%',
width: '100%',
overflex: 'hidden',
}}
>
<LightConeCardCompact bgt="light" lightConeId={lightConeId} />
Expand All @@ -327,8 +328,8 @@ export function EquipRow({
)
}
export function BuildTC({ buildTcId }: { buildTcId: string }) {
const build = useBuildTc(buildTcId)!
const { name, description } = build
const build = useBuildTc(buildTcId)
const { name = 'ERROR', description = 'ERROR' } = build ?? {}
const { active, onActive: onClick } = useActiveBuildSwap('tc', buildTcId)
return (
<BuildBase
Expand Down
5 changes: 3 additions & 2 deletions libs/sr/page-team/src/TeamCalcProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ function useCharacterAndEquipment(meta: TeammateDatum | undefined) {
)
}, [meta?.buildType, buildTc?.lightCone, lightCone])
const relicTagEntries = useMemo(() => {
if (meta?.buildType === 'tc' && buildTc) return relicTcData(buildTc?.relic)
const tcrelic = buildTc?.relic
if (meta?.buildType === 'tc' && tcrelic) return relicTcData(tcrelic)
if (!relics) return []
return relicsData(Object.values(relics).filter(notEmpty))
}, [buildTc, meta?.buildType, relics])
}, [buildTc?.relic, meta?.buildType, relics])
return useMemo(() => {
if (!character) return []
return withMember(
Expand Down
2 changes: 1 addition & 1 deletion libs/sr/page-team/src/TeammateDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function CurrentBuildDisplay() {
if (buildType === 'tc')
unFollow = database.buildTcs.follow(buildTcId, setDbDirty)
return () => unFollow()
})
}, [buildId, buildTcId, buildType, database, setDbDirty])
const [show, onShow, onHide] = useBoolState()
return (
<CardThemed sx={{ width: '100%' }}>
Expand Down
45 changes: 15 additions & 30 deletions libs/sr/ui/src/LightCone/LightConeEditor/LightConeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useTranslation } from 'react-i18next'
import { LightConeEditorCard } from './LightConeEditorCard'

export type LightConeEditorProps = {
lightConeIdToEdit?: string
lightConeIdToEdit?: string | '' | 'new'
cancelEdit: () => void
}

Expand Down Expand Up @@ -68,41 +68,26 @@ export function LightConeEditor({
const footer = useMemo(
() => (
<Box display="flex" gap={2}>
{dbLightCone ? (
<Button
startIcon={<Add />}
onClick={() => {
validatedLightcone && database.lightCones.new(validatedLightcone)
clear()
}}
disabled={!validatedLightcone}
color="primary"
>
{t('editor.btnAdd')}
</Button>
{validatedLightcone && dbLightCone && (
<Button
startIcon={<Add />}
startIcon={<Update />}
onClick={() => {
validatedLightcone &&
database.lightCones.set(dbLightCone.id, validatedLightcone)
clear()
}}
disabled={!validatedLightcone}
color="primary"
>
{t('editor.btnSave')}
</Button>
) : (
<Button
startIcon={<Add />}
onClick={() => {
validatedLightcone && database.lightCones.new(validatedLightcone)
clear()
}}
disabled={!validatedLightcone}
color="primary"
>
{t('editor.btnAdd')}
</Button>
)}
{validatedLightcone && dbLightCone && (
<Button
startIcon={<Update />}
onClick={() => {
lightConeState &&
database.lightCones.set(dbLightCone.id, lightConeState)
clear()
}}
disabled={!lightConeState}
color="success"
>
{t('editor.btnUpdate')}
Expand All @@ -125,7 +110,7 @@ export function LightConeEditor({
)}
</Box>
),
[clear, database, dbLightCone, lightConeState, t, validatedLightcone]
[clear, database, dbLightCone, t, validatedLightcone]
)

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function LightConeEditorCard({
return (
<CardThemed bgt="dark">
<CardHeader
title="Light Cone Editor"
title="Light Cone Editor" // TODO: translation
action={
<IconButton onClick={onClose}>
<CloseIcon />
Expand Down Expand Up @@ -175,7 +175,7 @@ function LevelDropdown({
<DropdownButton
title={
level
? `${t('lv')} ${level}/${ascensionMaxLevel[ascension!]}`
? `${t('lv')} ${level}/${ascensionMaxLevel[ascension ?? 0]}`
: t('common:selectlevel')
}
color="primary"
Expand Down
4 changes: 2 additions & 2 deletions libs/sr/ui/src/Relic/RelicCardCompact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ export function RelicSubCard({
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
textWrap: 'nowrap',
whiteSpace: 'nowrap',
}}
>
<StatIcon statKey={key} />
<span>
{relic.substats.stats[key].toFixed(statToFixed(key))}
{(relic.substats.stats[key] ?? 0).toFixed(statToFixed(key))}
{getUnitStr(key)}
</span>
</Typography>
Expand Down
32 changes: 18 additions & 14 deletions libs/sr/ui/src/Relic/RelicRarityDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DropdownButton, StarsDisplay } from '@genshin-optimizer/common/ui'
import type { RarityKey } from '@genshin-optimizer/sr/consts'
import {
allRelicRarityKeys,
type RelicRarityKey,
Expand Down Expand Up @@ -27,13 +28,7 @@ export function RelicRarityDropdown({
{...props}
title={
rarity ? (
showNumber ? (
<span>
{rarity} <StarsDisplay stars={1} inline />
</span>
) : (
<StarsDisplay stars={rarity} inline />
)
<StarNumDisplay stars={rarity} showNumber={showNumber} />
) : (
t('editor.rarity')
)
Expand All @@ -46,15 +41,24 @@ export function RelicRarityDropdown({
disabled={filter ? !filter?.(rarity) : false}
onClick={() => onRarityChange(rarity)}
>
{showNumber ? (
<span>
{rarity} <StarsDisplay stars={1} inline />
</span>
) : (
<StarsDisplay stars={rarity} inline />
)}
<StarNumDisplay stars={rarity} showNumber={showNumber} />
</MenuItem>
))}
</DropdownButton>
)
}
function StarNumDisplay({
stars,
showNumber,
}: {
stars: RarityKey
showNumber?: boolean
}) {
if (showNumber)
return (
<span>
{stars} <StarsDisplay stars={1} inline />
</span>
)
return <StarsDisplay stars={stars} inline />
}

0 comments on commit 9150390

Please sign in to comment.