"use client"; import React, { FC, useEffect, useState } from "react"; import { Check, ChevronsUpDown } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandItem, CommandList, } from "@/components/ui/command"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { toast } from "../../ui/use-toast"; import { cozynet_chuni_static_mapicon } from "@/prisma/schemas/artemis/generated/artemis"; import { getCurrentMapIcon, updatePlayerMapIcon } from "./actions"; const getNamePlateTextures = (id: number | undefined) => { if (id === undefined) return ""; const paddedId = id.toString().padStart(8, "0"); return `mapIcon/CHU_UI_MapIcon_${paddedId}.png`; }; type mapIcons = cozynet_chuni_static_mapicon; type SystemVoiceSelectionProps = { playerMapIconCustomization: { mapIcon: mapIcons[]; }; }; export const MapIconCustomization: FC = ({ playerMapIconCustomization, }) => { const FormSchema = z.object({ mapIconId: z.number({ required_error: "Please select an Avatar Head Item.", }), }); const form = useForm>({ resolver: zodResolver(FormSchema), }); const [mapIconId, setMapIconId] = useState(undefined); useEffect(() => { const fetchMapIcons = async () => { try { const data = await getCurrentMapIcon(); if (data.length > 0) { setMapIconId(data[0].mapIconId!); } } catch (error) { console.error("Error fetching avatar parts:", error); } }; fetchMapIcons(); }, []); function onSubmit(data: z.infer) { const uncangedMapIconId = mapIconId; const newMapIconId = data.mapIconId ?? uncangedMapIconId; updatePlayerMapIcon(newMapIconId).then(() => { setMapIconId(newMapIconId); }); resetFormValues(); toast({ title: "You submitted the following values:", description: (
          {JSON.stringify(data, null, 2)}
        
), }); function resetFormValues() { form.reset({ mapIconId: undefined, }); } } const getTexture = (id: number | undefined, defaultSrc: string) => { return id ? getNamePlateTextures(id) : defaultSrc; }; const MapIconTextures = { mapIconId: { src: mapIconId ? getTexture( form.watch("mapIconId"), `mapIcon/CHU_UI_MapIcon_${mapIconId.toString().padStart(8, "0")}.png`, ) : `systemVoiceThumbnails/CHU_UI_SystemVoice_Default.png`, }, }; return (
{Object.entries(MapIconTextures).map(([key, { src }]) => (
{""}
))}
( Select Map Icon No name plate found. {playerMapIconCustomization.mapIcon.map((part) => ( { form.setValue("mapIconId", part.id!); }} > {part.str} ))} )} />
); };