-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 'Additional Users' Popover Display Logic for Profile Clicks (#270)
* Modify profile click functionality to only show 'Additional Users' popover when more than 4 users are present * Separate UserPresence component from DocumentHeader component * Fix code format
- Loading branch information
Showing
2 changed files
with
101 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { | ||
Avatar, | ||
AvatarGroup, | ||
ListItem, | ||
ListItemAvatar, | ||
ListItemText, | ||
Paper, | ||
Popover, | ||
Tooltip, | ||
Typography, | ||
} from "@mui/material"; | ||
import { useState } from "react"; | ||
import { Presence } from "./DocumentHeader"; | ||
|
||
interface UserPresenceProps { | ||
presenceList: Presence[]; | ||
} | ||
|
||
function UserPresence(props: UserPresenceProps) { | ||
const { presenceList } = props; | ||
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null); | ||
const popoverOpen = Boolean(anchorEl); | ||
|
||
const handleOpenPopover = (event: React.MouseEvent<HTMLElement>) => { | ||
setAnchorEl(event.currentTarget); | ||
}; | ||
|
||
const handleClosePopover = () => { | ||
setAnchorEl(null); | ||
}; | ||
|
||
const MAX_VISIBLE_AVATARS = 4; | ||
const hiddenAvatars = presenceList.slice(MAX_VISIBLE_AVATARS); | ||
|
||
const renderAvatar = (presence: Presence) => ( | ||
<Tooltip key={presence.clientID} title={presence.presence.name}> | ||
<Avatar alt={presence.presence.name} sx={{ bgcolor: presence.presence.color }}> | ||
{presence.presence.name[0]} | ||
</Avatar> | ||
</Tooltip> | ||
); | ||
|
||
return ( | ||
<> | ||
<AvatarGroup> | ||
{presenceList.slice(0, MAX_VISIBLE_AVATARS).map(renderAvatar)} | ||
{presenceList.length > MAX_VISIBLE_AVATARS && ( | ||
<Avatar onClick={handleOpenPopover}> | ||
+{presenceList.length - MAX_VISIBLE_AVATARS} | ||
</Avatar> | ||
)} | ||
</AvatarGroup> | ||
<Popover | ||
open={popoverOpen} | ||
anchorEl={anchorEl} | ||
onClose={handleClosePopover} | ||
anchorOrigin={{ | ||
vertical: "bottom", | ||
horizontal: "left", | ||
}} | ||
> | ||
<Paper sx={{ padding: 2 }}> | ||
<Typography variant="subtitle2">Additional Users</Typography> | ||
{hiddenAvatars.map((presence) => ( | ||
<ListItem key={presence.clientID} sx={{ paddingY: 1 }}> | ||
<ListItemAvatar> | ||
<Avatar | ||
sx={{ | ||
bgcolor: presence.presence.color, | ||
width: 24, | ||
height: 24, | ||
fontSize: 12, | ||
}} | ||
> | ||
{presence.presence.name[0]} | ||
</Avatar> | ||
</ListItemAvatar> | ||
<ListItemText | ||
primary={presence.presence.name} | ||
primaryTypographyProps={{ | ||
variant: "body2", | ||
}} | ||
/> | ||
</ListItem> | ||
))} | ||
</Paper> | ||
</Popover> | ||
</> | ||
); | ||
} | ||
|
||
export default UserPresence; |