import { useState, useEffect, useRef, type FormEvent } from "react"; import defaultAvatar from "@/images/default-avatar.png"; import type { TextField } from "mdui/components/text-field"; import type { DialogProps } from "@/core/types"; import { MaterialDialog } from "@/core/components/Dialog"; import useProfile from "@/pages/chat/hooks/useProfile"; import { ImageCropper } from "./ImageCropper"; import { MaterialTextField } from "@/core/components/TextField"; export function ProfileDialog({ isOpen, onOpenChange }: DialogProps) { const { profileData, isLoading, isUpdating, updateProfileData, uploadProfilePictureData } = useProfile(); const [username, setUsername] = useState(profileData?.nickname ?? ""); const [description, setDescription] = useState(profileData?.description ?? ""); const [selectedImage, setSelectedImage] = useState(null); const [showCropper, setShowCropper] = useState(false); const fileInputRef = useRef(null); // Update form fields when profile data changes useEffect(() => { if (profileData) { setUsername(profileData.nickname || ""); setDescription(profileData.description || ""); } }, [profileData]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const success = await updateProfileData({ nickname: username.trim() || undefined, description: description.trim() || undefined }); if (success) { onOpenChange(false); } }; const handleImageSelect = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file && file.type.startsWith('image/')) { setSelectedImage(file); setShowCropper(true); } }; const handleCropComplete = async (croppedImageData: string) => { try { // Convert data URL to blob const response = await fetch(croppedImageData); const blob = await response.blob(); const success = await uploadProfilePictureData(blob); if (success) { setShowCropper(false); setSelectedImage(null); if (fileInputRef.current) { fileInputRef.current.value = ''; } } } catch (error) { console.error('Error processing cropped image:', error); } }; const handleCropCancel = () => { setShowCropper(false); setSelectedImage(null); if (fileInputRef.current) { fileInputRef.current.value = ''; } }; const handleUploadClick = () => { fileInputRef.current?.click(); }; const profilePictureUrl = profileData?.profile_picture || defaultAvatar; return ( <>
Ваше фото { const target = e.target as HTMLImageElement; target.src = defaultAvatar; }} />
) => setUsername((e.target as TextField).value)} autocomplete="username" disabled={isLoading || isUpdating} />
) => setDescription((e.target as TextField).value)} placeholder="Расскажите о себе..." autocomplete="none" disabled={isLoading || isUpdating} />
{isUpdating ? "Сохранение..." : "Сохранить изменения"} onOpenChange(false)} disabled={isUpdating} > Закрыть
{/* Image Cropper Dialog */}

Обрезать фото профиля

); }