@@ -14,14 +14,16 @@ import {
1414 Paperclip,
1515 Pencil,
1616 SendHorizontal,
17+ Sticker,
1718 ThumbsDown,
1819 ThumbsUp,
1920 Trash2,
21+ X,
2022 } from "lucide-react";
2123 import { Button } from "@/components/ui/button";
2224 import { cn } from "cn";
23-import { api, upload } from "../lib/api";
24-import type { Channel, Message, Reaction, UserSummary } from "../lib/types";
25+import { api, gifs, upload } from "../lib/api";
26+import type { Channel, Gif, Message, Reaction, UserSummary } from "../lib/types";
2527 import Markdown from "./Markdown";
2628
2729 const reactions: { key: Reaction; label: string; Icon: typeof ThumbsUp }[] = [
@@ -71,6 +73,7 @@ export default function ChatPane({
7173 const [mention, setMention] = useState<{ query: string; start: number; index: number } | null>(
7274 null,
7375 );
76+ const [picker, setPicker] = useState<{ gifs: Gif[]; query: string } | null>(null);
7477 const listRef = useRef<HTMLDivElement>(null);
7578 const contentRef = useRef<HTMLDivElement>(null);
7679 const stickRef = useRef(true);
@@ -216,6 +219,26 @@ export default function ChatPane({
216219 setCollapsed(new Set(messages.filter((m) => hasImage(m.body)).map((m) => m.id)));
217220 }
218221
222+ async function openGifs(query: string) {
223+ try {
224+ const results = await gifs(query);
225+ setPicker({ gifs: results, query });
226+ } catch {
227+ // ignore fetch failures
228+ }
229+ }
230+
231+ function pickGif(g: Gif) {
232+ insert(`\n`);
233+ setPicker(null);
234+ requestAnimationFrame(() => {
235+ const el = textareaRef.current;
236+ if (!el) return;
237+ el.focus();
238+ el.setSelectionRange(el.value.length, el.value.length);
239+ });
240+ }
241+
219242 function startEdit(m: Message) {
220243 setEditingId(m.id);
221244 setEditBody(m.body);
@@ -251,6 +274,11 @@ export default function ChatPane({
251274 collapseAll();
252275 return;
253276 }
277+ const gifCommand = /^\/gif(?:\s+(.*))?$/.exec(text);
278+ if (gifCommand) {
279+ openGifs(gifCommand[1]?.trim() ?? "");
280+ return;
281+ }
254282 try {
255283 await api(`/channels/${channel.id}/messages`, {
256284 method: "POST",
@@ -267,6 +295,11 @@ export default function ChatPane({
267295 }
268296
269297 function onKeyDown(e: KeyboardEvent<HTMLTextAreaElement>) {
298+ if (e.key === "Escape" && picker) {
299+ e.preventDefault();
300+ setPicker(null);
301+ return;
302+ }
270303 const matches = mention ? suggest(mention.query) : [];
271304 if (mention && matches.length > 0) {
272305 if (e.key === "ArrowDown") {
@@ -443,6 +476,58 @@ export default function ChatPane({
443476 onSubmit={submit}
444477 className="relative flex items-end gap-2 border-t border-sepia/15 p-3"
445478 >
479+ {picker && (
480+ <div className="absolute bottom-full left-2 mb-1 w-[min(24rem,90vw)] rounded-lg border border-border bg-panel p-2 shadow-lg">
481+ <div className="mb-1 flex items-center gap-1">
482+ <input
483+ autoFocus
484+ value={picker.query}
485+ onChange={(e) => setPicker({ ...picker, query: e.target.value })}
486+ onKeyDown={(e) => {
487+ if (e.key === "Enter") {
488+ e.preventDefault();
489+ openGifs(picker.query.trim());
490+ } else if (e.key === "Escape") {
491+ e.preventDefault();
492+ setPicker(null);
493+ }
494+ }}
495+ placeholder="search gifs"
496+ className="h-8 min-w-0 flex-1 rounded-lg border border-input bg-transparent px-2 text-sm outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 dark:bg-input/30"
497+ />
498+ <Button
499+ type="button"
500+ variant="ghost"
501+ size="icon-xs"
502+ title="cancel"
503+ onClick={() => setPicker(null)}
504+ >
505+ <X />
506+ </Button>
507+ </div>
508+ {picker.gifs.length === 0 ? (
509+ <div className="px-1 py-2 text-sm text-sepia/50">no results</div>
510+ ) : (
511+ <div className="grid grid-cols-3 gap-1">
512+ {picker.gifs.map((g) => (
513+ <button
514+ key={g.url}
515+ type="button"
516+ onClick={() => pickGif(g)}
517+ className="overflow-hidden rounded border border-sepia/15 hover:border-gold/50"
518+ >
519+ <img
520+ src={g.preview}
521+ alt=""
522+ loading="lazy"
523+ className="h-24 w-full object-cover"
524+ />
525+ </button>
526+ ))}
527+ </div>
528+ )}
529+ </div>
530+ )}
446531 {mention && suggest(mention.query).length > 0 && (
447532 <div className="absolute bottom-full left-2 mb-1 w-56 overflow-hidden rounded-lg border border-border bg-panel shadow-lg">
448533 {suggest(mention.query).map((u, i) => (
@@ -481,6 +566,9 @@ export default function ChatPane({
481566 >
482567 <Paperclip />
483568 </Button>
569+ <Button type="button" variant="ghost" size="icon" title="gif" onClick={() => openGifs("")}>
570+ <Sticker />
571+ </Button>
484572 <textarea
485573 ref={textareaRef}
486574 rows={1}