mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-01 15:48:23 -05:00
58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
import type { MainWeaponId } from "@sendou/in-game-lists/types";
|
|
import { useActionSubmit } from "~/hooks/useActionSubmit";
|
|
import { useRecentlyReportedWeapons } from "~/hooks/useRecentlyReportedWeapons";
|
|
import { weaponReportActionSchema } from "./match-page-schemas";
|
|
import type { WeaponReporterMap, WeaponReporterProps } from "./WeaponReporter";
|
|
|
|
/**
|
|
* Wires the `<WeaponReporter />` component to the standard
|
|
* `REPORT_WEAPON` / `UNDO_WEAPON_REPORT` fetcher actions and to the
|
|
* locally persisted recently-reported weapons list.
|
|
*
|
|
* `maps` is the maps the viewer can report a weapon for, in play order, each
|
|
* carrying its `mapIndex` in the match's map list — a viewer who sat out a map
|
|
* simply has no entry for it. `pastReported` is the weapons the viewer has
|
|
* already reported, paired with the `mapIndex` they were reported for.
|
|
*/
|
|
export function useMatchWeaponReport({
|
|
maps,
|
|
pastReported,
|
|
}: {
|
|
maps: WeaponReporterMap[];
|
|
pastReported: { mapIndex: number; weaponSplId: MainWeaponId }[];
|
|
}): WeaponReporterProps {
|
|
const weaponReport = useActionSubmit(weaponReportActionSchema);
|
|
const { recentlyReportedWeapons, addRecentlyReportedWeapon } =
|
|
useRecentlyReportedWeapons();
|
|
|
|
const reportedMapIndexes = new Set(pastReported.map((w) => w.mapIndex));
|
|
const nextMapIndex =
|
|
maps.find((map) => !reportedMapIndexes.has(map.mapIndex))?.mapIndex ?? -1;
|
|
const undoMapIndex = pastReported.reduce(
|
|
(max, w) => Math.max(max, w.mapIndex),
|
|
-1,
|
|
);
|
|
|
|
return {
|
|
maps,
|
|
pastReported: [...pastReported]
|
|
.sort((a, b) => a.mapIndex - b.mapIndex)
|
|
.map((w) => w.weaponSplId),
|
|
nextMapIndex,
|
|
quickSelectWeaponIds: recentlyReportedWeapons,
|
|
isSubmitting: weaponReport.state !== "idle",
|
|
onSubmit: (weaponSplId) => {
|
|
addRecentlyReportedWeapon(weaponSplId);
|
|
if (nextMapIndex < 0) return;
|
|
weaponReport.submit("REPORT_WEAPON", {
|
|
weaponSplId,
|
|
mapIndex: nextMapIndex,
|
|
});
|
|
},
|
|
onUndo: () => {
|
|
if (undoMapIndex < 0) return;
|
|
weaponReport.submit("UNDO_WEAPON_REPORT", { mapIndex: undoMapIndex });
|
|
},
|
|
};
|
|
}
|