| 1 | import { SmilePlus } from "lucide-react"; |
| 2 | |
| 3 | import type { ChatReaction } from "@g1t/contracts"; |
| 4 | |
| 5 | import { useEmojiContext } from "./context"; |
| 6 | import { EmojiPickerPopover } from "./picker"; |
| 7 | import { EmojiGlyph } from "./render"; |
| 8 | import { Hint } from "../ui/hint"; |
| 9 | import { reactorsLine } from "../../lib/emoji"; |
| 10 | |
| 11 | /** |
| 12 | * A message's reactions: one pill per emoji with how many used it, lit when |
| 13 | * the viewer did. Hovering one says who; clicking it adds or takes back the |
| 14 | * viewer's own, at once (the page applies it before the service answers). |
| 15 | * `+` opens the picker. Without a way to react (no conversation around |
| 16 | * it), the pills only show. |
| 17 | */ |
| 18 | export function ReactionBar({ messageId, reactions }: { messageId: string; reactions: readonly ChatReaction[] | undefined }) { |
| 19 | const { react, me, byName, usercontent } = useEmojiContext(); |
| 20 | if (!reactions?.length) return null; |
| 21 | const meId = me?.kind === "user" ? me.id : null; |
| 22 | return ( |
| 23 | <div className="mt-1 flex flex-wrap items-center gap-1" aria-label="Reactions"> |
| 24 | {reactions.map((reaction) => { |
| 25 | const custom = /^:([a-z0-9_+-]{2,32}):$/.exec(reaction.emoji); |
| 26 | const label = ( |
| 27 | <span className="flex max-w-64 flex-col items-center gap-1 py-0.5 text-center"> |
| 28 | <EmojiGlyph emoji={reaction.emoji} byName={byName} usercontent={usercontent} size={32} /> |
| 29 | <span> |
| 30 | {reactorsLine(reaction, meId)} reacted |
| 31 | {custom && <span className="text-faint"> with {reaction.emoji}</span>} |
| 32 | </span> |
| 33 | </span> |
| 34 | ); |
| 35 | return ( |
| 36 | <Hint key={reaction.emoji} label={label}> |
| 37 | <button |
| 38 | type="button" |
| 39 | aria-pressed={reaction.me} |
| 40 | aria-label={`${reaction.emoji}, ${reaction.count} ${reaction.count === 1 ? "reaction" : "reactions"}${reaction.me ? ", including yours" : ""}`} |
| 41 | disabled={!react} |
| 42 | onClick={() => react?.(messageId, reaction.emoji, !reaction.me)} |
| 43 | className={`flex h-6 items-center gap-1 rounded-full border px-1.5 text-xs font-medium tabular-nums transition-colors disabled:cursor-default ${ |
| 44 | reaction.me |
| 45 | ? "border-accent/60 bg-accent/15 text-accent hover:bg-accent/25" |
| 46 | : "border-line bg-surface text-muted hover:border-line-strong hover:bg-raised hover:text-fg" |
| 47 | }`} |
| 48 | > |
| 49 | <EmojiGlyph emoji={reaction.emoji} byName={byName} usercontent={usercontent} size={15} /> |
| 50 | {reaction.count} |
| 51 | </button> |
| 52 | </Hint> |
| 53 | ); |
| 54 | })} |
| 55 | {react && <AddReaction messageId={messageId} variant="pill" />} |
| 56 | </div> |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Adding a reaction: the `+` pill after the reactions, or (`toolbar`) the |
| 62 | * smiley in a message's hover actions. |
| 63 | */ |
| 64 | export function AddReaction({ messageId, variant = "toolbar" }: { messageId: string; variant?: "pill" | "toolbar" }) { |
| 65 | const { react } = useEmojiContext(); |
| 66 | if (!react) return null; |
| 67 | const button = |
| 68 | variant === "pill" ? ( |
| 69 | <button |
| 70 | type="button" |
| 71 | aria-label="Add a reaction" |
| 72 | className="flex h-6 items-center rounded-full border border-line bg-surface px-1.5 text-faint transition-colors hover:border-line-strong hover:bg-raised hover:text-fg" |
| 73 | > |
| 74 | <SmilePlus size={14} /> |
| 75 | </button> |
| 76 | ) : ( |
| 77 | <button |
| 78 | type="button" |
| 79 | aria-label="Add a reaction" |
| 80 | className="flex size-7 items-center justify-center rounded-md text-muted hover:bg-raised hover:text-fg" |
| 81 | > |
| 82 | <SmilePlus size={15} /> |
| 83 | </button> |
| 84 | ); |
| 85 | return ( |
| 86 | <EmojiPickerPopover onPick={(emoji) => react(messageId, emoji, true)} side={variant === "pill" ? "top" : "bottom"} align={variant === "pill" ? "start" : "end"}> |
| 87 | <Hint label="Add a reaction">{button}</Hint> |
| 88 | </EmojiPickerPopover> |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** The reactions a phone's message sheet offers in one tap. */ |
| 93 | const QUICK = ["👍", "❤️", "😂", "🎉", "👀", "✅"]; |
| 94 | |
| 95 | /** |
| 96 | * A row of one-tap reactions and the full picker, for a message's actions |
| 97 | * on a phone (no hover there). `onDone` closes whatever it sits in. |
| 98 | */ |
| 99 | export function QuickReactions({ messageId, onDone }: { messageId: string; onDone: () => void }) { |
| 100 | const { react } = useEmojiContext(); |
| 101 | if (!react) return null; |
| 102 | const pick = (emoji: string) => { |
| 103 | react(messageId, emoji, true); |
| 104 | onDone(); |
| 105 | }; |
| 106 | return ( |
| 107 | <div className="flex items-center justify-between gap-1 px-2 pb-2" aria-label="React"> |
| 108 | {QUICK.map((emoji) => ( |
| 109 | <button |
| 110 | key={emoji} |
| 111 | type="button" |
| 112 | aria-label={`React with ${emoji}`} |
| 113 | onClick={() => pick(emoji)} |
| 114 | className="flex size-11 items-center justify-center rounded-full bg-surface text-[1.375rem] active:bg-raised" |
| 115 | > |
| 116 | {emoji} |
| 117 | </button> |
| 118 | ))} |
| 119 | <EmojiPickerPopover onPick={pick} side="top" align="end"> |
| 120 | <button type="button" aria-label="More reactions" className="flex size-11 items-center justify-center rounded-full bg-surface text-muted active:bg-raised"> |
| 121 | <SmilePlus size={20} /> |
| 122 | </button> |
| 123 | </EmojiPickerPopover> |
| 124 | </div> |
| 125 | ); |
| 126 | } |