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

59 wiersze
1.6 KiB
TypeScript
Czysty Zwykły widok Historia

import React, { useEffect, useState } from "react";
2021-02-04 00:11:34 +00:00
import ReactDOM from "react-dom";
import { PageContext, PAGE_QUERY_ARG } from "./page";
import { pageNames, Pages, toPageName, DEFAULT_PAGE } from "./pages";
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}!`);
}
function getWindowSearch(): URLSearchParams {
return new URLSearchParams(window.location.search);
}
/**
* Call the given handler whenever a `popstate` event
* occurs.
*
* Return a function that wraps `window.history.pushState()`;
* the given handler will be called immediately afterwards.
*/
function usePushState(onPushOrPopState: () => void) {
useEffect(() => {
window.addEventListener("popstate", onPushOrPopState);
return () => {
window.removeEventListener("popstate", onPushOrPopState);
};
}, [onPushOrPopState]);
return function pushState(href: string) {
window.history.pushState(null, "", href);
onPushOrPopState();
};
}
2021-02-14 17:32:55 +00:00
const App: React.FC<{}> = (props) => {
const [search, setSearch] = useState(getWindowSearch());
const updateSearchFromWindow = () => setSearch(getWindowSearch());
const currPage = toPageName(search.get(PAGE_QUERY_ARG) || "", DEFAULT_PAGE);
const PageComponent = Pages[currPage];
const pushState = usePushState(updateSearchFromWindow);
const ctx: PageContext = {
currPage,
allPages: pageNames,
pushState,
};
2021-02-14 02:13:04 +00:00
return (
<PageContext.Provider value={ctx}>
<PageComponent />
</PageContext.Provider>
2021-02-14 02:13:04 +00:00
);
};
2021-02-14 17:32:55 +00:00
ReactDOM.render(<App />, appEl);