signal-desktop/ts/components/AvatarPreview.tsx

203 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-08-06 00:17:05 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { CSSProperties } from 'react';
import React, { useEffect, useState } from 'react';
2021-08-06 00:17:05 +00:00
import { noop } from 'lodash';
import * as log from '../logging/log';
import type { LocalizerType } from '../types/Util';
2021-08-06 00:17:05 +00:00
import { Spinner } from './Spinner';
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',
}
export const AvatarPreview = ({
avatarColor = AvatarColors[0],
avatarPath,
avatarValue,
conversationTitle,
i18n,
isEditable,
isGroup,
onAvatarLoaded,
onClear,
onClick,
style = {},
}: 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
// 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(() => {
if (!isEditable) {
return;
}
if (!avatarPath) {
2021-08-06 00:17:05 +00:00
return noop;
}
let shouldCancel = false;
(async () => {
try {
const buffer = await imagePathToBytes(avatarPath);
2021-08-06 00:17:05 +00:00
if (shouldCancel) {
return;
}
setAvatarPreview(buffer);
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;
};
}, [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;
let encodedPath: string | undefined;
2021-08-06 00:17:05 +00:00
if (avatarValue && !objectUrl) {
imageStatus = ImageStatus.Loading;
} 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;
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';
}
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={
imageStatus === ImageStatus.HasImage && encodedPath
2021-08-06 00:17:05 +00:00
? {
...componentStyle,
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
aria-label={i18n('delete')}
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>
);
};