This repository has been archived on 2024-06-01. You can view files and clone it, but cannot push or open issues or pull requests.

78 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-04-23 03:22:31 +01:00
import { ReactElement } from "react";
import { ActionMenu } from "@/app/components/action-menu";
2024-04-23 03:39:17 +01:00
import { Metadata } from "next";
import moment from "moment";
2024-04-23 03:49:14 +01:00
import { notFound } from "next/navigation";
2024-04-23 04:24:13 +01:00
import { CodeBlock } from "@/app/components/codeBlock";
2024-04-23 03:22:31 +01:00
type PasteProps = {
params: {
id: string;
};
};
type Paste = {
/**
* The paste content.
*/
2024-04-23 03:49:14 +01:00
content: string;
2024-04-23 03:22:31 +01:00
2024-04-23 03:39:17 +01:00
/**
* The date the paste was created.
*/
2024-04-23 03:49:14 +01:00
created: number;
2024-04-23 03:22:31 +01:00
};
2024-04-23 03:39:17 +01:00
export async function generateMetadata({
params: { id },
}: PasteProps): Promise<Metadata> {
const data: Paste | undefined = await getData(id);
2024-04-23 03:49:14 +01:00
if (data == undefined) {
2024-04-23 03:39:17 +01:00
return {
description: "Not found",
};
}
return {
2024-04-23 05:05:32 +01:00
title: `Paste - ${id}`,
2024-04-23 03:49:14 +01:00
description: `Created: ${moment(data.created)}\n\nClick to view the paste.`,
2024-04-23 03:39:17 +01:00
};
}
2024-04-23 03:49:14 +01:00
async function getData(id: string): Promise<Paste | undefined> {
const response: Response = await fetch(
`${process.env.NEXT_PUBLIC_API_ENDPOINT}/${id}`,
{
next: {
revalidate: 300, // Keep this response cached for 5 minutes
},
},
);
2024-04-23 03:22:31 +01:00
const json = await response.json();
if (json.code && json.message) {
2024-04-23 03:49:14 +01:00
return undefined;
2024-04-23 03:22:31 +01:00
}
return json as Paste;
}
export default async function Paste({
params: { id },
}: PasteProps): Promise<ReactElement> {
const data: Paste | undefined = await getData(id);
2024-04-23 03:22:31 +01:00
2024-04-23 03:49:14 +01:00
if (data == undefined) {
return notFound();
2024-04-23 03:22:31 +01:00
}
return (
<div className="relative">
<ActionMenu />
<div className="p-1 hljs !bg-transparent text-sm">
2024-04-23 04:24:13 +01:00
<CodeBlock code={data.content} />
</div>
2024-04-23 03:22:31 +01:00
</div>
);
}