2022-05-04 17:09:48 +01:00
|
|
|
/*
|
2024-08-28 08:44:39 -04:00
|
|
|
Copyright 2022-2024 New Vector Ltd
|
2022-05-04 17:09:48 +01:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-06-30 16:43:28 +01:00
|
|
|
import {
|
|
|
|
|
AllHTMLAttributes,
|
|
|
|
|
useEffect,
|
|
|
|
|
useCallback,
|
|
|
|
|
useState,
|
|
|
|
|
ChangeEvent,
|
2024-08-28 08:44:39 -04:00
|
|
|
useRef,
|
|
|
|
|
FC,
|
2023-06-30 16:43:28 +01:00
|
|
|
} from "react";
|
2022-02-18 16:02:27 -08:00
|
|
|
import classNames from "classnames";
|
2022-10-10 09:19:10 -04:00
|
|
|
import { useTranslation } from "react-i18next";
|
2024-08-28 08:44:39 -04:00
|
|
|
import { Button, Menu, MenuItem } from "@vector-im/compound-web";
|
|
|
|
|
import {
|
|
|
|
|
DeleteIcon,
|
|
|
|
|
EditIcon,
|
|
|
|
|
ShareIcon,
|
|
|
|
|
} from "@vector-im/compound-design-tokens/assets/web/icons";
|
2022-08-09 11:44:46 +02:00
|
|
|
|
|
|
|
|
import { Avatar, Size } from "../Avatar";
|
2022-02-18 16:02:27 -08:00
|
|
|
import styles from "./AvatarInputField.module.css";
|
|
|
|
|
|
2022-08-09 11:44:46 +02:00
|
|
|
interface Props extends AllHTMLAttributes<HTMLInputElement> {
|
|
|
|
|
id: string;
|
|
|
|
|
label: string;
|
2023-07-14 14:46:45 -04:00
|
|
|
avatarUrl: string | undefined;
|
2023-08-31 15:46:09 +02:00
|
|
|
userId: string;
|
2022-08-09 11:44:46 +02:00
|
|
|
displayName: string;
|
|
|
|
|
onRemoveAvatar: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 08:44:39 -04:00
|
|
|
export const AvatarInputField: FC<Props> = ({
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
className,
|
|
|
|
|
avatarUrl,
|
|
|
|
|
userId,
|
|
|
|
|
displayName,
|
|
|
|
|
onRemoveAvatar,
|
|
|
|
|
...rest
|
|
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const [removed, setRemoved] = useState(false);
|
|
|
|
|
const [objUrl, setObjUrl] = useState<string | undefined>(undefined);
|
|
|
|
|
const [menuOpen, setMenuOpen] = useState(false);
|
|
|
|
|
|
|
|
|
|
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const currentInput = fileInputRef.current!;
|
|
|
|
|
|
|
|
|
|
const onChange = (e: Event): void => {
|
|
|
|
|
const inputEvent = e as unknown as ChangeEvent<HTMLInputElement>;
|
|
|
|
|
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 (
|
|
|
|
|
<div className={classNames(styles.avatarInputField, className)}>
|
|
|
|
|
<Avatar
|
|
|
|
|
id={userId}
|
|
|
|
|
className={styles.avatar}
|
|
|
|
|
name={displayName}
|
|
|
|
|
size={Size.XL}
|
|
|
|
|
src={removed ? undefined : objUrl || avatarUrl}
|
|
|
|
|
/>
|
|
|
|
|
<input
|
|
|
|
|
id={id}
|
|
|
|
|
accept="image/*"
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
type="file"
|
|
|
|
|
className={styles.fileInput}
|
|
|
|
|
role="button"
|
|
|
|
|
aria-label={label}
|
|
|
|
|
{...rest}
|
|
|
|
|
/>
|
|
|
|
|
<div className={styles.edit}>
|
|
|
|
|
{(avatarUrl || objUrl) && !removed ? (
|
|
|
|
|
<Menu
|
|
|
|
|
title={t("action.edit")}
|
|
|
|
|
showTitle={false}
|
|
|
|
|
open={menuOpen}
|
|
|
|
|
onOpenChange={setMenuOpen}
|
|
|
|
|
trigger={
|
|
|
|
|
<Button
|
|
|
|
|
iconOnly
|
|
|
|
|
Icon={EditIcon}
|
|
|
|
|
kind="tertiary"
|
|
|
|
|
size="sm"
|
|
|
|
|
aria-label={t("action.edit")}
|
|
|
|
|
/>
|
|
|
|
|
}
|
2022-03-02 19:18:23 -08:00
|
|
|
>
|
2024-08-28 08:44:39 -04:00
|
|
|
<MenuItem
|
|
|
|
|
Icon={ShareIcon}
|
|
|
|
|
label={t("action.upload_file")}
|
|
|
|
|
onSelect={onSelectUpload}
|
|
|
|
|
/>
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={DeleteIcon}
|
|
|
|
|
label={t("action.remove")}
|
|
|
|
|
kind="critical"
|
|
|
|
|
onSelect={onSelectRemove}
|
|
|
|
|
/>
|
|
|
|
|
</Menu>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
iconOnly
|
|
|
|
|
Icon={EditIcon}
|
|
|
|
|
kind="tertiary"
|
|
|
|
|
size="sm"
|
|
|
|
|
aria-label={t("action.edit")}
|
|
|
|
|
onClick={onSelectUpload}
|
|
|
|
|
/>
|
2022-03-02 19:18:23 -08:00
|
|
|
)}
|
2022-02-18 16:02:27 -08:00
|
|
|
</div>
|
2024-08-28 08:44:39 -04:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2023-12-19 11:00:33 -05:00
|
|
|
|
|
|
|
|
AvatarInputField.displayName = "AvatarInputField";
|