Skip to content

Commit

Permalink
feat: persist config in url, show title
Browse files Browse the repository at this point in the history
  • Loading branch information
80avin committed Apr 3, 2024
1 parent 48d3e6e commit 353efb2
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import { useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";
import { PlanViz, planRaw1, sample1 } from "./lib/planViz";
import { TreeView } from "./components/TreeView";
import lzString from "lz-string";

function updateUrl(doc) {
doc = {
title: document.title,
...doc,
};
const url = new URL(window.location.href);
url.searchParams.set(
"q",
lzString.compressToEncodedURIComponent(JSON.stringify(doc)),
);
window.location.href = url.toString();
}

function App() {
const [count, setCount] = useState(0);
const [text, setText] = useState(planRaw1);
const [text, _setText] = useState(planRaw1);
const [code, _output] = sample1(text);
const debounceRef = useRef<number | null>(null);
const setText = useCallback((t: string) => {
_setText(t);
if (debounceRef.current) {
clearTimeout(debounceRef.current);
}
debounceRef.current = setTimeout(() => {
updateUrl({
text: t,
});
debounceRef.current = null;
}, 1000);
}, []);

useEffect(() => {
const url = new URL(window.location.href);
const qString = url.searchParams.get("q");
Expand Down Expand Up @@ -37,17 +64,13 @@ function App() {
isLZString = false;
}
if (doc.text) {
setText(doc.text);
_setText(doc.text);
}
if (doc.title) {
document.title = doc.title;
}
if (!isLZString) {
url.searchParams.set(
"q",
lzString.compressToEncodedURIComponent(JSON.stringify(doc)),
);
window.location.href = url.toString();
updateUrl(doc);
}
} catch (err) {}
}, []);
Expand All @@ -63,6 +86,7 @@ function App() {
}}
>
<div className="w100pct-on-focus">
<code>{document.title}</code>
<textarea
style={{ whiteSpace: "pre" }}
rows={100}
Expand Down

0 comments on commit 353efb2

Please sign in to comment.