@@ -10,11 +10,18 @@ import {
1010 } from "react";
1111 import { Paperclip, SendHorizontal } from "lucide-react";
1212 import { Button } from "@/components/ui/button";
13+import { cn } from "cn";
1314 import { api, upload } from "../lib/api";
14-import type { Channel, Message } from "../lib/types";
15+import type { Channel, Message, UserSummary } from "../lib/types";
1516 import Markdown from "./Markdown";
1617
1718 const maxInputHeight = 200;
19+const maxSuggestions = 8;
20+
21+// isMentioned reports whether body contains "@username" as a whole word.
22+function isMentioned(body: string, username: string) {
23+ return new RegExp(`(^|[^\\w-])@${username}(?![\\w-])`, "i").test(body);
24+}
1825
1926 // prose supplies the block-level markdown styling; the CSS vars retune its
2027 // palette to the sepia theme.
@@ -28,11 +35,18 @@ function clock(ts: number) {
2835 export default function ChatPane({
2936 channel,
3037 messages,
38+ users,
39+ username,
3140 }: {
3241 channel: Channel | null;
3342 messages: Message[];
43+ users: UserSummary[];
44+ username: string;
3445 }) {
3546 const [body, setBody] = useState("");
47+ const [mention, setMention] = useState<{ query: string; start: number; index: number } | null>(
48+ null,
49+ );
3650 const listRef = useRef<HTMLDivElement>(null);
3751 const contentRef = useRef<HTMLDivElement>(null);
3852 const stickRef = useRef(true);
@@ -90,6 +104,35 @@ export default function ChatPane({
90104 setBody((b) => b.slice(0, start) + text + b.slice(end));
91105 }
92106
107+ function suggest(query: string) {
108+ const q = query.toLowerCase();
109+ return users.filter((u) => u.username.toLowerCase().startsWith(q)).slice(0, maxSuggestions);
110+ }
111+
112+ // detectMention looks for an "@partial" token ending at the caret.
113+ function detectMention(value: string, caret: number) {
114+ const m = /(?:^|\s)@([\w-]*)$/.exec(value.slice(0, caret));
115+ const query = m?.[1];
116+ setMention(
117+ query !== undefined && suggest(query).length > 0
118+ ? { query, start: caret - query.length, index: 0 }
119+ : null,
120+ );
121+ }
122+
123+ function chooseMention(name: string) {
124+ const el = textareaRef.current;
125+ if (!el || !mention) return;
126+ const caret = el.selectionStart ?? body.length;
127+ setBody(body.slice(0, mention.start) + name + " " + body.slice(caret));
128+ setMention(null);
129+ const pos = mention.start + name.length + 1;
130+ requestAnimationFrame(() => {
131+ el.focus();
132+ el.setSelectionRange(pos, pos);
133+ });
134+ }
135+
93136 async function attach(files: File[]) {
94137 for (const file of files) {
95138 if (!file.type.startsWith("image/")) continue;
@@ -113,6 +156,7 @@ export default function ChatPane({
113156 const text = body.trim();
114157 if (!text || !channel) return;
115158 setBody("");
159+ setMention(null);
116160 try {
117161 await api(`/channels/${channel.id}/messages`, {
118162 method: "POST",
@@ -129,6 +173,29 @@ export default function ChatPane({
129173 }
130174
131175 function onKeyDown(e: KeyboardEvent<HTMLTextAreaElement>) {
176+ const matches = mention ? suggest(mention.query) : [];
177+ if (mention && matches.length > 0) {
178+ if (e.key === "ArrowDown") {
179+ e.preventDefault();
180+ setMention({ ...mention, index: (mention.index + 1) % matches.length });
181+ return;
182+ }
183+ if (e.key === "ArrowUp") {
184+ e.preventDefault();
185+ setMention({ ...mention, index: (mention.index - 1 + matches.length) % matches.length });
186+ return;
187+ }
188+ if (e.key === "Enter" || e.key === "Tab") {
189+ e.preventDefault();
190+ chooseMention(matches[mention.index].username);
191+ return;
192+ }
193+ if (e.key === "Escape") {
194+ e.preventDefault();
195+ setMention(null);
196+ return;
197+ }
198+ }
132199 if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) {
133200 e.preventDefault();
134201 send();
@@ -145,8 +212,15 @@ export default function ChatPane({
145212 <div ref={contentRef}>
146213 {messages.map((m) => {
147214 const action = m.body.startsWith("/me ") ? m.body.slice(4) : null;
215+ const hit = !action && isMentioned(m.body, username);
148216 return (
149- <div key={m.id} className="flex gap-1.5 py-0.5 text-sm leading-6">
217+ <div
218+ key={m.id}
219+ className={cn(
220+ "-mx-1.5 flex gap-1.5 rounded px-1.5 py-0.5 text-sm leading-6",
221+ hit && "bg-[color-mix(in_srgb,var(--gold)_14%,transparent)]",
222+ )}
223+ >
150224 <span className="shrink-0">
151225 <span className="text-sepia/40">{clock(m.createdAt)}</span>{" "}
152226 {!action && <span className="text-gold/90">{m.username}</span>}
@@ -165,7 +239,28 @@ export default function ChatPane({
165239 })}
166240 </div>
167241 </div>
168- <form onSubmit={submit} className="flex items-end gap-2 border-t border-sepia/15 p-3">
242+ <form
243+ onSubmit={submit}
244+ className="relative flex items-end gap-2 border-t border-sepia/15 p-3"
245+ >
246+ {mention && suggest(mention.query).length > 0 && (
247+ <div className="absolute bottom-full left-2 mb-1 w-56 overflow-hidden rounded-lg border border-border bg-panel shadow-lg">
248+ {suggest(mention.query).map((u, i) => (
249+ <button
250+ key={u.id}
251+ type="button"
252+ onMouseDown={(e) => e.preventDefault()}
253+ onClick={() => chooseMention(u.username)}
254+ className={cn(
255+ "block w-full px-3 py-1.5 text-left text-sm",
256+ i === mention.index ? "bg-accent text-accent-foreground" : "text-sepia",
257+ )}
258+ >
259+ {u.username}
260+ </button>
261+ ))}
262+ </div>
263+ )}
169264 <input
170265 ref={fileRef}
171266 type="file"
@@ -191,7 +286,10 @@ export default function ChatPane({
191286 rows={1}
192287 placeholder={`message #${channel.name}`}
193288 value={body}
194- onChange={(e) => setBody(e.target.value)}
289+ onChange={(e) => {
290+ setBody(e.target.value);
291+ detectMention(e.target.value, e.target.selectionStart ?? e.target.value.length);
292+ }}
195293 onKeyDown={onKeyDown}
196294 onPaste={onPaste}
197295 className="max-h-[200px] w-full resize-none rounded-lg border border-input bg-transparent px-2.5 py-1 text-base leading-6 outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 dark:bg-input/30 md:text-sm"