// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { ChangeEventHandler, useEffect, useRef, useState } from 'react'; import { noop } from 'lodash'; import { LocalizerType } from '../types/Util'; import { processImageFile } from '../util/processImageFile'; export type PropsType = { className: string; i18n: LocalizerType; onChange: (avatar: Uint8Array) => unknown; }; export const AvatarUploadButton = ({ className, i18n, onChange, }: PropsType): JSX.Element => { const fileInputRef = useRef(null); const [processingFile, setProcessingFile] = useState(); useEffect(() => { if (!processingFile) { return noop; } let shouldCancel = false; (async () => { let newAvatar: Uint8Array; try { newAvatar = await processImageFile(processingFile); } catch (err) { // Processing errors should be rare; if they do, we silently fail. In an ideal // world, we may want to show a toast instead. return; } if (shouldCancel) { return; } setProcessingFile(undefined); onChange(newAvatar); })(); return () => { shouldCancel = true; }; }, [onChange, processingFile]); const onInputChange: ChangeEventHandler = event => { const file = event.target.files && event.target.files[0]; if (file) { setProcessingFile(file); } }; return ( <> ); };