Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
325 changes: 221 additions & 104 deletions desktop/src/features/messages/ui/MessageActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ import { toast } from "sonner";

import { buildMessageLink } from "@/features/messages/lib/messageLink";
import { EmojiPicker } from "@/features/custom-emoji/ui/EmojiPicker";
import { useCustomEmoji } from "@/features/custom-emoji/hooks";
import { getThreadReference } from "@/features/messages/lib/threading";
import type {
TimelineMessage,
TimelineReaction,
} from "@/features/messages/types";
import {
recordQuickReactionEmoji,
useQuickReactionEmojis,
} from "@/features/messages/ui/useQuickReactionEmojis";
import { reactionEmojiUrl } from "@/shared/api/customEmoji";
import { cn } from "@/shared/lib/cn";
import { emojiDisplayName } from "@/shared/lib/emojiName";
import { rewriteRelayUrl } from "@/shared/lib/mediaUrl";
import {
AlertDialog,
AlertDialogAction,
Expand All @@ -43,6 +51,9 @@ import { isPositiveEmojiParticle } from "@/shared/ui/EmojiBurstProvider";
import { Popover, PopoverContent, PopoverTrigger } from "@/shared/ui/popover";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/shared/ui/tooltip";

const ACTION_BUTTON_CLASS = "h-8 w-8 rounded-full p-0";
const ACTION_ICON_CLASS = "!h-4 !w-4";

function copyToClipboard(text: string, successMessage: string) {
void navigator.clipboard
.writeText(text)
Expand Down Expand Up @@ -104,13 +115,13 @@ function MoreActionsMenu({
<DropdownMenuTrigger asChild>
<Button
aria-label="More actions"
className="h-6 w-6 rounded-full p-0"
className={ACTION_BUTTON_CLASS}
data-testid={`more-actions-${message.id}`}
size="sm"
type="button"
variant={open ? "secondary" : "ghost"}
>
<EllipsisVertical className="h-3 w-3" />
<EllipsisVertical className={ACTION_ICON_CLASS} />
</Button>
</DropdownMenuTrigger>
</TooltipTrigger>
Expand Down Expand Up @@ -256,6 +267,59 @@ function MoreActionsMenu({
// MessageActionBar — reaction picker, reply button, and more-actions menu
// ---------------------------------------------------------------------------

function QuickReactionButton({
active,
customEmojiUrl,
emoji,
onSelect,
}: {
active: boolean;
customEmojiUrl?: string;
emoji: string;
onSelect: (emoji: string) => void;
}) {
const displayName = emojiDisplayName(emoji);
const mediaUrl = customEmojiUrl ? rewriteRelayUrl(customEmojiUrl) : null;

return (
<Tooltip>
<TooltipTrigger asChild>
<button
aria-label={`React with ${displayName}`}
aria-pressed={active}
className={cn(
"flex h-8 w-8 items-center justify-center rounded-full text-base leading-none transition-colors focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring",
active
? "bg-secondary text-secondary-foreground"
: "text-muted-foreground hover:bg-muted hover:text-foreground",
)}
onClick={() => onSelect(emoji)}
title={displayName}
type="button"
>
{mediaUrl ? (
<img
alt={emoji}
className="h-5 w-5 object-contain"
draggable={false}
src={mediaUrl}
/>
) : (
<span aria-hidden="true" className="translate-y-px">
{emoji}
</span>
)}
</button>
</TooltipTrigger>
<TooltipContent>{displayName}</TooltipContent>
</Tooltip>
);
}

function isCustomEmojiShortcode(emoji: string) {
return emoji.startsWith(":") && emoji.endsWith(":");
}

export function MessageActionBar({
channelId,
message,
Expand Down Expand Up @@ -289,6 +353,20 @@ export function MessageActionBar({
}) {
const [isReactionPickerOpen, setIsReactionPickerOpen] = React.useState(false);
const [isDropdownOpen, setIsDropdownOpen] = React.useState(false);
const customEmoji = useCustomEmoji();
const quickReactionEmojis = useQuickReactionEmojis(4);
const quickReactionItems = React.useMemo(
() =>
quickReactionEmojis
.map((emoji) => ({
customEmojiUrl: reactionEmojiUrl(emoji, customEmoji),
emoji,
}))
.filter(
(item) => !isCustomEmojiShortcode(item.emoji) || item.customEmojiUrl,
),
Comment on lines +365 to +367

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Backfill defaults after dropping stale custom reactions

When the first four stored recents are custom shortcodes that no longer exist in the workspace, useQuickReactionEmojis(4) has already applied the limit before this new filter runs, so this drops all four and the quick-reaction tray renders no buttons instead of falling back to the default emojis. Users can hit this after removing or renaming frequently used custom emoji; filter/prune stale custom entries before applying the limit, or backfill with defaults after this filter.

Useful? React with 👍 / 👎.

[customEmoji, quickReactionEmojis],
);
const hasReplyAction = Boolean(onReply);
const hasReactionAction = Boolean(onReactionSelect);

Expand All @@ -300,22 +378,53 @@ export function MessageActionBar({
Boolean(onUnfollowThread) ||
!message.pending;

if (!hasReplyAction && !hasReactionAction && !hasMoreMenuActions) {
return null;
}

const selectedReactionCount = reactions.filter(
(reaction) => reaction.reactedByCurrentUser,
).length;
const wouldAddReaction = (emoji: string) =>
!reactions.some(
(reaction) => reaction.emoji === emoji && reaction.reactedByCurrentUser,
);
const selectedReactionEmojis = new Set(
reactions
.filter((reaction) => reaction.reactedByCurrentUser)
.map((reaction) => reaction.emoji),
);
const wouldAddReaction = React.useCallback(
(emoji: string) =>
!reactions.some(
(reaction) => reaction.emoji === emoji && reaction.reactedByCurrentUser,
),
[reactions],
);
const handleReactionSelection = React.useCallback(
(emoji: string, closePicker = false) => {
if (!onReactionSelect) {
return;
}

if (wouldAddReaction(emoji) && isPositiveEmojiParticle(emoji)) {
onReactionBadgeBurstRequest?.(emoji);
}

void onReactionSelect(emoji)
.then(() => {
recordQuickReactionEmoji(emoji);
})
.catch(() => {})
.finally(() => {
if (closePicker) {
setIsReactionPickerOpen(false);
}
});
},
[onReactionBadgeBurstRequest, onReactionSelect, wouldAddReaction],
);

if (!hasReplyAction && !hasReactionAction && !hasMoreMenuActions) {
return null;
}

return (
<div
className={cn(
"overflow-hidden rounded-full border border-border/70 bg-background/95 shadow-xs backdrop-blur-sm supports-[backdrop-filter]:bg-background/85 transition-opacity duration-150 ease-out",
"-m-1 p-1 transition-opacity duration-150 ease-out",
"opacity-100 sm:pointer-events-none sm:opacity-0",
"sm:group-hover/message:pointer-events-auto sm:group-hover/message:opacity-100",
"sm:group-focus-within/message:pointer-events-auto sm:group-focus-within/message:opacity-100",
Expand All @@ -325,104 +434,112 @@ export function MessageActionBar({
)}
data-testid={`message-action-bar-${message.id}`}
>
<div className="flex items-center gap-1 p-1">
{hasReactionAction ? (
<Popover
onOpenChange={setIsReactionPickerOpen}
open={isReactionPickerOpen}
>
<div className="overflow-hidden rounded-full border border-border/70 bg-background/95 shadow-xs backdrop-blur-sm supports-[backdrop-filter]:bg-background/85">
<div className="flex items-center gap-0.5 p-1">
{hasReactionAction && quickReactionItems.length > 0 ? (
<>
<div className="hidden items-center gap-0.5 sm:flex">
{quickReactionItems.map(({ customEmojiUrl, emoji }) => (
<QuickReactionButton
active={selectedReactionEmojis.has(emoji)}
customEmojiUrl={customEmojiUrl}
emoji={emoji}
key={emoji}
onSelect={handleReactionSelection}
/>
))}
</div>
<div className="mx-0.5 hidden h-4 w-px bg-border/70 sm:block" />
</>
) : null}

{hasReactionAction ? (
<Popover
onOpenChange={setIsReactionPickerOpen}
open={isReactionPickerOpen}
>
<Tooltip>
<TooltipTrigger asChild>
<PopoverTrigger asChild>
<Button
aria-label="Open reactions"
className={ACTION_BUTTON_CLASS}
data-testid={`react-message-${message.id}`}
size="sm"
type="button"
variant={
isReactionPickerOpen || selectedReactionCount > 0
? "secondary"
: "ghost"
}
>
<SmilePlus className={ACTION_ICON_CLASS} />
</Button>
</PopoverTrigger>
</TooltipTrigger>
<TooltipContent>React</TooltipContent>
</Tooltip>
<PopoverContent
align="end"
className="w-auto p-0 rounded-2xl overflow-hidden border-0 bg-transparent shadow-none"
side="top"
sideOffset={10}
>
{reactionErrorMessage ? (
<div className="px-3 pt-3 pb-0">
<p className="text-xs text-destructive">
{reactionErrorMessage}
</p>
</div>
) : null}
<EmojiPicker
autoFocus
onSelect={(value) => {
// `value` is already a `native` glyph or a `:shortcode:` for
// custom emoji; the toggle mutation resolves the URL.
handleReactionSelection(value, true);
}}
/>
</PopoverContent>
</Popover>
) : null}

{hasReplyAction ? (
<Tooltip>
<TooltipTrigger asChild>
<PopoverTrigger asChild>
<Button
aria-label="Open reactions"
className="h-6 w-6 rounded-full p-0"
data-testid={`react-message-${message.id}`}
size="sm"
type="button"
variant={
isReactionPickerOpen || selectedReactionCount > 0
? "secondary"
: "ghost"
}
>
<SmilePlus className="h-3 w-3" />
</Button>
</PopoverTrigger>
<Button
aria-label="Reply"
className={ACTION_BUTTON_CLASS}
data-testid={`reply-message-${message.id}`}
onClick={() => {
onReply?.(message);
}}
size="sm"
type="button"
variant="ghost"
>
<CornerUpLeft className={ACTION_ICON_CLASS} />
</Button>
</TooltipTrigger>
<TooltipContent>React</TooltipContent>
<TooltipContent>Reply</TooltipContent>
</Tooltip>
<PopoverContent
align="end"
className="w-auto p-0 rounded-2xl overflow-hidden border-0 bg-transparent shadow-none"
side="top"
sideOffset={10}
>
{reactionErrorMessage ? (
<div className="px-3 pt-3 pb-0">
<p className="text-xs text-destructive">
{reactionErrorMessage}
</p>
</div>
) : null}
<EmojiPicker
autoFocus
onSelect={(value) => {
if (!onReactionSelect) {
return;
}
// `value` is already a `native` glyph or a `:shortcode:` for
// custom emoji; the toggle mutation resolves the URL.
if (
wouldAddReaction(value) &&
isPositiveEmojiParticle(value)
) {
onReactionBadgeBurstRequest?.(value);
}
void onReactionSelect(value).finally(() => {
setIsReactionPickerOpen(false);
});
}}
/>
</PopoverContent>
</Popover>
) : null}

{hasReplyAction ? (
<Tooltip>
<TooltipTrigger asChild>
<Button
aria-label="Reply"
className="h-6 w-6 rounded-full p-0"
data-testid={`reply-message-${message.id}`}
onClick={() => {
onReply?.(message);
}}
size="sm"
type="button"
variant="ghost"
>
<CornerUpLeft className="h-3 w-3" />
</Button>
</TooltipTrigger>
<TooltipContent>Reply</TooltipContent>
</Tooltip>
) : null}
) : null}

{hasMoreMenuActions ? (
<MoreActionsMenu
channelId={channelId}
message={message}
onDelete={onDelete}
onEdit={onEdit}
onFollowThread={onFollowThread}
onMarkUnread={onMarkUnread}
onOpenChange={setIsDropdownOpen}
onUnfollowThread={onUnfollowThread}
open={isDropdownOpen}
isFollowingThread={isFollowingThread}
/>
) : null}
{hasMoreMenuActions ? (
<MoreActionsMenu
channelId={channelId}
message={message}
onDelete={onDelete}
onEdit={onEdit}
onFollowThread={onFollowThread}
onMarkUnread={onMarkUnread}
onOpenChange={setIsDropdownOpen}
onUnfollowThread={onUnfollowThread}
open={isDropdownOpen}
isFollowingThread={isFollowingThread}
/>
) : null}
</div>
</div>
</div>
);
Expand Down
Loading