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/(pages)/page.tsx

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-04-23 03:22:31 +01:00
"use client";
import { ReactElement, useState } from "react";
import { ActionMenu } from "@/app/components/action-menu";
import { Button } from "@/app/components/ui/button";
2024-04-23 17:44:15 +01:00
import { useToast } from "@/app/components/ui/use-toast";
2024-04-23 03:22:31 +01:00
export default function Home(): ReactElement {
2024-04-23 17:44:15 +01:00
const { toast } = useToast();
2024-04-23 03:22:31 +01:00
const [value, setValue] = useState("");
/**
* Uploads the paste to the server.
*/
async function createPaste() {
// Ignore empty pastes, we don't want to save them
if (!value || value.length == 0) {
return;
}
2024-04-23 17:44:15 +01:00
// Limit the paste size to 400,000 characters
if (value.length > 400_000) {
toast({
title: "Paste",
description: "Pastes can't be longer than 400,000 characters",
});
return;
}
// Upload the paste to the server
2024-04-23 03:22:31 +01:00
const response = await fetch(
`${process.env.NEXT_PUBLIC_API_ENDPOINT}/upload`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: value,
},
);
// Redirect to the new paste
2024-04-23 03:22:31 +01:00
const data = await response.json();
window.location.href = `/${data.id}`;
}
return (
<div className="p-3 h-screen w-screen relative">
<div className="flex gap-2 h-full w-full text-sm">
<p className="hidden md:block">&gt;</p>
<textarea
onInput={(event) => {
setValue((event.target as HTMLTextAreaElement).value);
}}
className="w-full h-full bg-background outline-none resize-none"
/>
</div>
2024-04-23 17:22:29 +01:00
<div className="absolute top-0 right-0 mx-3 mt-2">
<ActionMenu>
<Button onClick={() => createPaste()}>Save</Button>
</ActionMenu>
</div>
2024-04-23 03:22:31 +01:00
</div>
);
}