Spaces:
Runtime error
Runtime error
Download frontend/src/components/chat/ProfileModal.jsx from cuiconnect/backend: direct link, hf CLI and curl.
- Browser
- Download file 4.33 kB
-
https://huggingface.co/spaces/cuiconnect/backend/resolve/main/frontend/src/components/chat/ProfileModal.jsx
- Command line
-
hf download hf://spaces/cuiconnect/backend/frontend/src/components/chat/ProfileModal.jsx
-
curl -L -o ProfileModal.jsx https://huggingface.co/spaces/cuiconnect/backend/resolve/main/frontend/src/components/chat/ProfileModal.jsx
4.33 kB
| import { useEffect, useState } from 'react' | |
| import { updateProfile } from '../../api/auth' | |
| import { getStoredUser, patchStoredUser } from '../../auth/storage' | |
| export default function ProfileModal({ open, onClose }) { | |
| const user = getStoredUser() | |
| const [fullName, setFullName] = useState('') | |
| const [email, setEmail] = useState('') | |
| const [error, setError] = useState('') | |
| const [saving, setSaving] = useState(false) | |
| useEffect(() => { | |
| if (!open || !user) return | |
| setFullName(user.full_name || '') | |
| setEmail(user.email || '') | |
| setError('') | |
| }, [open, user]) | |
| useEffect(() => { | |
| if (!open) return | |
| function onKey(e) { | |
| if (e.key === 'Escape') onClose() | |
| } | |
| window.addEventListener('keydown', onKey) | |
| return () => window.removeEventListener('keydown', onKey) | |
| }, [open, onClose]) | |
| async function handleSubmit(e) { | |
| e.preventDefault() | |
| setError('') | |
| setSaving(true) | |
| try { | |
| const body = {} | |
| if ((fullName || '').trim() !== (user?.full_name || '')) | |
| body.full_name = fullName.trim() || null | |
| if ((email || '').trim().toLowerCase() !== (user?.email || '').toLowerCase()) | |
| body.email = email.trim() | |
| if (Object.keys(body).length === 0) { | |
| onClose() | |
| return | |
| } | |
| const updated = await updateProfile(body) | |
| patchStoredUser({ | |
| email: updated.email, | |
| full_name: updated.full_name, | |
| user_id: updated.user_id, | |
| }) | |
| onClose() | |
| } catch (err) { | |
| setError(err.message || 'Could not update profile') | |
| } finally { | |
| setSaving(false) | |
| } | |
| } | |
| if (!open) return null | |
| return ( | |
| <div | |
| className="fixed inset-0 z-50 flex items-center justify-center bg-slate-900/40 p-4" | |
| role="dialog" | |
| aria-modal="true" | |
| aria-labelledby="profile-title" | |
| onMouseDown={(e) => { | |
| if (e.target === e.currentTarget) onClose() | |
| }} | |
| > | |
| <div className="w-full max-w-md rounded-2xl border border-slate-200 bg-white p-6 shadow-xl"> | |
| <h2 id="profile-title" className="text-lg font-semibold text-slate-900"> | |
| Edit profile | |
| </h2> | |
| <p className="mt-1 text-sm text-slate-600">Update your name or email.</p> | |
| <form onSubmit={handleSubmit} className="mt-6 flex flex-col gap-4"> | |
| {error ? ( | |
| <div | |
| role="alert" | |
| className="rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-800" | |
| > | |
| {error} | |
| </div> | |
| ) : null} | |
| <div className="flex flex-col gap-1.5"> | |
| <label htmlFor="profile-name" className="text-sm font-medium text-slate-700"> | |
| Full name | |
| </label> | |
| <input | |
| id="profile-name" | |
| type="text" | |
| value={fullName} | |
| onChange={(e) => setFullName(e.target.value)} | |
| className="rounded-lg border border-slate-300 px-3 py-2 text-slate-900 outline-none focus:border-comsats-blue focus:ring-2 focus:ring-comsats-blue/25" | |
| /> | |
| </div> | |
| <div className="flex flex-col gap-1.5"> | |
| <label htmlFor="profile-email" className="text-sm font-medium text-slate-700"> | |
| </label> | |
| <input | |
| id="profile-email" | |
| type="email" | |
| required | |
| value={email} | |
| onChange={(e) => setEmail(e.target.value)} | |
| className="rounded-lg border border-slate-300 px-3 py-2 text-slate-900 outline-none focus:border-comsats-blue focus:ring-2 focus:ring-comsats-blue/25" | |
| /> | |
| </div> | |
| <div className="mt-2 flex justify-end gap-2"> | |
| <button | |
| type="button" | |
| onClick={onClose} | |
| className="rounded-lg border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50" | |
| > | |
| Cancel | |
| </button> | |
| <button | |
| type="submit" | |
| disabled={saving} | |
| className="rounded-lg bg-comsats-purple px-4 py-2 text-sm font-semibold text-white hover:bg-comsats-blue disabled:opacity-60" | |
| > | |
| {saving ? 'Saving…' : 'Save'} | |
| </button> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| ) | |
| } | |