diff --git a/front/src/SideBySide.js b/front/src/SideBySide.js index a015d20..f4a87e7 100644 --- a/front/src/SideBySide.js +++ b/front/src/SideBySide.js @@ -4,10 +4,38 @@ import * as wasm from "alejandra-front"; import { Editor } from "./Editor"; 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 = () => { - const [path, setPath] = react.useState(randomPath()); + const [path, setPath] = react.useState(undefined); 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 () => { await wasm.default(); @@ -15,8 +43,10 @@ export const SideBySide = () => { }, [wasm]); react.useEffect(async () => { - const content = await get(path); - setBefore(content); + if (path !== undefined) { + const content = await get(path); + setBefore(content); + } }, [path]); if (loading) { @@ -29,6 +59,8 @@ export const SideBySide = () => { const after = wasm.format(before, "before.nix"); + const permalink = getPermalink(before, path); + return (
@@ -48,7 +80,23 @@ export const SideBySide = () => {
- + { + setBefore(code); + setPath(undefined); + }} + /> +
+ Permalink:  + {permalink.length > 2048 ? ( + "not available, exceeds 2048 characters" + ) : ( + + here + + )} +