Optimize profile avatar uploads and sync urls

This commit is contained in:
Fedor Indutny 2022-03-15 17:14:20 -07:00 committed by GitHub
parent 703bb8a3a3
commit 36ce4f27a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 147 additions and 77 deletions

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
import type { CSSProperties } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { noop } from 'lodash';
import * as log from '../logging/log';
@ -46,10 +46,6 @@ export const AvatarPreview = ({
onClick,
style = {},
}: PropsType): JSX.Element => {
const startingAvatarPathRef = useRef<undefined | string>(
avatarValue ? undefined : avatarPath
);
const [avatarPreview, setAvatarPreview] = useState<Uint8Array | undefined>();
// Loads the initial avatarPath if one is provided, but only if we're in editable mode.
@ -60,8 +56,7 @@ export const AvatarPreview = ({
return;
}
const startingAvatarPath = startingAvatarPathRef.current;
if (!startingAvatarPath) {
if (!avatarPath) {
return noop;
}
@ -69,14 +64,12 @@ export const AvatarPreview = ({
(async () => {
try {
const buffer = await imagePathToBytes(startingAvatarPath);
const buffer = await imagePathToBytes(avatarPath);
if (shouldCancel) {
return;
}
setAvatarPreview(buffer);
if (onAvatarLoaded) {
onAvatarLoaded(buffer);
}
onAvatarLoaded?.(buffer);
} catch (err) {
if (shouldCancel) {
return;
@ -92,7 +85,7 @@ export const AvatarPreview = ({
return () => {
shouldCancel = true;
};
}, [onAvatarLoaded, isEditable]);
}, [avatarPath, onAvatarLoaded, isEditable]);
// Ensures that when avatarValue changes we generate new URLs
useEffect(() => {