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.

58 lines
1.5 KiB
TypeScript

import { Const } from "../../../common/utils.ts";
import { swel } from "../../deps/swel.ts";
import { Dispatch } from "../../state/lib/store.ts";
import { Home, State as HomeState } from "../Home/view.ts";
import {
SchemaEditor,
State as SchemaEditorState,
} from "../SchemaEditor/view.ts";
import { CmpPaintComponent, Paint } from "../component.ts";
import { Action } from "./actions.ts";
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 AppState = {
view: ViewState;
};
type ViewRegistry_ = {
[K in ViewStateKind]: ViewState & { kind: K };
};
type ViewRegistry = {
// @ts-ignore: Weird
[K in keyof ViewRegistry_]: CmpPaintComponent<ViewRegistry_[K]["state"]>;
};
export class App extends CmpPaintComponent<AppState> {
private viewRegistry: ViewRegistry;
constructor(private dispatch: Dispatch<Action>) {
super();
this.viewRegistry = {
SchemaEditor: new SchemaEditor(this.dispatch),
Home: new Home(this.dispatch),
};
}
private container = swel("div", { className: "app" });
get el() {
return this.container;
}
paint_: Paint<AppState> = (cur) => {
const view = this.viewRegistry[cur.view.kind];
if (!this.previousModel || cur.view.kind != this.previousModel.view.kind) {
this.container.replaceChildren(view.el);
}
view.paint(cur.view.state as any as never);
};
}