File size: 12,927 Bytes
87337b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
import * as React from "react"
import { buttonVariants } from "@/components/ui/button"
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/components/ui/sheet"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
} from "@/components/ui/form"
import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
import { LoaderCircleIcon, UsersIcon } from "lucide-react"
import { set, useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import { Switch } from "../ui/switch"
import { useAppDispatch, useAppSelector } from "@/common/hooks"
import { Input } from "../ui/input"
import { setTrulienceSettings } from "@/store/reducers/global"
import { toast } from "sonner"
export function TrulienceCfgSheet() {
const dispatch = useAppDispatch()
const trulienceSettings = useAppSelector((state) => state.global.trulienceSettings)
return (
<Sheet>
<SheetTrigger
className={cn(
buttonVariants({ variant: "outline", size: "icon" }),
"bg-transparent"
)}
>
<UsersIcon />
</SheetTrigger>
<SheetContent className="w-[400px] overflow-y-auto sm:w-[540px]">
<SheetHeader>
<SheetTitle>Trulience Avatar</SheetTitle>
<SheetDescription>
You can configure the Trulience Avatar settings here. This will give you a nice avatar for your chat.
</SheetDescription>
</SheetHeader>
<div className="my-4">
<TrulienceCfgForm
initialData={{
enable_trulience_avatar: trulienceSettings.enabled,
trulience_avatar_id: trulienceSettings.avatarId,
trulience_avatar_token: trulienceSettings.avatarToken,
trulience_large_window: trulienceSettings.avatarDesktopLargeWindow,
trulience_sdk_url: trulienceSettings.trulienceSDK,
trulience_animation_url: trulienceSettings.animationURL,
}}
onUpdate={async (data) => {
if (data.enable_trulience_avatar === true) {
if (!data.trulience_avatar_id) {
toast.error("Trulience Settings", {
description: `Please provide Trulience Avatar ID`,
})
return
}
}
dispatch(setTrulienceSettings({
enabled: data.enable_trulience_avatar as boolean,
avatarId: data.trulience_avatar_id as string,
avatarToken: data.trulience_avatar_token as string,
avatarDesktopLargeWindow: data.trulience_large_window as boolean,
trulienceSDK: data.trulience_sdk_url as string,
animationURL: data.trulience_animation_url as string,
}))
toast.success("Trulience Settings", {
description: `Settings updated successfully`,
})
}}
/>
</div>
</SheetContent>
</Sheet>
);
}
const TrulienceCfgForm = ({
initialData,
onUpdate,
}: {
initialData: Record<string, string | boolean | null | undefined>;
onUpdate: (data: Record<string, string | boolean | null>) => void;
}) => {
const formSchema = z.record(z.string(),
z.union([z.string(), z.boolean(), z.null()]));
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: initialData,
});
const { watch } = form;
// Watch for changes in "enable_trulience_avatar" field
const enableTrulienceAvatar = watch("enable_trulience_avatar");
const onSubmit = (data: z.infer<typeof formSchema>) => {
onUpdate(data);
};
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
key={"enable_trulience_avatar"}
control={form.control}
name={"enable_trulience_avatar"}
render={({ field }) => (
<FormItem>
<FormLabel>Enable Trulience Avatar</FormLabel>
<div className="flex justify-between items-center">
<FormControl>
<div className="flex items-center space-x-2">
<Switch
checked={field.value === true}
onCheckedChange={field.onChange}
/>
</div>
</FormControl>
</div>
</FormItem>
)}
/>
{
enableTrulienceAvatar && (
<>
<FormField
key={"trulience_avatar_id"}
control={form.control}
name={"trulience_avatar_id"}
render={({ field }) => (
<FormItem>
<FormLabel>Trulience Avatar ID</FormLabel>
<div className="flex justify-between items-center">
<FormControl>
<Input
{...field}
value={
field.value === null || field.value === undefined
? ""
: field.value.toString()
}
type={"text"}
/>
</FormControl>
</div>
</FormItem>
)}
/>
<FormField
key={"trulience_avatar_token"}
control={form.control}
name={"trulience_avatar_token"}
render={({ field }) => (
<FormItem>
<FormLabel>Trulience Avatar Token</FormLabel>
<div className="flex justify-between items-center">
<FormControl>
<Input
{...field}
value={
field.value === null || field.value === undefined
? ""
: field.value.toString()
}
type={"text"}
/>
</FormControl>
</div>
</FormItem>
)}
/>
<FormField
key={"trulience_large_window"}
control={form.control}
name={"trulience_large_window"}
render={({ field }) => (
<FormItem>
<FormLabel>Trulience Large Window</FormLabel>
<div className="flex justify-between items-center">
<FormControl>
<div className="flex items-center space-x-2">
<Switch
checked={field.value === true}
onCheckedChange={field.onChange}
/>
</div>
</FormControl>
</div>
</FormItem>
)}
/>
<FormField
key={"trulience_sdk_url"}
control={form.control}
name={"trulience_sdk_url"}
render={({ field }) => (
<FormItem>
<FormLabel>Trulience SDK URL</FormLabel>
<div className="flex justify-between items-center">
<FormControl>
<Input
{...field}
value={
field.value === null || field.value === undefined
? ""
: field.value.toString()
}
type={"text"}
/>
</FormControl>
</div>
</FormItem>
)}
/>
<FormField
key={"trulience_animation_url"}
control={form.control}
name={"trulience_animation_url"}
render={({ field }) => (
<FormItem>
<FormLabel>Trulience Animation URL</FormLabel>
<div className="flex justify-between items-center">
<FormControl>
<Input
{...field}
value={
field.value === null || field.value === undefined
? ""
: field.value.toString()
}
type={"text"}
/>
</FormControl>
</div>
</FormItem>
)}
/>
</>
)
}
<Button type="submit" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? (
<>
<LoaderCircleIcon className="h-4 w-4 animate-spin" />
<span>Saving...</span>
</>
) : (
"Save changes"
)}
</Button>
</form>
</Form>
);
};
|