mysticsymbolic.github.io/lib/page.tsx

125 wiersze
2.9 KiB
TypeScript
Czysty Zwykły widok Historia

import React, { MouseEvent, useContext } from "react";
import { Helmet } from "react-helmet";
import type { PageName } from "./pages";
import { getFriendlyPageName } from "./pages/friendly-page-names";
import "./page.css";
export type PageContext = {
currPage: PageName;
allPages: PageName[];
pushState: (href: string) => void;
Always permalink to Mandalas. (#99) This addresses #61 by making mandalas permalinked. The URL to a mandala will change whenever the user stops fiddling with it for 250 ms. This means that the user can always reload the page to get a reasonably recent version of what they were creating, and they can use the browser's "back" and "next" buttons to effectively undo/redo recent changes. They can also copy the URL to share it with others. ## About the serialization format Originally, I stored the state of the user's mandala in the URL using JSON. This had a number of drawbacks, though: * **It was really long.** A mandala serialization was almost 1k characters, which was a very big URL, and one that some sharing platforms might even reject. * **It wasn't type-checked in any way.** Unless I added some kind of JSON schema validation (which I didn't), the serialization was simply deserialized and assumed to be in the proper format. This could result in confusing exceptions during render time, rather than decisively exploding at deserialization time. To resolve these limitations, and because I thought it would be fun, I decided to store the mandala state using a serialization format called [Apache Avro][]. I first read about this in Kleppmann's [Designing Data-Intensive Applications][kleppmann] and was intrigued by both its compactness (a serialized mandala is around 80-120 characters) and schema evolution properties. It might be going a bit overboard, but again, I thought it would be fun and I wanted to play around with Avro. Also, I tried architecting things in such a way that all the Avro code is in its own file, and can easily be removed (or swapped out for another serialization format) if we decide it's dumb. [Apache Avro]: http://avro.apache.org/ [kleppmann]: https://dataintensive.net/ ## Other changes This PR also makes a few other changes: * Tests can now import files with JSX in them (I don't think this was required for the final state of this PR, but I figured I'd leave it in there for its almost inevitable use in the future). * The value labels for number sliders now have a fixed width, which eliminates a weird "jitter" effect that sometimes occurred when using them.
2021-04-24 12:46:32 +00:00
search: URLSearchParams;
};
export const PageContext = React.createContext<PageContext>({
currPage: "vocabulary",
allPages: [],
Always permalink to Mandalas. (#99) This addresses #61 by making mandalas permalinked. The URL to a mandala will change whenever the user stops fiddling with it for 250 ms. This means that the user can always reload the page to get a reasonably recent version of what they were creating, and they can use the browser's "back" and "next" buttons to effectively undo/redo recent changes. They can also copy the URL to share it with others. ## About the serialization format Originally, I stored the state of the user's mandala in the URL using JSON. This had a number of drawbacks, though: * **It was really long.** A mandala serialization was almost 1k characters, which was a very big URL, and one that some sharing platforms might even reject. * **It wasn't type-checked in any way.** Unless I added some kind of JSON schema validation (which I didn't), the serialization was simply deserialized and assumed to be in the proper format. This could result in confusing exceptions during render time, rather than decisively exploding at deserialization time. To resolve these limitations, and because I thought it would be fun, I decided to store the mandala state using a serialization format called [Apache Avro][]. I first read about this in Kleppmann's [Designing Data-Intensive Applications][kleppmann] and was intrigued by both its compactness (a serialized mandala is around 80-120 characters) and schema evolution properties. It might be going a bit overboard, but again, I thought it would be fun and I wanted to play around with Avro. Also, I tried architecting things in such a way that all the Avro code is in its own file, and can easily be removed (or swapped out for another serialization format) if we decide it's dumb. [Apache Avro]: http://avro.apache.org/ [kleppmann]: https://dataintensive.net/ ## Other changes This PR also makes a few other changes: * Tests can now import files with JSX in them (I don't think this was required for the final state of this PR, but I figured I'd leave it in there for its almost inevitable use in the future). * The value labels for number sliders now have a fixed width, which eliminates a weird "jitter" effect that sometimes occurred when using them.
2021-04-24 12:46:32 +00:00
search: new URLSearchParams(),
pushState: () => {
throw new Error("No page context is defined!");
},
});
export const PAGE_QUERY_ARG = "p";
function isNormalLinkClick(e: MouseEvent): boolean {
return !e.shiftKey && !e.altKey && !e.metaKey && !e.ctrlKey && e.button === 0;
}
const PageLink: React.FC<{ page: PageName }> = ({ page }) => {
const href = `?${PAGE_QUERY_ARG}=${encodeURIComponent(page)}`;
const { pushState } = useContext(PageContext);
const handleClick = (e: MouseEvent) => {
if (isNormalLinkClick(e)) {
pushState(href);
e.preventDefault();
}
};
return (
<a href={href} onClick={handleClick}>
{getFriendlyPageName(page)}
</a>
);
};
const Navbar: React.FC<{}> = (props) => {
const pc = useContext(PageContext);
return (
<nav>
<ul className="navbar">
{pc.allPages.map((pageName) => (
<li key={pageName}>
{pc.currPage === pageName ? (
getFriendlyPageName(pageName)
) : (
<PageLink page={pageName} />
)}
</li>
))}
</ul>
</nav>
);
};
export type PageProps = {
title: string;
children?: any;
};
export const Page: React.FC<PageProps> = ({ title, children }) => {
const fullTitle = ` Mystic Symbolic ${title}`;
return (
<div className="page">
<Helmet>
<title>{fullTitle}</title>
</Helmet>
<header>
<h1>
<img
src="img/mysticsymbolic-logo-sun.svg"
alt="Mystic Symbolic"
title="Mystic Symbolic"
className="MSlogo"
/>{" "}
{title}
</h1>
<Navbar />
</header>
{children}
<footer>
<p>
For more details about this project, see its{" "}
2021-06-07 11:39:52 +00:00
<a
href="https://github.com/toolness/mystic-symbolic"
target="_blank"
rel="noopener noreferrer"
>
GitHub repository
2021-06-07 11:39:52 +00:00
</a>{" "}
and{" "}
<a
href="https://blog.ninapaley.com/category/mysticsymbolic/"
target="_blank"
rel="noopener noreferrer"
>
Nina Paley's blog
</a>
2021-06-10 21:26:49 +00:00
. You can also{" "}
<a
href="https://www.gofundme.com/f/mysticsymbolic-development"
target="_blank"
rel="noopener noreferrer"
>
donate to the project
</a>
.
</p>
</footer>
</div>
);
};