2024-07-26 19:15:13 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import React, { FC, useEffect, useState } from "react";
|
|
|
|
import { chuni_static_avatar } from "@/prisma/schemas/artemis/generated/artemis";
|
|
|
|
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,
|
|
|
|
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";
|
|
|
|
import { toast } from "../../ui/use-toast";
|
|
|
|
import { cozynet_chuni_static_systemvoice } from "@/prisma/schemas/artemis/generated/artemis";
|
2024-08-02 01:42:14 +00:00
|
|
|
import {
|
|
|
|
getCurrentSystemVoice,
|
|
|
|
getSystemVoices,
|
|
|
|
updatePlayerSystemVoiceId,
|
|
|
|
} from "./actions";
|
2024-07-26 19:15:13 +00:00
|
|
|
|
|
|
|
const getNamePlateTextures = (id: number | undefined) => {
|
|
|
|
if (id === undefined) return "";
|
|
|
|
// Pad the id to be 8 digits long, using leading zeros
|
|
|
|
const paddedId = id.toString().padStart(8, "0");
|
|
|
|
return `systemVoiceThumbnails/CHU_UI_SystemVoice_${paddedId}.png`;
|
|
|
|
};
|
|
|
|
|
|
|
|
type systemvoice = cozynet_chuni_static_systemvoice;
|
|
|
|
|
|
|
|
type SystemVoiceSelectionProps = {
|
|
|
|
playerSystemVoiceSelectionData: {
|
|
|
|
systemVoices: systemvoice[];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const SystemVoiceCustomization: FC<SystemVoiceSelectionProps> = ({
|
|
|
|
playerSystemVoiceSelectionData,
|
|
|
|
}) => {
|
|
|
|
const FormSchema = z.object({
|
|
|
|
PlayerSystemVoice: z.number({
|
|
|
|
required_error: "Please select a System Voice.",
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof FormSchema>>({
|
|
|
|
resolver: zodResolver(FormSchema),
|
|
|
|
});
|
|
|
|
|
|
|
|
const [systemVoiceId, setSytemVoiceId] = useState<number | undefined>(
|
2024-08-02 01:42:14 +00:00
|
|
|
undefined,
|
2024-07-26 19:15:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchSystemVoices = async () => {
|
|
|
|
try {
|
|
|
|
const data = await getCurrentSystemVoice();
|
|
|
|
if (data.length > 0) {
|
|
|
|
setSytemVoiceId(data[0].voiceId!);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching system voices:", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchSystemVoices();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
function onSubmit(data: z.infer<typeof FormSchema>) {
|
2024-08-02 01:42:14 +00:00
|
|
|
const unchagedSystemVoiceId = systemVoiceId;
|
|
|
|
const newSystemVoiceId = data.PlayerSystemVoice ?? unchagedSystemVoiceId;
|
|
|
|
|
|
|
|
updatePlayerSystemVoiceId(newSystemVoiceId).then(() => {
|
|
|
|
setSytemVoiceId(newSystemVoiceId);
|
|
|
|
});
|
|
|
|
resetFormValues();
|
|
|
|
|
2024-07-26 19:15:13 +00:00
|
|
|
toast({
|
|
|
|
title: "You submitted the following values:",
|
|
|
|
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>
|
|
|
|
),
|
|
|
|
});
|
2024-08-02 01:42:14 +00:00
|
|
|
|
|
|
|
function resetFormValues() {
|
|
|
|
form.reset({
|
|
|
|
PlayerSystemVoice: undefined,
|
|
|
|
});
|
|
|
|
}
|
2024-07-26 19:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getTexture = (id: number | undefined, defaultSrc: string) => {
|
|
|
|
return id ? getNamePlateTextures(id) : defaultSrc;
|
|
|
|
};
|
|
|
|
|
2024-08-02 01:42:14 +00:00
|
|
|
const systemVoiceTextures = {
|
|
|
|
SystemVoice: {
|
2024-07-26 19:15:13 +00:00
|
|
|
src: systemVoiceId
|
|
|
|
? getTexture(
|
|
|
|
form.watch("PlayerSystemVoice"),
|
2024-08-02 01:42:14 +00:00
|
|
|
`systemVoiceThumbnails/CHU_UI_SystemVoice_${systemVoiceId.toString().padStart(8, "0")}.png`,
|
2024-07-26 19:15:13 +00:00
|
|
|
)
|
|
|
|
: `systemVoiceThumbnails/CHU_UI_SystemVoice_Default.png`, // Provide a default texture or handle the case when systemVoiceId is undefined
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-07-28 05:10:53 +00:00
|
|
|
<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">
|
2024-07-28 05:10:53 +00:00
|
|
|
<div className="">
|
2024-08-02 01:42:14 +00:00
|
|
|
{Object.entries(systemVoiceTextures).map(([key, { src }]) => (
|
2024-07-28 05:10:53 +00:00
|
|
|
<div>
|
|
|
|
<img className="w-[200px]" src={src} alt={""} />
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-07-26 19:15:13 +00:00
|
|
|
<Form {...form}>
|
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="PlayerSystemVoice"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem className="flex flex-col">
|
|
|
|
<FormLabel>Select System Voice</FormLabel>
|
|
|
|
<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-26 19:15:13 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
{field.value
|
|
|
|
? playerSystemVoiceSelectionData.systemVoices.find(
|
2024-08-02 01:42:14 +00:00
|
|
|
(part) => part.id === field.value,
|
2024-07-26 19:15:13 +00:00
|
|
|
)?.str
|
|
|
|
: "Select System Voice"}
|
|
|
|
<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 system voice found.</CommandEmpty>
|
|
|
|
<CommandGroup>
|
|
|
|
{playerSystemVoiceSelectionData.systemVoices.map(
|
|
|
|
(part) => (
|
|
|
|
<CommandItem
|
|
|
|
value={part.str ?? ""}
|
|
|
|
key={part.id}
|
|
|
|
onSelect={() => {
|
|
|
|
form.setValue("PlayerSystemVoice", part.id!);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Check
|
|
|
|
className={cn(
|
|
|
|
"mr-2 h-4 w-4",
|
|
|
|
part.id === field.value
|
|
|
|
? "opacity-100"
|
2024-08-02 01:42:14 +00:00
|
|
|
: "opacity-0",
|
2024-07-26 19:15:13 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{part.str}
|
|
|
|
</CommandItem>
|
2024-08-02 01:42:14 +00:00
|
|
|
),
|
2024-07-26 19:15:13 +00:00
|
|
|
)}
|
|
|
|
</CommandGroup>
|
|
|
|
</CommandList>
|
|
|
|
</Command>
|
|
|
|
</PopoverContent>
|
|
|
|
</Popover>
|
|
|
|
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="flex justify-end">
|
|
|
|
<Button type="submit">Submit</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
</main>
|
|
|
|
);
|
|
|
|
};
|