2024-04-23 03:22:31 +01:00
|
|
|
"use client";
|
|
|
|
|
2024-04-24 18:42:42 +01:00
|
|
|
import { ReactElement, useEffect, useState } from "react";
|
2024-04-23 03:22:31 +01:00
|
|
|
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 21:35:47 +01:00
|
|
|
import { ArrowPathIcon } from "@heroicons/react/16/solid";
|
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("");
|
2024-04-23 21:35:47 +01:00
|
|
|
const [creating, setCreating] = useState(false);
|
2024-04-23 03:22:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads the paste to the server.
|
|
|
|
*/
|
|
|
|
async function createPaste() {
|
|
|
|
// Ignore empty pastes, we don't want to save them
|
|
|
|
if (!value || value.length == 0) {
|
2024-04-24 17:46:47 +01:00
|
|
|
toast({
|
|
|
|
title: "Paste",
|
|
|
|
description: "Paste is empty",
|
|
|
|
});
|
2024-04-23 03:22:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-23 21:35:47 +01:00
|
|
|
setCreating(true);
|
2024-04-23 16:37:33 +01:00
|
|
|
// 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: {
|
2024-04-25 18:53:40 +01:00
|
|
|
"Content-Type": "text/plain",
|
2024-04-23 03:22:31 +01:00
|
|
|
},
|
|
|
|
body: value,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const data = await response.json();
|
2024-04-23 21:23:18 +01:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
toast({
|
|
|
|
title: "Paste",
|
|
|
|
description: data.message,
|
|
|
|
});
|
2024-04-23 21:35:47 +01:00
|
|
|
setCreating(false);
|
2024-04-23 21:23:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the new paste
|
2024-04-23 03:22:31 +01:00
|
|
|
window.location.href = `/${data.id}`;
|
|
|
|
}
|
|
|
|
|
2024-04-24 18:42:42 +01:00
|
|
|
useEffect(() => {
|
|
|
|
document.getElementById("paste-input")?.focus();
|
|
|
|
}, []);
|
|
|
|
|
2024-04-23 03:22:31 +01:00
|
|
|
return (
|
|
|
|
<div className="p-3 h-screen w-screen relative">
|
2024-04-24 18:29:29 +01:00
|
|
|
<div className="flex gap-2 h-full w-full text-xs">
|
|
|
|
{/* > */}
|
2024-04-23 03:22:31 +01:00
|
|
|
<p className="hidden md:block">></p>
|
2024-04-24 18:29:29 +01:00
|
|
|
|
|
|
|
{/* Input */}
|
2024-04-23 03:22:31 +01:00
|
|
|
<textarea
|
|
|
|
onInput={(event) => {
|
|
|
|
setValue((event.target as HTMLTextAreaElement).value);
|
|
|
|
}}
|
2024-04-24 18:42:42 +01:00
|
|
|
id="paste-input"
|
2024-04-23 03:22:31 +01:00
|
|
|
className="w-full h-full bg-background outline-none resize-none"
|
2024-04-24 18:42:42 +01:00
|
|
|
placeholder="Paste your code here..."
|
2024-04-23 03:22:31 +01:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2024-04-24 18:29:29 +01:00
|
|
|
{/* Action Menu */}
|
2024-04-24 17:35:25 +01:00
|
|
|
<ActionMenu>
|
|
|
|
<Button onClick={() => createPaste()} className="flex gap-1">
|
|
|
|
{creating && (
|
|
|
|
<div className="animate-spin">
|
|
|
|
<ArrowPathIcon width={16} height={16} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
</ActionMenu>
|
2024-04-23 03:22:31 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|