1
0
Fork 0
mirror of https://github.com/kou029w/quot.git synced 2025-01-19 08:28:09 +00:00
quot/app/src/app.tsx

48 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-08-23 09:49:34 +09:00
import "./app.css";
2022-08-23 09:01:47 +09:00
import { createSignal } from "solid-js";
import Index from "./pages/index";
import Page from "./pages/page";
2022-08-24 13:17:37 +09:00
import random from "./helpers/random";
2022-08-23 09:01:47 +09:00
const routes = {
"/": Index,
};
export default () => {
const [pathname, setPathname] = createSignal(
document.location.pathname as keyof typeof routes
);
document.body.addEventListener("click", (e) => {
if (
e.target instanceof HTMLAnchorElement &&
e.target.origin === document.location.origin &&
e.target.pathname in routes // TODO: support params ... solid router を入れよう
) {
e.preventDefault();
window.history.pushState({}, "", e.target.href);
setPathname(e.target.pathname as keyof typeof routes);
}
});
window.addEventListener("popstate", () => {
setPathname(document.location.pathname as keyof typeof routes);
});
return () => (
<>
<header>
2022-08-23 09:49:34 +09:00
<h1>
<a href="/">Quot</a>
</h1>
<a href="/new">📄</a>
2022-08-23 09:01:47 +09:00
</header>
2022-08-24 13:17:37 +09:00
{routes[pathname()] ?? (
2022-08-29 10:39:16 +09:00
<Page
id={parseInt(pathname().slice(1), 16) || random()}
/>
2022-08-24 13:17:37 +09:00
)}
2022-08-23 09:01:47 +09:00
</>
);
};