added reaction popover

This commit is contained in:
PhilReact 2024-12-03 19:28:18 +02:00
parent 85500bdf8c
commit c55bc3ec0c

View File

@ -1,8 +1,8 @@
import { Message } from "@chatscope/chat-ui-kit-react";
import React, { useEffect } from "react";
import React, { useEffect, useState } from "react";
import { useInView } from "react-intersection-observer";
import { MessageDisplay } from "./MessageDisplay";
import { Avatar, Box, ButtonBase, Typography } from "@mui/material";
import { Avatar, Box, Button, ButtonBase, List, ListItem, ListItemText, Popover, Typography } from "@mui/material";
import { formatTimestamp } from "../../utils/time";
import { getBaseApi } from "../../background";
import { getBaseApiReact } from "../../App";
@ -35,6 +35,8 @@ export const MessageItem = ({
lastSignature,
onEdit
}) => {
const [anchorEl, setAnchorEl] = useState(null);
const [selectedReaction, setSelectedReaction] = useState(null);
const { ref, inView } = useInView({
threshold: 0.7, // Fully visible
triggerOnce: false, // Only trigger once when it becomes visible
@ -243,18 +245,16 @@ export const MessageItem = ({
// const myReaction = reactions
if(numberOfReactions === 0) return null
return (
<ButtonBase sx={{
<ButtonBase key={reaction} sx={{
height: '30px',
minWidth: '45px',
background: 'var(--bg-2)',
borderRadius: '7px'
}} onClick={()=> {
if(reactions[reaction] && reactions[reaction]?.find((item)=> item?.sender === myAddress)){
handleReaction(reaction, message, false)
} else {
handleReaction(reaction, message, true)
}
}}>
}} onClick={(event) => {
event.stopPropagation(); // Prevent event bubbling
setAnchorEl(event.currentTarget);
setSelectedReaction(reaction);
}}>
<div>{reaction}</div> {numberOfReactions > 1 && (
<Typography sx={{
marginLeft: '4px'
@ -264,6 +264,73 @@ export const MessageItem = ({
)
})}
</Box>
{selectedReaction && (
<Popover
open={Boolean(anchorEl)}
anchorEl={anchorEl}
onClose={() => {
setAnchorEl(null);
setSelectedReaction(null);
}}
anchorOrigin={{
vertical: "top",
horizontal: "center",
}}
transformOrigin={{
vertical: "bottom",
horizontal: "center",
}}
PaperProps={{
style: {
backgroundColor: "#232428",
color: "white",
},
}}
>
<Box sx={{ p: 2 }}>
<Typography variant="subtitle1" sx={{ marginBottom: 1 }}>
People who reacted with {selectedReaction}
</Typography>
<List sx={{
overflow: 'auto',
maxWidth: '80vw',
maxHeight: '300px'
}}>
{reactions[selectedReaction]?.map((reactionItem) => (
<ListItem key={reactionItem.sender}>
<ListItemText
primary={reactionItem.senderName || reactionItem.sender}
/>
</ListItem>
))}
</List>
<Button
variant="contained"
color="primary"
onClick={() => {
if (
reactions[selectedReaction]?.find(
(item) => item?.sender === myAddress
)
) {
handleReaction(selectedReaction, message, false); // Remove reaction
} else {
handleReaction(selectedReaction, message, true); // Add reaction
}
setAnchorEl(null);
setSelectedReaction(null);
}}
sx={{ marginTop: 2 }}
>
{reactions[selectedReaction]?.find(
(item) => item?.sender === myAddress
)
? "Remove Reaction"
: "Add Reaction"}
</Button>
</Box>
</Popover>
)}
<Box sx={{
display: 'flex',
alignItems: 'center',