forked from phenom/healer-man
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { create } from "zustand";
|
|
import { fetchDungeonRouteApprovals } from "../app/dungeonSimulationClient";
|
|
import type { DungeonRouteApproval, DungeonRouteDefinitionV1 } from "./dungeonSimulationTypes";
|
|
import {
|
|
cacheDungeonRouteApprovals,
|
|
readCachedDungeonRouteApprovals,
|
|
routeHasExactApproval,
|
|
} from "./dungeonRoutes";
|
|
|
|
export interface DungeonRouteState {
|
|
approvals: readonly DungeonRouteApproval[];
|
|
loading: boolean;
|
|
refreshedAt: number | null;
|
|
refresh: () => Promise<void>;
|
|
isApproved: (route: DungeonRouteDefinitionV1 | null) => boolean;
|
|
}
|
|
|
|
export const useDungeonRouteStore = create<DungeonRouteState>((set, get) => ({
|
|
approvals: readCachedDungeonRouteApprovals(),
|
|
loading: false,
|
|
refreshedAt: null,
|
|
refresh: async () => {
|
|
if (get().loading) return;
|
|
set({ loading: true });
|
|
try {
|
|
const approvals = await fetchDungeonRouteApprovals();
|
|
cacheDungeonRouteApprovals(approvals);
|
|
set({ approvals, refreshedAt: Date.now(), loading: false });
|
|
} catch {
|
|
// An exact-hash cached approval is intentionally usable during outages.
|
|
set({ loading: false });
|
|
}
|
|
},
|
|
isApproved: (route) => routeHasExactApproval(route, get().approvals),
|
|
}));
|