ソースを参照

fix typescript

lloydzhou 1 年間 前
コミット
ab9f5382b2
3 ファイル変更27 行追加14 行削除
  1. 2 2
      app/api/artifact/route.ts
  2. 24 11
      app/components/artifact.tsx
  3. 1 1
      app/components/home.tsx

+ 2 - 2
app/api/artifact/route.ts

@@ -4,7 +4,7 @@ import { getServerSideConfig } from "@/app/config/server";
 
 async function handle(req: NextRequest, res: NextResponse) {
   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}`;
   const storeHeaders = () => ({
     Authorization: `Bearer ${serverConfig.cloudflareKVApiKey}`,
@@ -32,7 +32,7 @@ async function handle(req: NextRequest, res: NextResponse) {
   }
   if (req.method === "GET") {
     const id = req?.nextUrl?.searchParams?.get("id");
-    const res = await fetch(storeUrl(id), {
+    const res = await fetch(storeUrl(id as string), {
       headers: storeHeaders(),
       method: "GET",
     });

+ 24 - 11
app/components/artifact.tsx

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

+ 1 - 1
app/components/home.tsx

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