daphnis/components/(customization)/avatarcustomization/page.tsx

595 lines
22 KiB
TypeScript
Raw Normal View History

2024-07-23 18:35:30 +00:00
"use client";
2024-07-21 16:19:30 +00:00
import React, { FC, useEffect, useState } from "react";
2024-07-23 18:35:30 +00:00
import { chuni_static_avatar } from "@/prisma/schemas/artemis/generated/artemis";
import { Check, ChevronsUpDown } from "lucide-react";
import { getCurrentAvatarParts } from "./actions";
2024-07-23 18:35:30 +00:00
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Form,
FormControl,
FormDescription,
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";
2024-07-26 17:19:37 +00:00
import { toast } from "../../ui/use-toast";
import { updateAvatarParts } from "./actions";
type chunithm_avatar = chuni_static_avatar;
2024-07-24 20:22:30 +00:00
const getAvatarTextureSrc = (id: number | undefined) => {
if (id === undefined) return "";
return `avatarAccessory/CHU_UI_Avatar_Tex_0${id}.png`;
};
2024-07-23 18:35:30 +00:00
type AvatarSelectionProps = {
2024-07-23 19:34:53 +00:00
avatarHeadSelectionData: {
avatarParts: chunithm_avatar[];
};
avatarFaceSelectionData: {
avatarParts: chunithm_avatar[];
};
avatarItemSelectionData: {
avatarParts: chunithm_avatar[];
};
avatarBackSelectionData: {
avatarParts: chunithm_avatar[];
};
avatarWearSelectionData: {
2024-07-23 18:35:30 +00:00
avatarParts: chunithm_avatar[];
};
2024-07-23 00:59:17 +00:00
};
2024-07-23 18:35:30 +00:00
export const AvatarCustomization: FC<AvatarSelectionProps> = ({
2024-07-23 19:34:53 +00:00
avatarHeadSelectionData,
avatarFaceSelectionData,
avatarItemSelectionData,
avatarBackSelectionData,
avatarWearSelectionData,
2024-07-23 18:35:30 +00:00
}) => {
const FormSchema = z.object({
2024-08-02 01:42:14 +00:00
AvatarHeadAccessory: z.number().optional(),
AvatarFaceAccessory: z.number().optional(),
AvatarItemAccessory: z.number().optional(),
AvatarBackAccessory: z.number().optional(),
AvatarWearAccessory: z.number().optional(),
2024-07-23 18:35:30 +00:00
});
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
});
const [avatarFaceId, setAvatarFaceId] = useState<number | undefined>(
2024-08-02 01:42:14 +00:00
undefined,
);
const [avatarSkinId, setAvatarSkinId] = useState<number | undefined>(
2024-08-02 01:42:14 +00:00
undefined,
);
const [avatarHeadId, setAvatarHeadId] = useState<number | undefined>(
2024-08-02 01:42:14 +00:00
undefined,
);
const [avatarWearId, setAvatarWearId] = useState<number | undefined>(
2024-08-02 01:42:14 +00:00
undefined,
);
const [avatarBackId, setAvatarBackId] = useState<number | undefined>(
2024-08-02 01:42:14 +00:00
undefined,
);
const [avatarItemId, setAvatarItemId] = useState<number | undefined>(
2024-08-02 01:42:14 +00:00
undefined,
);
useEffect(() => {
const fetchAvatarParts = async () => {
try {
const data = await getCurrentAvatarParts();
2024-08-02 01:42:14 +00:00
setAvatarFaceId(data[0].avatarFace!);
setAvatarSkinId(data[0].avatarSkin!);
setAvatarHeadId(data[0].avatarHead!);
setAvatarWearId(data[0].avatarWear!);
setAvatarBackId(data[0].avatarBack!);
setAvatarItemId(data[0].avatarItem!);
} catch (error) {
console.error("Error fetching avatar parts:", error);
}
};
fetchAvatarParts();
}, []);
2024-07-23 18:35:30 +00:00
function onSubmit(data: z.infer<typeof FormSchema>) {
2024-08-02 01:42:14 +00:00
// Existing state
const defaultHeadId = avatarHeadId;
const defaultFaceId = avatarFaceId;
const defaultBackId = avatarBackId;
const defaultWearId = avatarWearId;
const defaultItemId = avatarItemId;
2024-08-02 01:42:14 +00:00
// either change to the new body part id or fallback to the default if nothing has changed
const newHeadId = data.AvatarHeadAccessory ?? defaultHeadId;
const newFaceId = data.AvatarFaceAccessory ?? defaultFaceId;
const newBackId = data.AvatarBackAccessory ?? defaultBackId;
const newWearId = data.AvatarWearAccessory ?? defaultWearId;
const newItemId = data.AvatarItemAccessory ?? defaultItemId;
2024-08-02 01:42:14 +00:00
updateAvatarParts(newHeadId, newFaceId, newBackId, newWearId, newItemId)
.then(() => {
setAvatarHeadId(newHeadId);
setAvatarFaceId(newFaceId);
setAvatarBackId(newBackId);
setAvatarWearId(newWearId);
setAvatarItemId(newItemId);
toast({
title: "Avatar updated successfully!",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">
{JSON.stringify(data, null, 2)}
</code>
</pre>
),
});
resetFormValues();
})
.catch((error) => {
toast({
title: "Error updating avatar",
description: error.message,
variant: "destructive",
});
});
2024-07-23 18:35:30 +00:00
}
2024-08-02 01:42:14 +00:00
function resetFormValues() {
form.reset({
AvatarHeadAccessory: undefined,
AvatarFaceAccessory: undefined,
AvatarItemAccessory: undefined,
AvatarBackAccessory: undefined,
AvatarWearAccessory: undefined,
});
}
2024-07-24 20:22:30 +00:00
const getTexture = (id: number | undefined, defaultSrc: string) => {
return id ? getAvatarTextureSrc(id) : defaultSrc;
};
const AvatarTextures = {
AvatarHeadAccessory: {
src: getTexture(
form.watch("AvatarHeadAccessory"),
2024-08-02 01:42:14 +00:00
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarHeadId}.png`,
2024-07-24 20:22:30 +00:00
),
className: "avatar_head",
},
AvatarFaceAccessory: {
src: getTexture(
form.watch("AvatarFaceAccessory"),
2024-08-02 01:42:14 +00:00
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarFaceId}.png`,
2024-07-24 20:22:30 +00:00
),
className: "avatar_face",
},
AvatarItemAccessoryR: {
src: getTexture(
form.watch("AvatarItemAccessory"),
2024-08-02 01:42:14 +00:00
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarItemId}.png`,
2024-07-24 20:22:30 +00:00
),
className: "avatar_item_r ",
},
AvatarItemAccessoryL: {
src: getTexture(
form.watch("AvatarItemAccessory"),
2024-08-02 01:42:14 +00:00
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarItemId}.png`,
2024-07-24 20:22:30 +00:00
),
className: "avatar_item_l ",
},
AvatarBackAccessory: {
src: getTexture(
form.watch("AvatarBackAccessory"),
2024-08-02 01:42:14 +00:00
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarBackId}.png`,
2024-07-24 20:22:30 +00:00
),
className: "avatar_back",
},
AvatarWearAccessory: {
src: getTexture(
form.watch("AvatarWearAccessory"),
2024-08-02 01:42:14 +00:00
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarWearId}.png`,
2024-07-24 20:22:30 +00:00
),
className: "avatar_wear",
2024-07-24 19:18:05 +00:00
},
2024-07-24 20:22:30 +00:00
avatarSkinAccessory: {
src: getTexture(
avatarSkinId,
2024-08-02 01:42:14 +00:00
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarSkinId}.png`,
),
2024-07-24 19:18:05 +00:00
className: "avatar_skin",
},
AvatarRightHand: {
src: "avatarStatic/CHU_UI_Avatar_Tex_RightHand.png",
className: "avatar_hand_r",
},
AvatarSkinFootR: {
src: `avatarAccessory/CHU_UI_Avatar_Tex_0${avatarSkinId}.png`,
className: "avatar_skinfoot_r",
},
2024-07-24 19:18:05 +00:00
AvatarLeftHand: {
src: "avatarStatic/CHU_UI_Avatar_Tex_LeftHand.png",
className: "avatar_hand_l",
},
AvatarSkinFootL: {
src: `avatarAccessory/CHU_UI_Avatar_Tex_0${avatarSkinId}.png`,
2024-07-24 19:18:05 +00:00
className: "avatar_skinfoot_l",
},
2024-07-24 20:22:30 +00:00
AvatarFaceStatic: {
src: "avatarStatic/CHU_UI_Avatar_Tex_Face.png",
className: "avatar_face_static",
2024-07-24 19:18:05 +00:00
},
};
2024-07-23 18:35:30 +00:00
return (
<main className="flex flex-col items-center space-y-6">
{/* Avatar Customization Section */}
2024-08-02 01:42:14 +00:00
<div className="flex w-full justify-center">
<div className="avatar_base">
{Object.entries(AvatarTextures).map(([key, { className, src }]) => (
<div className={className} key={key}>
<img src={src} alt={""} />
</div>
))}
</div>
</div>
2024-07-24 19:18:05 +00:00
<div>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
control={form.control}
name="AvatarHeadAccessory"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel className="pb-2">Avatar Head Item</FormLabel>
2024-07-24 19:18:05 +00:00
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
className={cn(
"w-[300px] justify-between",
2024-08-02 01:42:14 +00:00
!field.value && "text-muted-foreground",
2024-07-24 19:18:05 +00:00
)}
>
{field.value
? avatarHeadSelectionData.avatarParts.find(
2024-08-02 01:42:14 +00:00
(part) =>
part.avatarAccessoryId === field.value,
2024-07-24 19:18:05 +00:00
)?.name
: "Select Avatar Head Item"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0">
<Command>
<CommandList>
<CommandEmpty>No avatar part found.</CommandEmpty>
<CommandGroup>
{avatarHeadSelectionData.avatarParts.map((part) => (
<CommandItem
value={part.name ?? ""}
key={part.avatarAccessoryId}
onSelect={() => {
form.setValue(
"AvatarHeadAccessory",
2024-08-02 01:42:14 +00:00
part.avatarAccessoryId!,
2024-07-24 19:18:05 +00:00
);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
2024-08-02 01:42:14 +00:00
: "opacity-0",
2024-07-24 19:18:05 +00:00
)}
/>
{part.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
2024-07-23 18:35:30 +00:00
2024-07-24 19:18:05 +00:00
<FormMessage />
</FormItem>
)}
/>
2024-07-24 20:22:30 +00:00
2024-07-24 19:18:05 +00:00
<FormField
control={form.control}
name="AvatarFaceAccessory"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel className="pb-2">Avatar Face Item</FormLabel>
2024-07-24 19:18:05 +00:00
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
className={cn(
"w-[300px] justify-between",
2024-08-02 01:42:14 +00:00
!field.value && "text-muted-foreground",
2024-07-24 19:18:05 +00:00
)}
>
{field.value
? avatarFaceSelectionData.avatarParts.find(
2024-08-02 01:42:14 +00:00
(part) =>
part.avatarAccessoryId === field.value,
2024-07-24 19:18:05 +00:00
)?.name
: "Select Avatar Face Item"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0">
<Command>
<CommandList>
2024-07-24 20:22:30 +00:00
<CommandEmpty>No avatar part found.</CommandEmpty>
2024-07-24 19:18:05 +00:00
<CommandGroup>
{avatarFaceSelectionData.avatarParts.map((part) => (
<CommandItem
value={part.name ?? ""}
key={part.avatarAccessoryId}
onSelect={() => {
form.setValue(
"AvatarFaceAccessory",
2024-08-02 01:42:14 +00:00
part.avatarAccessoryId!,
2024-07-24 19:18:05 +00:00
);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
2024-08-02 01:42:14 +00:00
: "opacity-0",
2024-07-24 19:18:05 +00:00
)}
/>
{part.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
2024-07-23 18:35:30 +00:00
2024-07-24 19:18:05 +00:00
<FormMessage />
</FormItem>
)}
/>
2024-07-24 20:22:30 +00:00
2024-07-24 19:18:05 +00:00
<FormField
control={form.control}
name="AvatarItemAccessory"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel className="pb-2">Avatar Hand Item</FormLabel>
2024-07-24 19:18:05 +00:00
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
className={cn(
"w-[300px] justify-between",
2024-08-02 01:42:14 +00:00
!field.value && "text-muted-foreground",
2024-07-24 19:18:05 +00:00
)}
>
{field.value
? avatarItemSelectionData.avatarParts.find(
2024-08-02 01:42:14 +00:00
(part) =>
part.avatarAccessoryId === field.value,
2024-07-24 19:18:05 +00:00
)?.name
2024-07-24 20:22:30 +00:00
: "Select Avatar Face Item"}
2024-07-24 19:18:05 +00:00
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0">
<Command>
<CommandList>
<CommandEmpty>No avatar part found.</CommandEmpty>
<CommandGroup>
{avatarItemSelectionData.avatarParts.map((part) => (
<CommandItem
value={part.name ?? ""}
key={part.avatarAccessoryId}
onSelect={() => {
form.setValue(
"AvatarItemAccessory",
2024-08-02 01:42:14 +00:00
part.avatarAccessoryId!,
2024-07-24 19:18:05 +00:00
);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
2024-08-02 01:42:14 +00:00
: "opacity-0",
2024-07-24 19:18:05 +00:00
)}
/>
{part.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
2024-07-23 18:35:30 +00:00
2024-07-24 19:18:05 +00:00
<FormMessage />
</FormItem>
)}
/>
2024-07-24 20:22:30 +00:00
2024-07-24 19:18:05 +00:00
<FormField
control={form.control}
name="AvatarBackAccessory"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel className="pb-2">Avatar Back Item</FormLabel>
2024-07-24 19:18:05 +00:00
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
className={cn(
"w-[300px] justify-between",
2024-08-02 01:42:14 +00:00
!field.value && "text-muted-foreground",
2024-07-24 19:18:05 +00:00
)}
>
{field.value
? avatarBackSelectionData.avatarParts.find(
2024-08-02 01:42:14 +00:00
(part) =>
part.avatarAccessoryId === field.value,
2024-07-24 19:18:05 +00:00
)?.name
: "Select Avatar Back Item"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0">
<Command>
<CommandList>
<CommandEmpty>No avatar part found.</CommandEmpty>
<CommandGroup>
{avatarBackSelectionData.avatarParts.map((part) => (
<CommandItem
value={part.name ?? ""}
key={part.avatarAccessoryId}
onSelect={() => {
form.setValue(
"AvatarBackAccessory",
2024-08-02 01:42:14 +00:00
part.avatarAccessoryId!,
2024-07-24 19:18:05 +00:00
);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
2024-08-02 01:42:14 +00:00
: "opacity-0",
2024-07-24 19:18:05 +00:00
)}
/>
{part.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
2024-07-23 18:35:30 +00:00
2024-07-24 19:18:05 +00:00
<FormMessage />
</FormItem>
)}
/>
2024-07-24 20:22:30 +00:00
2024-07-24 19:18:05 +00:00
<FormField
control={form.control}
name="AvatarWearAccessory"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel className="pb-2">Avatar Clothing Item</FormLabel>
2024-07-24 19:18:05 +00:00
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
className={cn(
"w-[300px] justify-between",
2024-08-02 01:42:14 +00:00
!field.value && "text-muted-foreground",
2024-07-24 19:18:05 +00:00
)}
>
{field.value
? avatarWearSelectionData.avatarParts.find(
2024-08-02 01:42:14 +00:00
(part) =>
part.avatarAccessoryId === field.value,
2024-07-24 19:18:05 +00:00
)?.name
2024-07-24 20:22:30 +00:00
: "Select Avatar Clothing Item"}
2024-07-24 19:18:05 +00:00
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0">
<Command>
<CommandList>
<CommandEmpty>No avatar part found.</CommandEmpty>
<CommandGroup>
{avatarWearSelectionData.avatarParts.map((part) => (
<CommandItem
value={part.name ?? ""}
key={part.avatarAccessoryId}
onSelect={() => {
form.setValue(
"AvatarWearAccessory",
2024-08-02 01:42:14 +00:00
part.avatarAccessoryId!,
2024-07-24 19:18:05 +00:00
);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
2024-08-02 01:42:14 +00:00
: "opacity-0",
2024-07-24 19:18:05 +00:00
)}
/>
{part.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
2024-07-23 18:35:30 +00:00
2024-07-24 19:18:05 +00:00
<FormMessage />
</FormItem>
)}
/>
2024-07-26 18:08:36 +00:00
<div className="flex justify-end">
<Button type="submit">Submit</Button>
</div>
2024-07-24 19:18:05 +00:00
</form>
</Form>
</div>
2024-07-23 18:35:30 +00:00
</main>
);
};