Skip to content

Commit

Permalink
Add use-latest-melee-id hook
Browse files Browse the repository at this point in the history
  • Loading branch information
xbtmatt committed Feb 19, 2025
1 parent c2b3c85 commit 0c4fc1e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
16 changes: 2 additions & 14 deletions src/typescript/frontend/src/components/pages/arena/ArenaClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import { ROUTES } from "router/routes";
import ChartContainer from "@/components/charts/ChartContainer";
import { type ClassValue } from "clsx";
import { useReliableSubscribe } from "@hooks/use-reliable-subscribe";
import { useEventStore } from "context/event-store-context";
import { compareBigInt } from "@sdk/utils";
import { useRouter } from "next/navigation";
import { useLatestMeleeID } from "@hooks/use-latest-melee-id";

const RewardsRemainingBox = ({ rewardsRemaining }: { rewardsRemaining: bigint }) => {
const { isMobile } = useMatchBreakpoints();
Expand Down Expand Up @@ -121,21 +120,10 @@ export const ArenaClient = (props: ArenaProps) => {

useReliableSubscribe({ eventTypes: ["Swap"], arena: true });

const asdf = useEventStore((s) => s.meleeEvents);
useEffect(() => console.log(asdf));

// `meleeEvents` is really small, probably only 2 events at a time ever, never more than a dozen in local development.
const latestMeleeID = useEventStore(
(s) =>
s.meleeEvents
.map((v) => v.melee.meleeID)
.toSorted((m1, m2) => compareBigInt(m1, m2))
.pop() ?? -1n
);
const latestMeleeID = useLatestMeleeID();

useEffect(() => {
if (latestMeleeID > props.arenaInfo.meleeID) {
console.log("new melee?");
router.refresh();
}
}, [latestMeleeID, props.arenaInfo.meleeID, router]);
Expand Down
17 changes: 17 additions & 0 deletions src/typescript/frontend/src/hooks/use-latest-melee-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEventStore } from "context/event-store-context";
import { useMemo } from "react";

export const useLatestMeleeID = () => {
const melees = useEventStore((s) => s.meleeEvents);

const latest = useMemo(
() =>
melees
.map(({ melee }) => melee.meleeID)
.sort()
.at(-1) ?? -1n,
[melees]
);

return latest;
};
2 changes: 1 addition & 1 deletion src/typescript/frontend/src/lib/store/event/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { type SubscribeBarsCallback } from "@static/charting_library/datafeed-ap
import { type LatestBar } from "./candlestick-bars";
import { type WritableDraft } from "immer";
import { type ClientState, type ClientActions } from "../websocket/store";
import { ArenaActions, type ArenaState } from "../arena/store";
import { type ArenaActions, type ArenaState } from "../arena/store";
import { type Flatten } from "@sdk-types";

// Aliased to avoid repeating the type names over and over.
Expand Down

0 comments on commit 0c4fc1e

Please sign in to comment.