Fix initial issues with navigation system

This commit is contained in:
KingRainbow44 2023-04-04 22:44:17 -04:00
parent e0b1f275dd
commit b17f97def6
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
9 changed files with 83 additions and 36 deletions

View File

@ -1,5 +1,5 @@
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { Page } from "@backend/types"; import type { Page } from "@backend/types";
const emitter = new EventEmitter(); const emitter = new EventEmitter();
const navigation = new EventEmitter(); const navigation = new EventEmitter();
@ -8,19 +8,12 @@ let navStack: Page[] = [];
let currentPage: number | null = -1; let currentPage: number | null = -1;
/** /**
* The initial setup function for this file. * Sets up the event system.
*/ */
export function setup(): void { export function setup(): void {
// Check if the window's href is a page. window.onpopstate = (event) => {
const page = window.location.href.split("/").pop(); navigate(event.state, false);
if (page == undefined) return; };
// Convert the page to a Page type.
const pageName = page.charAt(0).toUpperCase() + page.slice(1);
const pageType = pageName as Page;
// Navigate to the page.
navigate(pageType, false);
} }
/** /**

View File

@ -1 +1,10 @@
export type Page = "Home"; export type Page = "Home" | "Commands";
/**
* Checks if a string is a page.
*
* @param page The string to check.
*/
export function isPage(page: string): page is Page {
return ["Home", "Commands"].includes(page);
}

View File

@ -3,22 +3,44 @@ import React from "react";
import SideBar from "@views/SideBar"; import SideBar from "@views/SideBar";
import Content from "@views/Content"; import Content from "@views/Content";
import type { Page } from "@backend/types";
import "@css/App.scss"; import "@css/App.scss";
import "@css/Text.scss"; import "@css/Text.scss";
import { isPage } from "@backend/types";
// Based on the design at: https://www.figma.com/file/PDeAVDkTDF5vvUGGdaIZ39/GM-Handbook. // Based on the design at: https://www.figma.com/file/PDeAVDkTDF5vvUGGdaIZ39/GM-Handbook.
// Currently designed by: Magix. // Currently designed by: Magix.
class App extends React.Component<any, any> { interface IState {
initial: Page | null;
}
class App extends React.Component<{}, IState> {
constructor(props: any) { constructor(props: any) {
super(props); super(props);
// Check if the window's href is a page.
let targetPage = null;
const page = window.location.href.split("/").pop();
if (page != undefined) {
// Convert the page to a Page type.
const pageName = page.charAt(0).toUpperCase() + page.slice(1);
// Check if the page is a valid page.
if (isPage(pageName)) targetPage = pageName as Page;
}
this.state = {
initial: targetPage as Page | null
};
} }
render() { render() {
return ( return (
<div className={"App"}> <div className={"App"}>
<SideBar /> <SideBar />
<Content /> <Content initial={this.state.initial} />
</div> </div>
); );
} }

View File

@ -0,0 +1,17 @@
import React from "react";
class CommandsPage extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
render() {
return (
<div>
<h1>Commands</h1>
</div>
);
}
}
export default CommandsPage;

View File

@ -18,16 +18,16 @@ class HomePage extends React.Component<any, any> {
<h1 className={"HomePage_Title"}>Welcome back, Traveler~</h1> <h1 className={"HomePage_Title"}>Welcome back, Traveler~</h1>
<div className={"HomePage_Buttons"}> <div className={"HomePage_Buttons"}>
<HomeButton name={"Commands"} anchor={"commands"} /> <HomeButton name={"Commands"} anchor={"Commands"} />
<HomeButton name={"Characters"} anchor={"avatars"} /> <HomeButton name={"Characters"} anchor={"Home"} />
<HomeButton name={"Items"} anchor={"items"} /> <HomeButton name={"Items"} anchor={"Home"} />
<HomeButton name={"Entities"} anchor={"monsters"} /> <HomeButton name={"Entities"} anchor={"Home"} />
<HomeButton name={"Scenes"} anchor={"scenes"} /> <HomeButton name={"Scenes"} anchor={"Home"} />
</div> </div>
<div className={"HomePage_Buttons"}> <div className={"HomePage_Buttons"}>
<HomeButton name={"Quests"} anchor={"quests"} /> <HomeButton name={"Quests"} anchor={"Home"} />
<HomeButton name={"Achievements"} anchor={"achievements"} /> <HomeButton name={"Achievements"} anchor={"Home"} />
</div> </div>
</div> </div>

View File

@ -1,6 +1,7 @@
import React from "react"; import React from "react";
import HomePage from "@pages/HomePage"; import HomePage from "@pages/HomePage";
import CommandsPage from "@pages/CommandsPage";
import type { Page } from "@backend/types"; import type { Page } from "@backend/types";
@ -8,7 +9,7 @@ import "@css/views/Content.scss";
import { addNavListener, removeNavListener } from "@backend/events"; import { addNavListener, removeNavListener } from "@backend/events";
interface IProps { interface IProps {
initial?: Page; initial?: Page | null;
} }
interface IState { interface IState {
@ -46,6 +47,7 @@ class Content extends React.Component<IProps, IState> {
switch (this.state.current) { switch (this.state.current) {
default: return undefined; default: return undefined;
case "Home": return <HomePage />; case "Home": return <HomePage />;
case "Commands": return <CommandsPage />;
} }
} }
} }

View File

@ -14,13 +14,13 @@ class SideBar extends React.Component<any, any> {
<h1 className={"SideBar_Title"}>The Ultimate Anime Game Handbook</h1> <h1 className={"SideBar_Title"}>The Ultimate Anime Game Handbook</h1>
<div className={"SideBar_Buttons"}> <div className={"SideBar_Buttons"}>
<SideBarButton name={"Commands"} anchor={"commands"} /> <SideBarButton name={"Commands"} anchor={"Commands"} />
<SideBarButton name={"Characters"} anchor={"avatars"} /> <SideBarButton name={"Characters"} anchor={"Home"} />
<SideBarButton name={"Items"} anchor={"items"} /> <SideBarButton name={"Items"} anchor={"Home"} />
<SideBarButton name={"Entities"} anchor={"monsters"} /> <SideBarButton name={"Entities"} anchor={"Home"} />
<SideBarButton name={"Scenes"} anchor={"scenes"} /> <SideBarButton name={"Scenes"} anchor={"Home"} />
<SideBarButton name={"Quests"} anchor={"quests"} /> <SideBarButton name={"Quests"} anchor={"Home"} />
<SideBarButton name={"Achievements"} anchor={"achievements"} /> <SideBarButton name={"Achievements"} anchor={"Home"} />
</div> </div>
</div> </div>
); );

View File

@ -1,10 +1,13 @@
import React from "react"; import React from "react";
import type { Page } from "@backend/types";
import { navigate } from "@backend/events";
import "@css/widgets/HomeButton.scss"; import "@css/widgets/HomeButton.scss";
interface IProps { interface IProps {
name: string; name: string;
anchor: string; anchor: Page;
} }
class HomeButton extends React.PureComponent<IProps> { class HomeButton extends React.PureComponent<IProps> {
@ -17,8 +20,7 @@ class HomeButton extends React.PureComponent<IProps> {
* @private * @private
*/ */
private redirect(): void { private redirect(): void {
// TODO: Implement navigator system. navigate(this.props.anchor);
window.location.href = `/${this.props.anchor}`;
} }
render() { render() {

View File

@ -1,10 +1,13 @@
import React from "react"; import React from "react";
import type { Page } from "@backend/types";
import { navigate } from "@backend/events";
import "@css/widgets/SideBarButton.scss"; import "@css/widgets/SideBarButton.scss";
interface IProps { interface IProps {
name: string; name: string;
anchor: string; anchor: Page;
} }
class SideBarButton extends React.PureComponent<IProps> { class SideBarButton extends React.PureComponent<IProps> {
@ -17,8 +20,7 @@ class SideBarButton extends React.PureComponent<IProps> {
* @private * @private
*/ */
private redirect(): void { private redirect(): void {
// TODO: Implement navigator system. navigate(this.props.anchor);
window.location.href = `/${this.props.anchor}`;
} }
render() { render() {