mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 04:57:44 +00:00
feat: generate permalinks
This commit is contained in:
parent
923a2f8652
commit
804dd3cefa
1 changed files with 53 additions and 5 deletions
|
@ -4,10 +4,38 @@ import * as wasm from "alejandra-front";
|
||||||
import { Editor } from "./Editor";
|
import { Editor } from "./Editor";
|
||||||
import { get, randomPath } from "./nixpkgs";
|
import { get, randomPath } from "./nixpkgs";
|
||||||
|
|
||||||
|
const getPermalink = (before, path) => {
|
||||||
|
const searchParams = new URLSearchParams();
|
||||||
|
|
||||||
|
if (path !== undefined) {
|
||||||
|
searchParams.append("path", path);
|
||||||
|
} else {
|
||||||
|
searchParams.append("before", before);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
window.location.origin +
|
||||||
|
window.location.pathname +
|
||||||
|
`?${searchParams.toString()}`
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const SideBySide = () => {
|
export const SideBySide = () => {
|
||||||
const [path, setPath] = react.useState(randomPath());
|
const [path, setPath] = react.useState(undefined);
|
||||||
const [loading, setLoading] = react.useState(true);
|
const [loading, setLoading] = react.useState(true);
|
||||||
const [before, setBefore] = react.useState("let x = 123; in\n x");
|
const [before, setBefore] = react.useState("");
|
||||||
|
|
||||||
|
react.useEffect(async () => {
|
||||||
|
const searchParams = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
|
if (searchParams.has("path")) {
|
||||||
|
setPath(searchParams.get("path"));
|
||||||
|
} else if (searchParams.has("before")) {
|
||||||
|
setBefore(searchParams.get("before"));
|
||||||
|
} else {
|
||||||
|
setPath(randomPath());
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
react.useEffect(async () => {
|
react.useEffect(async () => {
|
||||||
await wasm.default();
|
await wasm.default();
|
||||||
|
@ -15,8 +43,10 @@ export const SideBySide = () => {
|
||||||
}, [wasm]);
|
}, [wasm]);
|
||||||
|
|
||||||
react.useEffect(async () => {
|
react.useEffect(async () => {
|
||||||
const content = await get(path);
|
if (path !== undefined) {
|
||||||
setBefore(content);
|
const content = await get(path);
|
||||||
|
setBefore(content);
|
||||||
|
}
|
||||||
}, [path]);
|
}, [path]);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
|
@ -29,6 +59,8 @@ export const SideBySide = () => {
|
||||||
|
|
||||||
const after = wasm.format(before, "before.nix");
|
const after = wasm.format(before, "before.nix");
|
||||||
|
|
||||||
|
const permalink = getPermalink(before, path);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<react.Fragment>
|
<react.Fragment>
|
||||||
<div className="flex items-center justify-center">
|
<div className="flex items-center justify-center">
|
||||||
|
@ -48,7 +80,23 @@ export const SideBySide = () => {
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center justify-center pt1">
|
<div className="flex items-center justify-center pt1">
|
||||||
<div className="f6 fl w-40">
|
<div className="f6 fl w-40">
|
||||||
<Editor code={before} onChange={setBefore} />
|
<Editor
|
||||||
|
code={before}
|
||||||
|
onChange={(code) => {
|
||||||
|
setBefore(code);
|
||||||
|
setPath(undefined);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<div className="flex items-center justify-center">
|
||||||
|
<span>Permalink: </span>
|
||||||
|
{permalink.length > 2048 ? (
|
||||||
|
"not available, exceeds 2048 characters"
|
||||||
|
) : (
|
||||||
|
<a className="blue underline" href={permalink}>
|
||||||
|
here
|
||||||
|
</a>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="w-10" />
|
<div className="w-10" />
|
||||||
<div className="f6 fl w-40">
|
<div className="f6 fl w-40">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue