2026-10-23 13:36:04 +03:00
|
|
|
import React, { useEffect, useRef, useState } from 'react'
|
2026-10-21 13:17:44 +03:00
|
|
|
import { api } from '../api.js'
|
|
|
|
|
|
2026-10-23 13:36:04 +03:00
|
|
|
// Fallback used until GET /api/config answers. Kept in sync with the backend
|
|
|
|
|
// defaults (user.MaxImageSize and the accepted image formats).
|
|
|
|
|
const DEFAULT_AVATAR_LIMITS = {
|
|
|
|
|
maxSizeBytes: 4 << 20,
|
|
|
|
|
maxWidth: 2048,
|
|
|
|
|
maxHeight: 2048,
|
|
|
|
|
allowedMimeTypes: ['image/jpeg', 'image/jpg', 'image/png'],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Builds the file picker `accept` value. MIME types cover the common case,
|
|
|
|
|
// the explicit extensions make sure both ".jpg" and ".jpeg" are offered even
|
|
|
|
|
// on pickers that do not recognise "image/jpg".
|
|
|
|
|
function buildAccept(mimeTypes) {
|
|
|
|
|
const types = mimeTypes?.length ? mimeTypes : DEFAULT_AVATAR_LIMITS.allowedMimeTypes
|
|
|
|
|
const extensions = ['.jpg', '.jpeg', '.png']
|
|
|
|
|
return [...types, ...extensions].join(',')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatBytes(bytes) {
|
|
|
|
|
if (!bytes || bytes <= 0) return '0 B'
|
|
|
|
|
const units = ['B', 'KB', 'MB', 'GB']
|
|
|
|
|
let value = bytes
|
|
|
|
|
let unit = 0
|
|
|
|
|
while (value >= 1024 && unit < units.length - 1) {
|
|
|
|
|
value /= 1024
|
|
|
|
|
unit += 1
|
|
|
|
|
}
|
|
|
|
|
const rounded = Number.isInteger(value) ? value : value.toFixed(1)
|
|
|
|
|
return `${rounded} ${units[unit]}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Maps accepted MIME types to a short human readable label (e.g. "JPEG, PNG").
|
|
|
|
|
function formatTypes(mimeTypes) {
|
|
|
|
|
const known = {
|
|
|
|
|
'image/jpeg': 'JPEG',
|
|
|
|
|
'image/jpg': 'JPEG',
|
|
|
|
|
'image/png': 'PNG',
|
|
|
|
|
}
|
|
|
|
|
const labels = []
|
|
|
|
|
for (const type of mimeTypes || []) {
|
|
|
|
|
const label = known[type] || type
|
|
|
|
|
if (!labels.includes(label)) labels.push(label)
|
|
|
|
|
}
|
|
|
|
|
return labels.join(', ')
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 13:17:44 +03:00
|
|
|
export default function ProfilePage({ user, onUserChange }) {
|
|
|
|
|
const [fullName, setFullName] = useState(user.fullName || '')
|
|
|
|
|
const [email, setEmail] = useState(user.email || '')
|
|
|
|
|
const [profileMsg, setProfileMsg] = useState(null)
|
|
|
|
|
|
|
|
|
|
const [passwords, setPasswords] = useState({ oldPassword: '', newPassword: '' })
|
|
|
|
|
const [passwordMsg, setPasswordMsg] = useState(null)
|
|
|
|
|
|
|
|
|
|
const [avatarMsg, setAvatarMsg] = useState(null)
|
|
|
|
|
const [avatarVersion, setAvatarVersion] = useState(Date.now())
|
|
|
|
|
const [avatarError, setAvatarError] = useState(false)
|
2026-10-23 13:36:04 +03:00
|
|
|
const [avatarLimits, setAvatarLimits] = useState(DEFAULT_AVATAR_LIMITS)
|
2026-10-21 13:17:44 +03:00
|
|
|
const fileRef = useRef(null)
|
|
|
|
|
|
2026-10-23 13:36:04 +03:00
|
|
|
// Query the backend for the avatar limits instead of hardcoding them, so the
|
|
|
|
|
// file picker, the size hint and the client-side check always match what the
|
|
|
|
|
// server enforces.
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let active = true
|
|
|
|
|
api
|
|
|
|
|
.getConfig()
|
|
|
|
|
.then((config) => {
|
|
|
|
|
if (active && config?.avatar) {
|
|
|
|
|
setAvatarLimits({
|
|
|
|
|
maxSizeBytes: config.avatar.maxSizeBytes ?? DEFAULT_AVATAR_LIMITS.maxSizeBytes,
|
|
|
|
|
maxWidth: config.avatar.maxWidth ?? DEFAULT_AVATAR_LIMITS.maxWidth,
|
|
|
|
|
maxHeight: config.avatar.maxHeight ?? DEFAULT_AVATAR_LIMITS.maxHeight,
|
|
|
|
|
allowedMimeTypes:
|
|
|
|
|
config.avatar.allowedMimeTypes?.length
|
|
|
|
|
? config.avatar.allowedMimeTypes
|
|
|
|
|
: DEFAULT_AVATAR_LIMITS.allowedMimeTypes,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
// Keep the hardcoded defaults when the config endpoint is unavailable.
|
|
|
|
|
})
|
|
|
|
|
return () => {
|
|
|
|
|
active = false
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
2026-10-21 13:17:44 +03:00
|
|
|
const initials = (user.fullName || user.login || '?').trim().charAt(0).toUpperCase()
|
2026-10-22 23:08:23 +03:00
|
|
|
const hasAvatar = Boolean(user.hasAvatar) && !avatarError
|
2026-10-21 13:17:44 +03:00
|
|
|
|
|
|
|
|
const saveProfile = async (event) => {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
setProfileMsg(null)
|
|
|
|
|
try {
|
|
|
|
|
const updated = await api.updateProfile({ fullName, email })
|
|
|
|
|
onUserChange(updated)
|
|
|
|
|
setProfileMsg({ type: 'ok', text: 'Profile updated' })
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setProfileMsg({ type: 'error', text: err.message })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const savePassword = async (event) => {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
setPasswordMsg(null)
|
|
|
|
|
try {
|
|
|
|
|
await api.updatePassword(passwords)
|
|
|
|
|
setPasswords({ oldPassword: '', newPassword: '' })
|
|
|
|
|
setPasswordMsg({ type: 'ok', text: 'Password changed' })
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setPasswordMsg({ type: 'error', text: err.message })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uploadAvatar = async (event) => {
|
|
|
|
|
const file = event.target.files?.[0]
|
|
|
|
|
if (!file) return
|
|
|
|
|
setAvatarMsg(null)
|
2026-10-23 13:36:04 +03:00
|
|
|
|
|
|
|
|
if (file.size > avatarLimits.maxSizeBytes) {
|
|
|
|
|
setAvatarMsg({
|
|
|
|
|
type: 'error',
|
|
|
|
|
text: `File is too large (${formatBytes(file.size)}). Maximum size is ${formatBytes(
|
|
|
|
|
avatarLimits.maxSizeBytes,
|
|
|
|
|
)}.`,
|
|
|
|
|
})
|
|
|
|
|
if (fileRef.current) fileRef.current.value = ''
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 13:17:44 +03:00
|
|
|
try {
|
|
|
|
|
await api.uploadAvatar(file)
|
2026-10-22 23:08:23 +03:00
|
|
|
const profile = await api.getProfile()
|
|
|
|
|
onUserChange(profile)
|
2026-10-21 13:17:44 +03:00
|
|
|
setAvatarVersion(Date.now())
|
|
|
|
|
setAvatarError(false)
|
|
|
|
|
setAvatarMsg({ type: 'ok', text: 'Avatar updated' })
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setAvatarMsg({ type: 'error', text: err.message })
|
|
|
|
|
} finally {
|
|
|
|
|
if (fileRef.current) fileRef.current.value = ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 23:08:23 +03:00
|
|
|
const removeAvatar = async () => {
|
|
|
|
|
if (!confirm('Remove your avatar?')) return
|
|
|
|
|
setAvatarMsg(null)
|
|
|
|
|
try {
|
|
|
|
|
await api.deleteAvatar()
|
|
|
|
|
const profile = await api.getProfile()
|
|
|
|
|
onUserChange(profile)
|
|
|
|
|
setAvatarError(false)
|
|
|
|
|
setAvatarMsg({ type: 'ok', text: 'Avatar removed' })
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setAvatarMsg({ type: 'error', text: err.message })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 13:17:44 +03:00
|
|
|
return (
|
|
|
|
|
<div className="grid">
|
|
|
|
|
<section className="card">
|
|
|
|
|
<h2>Avatar</h2>
|
|
|
|
|
<div className="avatar-row">
|
2026-10-22 23:08:23 +03:00
|
|
|
{hasAvatar ? (
|
2026-10-21 13:17:44 +03:00
|
|
|
<img
|
|
|
|
|
className="avatar"
|
|
|
|
|
src={`${api.avatarUrl()}?v=${avatarVersion}`}
|
|
|
|
|
alt="avatar"
|
|
|
|
|
onError={() => setAvatarError(true)}
|
|
|
|
|
/>
|
2026-10-22 23:08:23 +03:00
|
|
|
) : (
|
|
|
|
|
<div className="avatar avatar-fallback" aria-label="No avatar">
|
|
|
|
|
{initials}
|
|
|
|
|
</div>
|
2026-10-21 13:17:44 +03:00
|
|
|
)}
|
|
|
|
|
<div>
|
|
|
|
|
<input
|
|
|
|
|
ref={fileRef}
|
|
|
|
|
type="file"
|
2026-10-23 13:36:04 +03:00
|
|
|
accept={buildAccept(avatarLimits.allowedMimeTypes)}
|
2026-10-21 13:17:44 +03:00
|
|
|
onChange={uploadAvatar}
|
|
|
|
|
/>
|
2026-10-23 13:36:04 +03:00
|
|
|
<p className="hint">
|
2026-10-23 13:45:14 +03:00
|
|
|
{formatTypes(avatarLimits.allowedMimeTypes)}.
|
|
|
|
|
<span className="hint-line">
|
|
|
|
|
Max {formatBytes(avatarLimits.maxSizeBytes)}
|
|
|
|
|
{avatarLimits.maxWidth && avatarLimits.maxHeight
|
|
|
|
|
? `, up to ${avatarLimits.maxWidth}\u00d7${avatarLimits.maxHeight} px.`
|
|
|
|
|
: '.'}
|
|
|
|
|
</span>
|
2026-10-23 13:36:04 +03:00
|
|
|
</p>
|
2026-10-22 23:08:23 +03:00
|
|
|
{user.hasAvatar && (
|
|
|
|
|
<button className="btn-danger-sm" type="button" onClick={removeAvatar}>
|
|
|
|
|
Remove avatar
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
2026-10-21 13:17:44 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{avatarMsg && <p className={avatarMsg.type}>{avatarMsg.text}</p>}
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
<section className="card">
|
|
|
|
|
<h2>Profile</h2>
|
|
|
|
|
<form onSubmit={saveProfile}>
|
|
|
|
|
<label>
|
|
|
|
|
Login
|
|
|
|
|
<input value={user.login} disabled />
|
|
|
|
|
</label>
|
|
|
|
|
<label>
|
|
|
|
|
Full name
|
|
|
|
|
<input value={fullName} onChange={(e) => setFullName(e.target.value)} />
|
|
|
|
|
</label>
|
|
|
|
|
<label>
|
|
|
|
|
Email
|
|
|
|
|
<input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
|
|
|
|
|
</label>
|
|
|
|
|
<button className="btn-primary" type="submit">
|
|
|
|
|
Save profile
|
|
|
|
|
</button>
|
|
|
|
|
{profileMsg && <p className={profileMsg.type}>{profileMsg.text}</p>}
|
|
|
|
|
</form>
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
<section className="card">
|
|
|
|
|
<h2>Password</h2>
|
|
|
|
|
<form onSubmit={savePassword}>
|
|
|
|
|
<label>
|
|
|
|
|
Current password
|
|
|
|
|
<input
|
|
|
|
|
type="password"
|
|
|
|
|
value={passwords.oldPassword}
|
|
|
|
|
onChange={(e) => setPasswords((p) => ({ ...p, oldPassword: e.target.value }))}
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
<label>
|
|
|
|
|
New password
|
|
|
|
|
<input
|
|
|
|
|
type="password"
|
|
|
|
|
value={passwords.newPassword}
|
|
|
|
|
onChange={(e) => setPasswords((p) => ({ ...p, newPassword: e.target.value }))}
|
|
|
|
|
minLength={8}
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
<button className="btn-primary" type="submit">
|
|
|
|
|
Change password
|
|
|
|
|
</button>
|
|
|
|
|
{passwordMsg && <p className={passwordMsg.type}>{passwordMsg.text}</p>}
|
|
|
|
|
</form>
|
|
|
|
|
</section>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|