"use client"; import { Label } from "./ui/label"; import { Button } from "./ui/button"; import { Textarea } from "./ui/textarea"; import { CheckIcon, CornerDownLeft, X } from "lucide-react"; import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { getCustomization, ownServer, setCustomization, serverOwned as sOFunc, unownServer, userOwnedServer, } from "@/lib/api"; import toast from "react-hot-toast"; import { SignedIn, SignedOut, useUser } from "@clerk/nextjs"; import { OnlineServer } from "@/lib/types/server"; import Link from "next/link"; import { useEffectOnce } from "@/lib/useEffectOnce"; import Setting from "./ui/setting"; import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, Form, } from "./ui/form"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Input } from "./ui/input"; import "@/themes.css"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "./ui/card"; import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover"; import { themes } from "@/lib/themes"; import { useTheme } from "next-themes"; import { useColor } from "@/lib/use-color"; import { DiscordPopover } from "./misc/DiscordPopover"; import { Spinner } from "./ui/spinner"; import { BannerPopover } from "./misc/BannerPopover"; const formSchema = z.object({ description: z .string() .min(2, { message: "Description must be at least 2 characters.", }) .max(1250, { message: "Description cannot be longer than 1250 characters.", }), }); export default function ServerCustomize({ server, cs, setCS, }: { server: string; cs: string; setCS: Dispatch>; }) { const [serverOwned, setServerOwned] = useState(false); const [userOwned, setUserOwned] = useState(false); const [loading, setLoading] = useState(true); const [description, setDescription] = useState(""); const [get, setGet] = useState({}); const [author, setAuthor] = useState(""); const [minehutOwned, setMinehutOwned] = useState(false); const { resolvedTheme: mode } = useTheme(); const { user, isSignedIn } = useUser(); useEffect(() => { sOFunc(server).then((c) => { setServerOwned(c); getCustomization(server).then((b) => { setGet(b); setDescription(b != null ? b.description : ""); form.reset({ description: b != null ? b.description : "" }); setCS(b != null ? b.colorScheme : "zinc"); userOwnedServer(server).then((c) => { setUserOwned(c); fetch("https://api.minehut.com/servers").then((c) => { c.json().then((c: { servers: Array }) => { c.servers.forEach((v) => { setAuthor(v.author); if (v.name == server && isSignedIn) { if (user?.publicMetadata.player == v.author) { setMinehutOwned(true); } } setLoading(false); }); }); }); }); }); }); }, [isSignedIn]); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { description, }, }); if (loading) { return ( <>
); } return ( <> {!serverOwned && (
Do you own this server?
Create an account and link it to the owner of the server to customize it.
)}
{!serverOwned && user?.publicMetadata.player == null && (
Do you own this server?
Create an account and link it to the owner of the server to customize it.
)}
{!serverOwned && minehutOwned && (
Do you own this server?{" "}
)}
{!userOwned && ( <> {" "} Whoops.. something went wrong
You don't have permission to customize this server.
)} {serverOwned && userOwned && (
Description Edit the description of the server.
{ toast.promise(setCustomization(server, c), { success: "Successfully set description", loading: "Setting description..", error: "Error while setting descript", }); })} > ( Description