Skip to content

Commit

Permalink
fix typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Jul 24, 2024
1 parent e31bec3 commit ab9f538
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/api/artifact/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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",
});
Expand Down
35 changes: 24 additions & 11 deletions app/components/artifact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand All @@ -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);
}
});
}}
/>
Expand Down Expand Up @@ -168,7 +181,7 @@ export function Artifact() {
return (
<div
style={{
disply: "block",
display: "block",
width: "100%",
height: "100%",
position: "relative",
Expand All @@ -195,7 +208,7 @@ export function Artifact() {
autoHeight={false}
height={height - 36}
onLoad={(title) => {
setFileName(title);
setFileName(title as string);
setLoading(false);
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function Screen() {
if (isArtifact) {
return (
<Routes>
<Route exact path="/artifact/:id" element={<Artifact />} />
<Route path="/artifact/:id" element={<Artifact />} />
</Routes>
);
}
Expand Down

0 comments on commit ab9f538

Please sign in to comment.