Skip to content

Commit

Permalink
reset local position state when commit hash changes
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-d committed Dec 9, 2024
1 parent 54ada6d commit e4936e6
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions web/src/hooks/usePostions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type Position = {
isVested: boolean;
};

interface PositionStore {
interface PositionStoreState {
// positionsLocal is a list of positions modified by local actions
positionsLocal: {
[chainId: number]: {
Expand All @@ -58,6 +58,10 @@ interface PositionStore {
};
};
};
commitHash: string;
}

interface PositionStoreActions {
// receive new positions, preferring the newest version of each position
updatePositionsFromGraph: (
chainId: ChainIdTypes,
Expand All @@ -72,14 +76,25 @@ interface PositionStore {
address: `0x${string}`,
newPosition: Position,
) => void;
// clear state entirely, useful for invalidating if deployment address
// has changed
reset: () => void;
}

interface PositionStore extends PositionStoreState, PositionStoreActions {}

const initialState = {
positions: {},
positionsLocal: {},
commitHash: process.env.NEXT_PUBLIC_GIT_HASH,
};

const usePositionStore = create<PositionStore>()(
persist(
(set) => {
return {
positions: {},
positionsLocal: {},
...initialState,
reset: () => set(initialState),
updatePositionsFromGraph: (chainId, address, newPositions) =>
set(({ positions, positionsLocal }) => {
const positionsUpdated = newPositions.reduce(
Expand Down Expand Up @@ -187,10 +202,19 @@ export const usePositions = () => {
(useAccount().address?.toLowerCase() as `0x${string}`) || undefined;
const positionsData = useFragment(PositionsFragment, userData?.getWallet);
const positions = usePositionStore((s) => s.positions);
const { reset: resetPositions, commitHash } = usePositionStore();
const updatePositionLocal = usePositionStore((s) => s.updatePositionLocal);
const updatePositionsFromGraph = usePositionStore(
(s) => s.updatePositionsFromGraph,
);

// clear local positions if the commit hash has changed
useEffect(() => {
if (commitHash !== process.env.NEXT_PUBLIC_GIT_HASH) {
resetPositions();
}
}, [resetPositions, commitHash]);

const chainPositions = useMemo(
() =>
address
Expand Down

0 comments on commit e4936e6

Please sign in to comment.