diff --git a/src/app/(pages)/[id]/page.tsx b/src/app/(pages)/[id]/page.tsx index 9315082..e907b71 100644 --- a/src/app/(pages)/[id]/page.tsx +++ b/src/app/(pages)/[id]/page.tsx @@ -1,76 +1,27 @@ -import React, { cache, ReactElement } from "react"; +import React, { ReactElement } from "react"; import { ActionMenu } from "@/app/components/action-menu"; import { Metadata } from "next"; import moment from "moment"; import { notFound } from "next/navigation"; import { CodeBlock } from "@/app/components/code-block"; -import { detectLanguage } from "@/app/common/lang-detection/detection"; import { Button } from "@/app/components/ui/button"; import Link from "next/link"; - -type PasteProps = { - params: { - id: string; - }; -}; - -type Paste = { - /** - * The paste content. - */ - content: string; - - /** - * The date the paste was created. - */ - created: string; - - /** - * The detected language of the paste. - */ - language: string; -}; - -const getPaste = cache(async (id: string) => { - const response: Response = await fetch( - `${process.env.NEXT_PUBLIC_API_ENDPOINT}/${id}`, - { - next: { - revalidate: 300, // Keep this response cached for 5 minutes - }, - }, - ); - const json = await response.json(); - - if (json.code && json.message) { - return undefined; - } - return { - content: json.content, - created: json.created, - language: await detectLanguage(json.content), - }; -}) as (id: string) => Promise; +import { + generatePasteMetadata, + getPaste, + type Paste, +} from "@/app/common/pastes"; +import { PastePageProps } from "@/app/types/paste-page"; export async function generateMetadata({ params: { id }, -}: PasteProps): Promise { - const data: Paste | undefined = await getPaste(id); - if (data == undefined) { - return { - description: "Not found", - }; - } - - return { - title: `Paste - ${id}.${data.language}`, - description: `Click to view the paste.`, - }; +}: PastePageProps): Promise { + return generatePasteMetadata(id); } export default async function Paste({ params: { id }, -}: PasteProps): Promise { +}: PastePageProps): Promise { const data: Paste | undefined = await getPaste(id); if (data == undefined) { @@ -87,6 +38,9 @@ export default async function Paste({ + + +
diff --git a/src/app/(pages)/page.tsx b/src/app/(pages)/page.tsx index 0b1673c..a6d1f11 100644 --- a/src/app/(pages)/page.tsx +++ b/src/app/(pages)/page.tsx @@ -17,6 +17,10 @@ export default function Home(): ReactElement { async function createPaste() { // Ignore empty pastes, we don't want to save them if (!value || value.length == 0) { + toast({ + title: "Paste", + description: "Paste is empty", + }); return; } diff --git a/src/app/(pages)/raw/[id]/page.tsx b/src/app/(pages)/raw/[id]/page.tsx new file mode 100644 index 0000000..8df074c --- /dev/null +++ b/src/app/(pages)/raw/[id]/page.tsx @@ -0,0 +1,45 @@ +import React, { ReactElement } from "react"; +import { ActionMenu } from "@/app/components/action-menu"; +import { Metadata } from "next"; +import { notFound } from "next/navigation"; +import { Button } from "@/app/components/ui/button"; +import Link from "next/link"; +import { + generatePasteMetadata, + getPaste, + type Paste, +} from "@/app/common/pastes"; +import { PastePageProps } from "@/app/types/paste-page"; + +export async function generateMetadata({ + params: { id }, +}: PastePageProps): Promise { + return generatePasteMetadata(id); +} + +export default async function Paste({ + params: { id }, +}: PastePageProps): Promise { + const data: Paste | undefined = await getPaste(id); + + if (data == undefined) { + return notFound(); + } + + return ( +
+ + + + + + + + + +
+ {data.content} +
+
+ ); +} diff --git a/src/app/common/pastes.ts b/src/app/common/pastes.ts new file mode 100644 index 0000000..72da803 --- /dev/null +++ b/src/app/common/pastes.ts @@ -0,0 +1,63 @@ +import { cache } from "react"; +import { detectLanguage } from "@/app/common/lang-detection/detection"; +import { Metadata } from "next"; + +export type Paste = { + /** + * The paste content. + */ + content: string; + + /** + * The date the paste was created. + */ + created: string; + + /** + * The detected language of the paste. + */ + language: string; +}; + +/** + * Fetches a paste from the API. + */ +export const getPaste = cache(async (id: string) => { + const response: Response = await fetch( + `${process.env.NEXT_PUBLIC_API_ENDPOINT}/${id}`, + { + next: { + revalidate: 300, // Keep this response cached for 5 minutes + }, + }, + ); + const json = await response.json(); + + if (json.code && json.message) { + return undefined; + } + return { + content: json.content, + created: json.created, + language: await detectLanguage(json.content), + }; +}) as (id: string) => Promise; + +/** + * Generates metadata for a paste. + * + * @param id The ID of the paste. + */ +export async function generatePasteMetadata(id: string): Promise { + const data: Paste | undefined = await getPaste(id); + if (data == undefined) { + return { + description: "Not found", + }; + } + + return { + title: `Paste - ${id}.${data.language}`, + description: `Click to view the paste.`, + }; +} diff --git a/src/app/types/paste-page.ts b/src/app/types/paste-page.ts new file mode 100644 index 0000000..1de1bb5 --- /dev/null +++ b/src/app/types/paste-page.ts @@ -0,0 +1,5 @@ +export type PastePageProps = { + params: { + id: string; + }; +};