2026-04-21 11:30:07 +02:00
|
|
|
/**
|
|
|
|
|
* This file is part of the SplendidBear Websites' projects.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2026 @ www.splendidbear.org
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
|
*/
|
2026-04-13 15:50:28 +02:00
|
|
|
|
2026-04-22 12:15:06 +02:00
|
|
|
import React, { Fragment, useMemo, useRef } from 'react';
|
2026-04-21 11:30:07 +02:00
|
|
|
import { string } from 'prop-types';
|
|
|
|
|
import { useProfileDataProvider } from '@mine-hooks/useGameDataProvider';
|
2026-04-13 15:50:28 +02:00
|
|
|
|
2026-04-21 11:30:07 +02:00
|
|
|
export const AvatarUpload = ({ uploadUrl, initialThumbUrl, initials }) => {
|
|
|
|
|
const inputRef = useRef(null);
|
|
|
|
|
const [thumbUrl, setThumbUrl] = React.useState(initialThumbUrl || null);
|
|
|
|
|
const { uploadAvatarMutation: { isPending, error, mutate } } = useProfileDataProvider();
|
2026-04-13 15:50:28 +02:00
|
|
|
|
2026-04-22 12:15:06 +02:00
|
|
|
const errorMessage = useMemo(() => error?.message ?? null, [error]);
|
|
|
|
|
|
2026-04-21 11:30:07 +02:00
|
|
|
const handleChange = e => {
|
2026-04-13 15:50:28 +02:00
|
|
|
const file = e.target.files?.[0];
|
|
|
|
|
if (!file) return;
|
|
|
|
|
|
2026-04-21 11:30:07 +02:00
|
|
|
mutate({ uploadUrl, file }, {
|
|
|
|
|
onSuccess: data => {
|
2026-04-13 15:50:28 +02:00
|
|
|
setThumbUrl(data.thumbUrl);
|
|
|
|
|
|
|
|
|
|
const navImg = document.querySelector('.hero-auth-avatar:not(.hero-auth-avatar--initials)');
|
|
|
|
|
const navInitials = document.querySelector('.hero-auth-avatar.hero-auth-avatar--initials');
|
2026-04-21 11:30:07 +02:00
|
|
|
|
2026-04-13 15:50:28 +02:00
|
|
|
if (navImg) {
|
|
|
|
|
navImg.src = data.thumbUrl;
|
|
|
|
|
} else if (navInitials) {
|
|
|
|
|
const img = document.createElement('img');
|
|
|
|
|
img.src = data.thumbUrl;
|
|
|
|
|
img.alt = navInitials.textContent.trim();
|
|
|
|
|
img.className = 'hero-auth-avatar';
|
|
|
|
|
navInitials.replaceWith(img);
|
|
|
|
|
}
|
2026-04-21 11:30:07 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-13 15:50:28 +02:00
|
|
|
return (
|
2026-04-22 12:15:06 +02:00
|
|
|
<Fragment>
|
|
|
|
|
<div
|
|
|
|
|
className={`profile-avatar${isPending ? ' profile-avatar--loading' : ''}`}
|
|
|
|
|
title="Click to change profile picture"
|
|
|
|
|
onClick={() => inputRef.current?.click()}
|
|
|
|
|
>
|
|
|
|
|
{thumbUrl
|
|
|
|
|
? <img src={thumbUrl} alt={initials} className="profile-avatar__img" />
|
|
|
|
|
: <span className="profile-avatar__initials">{initials}</span>
|
|
|
|
|
}
|
|
|
|
|
<div className="profile-avatar__overlay">
|
|
|
|
|
<i className="fa fa-camera" />
|
|
|
|
|
</div>
|
|
|
|
|
<input
|
|
|
|
|
ref={inputRef}
|
|
|
|
|
type="file"
|
|
|
|
|
accept="image/jpeg,image/png,image/gif,image/webp"
|
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
/>
|
2026-04-13 15:50:28 +02:00
|
|
|
</div>
|
2026-04-21 11:30:07 +02:00
|
|
|
{errorMessage && <div className="profile-avatar__error">{errorMessage}</div>}
|
2026-04-22 12:15:06 +02:00
|
|
|
</Fragment>
|
2026-04-13 15:50:28 +02:00
|
|
|
);
|
2026-04-22 12:15:06 +02:00
|
|
|
};
|
2026-04-21 11:30:07 +02:00
|
|
|
|
|
|
|
|
AvatarUpload.propTypes = {
|
|
|
|
|
uploadUrl: string.isRequired,
|
|
|
|
|
initialThumbUrl: string,
|
|
|
|
|
initials: string.isRequired,
|
|
|
|
|
};
|