diff --git a/src/handbook/src/backend/events.ts b/src/handbook/src/backend/events.ts index 353424d93..a05bc946b 100644 --- a/src/handbook/src/backend/events.ts +++ b/src/handbook/src/backend/events.ts @@ -1,5 +1,5 @@ import { EventEmitter } from "events"; -import { Page } from "@backend/types"; +import type { Page } from "@backend/types"; const emitter = new EventEmitter(); const navigation = new EventEmitter(); @@ -8,19 +8,12 @@ let navStack: Page[] = []; let currentPage: number | null = -1; /** - * The initial setup function for this file. + * Sets up the event system. */ export function setup(): void { - // Check if the window's href is a page. - const page = window.location.href.split("/").pop(); - 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); + window.onpopstate = (event) => { + navigate(event.state, false); + }; } /** diff --git a/src/handbook/src/backend/types.ts b/src/handbook/src/backend/types.ts index 99a89e967..b9aa97d28 100644 --- a/src/handbook/src/backend/types.ts +++ b/src/handbook/src/backend/types.ts @@ -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); +} diff --git a/src/handbook/src/ui/App.tsx b/src/handbook/src/ui/App.tsx index d6bb266e5..e8f379f18 100644 --- a/src/handbook/src/ui/App.tsx +++ b/src/handbook/src/ui/App.tsx @@ -3,22 +3,44 @@ import React from "react"; import SideBar from "@views/SideBar"; import Content from "@views/Content"; +import type { Page } from "@backend/types"; + import "@css/App.scss"; import "@css/Text.scss"; +import { isPage } from "@backend/types"; + // Based on the design at: https://www.figma.com/file/PDeAVDkTDF5vvUGGdaIZ39/GM-Handbook. // Currently designed by: Magix. -class App extends React.Component { +interface IState { + initial: Page | null; +} + +class App extends React.Component<{}, IState> { constructor(props: any) { 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() { return (
- +
); } diff --git a/src/handbook/src/ui/pages/CommandsPage.tsx b/src/handbook/src/ui/pages/CommandsPage.tsx new file mode 100644 index 000000000..f811ed750 --- /dev/null +++ b/src/handbook/src/ui/pages/CommandsPage.tsx @@ -0,0 +1,17 @@ +import React from "react"; + +class CommandsPage extends React.Component { + constructor(props: any) { + super(props); + } + + render() { + return ( +
+

Commands

+
+ ); + } +} + +export default CommandsPage; diff --git a/src/handbook/src/ui/pages/HomePage.tsx b/src/handbook/src/ui/pages/HomePage.tsx index d5bab462a..f10c0b2e0 100644 --- a/src/handbook/src/ui/pages/HomePage.tsx +++ b/src/handbook/src/ui/pages/HomePage.tsx @@ -18,16 +18,16 @@ class HomePage extends React.Component {

Welcome back, Traveler~

- - - - - + + + + +
- - + +
diff --git a/src/handbook/src/ui/views/Content.tsx b/src/handbook/src/ui/views/Content.tsx index b15c0ac64..3bf00b3a8 100644 --- a/src/handbook/src/ui/views/Content.tsx +++ b/src/handbook/src/ui/views/Content.tsx @@ -1,6 +1,7 @@ import React from "react"; import HomePage from "@pages/HomePage"; +import CommandsPage from "@pages/CommandsPage"; import type { Page } from "@backend/types"; @@ -8,7 +9,7 @@ import "@css/views/Content.scss"; import { addNavListener, removeNavListener } from "@backend/events"; interface IProps { - initial?: Page; + initial?: Page | null; } interface IState { @@ -46,6 +47,7 @@ class Content extends React.Component { switch (this.state.current) { default: return undefined; case "Home": return ; + case "Commands": return ; } } } diff --git a/src/handbook/src/ui/views/SideBar.tsx b/src/handbook/src/ui/views/SideBar.tsx index 4c532a38b..bdcfa9351 100644 --- a/src/handbook/src/ui/views/SideBar.tsx +++ b/src/handbook/src/ui/views/SideBar.tsx @@ -14,13 +14,13 @@ class SideBar extends React.Component {

The Ultimate Anime Game Handbook

- - - - - - - + + + + + + +
); diff --git a/src/handbook/src/ui/widgets/HomeButton.tsx b/src/handbook/src/ui/widgets/HomeButton.tsx index 3d6286598..3e89a04db 100644 --- a/src/handbook/src/ui/widgets/HomeButton.tsx +++ b/src/handbook/src/ui/widgets/HomeButton.tsx @@ -1,10 +1,13 @@ import React from "react"; +import type { Page } from "@backend/types"; +import { navigate } from "@backend/events"; + import "@css/widgets/HomeButton.scss"; interface IProps { name: string; - anchor: string; + anchor: Page; } class HomeButton extends React.PureComponent { @@ -17,8 +20,7 @@ class HomeButton extends React.PureComponent { * @private */ private redirect(): void { - // TODO: Implement navigator system. - window.location.href = `/${this.props.anchor}`; + navigate(this.props.anchor); } render() { diff --git a/src/handbook/src/ui/widgets/SideBarButton.tsx b/src/handbook/src/ui/widgets/SideBarButton.tsx index 623d61d27..e6e518586 100644 --- a/src/handbook/src/ui/widgets/SideBarButton.tsx +++ b/src/handbook/src/ui/widgets/SideBarButton.tsx @@ -1,10 +1,13 @@ import React from "react"; +import type { Page } from "@backend/types"; +import { navigate } from "@backend/events"; + import "@css/widgets/SideBarButton.scss"; interface IProps { name: string; - anchor: string; + anchor: Page; } class SideBarButton extends React.PureComponent { @@ -17,8 +20,7 @@ class SideBarButton extends React.PureComponent { * @private */ private redirect(): void { - // TODO: Implement navigator system. - window.location.href = `/${this.props.anchor}`; + navigate(this.props.anchor); } render() {