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

Commit 4e5c2a3

Browse files
committed
feat: update checker app to support internal navigation
1 parent 1d919df commit 4e5c2a3

File tree

5 files changed

+125
-30
lines changed

5 files changed

+125
-30
lines changed

src/features/checker/store/CheckerProvider.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PropsWithChildren, useReducer } from "react";
1+
import { PropsWithChildren, useReducer, useEffect } from "react";
22

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

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

1616
export const CheckerProvider = ({ children }: PropsWithChildren) => {
1717
const [state, dispatch] = useReducer(checkerReducer, initialState);
18+
19+
useEffect(() => {
20+
// Initialize history state on first load
21+
if (window.history.state === null) {
22+
window.history.replaceState(
23+
{
24+
from: "external",
25+
route: state.route,
26+
},
27+
"",
28+
);
29+
}
30+
31+
const handlePopState = (event: PopStateEvent) => {
32+
if (event.state?.route) {
33+
// If navigating back to external route, let parent app handle it
34+
if (event.state.from === "external") {
35+
return;
36+
}
37+
dispatch({
38+
type: "SET_ROUTE_HISTORY",
39+
payload: {
40+
route: event.state.route,
41+
replace: true,
42+
},
43+
});
44+
}
45+
};
46+
47+
window.addEventListener("popstate", handlePopState);
48+
return () => window.removeEventListener("popstate", handlePopState);
49+
}, []);
50+
1851
return (
1952
<QueryClientProvider client={queryClient}>
2053
<CheckerContext.Provider value={state}>

src/features/checker/store/actions/actions.ts

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GoToSubmitFinalEvaluationAction,
66
SetInitialStateAction,
77
SetPoolDataAction,
8+
SetRouteHistoryAction,
89
} from "./types";
910

1011
export const setInitialStateAction = (
@@ -40,3 +41,10 @@ export const setPoolDataAction = (payload: SetPoolDataAction["payload"]): SetPoo
4041
type: "SET_POOL_DATA",
4142
payload,
4243
});
44+
45+
export const setRouteHistoryAction = (
46+
payload: SetRouteHistoryAction["payload"],
47+
): SetRouteHistoryAction => ({
48+
type: "SET_ROUTE_HISTORY",
49+
payload,
50+
});

src/features/checker/store/actions/types.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hex } from "viem";
22

3-
import { CheckerPoolData } from "../types";
3+
import { CheckerContextRoute, CheckerPoolData, CheckerRoute } from "../types";
44

55
export interface SetInitialStateAction {
66
type: "SET_INITIAL_STATE";
@@ -38,7 +38,16 @@ export interface SetPoolDataAction {
3838
payload: CheckerPoolData;
3939
}
4040

41+
export type SetRouteHistoryAction = {
42+
type: "SET_ROUTE_HISTORY";
43+
payload: {
44+
route: CheckerContextRoute;
45+
replace?: boolean;
46+
};
47+
};
48+
4149
export type CheckerAction =
50+
| SetRouteHistoryAction
4251
| SetInitialStateAction
4352
| SetPoolDataAction
4453
| GoToReviewApplicationsAction

src/features/checker/store/checkerReducer.ts

+60-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { generatePoolUUID } from "~checker/utils/generatePoolUUID";
22

33
import { CheckerAction } from "./actions";
4-
import { CheckerContextType, CheckerRoute } from "./types";
4+
import { CheckerContextRoute, CheckerContextType, CheckerRoute } from "./types";
55

66
export const checkerReducer = (
77
state: CheckerContextType,
@@ -10,26 +10,69 @@ export const checkerReducer = (
1010
switch (action.type) {
1111
case "SET_INITIAL_STATE":
1212
return { ...state, ...action.payload };
13-
case "GO_TO_REVIEW_APPLICATIONS":
14-
return { ...state, route: { id: CheckerRoute.ReviewApplications } };
15-
case "GO_TO_APPLICATION_EVALUATION_OVERVIEW":
16-
return {
17-
...state,
18-
route: {
19-
id: CheckerRoute.ApplicationEvaluationOverview,
20-
projectId: action.payload.projectId,
13+
case "GO_TO_REVIEW_APPLICATIONS": {
14+
const newRoute: CheckerContextRoute = { id: CheckerRoute.ReviewApplications };
15+
window.history.pushState(
16+
{
17+
route: newRoute,
18+
from: "internal",
2119
},
20+
"",
21+
);
22+
return { ...state, route: newRoute };
23+
}
24+
case "GO_TO_APPLICATION_EVALUATION_OVERVIEW": {
25+
const newRoute: CheckerContextRoute = {
26+
id: CheckerRoute.ApplicationEvaluationOverview,
27+
projectId: action.payload.projectId,
2228
};
23-
case "GO_TO_SUBMIT_APPLICATION_EVALUATION":
24-
return {
25-
...state,
26-
route: {
27-
id: CheckerRoute.SubmitApplicationEvaluation,
28-
projectId: action.payload.projectId,
29+
window.history.pushState(
30+
{
31+
route: newRoute,
32+
from: "internal",
2933
},
34+
"",
35+
);
36+
return { ...state, route: newRoute };
37+
}
38+
case "GO_TO_SUBMIT_APPLICATION_EVALUATION": {
39+
const newRoute: CheckerContextRoute = {
40+
id: CheckerRoute.SubmitApplicationEvaluation,
41+
projectId: action.payload.projectId,
3042
};
31-
case "GO_TO_SUBMIT_FINAL_EVALUATION":
32-
return { ...state, route: { id: CheckerRoute.SubmitFinalEvaluation } };
43+
window.history.pushState(
44+
{
45+
route: newRoute,
46+
from: "internal",
47+
},
48+
"",
49+
);
50+
return { ...state, route: newRoute };
51+
}
52+
case "GO_TO_SUBMIT_FINAL_EVALUATION": {
53+
const newRoute: CheckerContextRoute = { id: CheckerRoute.SubmitFinalEvaluation };
54+
window.history.pushState(
55+
{
56+
route: newRoute,
57+
from: "internal",
58+
},
59+
"",
60+
);
61+
return { ...state, route: newRoute };
62+
}
63+
case "SET_ROUTE_HISTORY": {
64+
const { route, replace = false } = action.payload;
65+
if (replace) {
66+
window.history.replaceState(
67+
{
68+
route,
69+
from: "internal",
70+
},
71+
"",
72+
);
73+
}
74+
return { ...state, route };
75+
}
3376
case "SET_POOL_DATA": {
3477
const { poolId, chainId } = action.payload;
3578
const poolUUID = generatePoolUUID(poolId, chainId);

src/features/checker/store/types.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,22 @@ export enum CheckerRoute {
4848
ApplicationEvaluation = "application-evaluation",
4949
}
5050

51+
export type CheckerContextRoute =
52+
| { id: CheckerRoute.SubmitFinalEvaluation }
53+
| { id: CheckerRoute.ReviewApplications }
54+
| {
55+
id: CheckerRoute.ApplicationEvaluationOverview | CheckerRoute.SubmitApplicationEvaluation;
56+
projectId: string;
57+
}
58+
| {
59+
id: CheckerRoute.ApplicationEvaluation;
60+
projectId: string;
61+
};
62+
5163
export interface CheckerContextType {
5264
poolsData: Record<string, CheckerPoolData>;
5365
poolId?: string;
5466
chainId?: number;
5567
address?: Hex;
56-
route:
57-
| { id: CheckerRoute.SubmitFinalEvaluation }
58-
| { id: CheckerRoute.ReviewApplications }
59-
| {
60-
id: CheckerRoute.ApplicationEvaluationOverview | CheckerRoute.SubmitApplicationEvaluation;
61-
projectId: string;
62-
}
63-
| {
64-
id: CheckerRoute.ApplicationEvaluation;
65-
projectId: string;
66-
};
68+
route: CheckerContextRoute;
6769
}

0 commit comments

Comments
 (0)