mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-02 08:07:40 -05:00
Move app to apps/web-react in pnpm workspace layout
This commit is contained in:
40
apps/web-react/app/hooks/useRecentlyReportedWeapons.ts
Normal file
40
apps/web-react/app/hooks/useRecentlyReportedWeapons.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user