fix typescript

This commit is contained in:
lloydzhou 2024-07-24 20:51:33 +08:00
parent e31bec3aff
commit ab9f5382b2
3 changed files with 27 additions and 14 deletions

View File

@ -4,7 +4,7 @@ import { getServerSideConfig } from "@/app/config/server";
async function handle(req: NextRequest, res: NextResponse) { async function handle(req: NextRequest, res: NextResponse) {
const serverConfig = getServerSideConfig(); const serverConfig = getServerSideConfig();
const storeUrl = (key) => const storeUrl = (key: string) =>
`https://api.cloudflare.com/client/v4/accounts/${serverConfig.cloudflareAccountId}/storage/kv/namespaces/${serverConfig.cloudflareKVNamespaceId}/values/${key}`; `https://api.cloudflare.com/client/v4/accounts/${serverConfig.cloudflareAccountId}/storage/kv/namespaces/${serverConfig.cloudflareKVNamespaceId}/values/${key}`;
const storeHeaders = () => ({ const storeHeaders = () => ({
Authorization: `Bearer ${serverConfig.cloudflareKVApiKey}`, Authorization: `Bearer ${serverConfig.cloudflareKVApiKey}`,
@ -32,7 +32,7 @@ async function handle(req: NextRequest, res: NextResponse) {
} }
if (req.method === "GET") { if (req.method === "GET") {
const id = req?.nextUrl?.searchParams?.get("id"); const id = req?.nextUrl?.searchParams?.get("id");
const res = await fetch(storeUrl(id), { const res = await fetch(storeUrl(id as string), {
headers: storeHeaders(), headers: storeHeaders(),
method: "GET", method: "GET",
}); });

View File

@ -67,23 +67,34 @@ export function HTMLPreview(props: {
style={{ width: "100%", height }} style={{ width: "100%", height }}
// src={`data:text/html,${encodeURIComponent(srcDoc)}`} // src={`data:text/html,${encodeURIComponent(srcDoc)}`}
srcDoc={srcDoc} srcDoc={srcDoc}
onLoad={(e) => props?.onLoad(title)} onLoad={(e) => props?.onLoad && props?.onLoad(title)}
></iframe> ></iframe>
); );
} }
export function ArtifactShareButton({ getCode, id, style, fileName }) { export function ArtifactShareButton({
getCode,
id,
style,
fileName,
}: {
getCode: () => string;
id?: string;
style?: any;
fileName?: string;
}) {
const [name, setName] = useState(id); const [name, setName] = useState(id);
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
const shareUrl = useMemo(() => const shareUrl = useMemo(
[location.origin, "#", Path.Artifact, "/", name].join(""), () => [location.origin, "#", Path.Artifact, "/", name].join(""),
[name],
); );
const upload = (code) => const upload = (code: string) =>
id id
? Promise.resolve({ id }) ? Promise.resolve({ id })
: fetch(ApiPath.Artifact, { : fetch(ApiPath.Artifact, {
method: "POST", method: "POST",
body: getCode(), body: code,
}) })
.then((res) => res.json()) .then((res) => res.json())
.then(({ id }) => { .then(({ id }) => {
@ -103,9 +114,11 @@ export function ArtifactShareButton({ getCode, id, style, fileName }) {
bordered bordered
title={Locale.Export.Artifact.Title} title={Locale.Export.Artifact.Title}
onClick={() => { onClick={() => {
upload(getCode()).then(({ id }) => { upload(getCode()).then((res) => {
setShow(true); if (res?.id) {
setName(id); setShow(true);
setName(res?.id);
}
}); });
}} }}
/> />
@ -168,7 +181,7 @@ export function Artifact() {
return ( return (
<div <div
style={{ style={{
disply: "block", display: "block",
width: "100%", width: "100%",
height: "100%", height: "100%",
position: "relative", position: "relative",
@ -195,7 +208,7 @@ export function Artifact() {
autoHeight={false} autoHeight={false}
height={height - 36} height={height - 36}
onLoad={(title) => { onLoad={(title) => {
setFileName(title); setFileName(title as string);
setLoading(false); setLoading(false);
}} }}
/> />

View File

@ -143,7 +143,7 @@ function Screen() {
if (isArtifact) { if (isArtifact) {
return ( return (
<Routes> <Routes>
<Route exact path="/artifact/:id" element={<Artifact />} /> <Route path="/artifact/:id" element={<Artifact />} />
</Routes> </Routes>
); );
} }