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.

91 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-04-23 17:22:29 +01:00
import { cache, ReactElement } from "react";
2024-04-23 03:22:31 +01:00
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";
import { CodeBlock } from "@/app/components/code-block";
2024-04-23 17:22:29 +01:00
import { detectLanguage } from "@/app/common/lang-detection/detection";
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:39:17 +01:00
2024-04-23 17:22:29 +01:00
/**
* The detected language of the paste.
*/
language: string;
};
2024-04-23 03:39:17 +01:00
2024-04-23 17:22:29 +01:00
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
},
},
);
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
}
2024-04-23 17:22:29 +01:00
return {
content: json.content,
created: json.created,
language: await detectLanguage(json.content),
};
}) as (id: string) => Promise<Paste | undefined>;
export async function generateMetadata({
params: { id },
}: PasteProps): Promise<Metadata> {
const data: Paste | undefined = await getPaste(id);
if (data == undefined) {
return {
description: "Not found",
};
}
return {
title: `Paste - ${id}.${data.language}`,
2024-04-23 17:22:29 +01:00
description: `Created: ${moment(data.created)}\n\nClick to view the paste.`,
};
2024-04-23 03:22:31 +01:00
}
export default async function Paste({
params: { id },
}: PasteProps): Promise<ReactElement> {
2024-04-23 17:22:29 +01:00
const data: Paste | undefined = await getPaste(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">
2024-04-23 17:22:29 +01:00
<div className="absolute top-0 right-0 flex flex-col items-end mx-3 mt-2">
<ActionMenu />
<p>{data.language}</p>
</div>
2024-04-23 03:22:31 +01:00
<div className="p-1 hljs !bg-transparent text-sm">
<CodeBlock code={data.content} language={data.language} />
</div>
2024-04-23 03:22:31 +01:00
</div>
);
}