@@ -1,7 +1,14 @@
1-import { useEffect, useRef, useState, type FormEvent, type KeyboardEvent } from "react";
2-import { SendHorizontal } from "lucide-react";
1+import {
2+ useEffect,
3+ useRef,
4+ useState,
5+ type ClipboardEvent,
6+ type FormEvent,
7+ type KeyboardEvent,
8+} from "react";
9+import { Paperclip, SendHorizontal } from "lucide-react";
310 import { Button } from "@/components/ui/button";
4-import { api } from "../lib/api";
11+import { api, upload } from "../lib/api";
512 import type { Channel, Message } from "../lib/types";
613 import Markdown from "./Markdown";
714
@@ -26,6 +33,7 @@ export default function ChatPane({
2633 const [body, setBody] = useState("");
2734 const endRef = useRef<HTMLDivElement>(null);
2835 const textareaRef = useRef<HTMLTextAreaElement>(null);
36+ const fileRef = useRef<HTMLInputElement>(null);
2937
3038 useEffect(() => {
3139 endRef.current?.scrollIntoView();
@@ -38,6 +46,35 @@ export default function ChatPane({
3846 el.style.height = `${Math.min(el.scrollHeight, maxInputHeight)}px`;
3947 }, [body]);
4048
49+ function insert(text: string) {
50+ const el = textareaRef.current;
51+ if (!el) {
52+ setBody((b) => b + text);
53+ return;
54+ }
55+ const { selectionStart: start, selectionEnd: end } = el;
56+ setBody((b) => b.slice(0, start) + text + b.slice(end));
57+ }
58+
59+ async function attach(files: File[]) {
60+ for (const file of files) {
61+ if (!file.type.startsWith("image/")) continue;
62+ try {
63+ const { url } = await upload(file);
64+ insert(`\n`);
65+ } catch {
66+ // ignore failed uploads
67+ }
68+ }
69+ }
70+
71+ function onPaste(e: ClipboardEvent<HTMLTextAreaElement>) {
72+ const images = [...e.clipboardData.files].filter((f) => f.type.startsWith("image/"));
73+ if (images.length === 0) return;
74+ e.preventDefault();
75+ attach(images);
76+ }
77+
4178 async function send() {
4279 const text = body.trim();
4380 if (!text || !channel) return;
@@ -85,6 +122,26 @@ export default function ChatPane({
85122 <div ref={endRef} />
86123 </div>
87124 <form onSubmit={submit} className="flex items-end gap-2 border-t border-sepia/15 p-3">
125+ <input
126+ ref={fileRef}
127+ type="file"
128+ accept="image/*"
129+ multiple
130+ hidden
131+ onChange={(e) => {
132+ attach([...(e.target.files ?? [])]);
133+ e.target.value = "";
134+ }}
135+ />
136+ <Button
137+ type="button"
138+ variant="ghost"
139+ size="icon"
140+ title="attach image"
141+ onClick={() => fileRef.current?.click()}
142+ >
143+ <Paperclip />
144+ </Button>
88145 <textarea
89146 ref={textareaRef}
90147 rows={1}
@@ -92,6 +149,7 @@ export default function ChatPane({
92149 value={body}
93150 onChange={(e) => setBody(e.target.value)}
94151 onKeyDown={onKeyDown}
152+ onPaste={onPaste}
95153 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"
96154 />
97155 <Button type="submit" size="icon" title="send">