-
Notifications
You must be signed in to change notification settings - Fork 688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(worker-playground): delay preview environment setup #6962
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { Toast } from "@cloudflare/component-toast"; | |
import { Div, Form, Label, Output } from "@cloudflare/elements"; | ||
import { isDarkMode, theme } from "@cloudflare/style-const"; | ||
import { createStyledComponent } from "@cloudflare/style-container"; | ||
import { useCallback, useContext, useEffect, useRef, useState } from "react"; | ||
import { useContext, useEffect, useState } from "react"; | ||
import { FrameError } from "../FrameErrorBoundary"; | ||
import { InputField } from "../InputField"; | ||
import { ServiceContext } from "../QuickEditor"; | ||
|
@@ -57,22 +57,26 @@ export function HTTPTab() { | |
setPreviewUrl, | ||
isPreviewUpdating, | ||
previewError, | ||
preview, | ||
} = useContext(ServiceContext); | ||
const [method, setMethod] = useState<HTTPMethod>("GET"); | ||
const [headers, setHeaders] = useState<HeaderEntry[]>([]); | ||
const [body, setBody] = useState(""); | ||
const [response, setResponse] = useState<Response | null>(null); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [url, setUrl] = useState(previewUrl); | ||
const [refreshTimestamp, setRefreshTimestamp] = useState<string>(); | ||
|
||
const hasBody = method !== "HEAD" && method !== "GET" && method !== "OPTIONS"; | ||
useEffect(() => { | ||
setUrl(previewUrl); | ||
}, [previewUrl]); | ||
Comment on lines
+67
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need an additional url state to load if the url is changed on submit. This is useful to decide whether we need to re-fetch the response from the same url. |
||
|
||
const onSendRequest = useCallback( | ||
async (e?: React.FormEvent<HTMLFormElement>) => { | ||
e?.preventDefault(); | ||
const hasBody = method !== "HEAD" && method !== "GET" && method !== "OPTIONS"; | ||
|
||
useEffect(() => { | ||
async function sendRequest() { | ||
if (previewHash !== undefined && previewUrl !== undefined) { | ||
try { | ||
setPreviewUrl(previewUrl); | ||
setIsLoading(true); | ||
setResponse( | ||
await fetchWorker( | ||
|
@@ -89,22 +93,33 @@ export function HTTPTab() { | |
setIsLoading(false); | ||
} | ||
} | ||
}, | ||
[previewHash, setPreviewUrl, previewUrl, method, headers, hasBody, body] | ||
); | ||
const ensureDevtoolsConnected = useRef(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why has this been deleted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable |
||
useEffect(() => { | ||
if (!ensureDevtoolsConnected.current && previewHash && !isLoading) { | ||
void onSendRequest(); | ||
ensureDevtoolsConnected.current = true; | ||
} | ||
}, [previewHash, isLoading, onSendRequest]); | ||
|
||
void sendRequest(); | ||
}, [ | ||
refreshTimestamp, | ||
previewHash, | ||
previewUrl, | ||
method, | ||
headers, | ||
hasBody, | ||
body, | ||
]); | ||
|
||
return ( | ||
<Div display="flex" flexDirection="column" width="100%"> | ||
<Form | ||
display="flex" | ||
onSubmit={(e) => void onSendRequest(e)} | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
preview(); | ||
|
||
if (url === previewUrl) { | ||
setRefreshTimestamp(new Date().toISOString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The timestamp has no use but trigger the effect above ( |
||
} else { | ||
setPreviewUrl(url); | ||
} | ||
}} | ||
p={2} | ||
gap={2} | ||
borderBottom="1px solid" | ||
|
@@ -124,10 +139,10 @@ export function HTTPTab() { | |
/> | ||
<InputField | ||
name="http_request_url" | ||
value={previewUrl} | ||
value={url} | ||
autoComplete="off" | ||
spellCheck={false} | ||
onChange={(e) => setPreviewUrl(e.target.value)} | ||
onChange={(e) => setUrl(e.target.value)} | ||
mb={0} | ||
/> | ||
<Button | ||
|
@@ -138,8 +153,8 @@ export function HTTPTab() { | |
disabled={ | ||
!previewHash || | ||
Boolean(previewError) || | ||
!previewUrl || | ||
!previewUrl.startsWith("/") | ||
!url || | ||
!url.startsWith("/") | ||
} | ||
data-tracking-name="send http tab request" | ||
> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this file need changed? It seems from a quick skim that it's doing roughly the same thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didn't share the URLBar implementation between the two tabs unlike the dashboard. Do you want me to fix that?