You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB
TypeScript

import { Reducer } from "preact/hooks";
import {
Action as HomeAction,
State as HomeState,
reduce as reduceHome,
} from "../Home/state.ts";
import {
Action as SchemaEditorAction,
State as SchemaEditorState,
reduce as reduceSchemaEditor,
} from "../SchemaEditor/state.ts";
import { ReturnTypes } from "../../../common/utils.ts";
export const goHome = () => ({ kind: "goHome" });
export type Action =
| HomeAction
| SchemaEditorAction
| ReturnTypes<[typeof goHome]>;
type ViewState_<Kind extends string, T> = { kind: Kind; state: T };
export type ViewState =
| ViewState_<"Home", HomeState>
| ViewState_<"SchemaEditor", SchemaEditorState>;
export type ViewStateKind = ViewState["kind"];
export type State = {
view: ViewState;
};
export const reduce: Reducer<State, Action> = (state, action) => {
if (action.kind == "goHome") {
return {
...state,
view: { kind: "Home", state: null },
};
} else if (state.view.kind === "Home") {
return {
...state,
view: reduceHome(state.view, action as HomeAction),
};
} else {
return {
...state,
view: reduceSchemaEditor(state.view, action as SchemaEditorAction),
};
}
};