/* Copyright 2022-2024 New Vector Ltd. SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ import { type AllHTMLAttributes, useEffect, useCallback, useState, type ChangeEvent, useRef, type FC, } from "react"; import classNames from "classnames"; import { useTranslation } from "react-i18next"; import { Button, Menu, MenuItem } from "@vector-im/compound-web"; import { DeleteIcon, EditIcon, ShareIcon, } from "@vector-im/compound-design-tokens/assets/web/icons"; import { Avatar, Size } from "../Avatar"; import styles from "./AvatarInputField.module.css"; interface Props extends AllHTMLAttributes { id: string; label: string; avatarUrl: string | undefined; userId: string; displayName: string; onRemoveAvatar: () => void; } export const AvatarInputField: FC = ({ id, label, className, avatarUrl, userId, displayName, onRemoveAvatar, ...rest }) => { const { t } = useTranslation(); const [removed, setRemoved] = useState(false); const [objUrl, setObjUrl] = useState(undefined); const [menuOpen, setMenuOpen] = useState(false); const fileInputRef = useRef(null); useEffect(() => { const currentInput = fileInputRef.current!; const onChange = (e: Event): void => { const inputEvent = e as unknown as ChangeEvent; if (inputEvent.target.files && inputEvent.target.files.length > 0) { setObjUrl(URL.createObjectURL(inputEvent.target.files[0])); setRemoved(false); } else { setObjUrl(undefined); } }; currentInput.addEventListener("change", onChange); return (): void => { currentInput?.removeEventListener("change", onChange); }; }); const onSelectUpload = useCallback(() => { fileInputRef.current!.click(); }, [fileInputRef]); const onSelectRemove = useCallback(() => { setRemoved(true); onRemoveAvatar(); }, [onRemoveAvatar]); return (
{(avatarUrl || objUrl) && !removed ? ( } > ) : (
); }; AvatarInputField.displayName = "AvatarInputField";