|
| 1 | +import { useRef, useEffect, useState, type KeyboardEvent } from "react"; |
| 2 | +import { useChat } from "@/hooks/useChat"; |
| 3 | + |
| 4 | +const SPECTATOR_COLORS = [ |
| 5 | + "#a78bfa", |
| 6 | + "#fb923c", |
| 7 | + "#34d399", |
| 8 | + "#f472b6", |
| 9 | + "#60a5fa", |
| 10 | + "#f87171", |
| 11 | + "#2dd4bf", |
| 12 | + "#c084fc", |
| 13 | + "#4ade80", |
| 14 | + "#e879f9", |
| 15 | +]; |
| 16 | + |
| 17 | +const HOST_COLOR = "#facc15"; |
| 18 | +const HOST_FONT_SIZE = 15; |
| 19 | +const SPECTATOR_FONT_SIZE = 13; |
| 20 | + |
| 21 | +const EMOJIS = [ |
| 22 | + "🦝","😀","😅","😂","🤣","🥰","😘","🤩", |
| 23 | + "🥳","🤯","🥶","🤓","😎","😱","🤑","😻", |
| 24 | + "🙀","😽","👍","👎","🤟","👌","🫶","🖕", |
| 25 | + "🫵","🏆","🎁","🪙","💰","🪅","🎊","🎉", |
| 26 | + "❤️","💖","❤️🔥","🔥", |
| 27 | +]; |
| 28 | + |
| 29 | +function getNickColor(nick: string, role: "host" | "spectator"): string { |
| 30 | + if (role === "host") return HOST_COLOR; |
| 31 | + let hash = 0; |
| 32 | + for (let i = 0; i < nick.length; i++) { |
| 33 | + hash = ((hash << 5) - hash) + nick.charCodeAt(i); |
| 34 | + hash |= 0; |
| 35 | + } |
| 36 | + return SPECTATOR_COLORS[Math.abs(hash) % SPECTATOR_COLORS.length]; |
| 37 | +} |
| 38 | + |
| 39 | +interface ChatPanelProps { |
| 40 | + className?: string; |
| 41 | +} |
| 42 | + |
| 43 | +export function ChatPanel({ className = "" }: ChatPanelProps) { |
| 44 | + const { messages, text, setText, sendMessage } = useChat(); |
| 45 | + const bottomRef = useRef<HTMLDivElement>(null); |
| 46 | + const inputRef = useRef<HTMLInputElement>(null); |
| 47 | + const [pickerOpen, setPickerOpen] = useState(false); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + bottomRef.current?.scrollIntoView({ behavior: "smooth" }); |
| 51 | + }, [messages]); |
| 52 | + |
| 53 | + const handleSend = () => { |
| 54 | + sendMessage(text); |
| 55 | + setText(""); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => { |
| 59 | + if (e.key === "Enter") { |
| 60 | + e.preventDefault(); |
| 61 | + handleSend(); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + const insertEmoji = (emoji: string) => { |
| 66 | + const input = inputRef.current; |
| 67 | + if (!input) { |
| 68 | + setText((prev) => prev + emoji); |
| 69 | + return; |
| 70 | + } |
| 71 | + const start = input.selectionStart ?? text.length; |
| 72 | + const end = input.selectionEnd ?? text.length; |
| 73 | + const next = text.slice(0, start) + emoji + text.slice(end); |
| 74 | + setText(next); |
| 75 | + // restore cursor after the inserted emoji |
| 76 | + requestAnimationFrame(() => { |
| 77 | + const pos = start + emoji.length; |
| 78 | + input.setSelectionRange(pos, pos); |
| 79 | + input.focus(); |
| 80 | + }); |
| 81 | + }; |
| 82 | + |
| 83 | + return ( |
| 84 | + <div |
| 85 | + className={`flex flex-col overflow-hidden rounded-xl border border-fuchsia-500/70 bg-card/80 shadow-[0_0_18px_hsla(300,90%,55%,0.28)] backdrop-blur-sm ${className}`} |
| 86 | + style={{ minWidth: 0 }} |
| 87 | + > |
| 88 | + <div className="flex-shrink-0 border-b border-fuchsia-500/30 px-4 py-2.5"> |
| 89 | + <h2 className="font-display text-xs tracking-widest text-primary/70 uppercase"> |
| 90 | + Чат зрителей |
| 91 | + </h2> |
| 92 | + </div> |
| 93 | + |
| 94 | + <div className="flex-1 min-h-0 overflow-y-auto px-3 py-2 space-y-1.5 [scrollbar-width:thin] [scrollbar-color:hsla(300,90%,55%,0.5)_transparent] [&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-track]:bg-transparent [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-fuchsia-500/50 [&::-webkit-scrollbar-thumb:hover]:bg-fuchsia-500/80"> |
| 95 | + {messages.map((msg) => ( |
| 96 | + <div |
| 97 | + key={msg.id} |
| 98 | + style={{ fontSize: msg.role === "host" ? HOST_FONT_SIZE : SPECTATOR_FONT_SIZE }} |
| 99 | + > |
| 100 | + <span style={{ color: getNickColor(msg.nick, msg.role), fontWeight: 600 }}> |
| 101 | + {msg.nick}: |
| 102 | + </span>{" "} |
| 103 | + <span style={{ color: "rgba(255,255,255,0.88)" }}>{msg.text}</span> |
| 104 | + </div> |
| 105 | + ))} |
| 106 | + <div ref={bottomRef} /> |
| 107 | + </div> |
| 108 | + |
| 109 | + {/* Emoji picker */} |
| 110 | + {pickerOpen && ( |
| 111 | + <div className="flex-shrink-0 border-t border-fuchsia-500/30 bg-card/90 p-1.5"> |
| 112 | + <div className="grid grid-cols-6 gap-0.5"> |
| 113 | + {EMOJIS.map((emoji) => ( |
| 114 | + <button |
| 115 | + key={emoji} |
| 116 | + type="button" |
| 117 | + onClick={() => insertEmoji(emoji)} |
| 118 | + className="flex items-center justify-center rounded p-1 text-base leading-none transition hover:bg-fuchsia-500/20 active:scale-90" |
| 119 | + style={{ fontSize: 18 }} |
| 120 | + > |
| 121 | + {emoji} |
| 122 | + </button> |
| 123 | + ))} |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + )} |
| 127 | + |
| 128 | + <div className="flex-shrink-0 flex items-center gap-1.5 border-t border-fuchsia-500/30 p-2"> |
| 129 | + {/* Emoji toggle */} |
| 130 | + |
| 131 | + <input |
| 132 | + ref={inputRef} |
| 133 | + type="text" |
| 134 | + value={text} |
| 135 | + onChange={(e) => setText(e.target.value)} |
| 136 | + onKeyDown={handleKeyDown} |
| 137 | + placeholder="Пиши сюдой..." |
| 138 | + className="flex-1 min-w-0 rounded border border-border/60 bg-background/50 px-2 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:border-primary/50 focus:outline-none" |
| 139 | + /> |
| 140 | + |
| 141 | +<button |
| 142 | + type="button" |
| 143 | + onClick={() => setPickerOpen((o) => !o)} |
| 144 | + title="Смайлики" |
| 145 | + className={`flex-shrink-0 flex items-center justify-center rounded border px-2 py-1.5 text-base transition ${ |
| 146 | + pickerOpen |
| 147 | + ? "border-fuchsia-500/60 bg-fuchsia-500/20 text-fuchsia-300" |
| 148 | + : "border-border/50 bg-background/40 text-foreground/60 hover:border-fuchsia-500/50 hover:text-fuchsia-300" |
| 149 | + }`} |
| 150 | + style={{ fontSize: 16 }} |
| 151 | + > |
| 152 | + 🙂 |
| 153 | + </button> |
| 154 | + |
| 155 | + <button |
| 156 | + type="button" |
| 157 | + onClick={handleSend} |
| 158 | + className="flex-shrink-0 rounded border border-primary/40 bg-primary/15 px-3 py-1.5 text-sm text-primary transition hover:bg-primary/25" |
| 159 | + > |
| 160 | + {'>'} |
| 161 | + </button> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + ); |
| 165 | +} |
0 commit comments