File size: 1,248 Bytes
63c7991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { notFound } from "next/navigation";
import CodeViewer from "@/components/code-viewer";
import client from "@/lib/prisma";
import type { Metadata } from "next";
import { cache } from "react";

export async function generateMetadata({
  params,
}: {
  params: { id: string };
}): Promise<Metadata> {
  const generatedApp = await getGeneratedAppByID(params.id);

  let prompt = generatedApp?.prompt;
  if (typeof prompt !== "string") {
    notFound();
  }

  let searchParams = new URLSearchParams();
  searchParams.set("prompt", prompt);

  return {
    title: "An app generated on LlamaCoder.io",
    description: `Prompt: ${generatedApp?.prompt}`,
    openGraph: {
      images: [`/api/og?${searchParams}`],
    },
  };
}

export default async function Page({ params }: { params: { id: string } }) {
  // if process.env.DATABASE_URL is not set, throw an error
  if (typeof params.id !== "string") {
    notFound();
  }

  const generatedApp = await getGeneratedAppByID(params.id);

  if (!generatedApp) {
    return <div>App not found</div>;
  }

  return <CodeViewer code={generatedApp.code} />;
}

const getGeneratedAppByID = cache(async (id: string) => {
  return client.generatedApp.findUnique({
    where: {
      id,
    },
  });
});