Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ const INITIAL_WIZARD_STATE: WizardState = {
results: null,
};

type WizardReducer = (draft: WizardState, action?: WizardAction) => void;
type WizardAction =
| { type: "SET_PLATFORM"; payload: SourcePlatform | null }
| { type: "SET_FILTERS"; payload: FilterState }
| { type: "SET_RESULTS"; payload: ResultsData | null }
| { type: "RESET"; payload: WizardState };

const updateIsReady = (draft: WizardState) => {
draft.isReady = !!draft.platform && draft.filters.isValid;
return draft;
};

const wizardReducer: WizardReducer = (draft, action) => {
const wizardReducer = (
draft: WizardState,
action?: WizardAction
): WizardState | void => {
if (action) {
switch (action.type) {
case "SET_PLATFORM":
Expand All @@ -49,12 +46,12 @@ const wizardReducer: WizardReducer = (draft, action) => {
draft.results = action.payload;
break;
case "RESET":
return updateIsReady(action.payload);
return action.payload;
}
}

// Validate and update isReady state after any change
updateIsReady(draft);
draft.isReady = !!draft.platform && draft.filters.isValid;
};

export type InitialStateRecipe = (draftInitialState: WizardState) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as React from "react";
import { useCallback, useRef } from "react";
import { produce } from "immer";
import { useImmerReducer } from "use-immer";

import { TargetProfile } from "@app/api/models";

Expand Down Expand Up @@ -28,72 +30,99 @@ const INITIAL_WIZARD_STATE: WizardState = {
results: null,
};

type WizardReducer = (state: WizardState, action: WizardAction) => WizardState;
type WizardAction =
| { type: "SET_PROFILE"; payload: TargetProfile }
| { type: "SET_PARAMETERS"; payload: ParameterState }
| { type: "SET_ADVANCED_OPTIONS"; payload: AdvancedOptionsState }
| { type: "SET_RESULTS"; payload: ResultsData | null }
| { type: "RESET" };

const validateWizardState = (state: WizardState): WizardState => {
const isReady =
!!state.profile &&
state.parameters.isValid &&
state.advancedOptions.isValid;
return { ...state, isReady };
| { type: "RESET"; payload: WizardState };

const wizardReducer = (
draft: WizardState,
action?: WizardAction
): WizardState | void => {
if (action) {
switch (action.type) {
case "SET_PROFILE":
draft.profile = action.payload;
break;
case "SET_PARAMETERS":
draft.parameters = action.payload;
break;
case "SET_ADVANCED_OPTIONS":
draft.advancedOptions = action.payload;
break;
case "SET_RESULTS":
draft.results = action.payload;
break;
case "RESET":
return action.payload;
}
}

// Validate and update isReady state after any change
draft.isReady =
!!draft.profile &&
draft.parameters.isValid &&
draft.advancedOptions.isValid;
};

const wizardReducer: WizardReducer = (state, action) => {
switch (action.type) {
case "SET_PROFILE":
return { ...state, profile: action.payload };
case "SET_PARAMETERS":
return { ...state, parameters: action.payload };
case "SET_ADVANCED_OPTIONS":
return { ...state, advancedOptions: action.payload };
case "SET_RESULTS":
return { ...state, results: action.payload };
case "RESET":
return INITIAL_WIZARD_STATE;
default:
return state;
export type InitialStateRecipe = (draftInitialState: WizardState) => void;

const useImmerInitialState = (
initialRecipe?: InitialStateRecipe
): WizardState => {
const initialRef = useRef<WizardState | null>(null);
if (initialRef.current === null) {
initialRef.current = produce(INITIAL_WIZARD_STATE, (draft) => {
initialRecipe?.(draft);
wizardReducer(draft);
});
}

return initialRef.current;
};

const validatedReducer: WizardReducer = (state, action) =>
validateWizardState(wizardReducer(state, action));
export const useWizardReducer = (init?: InitialStateRecipe) => {
// Ref: https://18.react.dev/reference/react/useReducer#avoiding-recreating-the-initial-state
// Allow RESET to have the same semantics as useReducer()'s initialState argument by just
// calculating the initial state once and storing it in a ref.
const firstInitialState = useImmerInitialState(init);

export const useWizardReducer = () => {
const [state, dispatch] = React.useReducer(
validatedReducer,
INITIAL_WIZARD_STATE,
validateWizardState
);
const [state, dispatch] = useImmerReducer(wizardReducer, firstInitialState);

// Create stable callbacks using useCallback
const setProfile = React.useCallback((profile: TargetProfile) => {
dispatch({ type: "SET_PROFILE", payload: profile });
}, []);
const setProfile = useCallback(
(profile: TargetProfile) => {
dispatch({ type: "SET_PROFILE", payload: profile });
},
[dispatch]
);

const setParameters = React.useCallback((parameters: ParameterState) => {
dispatch({ type: "SET_PARAMETERS", payload: parameters });
}, []);
const setParameters = useCallback(
(parameters: ParameterState) => {
dispatch({ type: "SET_PARAMETERS", payload: parameters });
},
[dispatch]
);

const setAdvancedOptions = React.useCallback(
const setAdvancedOptions = useCallback(
(advancedOptions: AdvancedOptionsState) => {
dispatch({ type: "SET_ADVANCED_OPTIONS", payload: advancedOptions });
},
[]
[dispatch]
);

const setResults = React.useCallback((results: ResultsData | null) => {
dispatch({ type: "SET_RESULTS", payload: results });
}, []);
const setResults = useCallback(
(results: ResultsData | null) => {
dispatch({ type: "SET_RESULTS", payload: results });
},
[dispatch]
);

const reset = React.useCallback(() => {
dispatch({ type: "RESET" });
}, []);
const reset = useCallback(() => {
dispatch({ type: "RESET", payload: firstInitialState });
}, [firstInitialState, dispatch]);

return {
state,
Expand Down
Loading