mysticsymbolic.github.io/lib/browser-main.tsx

70 wiersze
1.7 KiB
TypeScript
Czysty Zwykły widok Historia

2021-02-14 17:32:55 +00:00
import React from "react";
2021-02-04 00:11:34 +00:00
import ReactDOM from "react-dom";
2021-02-14 21:39:10 +00:00
import { WavesPage } from "./pages/waves-page";
2021-02-14 17:32:55 +00:00
import { VocabularyPage } from "./pages/vocabulary-page";
2021-02-15 13:34:22 +00:00
import { CreaturePage } from "./pages/creature-page";
2021-02-04 00:11:34 +00:00
2021-02-14 17:32:55 +00:00
const Pages = {
vocabulary: VocabularyPage,
2021-02-15 13:34:22 +00:00
creature: CreaturePage,
2021-02-14 21:39:10 +00:00
waves: WavesPage,
2021-02-14 17:32:55 +00:00
};
type PageName = keyof typeof Pages;
const pageNames = Object.keys(Pages) as PageName[];
2021-02-04 00:36:29 +00:00
2021-02-04 00:11:34 +00:00
const APP_ID = "app";
const appEl = document.getElementById(APP_ID);
if (!appEl) {
throw new Error(`Unable to find #${APP_ID}!`);
}
2021-02-14 17:32:55 +00:00
const App: React.FC<{}> = (props) => {
const page = new URLSearchParams(window.location.search);
const currPageName = toPageName(page.get("p") || "", "vocabulary");
const PageComponent = Pages[currPageName];
2021-02-14 02:13:04 +00:00
return (
<>
2021-02-14 17:32:55 +00:00
<main>
<PageComponent />
</main>
<footer>
<p>Other pages</p>
<ul>
{pageNames.map((pageName) => (
<li key={pageName}>
{currPageName === pageName ? (
pageName
) : (
<a href={`?p=${encodeURIComponent(pageName)}`}>{pageName}</a>
)}
</li>
))}
</ul>
<p>
For more details about this project, see its{" "}
<a href="https://github.com/toolness/mystic-symbolic" target="_blank">
GitHub repository
</a>
.
</p>
2021-02-14 17:32:55 +00:00
</footer>
2021-02-14 02:13:04 +00:00
</>
);
};
2021-02-14 17:32:55 +00:00
ReactDOM.render(<App />, appEl);
2021-02-04 00:53:01 +00:00
2021-02-14 17:32:55 +00:00
function isPageName(page: string): page is PageName {
return pageNames.includes(page as any);
}
2021-02-04 00:11:34 +00:00
2021-02-14 17:32:55 +00:00
function toPageName(page: string, defaultValue: PageName): PageName {
if (isPageName(page)) return page;
return defaultValue;
}