Files
piik/src/client/channel/components/ChannelContentReactions.tsx
T
AAA f730e84bc8 fix(ui): keep room controls usable through layout changes
The shared room sweep fixes narrow stage sizing, caption overlap, covered chat reads, phone history, focus restoration, settings error placement and row alignment at their existing owners. Room and Network settings share one draft and save action. The two shell stylesheets are consolidated; hidden current-room links and obsolete grid rules are removed. The handbook, matching guides and four generated room screenshots follow the shared components.

Chrome 156 interaction checks covered both themes, 320px phones, short windows, classic scrollbars, native-caption simulation, eight retained players, nested Back/Escape and room-tool popout. Full Web validation passed 2485 cases and exposed one assertion on removed hidden local-preview text; the preview identity now lives in the player accessible label and all 15 affected playback/layout checks pass. TypeScript and Browser SDK types pass. Actual WebView caption dragging and physical mobile capture are outside this browser check.
2026-11-26 18:57:48 -05:00

68 lines
4.7 KiB
TypeScript

import { useId, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, type ReactNode } from "react";
import { REACTION_IDS } from "../../../shared/room-interactions";
import { ReactionBurst, ReactionIcon } from "../../components/living/RoomReaction";
import { RoomReactionGrid } from "../../components/living/RoomReactionPicker";
import { Tooltip } from "../../components/living/Tooltip";
import { useAnchoredPopover } from "../../components/living/use-anchored-popover";
import { useLayerHistory } from "../../ui/layer-history";
import { useCopy } from "../../ui/copy";
import { Glyph } from "../../ui/icons";
import type { ContentReactionTarget, RoomInteractionSession } from "../../lib/room-interactions";
import type { MessageReactions } from "../protocol";
import "./channel-message-reactions.css";
const noSubscription = () => () => {};
const noSnapshot = () => null;
/** Counts come from committed snapshots; live events alone play one shared effect. */
export function ChannelContentReactions({ reactions, session, target, disabled = false, effectAnchor, effectsEnabled = true, actions }: {
reactions?: MessageReactions; session: RoomInteractionSession | null; target: ContentReactionTarget;
disabled?: boolean;
effectAnchor?: () => Element | null; effectsEnabled?: boolean;
actions?: ReactNode;
}) {
const { t } = useCopy();
const state = useSyncExternalStore(session?.subscribe ?? noSubscription, session?.getReactionSnapshot ?? noSnapshot, noSnapshot);
const label = "messageSeq" in target ? t("channel.contentReactions.reactToThisMessage") : t("channel.contentReactions.reactToThisScreen");
const trigger = useRef<HTMLButtonElement>(null), menu = useRef<HTMLDivElement>(null), id = useId();
const [open, setOpen] = useState(false);
const targetId = "messageSeq" in target ? target.messageId : target.publicationId;
// Mounting history or returning to a view must not replay already received events.
const existingEvents = useMemo(() => new Set(state?.reactions.map(event => event.id)), [session, targetId, effectsEnabled]);
useLayoutEffect(() => {
if (menu.current?.matches(":popover-open")) menu.current.hidePopover();
}, [session, targetId]);
useAnchoredPopover(trigger, menu, { side: "above", align: "center", enabled: open, closeWhenAnchorClipped: "messageSeq" in target });
useLayerHistory(menu, open);
const canSend = !!state?.canSend && !disabled;
const send = (reaction: typeof REACTION_IDS[number]) => { if (canSend) session?.send({ kind: "reaction", reaction }, target); };
const counts = REACTION_IDS.filter(reaction => (reactions?.counts[reaction] ?? 0) > 0).map(reaction => {
const count = reactions!.counts[reaction]!;
return <button key={reaction} className="lr-chip c3-message-reaction" type="button"
aria-label={`${t(`interaction.reaction.${reaction}`)} ${count}`} disabled={!canSend} onClick={() => send(reaction)}>
<ReactionIcon reaction={reaction} /><span>{count}</span>
</button>;
});
const effects = effectsEnabled && state?.reactions.filter(event => !existingEvents.has(event.id) && event.contentTarget && ("messageSeq" in target
? "messageSeq" in event.contentTarget && event.contentTarget.messageSeq === target.messageSeq && event.contentTarget.messageId === target.messageId
: "publicationId" in event.contentTarget && event.contentTarget.publicationId === target.publicationId));
return <div className="c3-message-reactions" data-has-counts={counts.length > 0 || undefined}>
<span className="c3-message-reaction-counts lr-embedded-actions">{counts}</span>
<span className="c3-content-actions lr-embedded-actions"><Tooltip kind="hint-reaction-picker" className="c3-message-action" disabled={open} text={label}>
<button ref={trigger} popoverTarget={id} className="lr-btn is-dense is-quiet c3-add-reaction" type="button" aria-label={label}
aria-haspopup="dialog" aria-expanded={open} aria-controls={id} disabled={disabled || !session}>
<Glyph name="smile" size={16} /><Glyph name="plus" size={10} />
</button>
</Tooltip>{actions}</span>
<div ref={menu} id={id} popover="auto" role="dialog" className="lr-floating-surface c3-message-reaction-menu"
onToggle={event => { if (event.target === event.currentTarget) setOpen(event.newState === "open"); }} aria-label={label}>
{open && <RoomReactionGrid reactionIds={state?.reactionIds ?? REACTION_IDS} canSend={canSend} onSend={reaction => {
send(reaction);
menu.current?.hidePopover();
}} />}
</div>
{session && effects && effects.map(event => event.payload.kind === "reaction" && <ReactionBurst key={event.id}
reaction={event.payload.reaction} id={event.id} expiresAt={event.expiresAt} now={session.now} anchor={effectAnchor} origin={() => trigger.current} />)}
</div>;
}