mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-08-08 14:30:55 -05:00
41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import { z } from "zod";
|
|
import type { MainWeaponId } from "~/modules/in-game-lists/types";
|
|
import { mainWeaponIds } from "~/modules/in-game-lists/weapon-ids";
|
|
import { usePersistedState } from "~/modules/persisted-state/hooks";
|
|
import * as PersistedState from "~/modules/persisted-state/persisted-state";
|
|
import { numericEnum } from "~/utils/zod";
|
|
|
|
const MAX_REPORTED_WEAPONS = 7;
|
|
|
|
export const recentlyReportedWeaponsPersisted = PersistedState.define({
|
|
key: "sq__recently-reported-weapons",
|
|
storage: "local",
|
|
schema: z.array(numericEnum(mainWeaponIds)),
|
|
default: [],
|
|
});
|
|
|
|
/**
|
|
* This hook provides access to the list of recently reported weapons,
|
|
* which is persisted in local storage, and a function to add a new weapon
|
|
* to the list.
|
|
*
|
|
* If a weapon is added that already exists in the list, it will be moved to the front of the list.
|
|
* If the list exceeds the maximum number of reported weapons, the oldest weapon will be removed.
|
|
*/
|
|
export function useRecentlyReportedWeapons() {
|
|
const [recentlyReportedWeapons, setRecentlyReportedWeapons] =
|
|
usePersistedState(recentlyReportedWeaponsPersisted);
|
|
|
|
const addRecentlyReportedWeapon = (weapon: MainWeaponId) => {
|
|
setRecentlyReportedWeapons((previous) =>
|
|
PersistedState.prependToRecentList(
|
|
previous,
|
|
weapon,
|
|
MAX_REPORTED_WEAPONS,
|
|
),
|
|
);
|
|
};
|
|
|
|
return { recentlyReportedWeapons, addRecentlyReportedWeapon };
|
|
}
|