import { usePublicKey } from "@/app/providers/AccountProvider/usePublicKey"; import { useAvatars } from "@/app/providers/AvatarProvider/useAvatars"; import { useAvatarChange } from "@/app/providers/AvatarProvider/useChangeAvatar"; import { useImageViewer } from "@/app/providers/ImageViewerProvider/useImageViewer"; import { imagePrepareForNetworkTransfer } from "@/app/utils/utils"; import { Avatar, Box, Flex, Overlay } from "@mantine/core"; import { useFileDialog } from "@mantine/hooks"; import { IconCamera } from "@tabler/icons-react"; import { useState } from "react"; interface ActionAvatarProps { title: string; publicKey: string; forceChangeable?: boolean; } export function ActionAvatar(props : ActionAvatarProps) { const [overlay, setOverlay] = useState(false); const publicKey = usePublicKey(); const changeAvatar = useAvatarChange(); const avatars = useAvatars(props.publicKey, true); const {open} = useImageViewer(); const fileDialog = useFileDialog({ multiple: false, accept: 'image/*', onChange: async (files) => { if(!files){ return; } if(files.length == 0){ return; } const file = files[0]; const base64Image = await imagePrepareForNetworkTransfer(file); changeAvatar(base64Image, props.publicKey); } }); const onClickAvatar = () => { if(props.publicKey != publicKey && !props.forceChangeable){ open(avatars.map(a => ({src: a.avatar, timestamp: a.timestamp})), 0); return; } fileDialog.open(); } return ( setOverlay(true)} onMouseLeave={() => setOverlay(false)} style={{ cursor: 'pointer' }} onClick={onClickAvatar} pos={'relative'}> 0 ? avatars[0].avatar : undefined} > {(overlay && (props.publicKey == publicKey || props.forceChangeable)) && } ); }