2021-08-06 00:17:05 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { CSSProperties } from 'react';
|
2022-03-16 00:14:20 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-08-06 00:17:05 +00:00
|
|
|
import { noop } from 'lodash';
|
|
|
|
|
|
|
|
import * as log from '../logging/log';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2021-08-06 00:17:05 +00:00
|
|
|
import { Spinner } from './Spinner';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { AvatarColorType } from '../types/Colors';
|
|
|
|
import { AvatarColors } from '../types/Colors';
|
2021-08-06 00:17:05 +00:00
|
|
|
import { getInitials } from '../util/getInitials';
|
2021-09-24 00:49:05 +00:00
|
|
|
import { imagePathToBytes } from '../util/imagePathToBytes';
|
2021-08-06 00:17:05 +00:00
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
avatarColor?: AvatarColorType;
|
|
|
|
avatarPath?: string;
|
2021-09-24 00:49:05 +00:00
|
|
|
avatarValue?: Uint8Array;
|
2021-08-06 00:17:05 +00:00
|
|
|
conversationTitle?: string;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
isEditable?: boolean;
|
|
|
|
isGroup?: boolean;
|
2021-09-24 00:49:05 +00:00
|
|
|
onAvatarLoaded?: (avatarBuffer: Uint8Array) => unknown;
|
2021-08-06 00:17:05 +00:00
|
|
|
onClear?: () => unknown;
|
|
|
|
onClick?: () => unknown;
|
|
|
|
style?: CSSProperties;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ImageStatus {
|
|
|
|
Nothing = 'nothing',
|
|
|
|
Loading = 'loading',
|
|
|
|
HasImage = 'has-image',
|
|
|
|
}
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export function AvatarPreview({
|
2021-08-06 00:17:05 +00:00
|
|
|
avatarColor = AvatarColors[0],
|
|
|
|
avatarPath,
|
|
|
|
avatarValue,
|
|
|
|
conversationTitle,
|
|
|
|
i18n,
|
|
|
|
isEditable,
|
|
|
|
isGroup,
|
|
|
|
onAvatarLoaded,
|
|
|
|
onClear,
|
|
|
|
onClick,
|
|
|
|
style = {},
|
2022-11-18 00:45:19 +00:00
|
|
|
}: PropsType): JSX.Element {
|
2021-09-24 00:49:05 +00:00
|
|
|
const [avatarPreview, setAvatarPreview] = useState<Uint8Array | undefined>();
|
2021-08-06 00:17:05 +00:00
|
|
|
|
2021-09-27 19:16:42 +00:00
|
|
|
// Loads the initial avatarPath if one is provided, but only if we're in editable mode.
|
|
|
|
// If we're not editable, we assume that we either have an avatarPath or we show a
|
|
|
|
// default avatar.
|
2021-08-06 00:17:05 +00:00
|
|
|
useEffect(() => {
|
2021-09-27 19:16:42 +00:00
|
|
|
if (!isEditable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:14:20 +00:00
|
|
|
if (!avatarPath) {
|
2021-08-06 00:17:05 +00:00
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shouldCancel = false;
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void (async () => {
|
2021-08-06 00:17:05 +00:00
|
|
|
try {
|
2022-03-16 00:14:20 +00:00
|
|
|
const buffer = await imagePathToBytes(avatarPath);
|
2021-08-06 00:17:05 +00:00
|
|
|
if (shouldCancel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setAvatarPreview(buffer);
|
2022-03-16 00:14:20 +00:00
|
|
|
onAvatarLoaded?.(buffer);
|
2021-08-06 00:17:05 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (shouldCancel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.warn(
|
|
|
|
`Failed to convert image URL to array buffer. Error message: ${
|
|
|
|
err && err.message
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
shouldCancel = true;
|
|
|
|
};
|
2022-03-16 00:14:20 +00:00
|
|
|
}, [avatarPath, onAvatarLoaded, isEditable]);
|
2021-08-06 00:17:05 +00:00
|
|
|
|
|
|
|
// Ensures that when avatarValue changes we generate new URLs
|
|
|
|
useEffect(() => {
|
|
|
|
if (avatarValue) {
|
|
|
|
setAvatarPreview(avatarValue);
|
|
|
|
} else {
|
|
|
|
setAvatarPreview(undefined);
|
|
|
|
}
|
|
|
|
}, [avatarValue]);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
// Creates the object URL to render the Uint8Array image
|
2021-08-06 00:17:05 +00:00
|
|
|
const [objectUrl, setObjectUrl] = useState<undefined | string>();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!avatarPreview) {
|
|
|
|
setObjectUrl(undefined);
|
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = URL.createObjectURL(new Blob([avatarPreview]));
|
|
|
|
setObjectUrl(url);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
};
|
|
|
|
}, [avatarPreview]);
|
|
|
|
|
|
|
|
let imageStatus: ImageStatus;
|
2021-09-29 20:18:27 +00:00
|
|
|
let encodedPath: string | undefined;
|
2021-08-06 00:17:05 +00:00
|
|
|
if (avatarValue && !objectUrl) {
|
|
|
|
imageStatus = ImageStatus.Loading;
|
2021-09-29 20:18:27 +00:00
|
|
|
} else if (objectUrl) {
|
|
|
|
encodedPath = objectUrl;
|
|
|
|
imageStatus = ImageStatus.HasImage;
|
|
|
|
} else if (avatarPath) {
|
|
|
|
encodedPath = encodeURI(avatarPath);
|
2021-08-06 00:17:05 +00:00
|
|
|
imageStatus = ImageStatus.HasImage;
|
|
|
|
} else {
|
|
|
|
imageStatus = ImageStatus.Nothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isLoading = imageStatus === ImageStatus.Loading;
|
|
|
|
|
2021-11-01 19:13:35 +00:00
|
|
|
const clickProps = onClick
|
|
|
|
? {
|
|
|
|
role: 'button',
|
|
|
|
onClick,
|
|
|
|
tabIndex: 0,
|
|
|
|
onKeyDown: (event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
|
|
if (event.key === 'Enter' || event.key === ' ') {
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {};
|
2021-08-06 00:17:05 +00:00
|
|
|
const componentStyle = {
|
|
|
|
...style,
|
|
|
|
};
|
|
|
|
if (onClick) {
|
|
|
|
componentStyle.cursor = 'pointer';
|
|
|
|
}
|
|
|
|
|
2021-09-27 19:16:42 +00:00
|
|
|
if (imageStatus === ImageStatus.Nothing) {
|
2021-08-06 00:17:05 +00:00
|
|
|
return (
|
|
|
|
<div className="AvatarPreview">
|
|
|
|
<div
|
|
|
|
className={`AvatarPreview__avatar BetterAvatarBubble--${avatarColor}`}
|
|
|
|
{...clickProps}
|
|
|
|
style={componentStyle}
|
|
|
|
>
|
|
|
|
{isGroup ? (
|
|
|
|
<div
|
|
|
|
className={`BetterAvatarBubble--${avatarColor}--icon AvatarPreview__group`}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
getInitials(conversationTitle)
|
|
|
|
)}
|
|
|
|
{isEditable && <div className="AvatarPreview__upload" />}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="AvatarPreview">
|
|
|
|
<div
|
|
|
|
className={`AvatarPreview__avatar AvatarPreview__avatar--${imageStatus}`}
|
|
|
|
{...clickProps}
|
|
|
|
style={
|
2021-09-29 20:18:27 +00:00
|
|
|
imageStatus === ImageStatus.HasImage && encodedPath
|
2021-08-06 00:17:05 +00:00
|
|
|
? {
|
|
|
|
...componentStyle,
|
2021-09-29 20:18:27 +00:00
|
|
|
backgroundImage: `url('${encodedPath}')`,
|
2021-08-06 00:17:05 +00:00
|
|
|
}
|
|
|
|
: componentStyle
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{isLoading && (
|
|
|
|
<Spinner size="70px" svgSize="normal" direction="on-avatar" />
|
|
|
|
)}
|
|
|
|
{imageStatus === ImageStatus.HasImage && onClear && (
|
|
|
|
<button
|
2023-03-30 00:03:25 +00:00
|
|
|
aria-label={i18n('icu:delete')}
|
2021-08-06 00:17:05 +00:00
|
|
|
className="AvatarPreview__clear"
|
|
|
|
onClick={onClear}
|
|
|
|
tabIndex={-1}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
)}
|
2021-08-06 21:35:25 +00:00
|
|
|
{isEditable && <div className="AvatarPreview__upload" />}
|
2021-08-06 00:17:05 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|