Spaces:
Running
Running
File size: 12,310 Bytes
27127dd |
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
import React, { useEffect } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { useMutation } from "@tanstack/react-query";
import { apiRequest, queryClient } from "@/lib/queryClient";
import { useAuth } from "@/hooks/use-auth";
import { useToast } from "@/hooks/use-toast";
import { updateUserProfileSchema } from "@shared/schema";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogFooter,
} from "@/components/ui/dialog";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Button } from "@/components/ui/button";
import { Loader2 } from "lucide-react";
// Create a form schema
const profileFormSchema = z.object({
fullName: z.string().optional(),
location: z.string().optional(),
interests: z.array(z.string()).optional(),
interestsInput: z.string().optional(), // For input field value only, not submitted
profession: z.string().optional(),
pets: z.string().optional(),
systemContext: z.string().optional(),
});
type ProfileFormValues = z.infer<typeof profileFormSchema>;
interface UserSettingsModalProps {
isOpen: boolean;
onClose: () => void;
}
export default function UserSettingsModal({
isOpen,
onClose,
}: UserSettingsModalProps) {
const { user } = useAuth();
const { toast } = useToast();
// Create form with default values
const form = useForm<ProfileFormValues>({
resolver: zodResolver(profileFormSchema),
defaultValues: {
fullName: "",
location: "",
interests: [],
interestsInput: "",
profession: "",
pets: "",
systemContext: "",
},
});
// Update form when user data changes
useEffect(() => {
if (user) {
// Convert interests array to comma-separated string for display
const interestsString = user.interests?.join(", ") || "";
form.reset({
fullName: user.fullName || "",
location: user.location || "",
interests: user.interests || [],
interestsInput: interestsString,
profession: user.profession || "",
pets: user.pets || "",
systemContext: user.systemContext || "",
});
}
}, [user, form]);
const updateProfileMutation = useMutation({
mutationFn: async (data: ProfileFormValues) => {
const res = await apiRequest("PATCH", "/api/user/profile", data);
if (!res.ok) {
const errorData = await res.json();
throw new Error(errorData.message || "Failed to update profile");
}
return await res.json();
},
onSuccess: (updatedUser) => {
queryClient.setQueryData(["/api/user"], updatedUser);
toast({
title: "Profile updated",
description: "Your profile has been updated successfully.",
});
onClose();
},
onError: (error: Error) => {
toast({
title: "Update failed",
description: error.message,
variant: "destructive",
});
},
});
const onSubmit = async (data: ProfileFormValues) => {
// Create a copy of the data object without interestsInput
const { interestsInput, ...submitData } = data;
// Submit data without the temporary interestsInput field
await updateProfileMutation.mutateAsync(submitData);
};
// Convert string to array for interests field if needed
const handleInterestsChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
// Just store the input value as is, don't process it yet
form.setValue("interestsInput", value);
// Process for the actual interests field that gets submitted
const interestsArray = value
.split(",")
.map((item) => item.trim())
.filter((item) => item !== "");
form.setValue("interests", interestsArray);
};
return (
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent className="sm:max-w-[525px] max-h-[90vh] overflow-y-auto">
<DialogHeader>
<DialogTitle>User Settings</DialogTitle>
<DialogDescription>
Update your profile information and AI assistant preferences
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<div className="space-y-4">
{/* Profile Information Section */}
<div className="border-b pb-2">
<h3 className="text-lg font-medium">Profile Information</h3>
</div>
<FormField
control={form.control}
name="fullName"
render={({ field }) => (
<FormItem>
<FormLabel>Full Name</FormLabel>
<FormControl>
<Input placeholder="Your full name" {...field} value={field.value || ""} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="location"
render={({ field }) => (
<FormItem>
<FormLabel>Location</FormLabel>
<FormControl>
<Input placeholder="Your location" {...field} value={field.value || ""} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="profession"
render={({ field }) => (
<FormItem>
<FormLabel>Profession</FormLabel>
<FormControl>
<Input placeholder="Your profession" {...field} value={field.value || ""} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="interestsInput"
render={({ field }) => (
<FormItem>
<FormLabel>Interests</FormLabel>
<FormControl>
<Input
placeholder="Interests (comma-separated)"
{...field}
onChange={handleInterestsChange}
/>
</FormControl>
<FormDescription>
Enter your interests separated by commas
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="pets"
render={({ field }) => (
<FormItem>
<FormLabel>Pets</FormLabel>
<FormControl>
<Input placeholder="Your pets" {...field} value={field.value || ""} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{/* AI Assistant Preferences Section */}
<div className="border-b pb-2 pt-4">
<h3 className="text-lg font-medium">AI Assistant Preferences</h3>
</div>
<FormField
control={form.control}
name="additionalInfo"
render={({ field }) => (
<FormItem>
<FormLabel>Additional Information</FormLabel>
<FormControl>
<Textarea
placeholder="Add any additional information about yourself that you'd like the AI to know"
className="min-h-[100px]"
{...field}
value={field.value || ""}
/>
</FormControl>
<FormDescription>
This information will be included in your AI context
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<div className="space-y-4">
<div className="flex justify-between items-center">
<FormLabel className="text-base">System Context</FormLabel>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => {
// Generate structured context from profile fields
const fullName = form.getValues("fullName");
const location = form.getValues("location");
const interests = form.getValues("interests");
const profession = form.getValues("profession");
const pets = form.getValues("pets");
const additionalInfo = form.getValues("additionalInfo");
// Format profile information in a structured way
let profileInfo = "";
if (fullName) profileInfo += `name: ${fullName}\n`;
if (location) profileInfo += `location: ${location}\n`;
if (interests && interests.length > 0) profileInfo += `interests: ${interests.join(", ")}\n`;
if (profession) profileInfo += `profession: ${profession}\n`;
if (pets) profileInfo += `pets: ${pets}\n`;
if (additionalInfo) profileInfo += `additional_info: ${additionalInfo}\n`;
// Get existing context
const currentContext = form.getValues("systemContext") || "";
// Set the new structured context
form.setValue("systemContext", profileInfo + "\n" + currentContext);
}}
>
Include Profile Info
</Button>
</div>
<FormField
control={form.control}
name="systemContext"
render={({ field }) => (
<FormItem>
<FormControl>
<Textarea
placeholder="Add custom context for the AI assistant to understand your requirements better"
className="min-h-[150px]"
{...field}
value={field.value || ""}
/>
</FormControl>
<FormDescription>
This context will be provided to the AI assistant for all your conversations.
Use key-value pairs like "name: Your Name" for best results.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</div>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={onClose}
disabled={updateProfileMutation.isPending}
>
Cancel
</Button>
<Button
type="submit"
disabled={updateProfileMutation.isPending}
>
{updateProfileMutation.isPending ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Saving...
</>
) : (
"Save Changes"
)}
</Button>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
);
} |