Skip to content

Commit

Permalink
[playground] Use in-memory state (#4048)
Browse files Browse the repository at this point in the history
* Use in-memory state

* Update hash before preview returns
  • Loading branch information
penalosa authored Oct 5, 2023
1 parent 35760c3 commit ef35c3b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
5 changes: 3 additions & 2 deletions packages/workers-playground/src/QuickEditor/QuickEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ export default function QuickEditor() {
useEffect(() => {
observeDarkMode(() => setDarkMode(isDarkMode()));
}, []);
const workerHash = window.location.hash.slice(1);

const [previewUrl, setPreviewUrl] = React.useState(`/`);

const [initialWorkerContentHash, setInitialHash] = React.useState(workerHash);
const [initialWorkerContentHash, setInitialHash] = React.useState(
window.location.hash.slice(1)
);

function updateWorkerHash(hash: string) {
history.replaceState(null, "", hash);
Expand Down
17 changes: 11 additions & 6 deletions packages/workers-playground/src/QuickEditor/TopBar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useEffect, useState } from "react";
import { useContext, useEffect, useState } from "react";
import { A, Div, Form, Span, Strong } from "@cloudflare/elements";
import { createComponent } from "@cloudflare/style-container";
import { Button } from "@cloudflare/component-button";
import { Icon } from "@cloudflare/component-icon";
import { BAR_HEIGHT } from "./constants";
import { WorkersLogo } from "./WorkersLogo";
import { Input } from "@cloudflare/component-input";
import { ServiceContext } from "./QuickEditor";

const Wrapper = createComponent(({ theme }) => ({
display: "flex",
Expand All @@ -19,6 +20,7 @@ const Wrapper = createComponent(({ theme }) => ({
}));

export function TopBar() {
const { previewHash } = useContext(ServiceContext);
const [isEditing, setIsEditing] = useState(false);

const [hasCopied, setHasCopied] = useState(false);
Expand All @@ -29,8 +31,6 @@ export function TopBar() {
return searchParams.get("name") || "workers-playground";
});

const workerHash = location.hash.slice(1);

function setValue(v: string) {
const sanitised = v.replace(/[^a-z0-9-]+/g, "-");
_setValue(sanitised);
Expand Down Expand Up @@ -119,6 +119,7 @@ export function TopBar() {
<Button
type="primary"
inverted={true}
disabled={!Boolean(previewHash?.serialised)}
onClick={() => {
void navigator.clipboard.writeText(location.href);
setHasCopied(!hasCopied);
Expand All @@ -131,10 +132,14 @@ export function TopBar() {

<A
target="_blank"
href={`https://dash.cloudflare.com/workers-and-pages/deploy/playground/${value}#${workerHash}`}
style={workerHash ? undefined : { pointerEvents: "none" }}
href={`https://dash.cloudflare.com/workers-and-pages/deploy/playground/${value}#${previewHash?.serialised}`}
style={previewHash?.serialised ? undefined : { pointerEvents: "none" }}
>
<Button type="primary" disabled={!Boolean(workerHash)} tabIndex={-1}>
<Button
type="primary"
disabled={!Boolean(previewHash?.serialised)}
tabIndex={-1}
>
Deploy
</Button>
</A>
Expand Down
24 changes: 16 additions & 8 deletions packages/workers-playground/src/QuickEditor/useDraftWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function serialiseWorker(service: PartialWorker): FormData {
export type PreviewHash = {
previewUrl: string;
devtoolsUrl: string;
playgroundUrl: string;
serialised: string;
};

async function compressWorker(worker: FormData) {
Expand All @@ -102,8 +102,14 @@ async function compressWorker(worker: FormData) {
);
}

async function updatePreviewHash(content: Worker): Promise<PreviewHash> {
async function updatePreviewHash(
content: Worker,
updateWorkerHash: (hash: string) => void
): Promise<PreviewHash> {
const worker = serialiseWorker(content);
const serialised = await compressWorker(worker);
const playgroundUrl = `/playground#${serialised}`;
updateWorkerHash(playgroundUrl);

const res = await fetch("/playground/api/worker", {
method: "POST",
Expand All @@ -117,13 +123,13 @@ async function updatePreviewHash(content: Worker): Promise<PreviewHash> {
}

return {
playgroundUrl: `/playground#${await compressWorker(worker)}`,
previewUrl: `https://${v4()}.${
import.meta.env.VITE_PLAYGROUND_PREVIEW
}/.update-preview-token?token=${encodeURIComponent(deploy.preview)}`,
devtoolsUrl: `wss://${import.meta.env.VITE_PLAYGROUND_ROOT}${
deploy.inspector
}`,
serialised: serialised,
};
}

Expand Down Expand Up @@ -165,11 +171,13 @@ export function useDraftWorker(
}
try {
setIsPreviewUpdating(true);
const hash = await updatePreviewHash({
...worker,
...(wk ?? draftWorker),
});
updateWorkerHash(hash.playgroundUrl);
const hash = await updatePreviewHash(
{
...worker,
...(wk ?? draftWorker),
},
updateWorkerHash
);
setPreviewHash(hash);
setDevtoolsUrl(hash.devtoolsUrl);
} catch (e: unknown) {
Expand Down

0 comments on commit ef35c3b

Please sign in to comment.