Skip to content

Commit 74abdb1

Browse files
committed
chore: make url cleaner / add debounce to playground
1 parent bf68f54 commit 74abdb1

File tree

2 files changed

+14
-43
lines changed

2 files changed

+14
-43
lines changed

web/src/Playground.tsx

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import { ApplyOverlay, CalculateOverlay, GetInfo } from "./bridge";
1313
import { Alert } from "@speakeasy-api/moonshine";
1414
import { blankOverlay, petstore } from "./defaults";
1515
import { useAtom } from "jotai";
16-
import { throttledPushState } from "./url";
1716
import speakeasyWhiteLogo from "./assets/speakeasy-white.svg";
1817
import openapiLogo from "./assets/openapi.svg";
19-
import { atomWithHash } from "jotai-location";
18+
import { atom } from "jotai";
2019
import { compress, decompress } from "@/compress";
2120
import { CopyButton } from "@/components/CopyButton";
2221
import { Button } from "@/components/ui/button";
@@ -27,17 +26,11 @@ import {
2726
ImperativePanelGroupHandle,
2827
} from "react-resizable-panels";
2928
import posthog from "posthog-js";
30-
import { useMediaQuery } from "usehooks-ts";
29+
import { useDebounceCallback, useMediaQuery } from "usehooks-ts";
3130

32-
const originalOpenAPI = atomWithHash("originalOpenAPI", petstore, {
33-
setHash: throttledPushState,
34-
});
35-
const changedOpenAPI = atomWithHash("changedOpenAPI", petstore, {
36-
setHash: throttledPushState,
37-
});
38-
const overlay = atomWithHash("overlay", blankOverlay, {
39-
setHash: throttledPushState,
40-
});
31+
const originalOpenAPI = atom(petstore);
32+
const changedOpenAPI = atom(petstore);
33+
const overlay = atom(blankOverlay);
4134
const Link = ({ children, href }: { children: ReactNode; href: string }) => (
4235
<a
4336
className="border-b border-transparent pb-[2px] transition-all duration-200 hover:border-current "
@@ -50,14 +43,6 @@ const Link = ({ children, href }: { children: ReactNode; href: string }) => (
5043
</a>
5144
);
5245

53-
function removeShareURL() {
54-
const currentUrl = new URL(window.location.href);
55-
if (currentUrl.searchParams.has("s")) {
56-
currentUrl.searchParams.delete("s");
57-
history.pushState(null, "", currentUrl.toString());
58-
}
59-
}
60-
6146
function Playground() {
6247
const [ready, setReady] = useState(false);
6348

@@ -152,7 +137,6 @@ function Playground() {
152137
const onChangeA = useCallback(
153138
async (value: string | undefined, _: editor.IModelContentChangedEvent) => {
154139
try {
155-
removeShareURL();
156140
setResultLoading(true);
157141
setOriginal(value || "");
158142
const res = await CalculateOverlay(value || "", changed, true);
@@ -169,10 +153,11 @@ function Playground() {
169153
[changed, original],
170154
);
171155

156+
const onChangeADebounced = useDebounceCallback(onChangeA, 500);
157+
172158
const onChangeB = useCallback(
173159
async (value: string | undefined, _: editor.IModelContentChangedEvent) => {
174160
try {
175-
removeShareURL();
176161
setResultLoading(true);
177162
setChanged(value || "");
178163
const res = await CalculateOverlay(original, value || "", true);
@@ -189,10 +174,11 @@ function Playground() {
189174
[changed, original],
190175
);
191176

177+
const onChangeBDebounced = useDebounceCallback(onChangeB, 500);
178+
192179
const onChangeC = useCallback(
193180
async (value: string | undefined, _: editor.IModelContentChangedEvent) => {
194181
try {
195-
removeShareURL();
196182
setChangedLoading(true);
197183
setResult(value || "");
198184
const res = await ApplyOverlay(original, value || "", true);
@@ -209,6 +195,8 @@ function Playground() {
209195
[changed, original],
210196
);
211197

198+
const onChangeCDebounced = useDebounceCallback(onChangeC, 500);
199+
212200
useEffect(() => {
213201
const tryHandlePageTitle = async () => {
214202
try {
@@ -364,7 +352,7 @@ function Playground() {
364352
<Editor
365353
readonly={false}
366354
value={original}
367-
onChange={onChangeA}
355+
onChange={onChangeADebounced}
368356
title="Original"
369357
index={0}
370358
maxOnClick={maxLayout}
@@ -377,7 +365,7 @@ function Playground() {
377365
<Editor
378366
readonly={false}
379367
value={changed}
380-
onChange={onChangeB}
368+
onChange={onChangeBDebounced}
381369
loading={changedLoading}
382370
title={"Original + Overlay"}
383371
index={1}
@@ -391,7 +379,7 @@ function Playground() {
391379
<Editor
392380
readonly={false}
393381
value={result}
394-
onChange={onChangeC}
382+
onChange={onChangeCDebounced}
395383
loading={resultLoading}
396384
title={"Overlay"}
397385
index={2}

web/src/url.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)