2024-10-28 10:30:53 -05:00
|
|
|
import {
|
|
|
|
|
compositeStatsKeys,
|
|
|
|
|
deltaTankStats,
|
|
|
|
|
fetchTankDefinitions,
|
|
|
|
|
getTankStats,
|
|
|
|
|
idToRegion,
|
|
|
|
|
type IndividualTankStats,
|
|
|
|
|
} from '@blitzkit/core';
|
2024-09-07 21:45:00 -05:00
|
|
|
import strings from '@blitzkit/core/lang/en-US.json';
|
2024-11-04 10:36:43 -06:00
|
|
|
import { ReloadIcon } from '@radix-ui/react-icons';
|
|
|
|
|
import { ContextMenu } from '@radix-ui/themes';
|
2024-10-28 10:30:53 -05:00
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
2024-10-05 23:20:04 -05:00
|
|
|
import { breakdownConfig } from '../../constants/embeds';
|
2024-10-28 10:30:53 -05:00
|
|
|
import { EmbedBreakdownPersistent } from '../../stores/embedBreakdownPersistent';
|
2024-10-05 23:20:04 -05:00
|
|
|
import { useEmbedStateCurry } from '../../stores/embedState/utilities';
|
|
|
|
|
import { BreakdownEmbedCard, BreakdownEmbedWrapper } from '../TanksEmbed';
|
2024-08-08 14:18:22 -05:00
|
|
|
|
2024-09-08 21:52:12 -05:00
|
|
|
export const compositeStatsKeysOptions = compositeStatsKeys.map((value) => ({
|
2024-08-10 13:29:10 -05:00
|
|
|
label: strings.common.composite_stats[value],
|
|
|
|
|
value,
|
|
|
|
|
}));
|
|
|
|
|
|
2024-10-28 10:30:53 -05:00
|
|
|
const tankDefinitions = await fetchTankDefinitions();
|
2024-10-05 23:20:04 -05:00
|
|
|
|
2024-08-08 14:18:22 -05:00
|
|
|
export function BreakdownPreview() {
|
2024-10-28 10:31:49 -05:00
|
|
|
const tanks = Object.values(tankDefinitions.tanks);
|
2024-10-28 10:30:53 -05:00
|
|
|
const { useEmbedState } = useEmbedStateCurry<typeof breakdownConfig>();
|
2024-08-08 14:18:22 -05:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<BreakdownEmbedWrapper>
|
2024-10-28 10:30:53 -05:00
|
|
|
{useEmbedState('showTotal') && <BreakdownEmbedCard tank={null} />}
|
2024-08-08 14:18:22 -05:00
|
|
|
|
2024-10-28 10:30:53 -05:00
|
|
|
{tanks.slice(0, useEmbedState('listMaxTanks')).map((tank) => (
|
2024-08-08 14:18:22 -05:00
|
|
|
<BreakdownEmbedCard key={tank.id} tank={tank} />
|
|
|
|
|
))}
|
|
|
|
|
</BreakdownEmbedWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-10-27 22:29:28 -05:00
|
|
|
|
|
|
|
|
export function BreakdownRenderer() {
|
2024-10-28 10:30:53 -05:00
|
|
|
return (
|
|
|
|
|
<EmbedBreakdownPersistent.Provider>
|
|
|
|
|
<BreakdownRendererContent />
|
|
|
|
|
</EmbedBreakdownPersistent.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function BreakdownRendererContent() {
|
|
|
|
|
const { useEmbedState } = useEmbedStateCurry<typeof breakdownConfig>();
|
|
|
|
|
const params = new URLSearchParams(window.location.search);
|
2024-11-04 10:36:43 -06:00
|
|
|
const mutateEmbedBreakdownPersistent = EmbedBreakdownPersistent.useMutation();
|
2024-10-28 10:30:53 -05:00
|
|
|
const id = Number(params.get('id'));
|
|
|
|
|
const tankStatsA = EmbedBreakdownPersistent.use((state) => state[id] ?? []);
|
|
|
|
|
const [tankStatsB, setTankStatsB] = useState<IndividualTankStats[]>([]);
|
|
|
|
|
const diff = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
deltaTankStats(tankStatsA, tankStatsB).sort(
|
|
|
|
|
(a, b) => b.last_battle_time - a.last_battle_time,
|
|
|
|
|
),
|
|
|
|
|
[tankStatsB],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const interval = setInterval(async () => {
|
|
|
|
|
const newB = await getTankStats(idToRegion(id), id);
|
|
|
|
|
|
|
|
|
|
if (newB === null) return;
|
|
|
|
|
|
|
|
|
|
setTankStatsB(newB);
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
|
}, []);
|
2024-10-27 22:29:28 -05:00
|
|
|
|
2024-10-27 22:38:14 -05:00
|
|
|
return (
|
2024-11-04 10:36:43 -06:00
|
|
|
<ContextMenu.Root>
|
|
|
|
|
<ContextMenu.Trigger>
|
|
|
|
|
<BreakdownEmbedWrapper>
|
|
|
|
|
{useEmbedState('showTotal') && <BreakdownEmbedCard tank={null} />}
|
2024-10-27 22:38:14 -05:00
|
|
|
|
2024-11-04 10:36:43 -06:00
|
|
|
{diff.slice(0, useEmbedState('listMaxTanks')).map((diff) => {
|
|
|
|
|
const tank = tankDefinitions.tanks[diff.tank_id];
|
2024-10-28 10:30:53 -05:00
|
|
|
|
2024-11-04 10:36:43 -06:00
|
|
|
return <BreakdownEmbedCard key={tank.id} tank={tank} />;
|
|
|
|
|
})}
|
|
|
|
|
</BreakdownEmbedWrapper>
|
|
|
|
|
</ContextMenu.Trigger>
|
|
|
|
|
|
|
|
|
|
<ContextMenu.Content>
|
|
|
|
|
<ContextMenu.Item
|
|
|
|
|
color="red"
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
const newA = await getTankStats(idToRegion(id), id);
|
|
|
|
|
|
|
|
|
|
if (newA === null) return;
|
|
|
|
|
|
|
|
|
|
mutateEmbedBreakdownPersistent((draft) => {
|
|
|
|
|
draft[id] = newA;
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<ReloadIcon /> Reset
|
|
|
|
|
</ContextMenu.Item>
|
|
|
|
|
</ContextMenu.Content>
|
|
|
|
|
</ContextMenu.Root>
|
2024-10-27 22:38:14 -05:00
|
|
|
);
|
2024-10-27 22:29:28 -05:00
|
|
|
}
|