Files
blitzkit/src/stores/session.ts
T

92 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-10-01 18:43:40 -05:00
import { produce } from 'immer';
2023-09-19 15:52:39 +00:00
import { create } from 'zustand';
2023-12-06 12:40:43 -06:00
import { persist } from 'zustand/middleware';
2023-10-01 18:43:40 -05:00
import { CustomColumnDisplay } from '../app/tools/session/components/CustomColumn';
2023-12-03 10:43:42 -06:00
import { AccentColor, GrayColor } from '../constants/radixColors';
2023-09-19 15:52:39 +00:00
import { Region } from '../constants/regions';
2023-10-16 20:33:12 -05:00
import { WARGAMING_APPLICATION_ID } from '../constants/wargamingApplicationID';
2023-10-20 10:50:54 -05:00
import fetchBlitz from '../core/blitz/fetchBlitz';
2023-10-16 20:33:12 -05:00
import { NormalizedTankStats, TanksStats } from '../types/tanksStats';
2023-09-19 15:52:39 +00:00
2023-10-01 16:37:51 -05:00
type Session = (
2023-09-19 15:52:39 +00:00
| {
isTracking: false;
}
| {
isTracking: true;
region: Region;
id: number;
nickname: string;
tankStats: NormalizedTankStats;
2023-09-24 17:02:09 -05:00
time: number;
2023-10-01 16:37:51 -05:00
}
) & {
2023-12-13 11:46:36 -06:00
title: string;
2023-10-25 10:27:56 -05:00
resetPrompt: boolean;
embedPrompt: boolean;
2023-10-01 18:43:40 -05:00
customColumns: {
display: CustomColumnDisplay;
showDelta: boolean;
}[] & { length: 4 };
showTotal: boolean;
2023-10-01 19:00:05 -05:00
showCareer: boolean;
2023-12-03 10:43:42 -06:00
color: AccentColor | GrayColor;
2023-10-01 16:37:51 -05:00
};
2023-09-19 15:52:39 +00:00
2023-10-16 20:33:12 -05:00
export async function setSession(region: Region, id: number, nickname: string) {
// TODO: generalize this function and also check for more of these kind
const rawTankStats = (
await fetchBlitz<TanksStats>(
`https://api.wotblitz.${region}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
)
)[id];
const tankStats = rawTankStats.reduce<NormalizedTankStats>(
(accumulator, tank) => ({
...accumulator,
[tank.tank_id]: tank,
}),
{},
);
useSession.setState({
isTracking: true,
id,
region,
nickname,
tankStats,
time: Date.now(),
});
}
export function resetSession() {
const session = useSession.getState();
if (!session.isTracking) return;
setSession(session.region, session.id, session.nickname);
}
2023-09-19 15:52:39 +00:00
export const useSession = create<Session>()(
2023-12-06 12:40:43 -06:00
persist(
(set) => ({
2023-12-13 12:08:55 -06:00
title: 'Untitled session',
2023-12-06 12:40:43 -06:00
isTracking: false,
resetPrompt: true,
showTotal: true,
showCareer: true,
embedPrompt: true,
2023-10-01 18:43:40 -05:00
2023-12-06 12:40:43 -06:00
customColumns: [
{ display: 'battles', showDelta: false },
{ display: 'winrate', showDelta: true },
{ display: 'wn8', showDelta: false },
{ display: 'damage', showDelta: true },
],
color: 'slate',
}),
{ name: 'session' },
2023-10-01 16:37:51 -05:00
),
2023-09-19 15:52:39 +00:00
);
2023-10-01 18:43:40 -05:00
export default function mutateSession(recipe: (draft: Session) => void) {
useSession.setState(produce(recipe));
}