mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-10 04:40:46 -05:00
30 lines
800 B
TypeScript
30 lines
800 B
TypeScript
import { GetBuildsByUserData } from "prisma/queries/getBuildsByUser";
|
|
import { useState } from "react";
|
|
import useSWR from "swr";
|
|
|
|
export function useBuildsByUser(userId?: number) {
|
|
const [weapon, setWeapon] = useState<string | null>(null);
|
|
|
|
const { data = [] } = useSWR<GetBuildsByUserData>(
|
|
userId ? `/api/users/${userId}/builds` : null
|
|
);
|
|
|
|
const weaponCounts = data.reduce((acc: [string, number][], build) => {
|
|
const foundTuple = acc.find((tuple) => tuple[0] === build.weapon);
|
|
if (foundTuple) {
|
|
foundTuple[1] = foundTuple[1] + 1;
|
|
return acc;
|
|
}
|
|
|
|
acc.push([build.weapon, 1]);
|
|
return acc;
|
|
}, []);
|
|
|
|
return {
|
|
data: weapon ? data.filter((build) => build.weapon === weapon) : data,
|
|
weaponCounts,
|
|
setWeapon,
|
|
buildCount: data.length,
|
|
};
|
|
}
|