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.
Frontend/src/app/common/pastes.ts
Liam f185270a5b
All checks were successful
Deploy App / docker (ubuntu-latest) (push) Successful in 1m4s
cleanup and add a raw page
2024-04-24 17:46:47 +01:00

64 lines
1.3 KiB
TypeScript

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<Paste | undefined>;
/**
* Generates metadata for a paste.
*
* @param id The ID of the paste.
*/
export async function generatePasteMetadata(id: string): Promise<Metadata> {
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.`,
};
}