Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

update checker app to support internal navigation #75

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/features/checker/store/CheckerProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PropsWithChildren, useReducer } from "react";
import { PropsWithChildren, useReducer, useEffect } from "react";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

Expand All @@ -15,6 +15,39 @@ const queryClient = new QueryClient();

export const CheckerProvider = ({ children }: PropsWithChildren) => {
const [state, dispatch] = useReducer(checkerReducer, initialState);

useEffect(() => {
// Initialize history state on first load
if (window.history.state === null) {
window.history.replaceState(
{
from: "external",
route: state.route,
},
"",
);
}

const handlePopState = (event: PopStateEvent) => {
if (event.state?.route) {
// If navigating back to external route, let parent app handle it
if (event.state.from === "external") {
return;
}
dispatch({
type: "SET_ROUTE_HISTORY",
payload: {
route: event.state.route,
replace: true,
},
});
}
};

window.addEventListener("popstate", handlePopState);
return () => window.removeEventListener("popstate", handlePopState);
}, []);

return (
<QueryClientProvider client={queryClient}>
<CheckerContext.Provider value={state}>
Expand Down
8 changes: 8 additions & 0 deletions src/features/checker/store/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SetInitialStateAction,
SetPoolDataAction,
SetPoolFetchStateAction,
SetRouteHistoryAction,
} from "./types";

export const setInitialStateAction = (
Expand Down Expand Up @@ -48,3 +49,10 @@ export const setPoolFetchStateAction = (
type: "SET_POOL_DATA_FETCH_STATE",
payload,
});

export const setRouteHistoryAction = (
payload: SetRouteHistoryAction["payload"],
): SetRouteHistoryAction => ({
type: "SET_ROUTE_HISTORY",
payload,
});
11 changes: 10 additions & 1 deletion src/features/checker/store/actions/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hex } from "viem";

import { CheckerPoolData, CheckerPoolFetchState } from "../types";
import { CheckerContextRoute, CheckerPoolData, CheckerPoolFetchState, CheckerRoute } from "../types";

export interface SetInitialStateAction {
type: "SET_INITIAL_STATE";
Expand Down Expand Up @@ -43,7 +43,16 @@ export interface SetPoolFetchStateAction {
payload: CheckerPoolFetchState;
}

export type SetRouteHistoryAction = {
type: "SET_ROUTE_HISTORY";
payload: {
route: CheckerContextRoute;
replace?: boolean;
};
};

export type CheckerAction =
| SetRouteHistoryAction
| SetInitialStateAction
| SetPoolDataAction
| GoToReviewApplicationsAction
Expand Down
77 changes: 60 additions & 17 deletions src/features/checker/store/checkerReducer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generatePoolUUID } from "~checker/utils/generatePoolUUID";

import { CheckerAction } from "./actions";
import { CheckerContextType, CheckerRoute } from "./types";
import { CheckerContextRoute, CheckerContextType, CheckerRoute } from "./types";

export const checkerReducer = (
state: CheckerContextType,
Expand All @@ -22,26 +22,69 @@ export const checkerReducer = (
},
};
}
case "GO_TO_REVIEW_APPLICATIONS":
return { ...state, route: { id: CheckerRoute.ReviewApplications } };
case "GO_TO_APPLICATION_EVALUATION_OVERVIEW":
return {
...state,
route: {
id: CheckerRoute.ApplicationEvaluationOverview,
projectId: action.payload.projectId,
case "GO_TO_REVIEW_APPLICATIONS": {
const newRoute: CheckerContextRoute = { id: CheckerRoute.ReviewApplications };
window.history.pushState(
{
route: newRoute,
from: "internal",
},
"",
);
return { ...state, route: newRoute };
}
case "GO_TO_APPLICATION_EVALUATION_OVERVIEW": {
const newRoute: CheckerContextRoute = {
id: CheckerRoute.ApplicationEvaluationOverview,
projectId: action.payload.projectId,
};
case "GO_TO_SUBMIT_APPLICATION_EVALUATION":
return {
...state,
route: {
id: CheckerRoute.SubmitApplicationEvaluation,
projectId: action.payload.projectId,
window.history.pushState(
{
route: newRoute,
from: "internal",
},
"",
);
return { ...state, route: newRoute };
}
case "GO_TO_SUBMIT_APPLICATION_EVALUATION": {
const newRoute: CheckerContextRoute = {
id: CheckerRoute.SubmitApplicationEvaluation,
projectId: action.payload.projectId,
};
case "GO_TO_SUBMIT_FINAL_EVALUATION":
return { ...state, route: { id: CheckerRoute.SubmitFinalEvaluation } };
window.history.pushState(
{
route: newRoute,
from: "internal",
},
"",
);
return { ...state, route: newRoute };
}
case "GO_TO_SUBMIT_FINAL_EVALUATION": {
const newRoute: CheckerContextRoute = { id: CheckerRoute.SubmitFinalEvaluation };
window.history.pushState(
{
route: newRoute,
from: "internal",
},
"",
);
return { ...state, route: newRoute };
}
case "SET_ROUTE_HISTORY": {
const { route, replace = false } = action.payload;
if (replace) {
window.history.replaceState(
{
route,
from: "internal",
},
"",
);
}
return { ...state, route };
}
case "SET_POOL_DATA": {
const { poolId, chainId } = action.payload;
const poolUUID = generatePoolUUID(poolId, chainId);
Expand Down
24 changes: 13 additions & 11 deletions src/features/checker/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,23 @@ export enum CheckerRoute {
ApplicationEvaluation = "application-evaluation",
}

export type CheckerContextRoute =
| { id: CheckerRoute.SubmitFinalEvaluation }
| { id: CheckerRoute.ReviewApplications }
| {
id: CheckerRoute.ApplicationEvaluationOverview | CheckerRoute.SubmitApplicationEvaluation;
projectId: string;
}
| {
id: CheckerRoute.ApplicationEvaluation;
projectId: string;
};

export interface CheckerContextType {
poolsData: Record<string, CheckerPoolData>;
poolsFetchState: Record<string, CheckerPoolFetchState>;
poolId?: string;
chainId?: number;
address?: Hex;
route:
| { id: CheckerRoute.SubmitFinalEvaluation }
| { id: CheckerRoute.ReviewApplications }
| {
id: CheckerRoute.ApplicationEvaluationOverview | CheckerRoute.SubmitApplicationEvaluation;
projectId: string;
}
| {
id: CheckerRoute.ApplicationEvaluation;
projectId: string;
};
route: CheckerContextRoute;
}
Loading