xonecas.io
8058
Ir

irvc / commit 55c9351

feat: image toggle and `/collapse`

xonecas2026-10-12 55c9351f1558a19f0a74a03b5720eed9849373a6 parent 4587245

2 files changed

web/src/components/ChatPane.tsx+28−1
@@ -23,6 +23,11 @@ function isMentioned(body: string, username: string) {
2323 return new RegExp(`(^|[^\\w-])@${username}(?![\\w-])`, "i").test(body);
2424 }
2525
26+// hasImage reports whether body contains a markdown image.
27+function hasImage(body: string) {
28+ return /!\[[^\]]*\]\([^)]+\)/.test(body);
29+}
30+
2631 // prose supplies the block-level markdown styling; the CSS vars retune its
2732 // palette to the sepia theme.
2833 const bodyClass =
@@ -44,6 +49,7 @@ export default function ChatPane({
4449 username: string;
4550 }) {
4651 const [body, setBody] = useState("");
52+ const [collapsed, setCollapsed] = useState<Set<number>>(new Set());
4753 const [mention, setMention] = useState<{ query: string; start: number; index: number } | null>(
4854 null,
4955 );
@@ -152,11 +158,28 @@ export default function ChatPane({
152158 attach(images);
153159 }
154160
161+ function toggleImage(id: number) {
162+ setCollapsed((prev) => {
163+ const next = new Set(prev);
164+ next.has(id) ? next.delete(id) : next.add(id);
165+ return next;
166+ });
167+ }
168+
169+ // /collapse collapses every image in the current channel.
170+ function collapseAll() {
171+ setCollapsed(new Set(messages.filter((m) => hasImage(m.body)).map((m) => m.id)));
172+ }
173+
155174 async function send() {
156175 const text = body.trim();
157176 if (!text || !channel) return;
158177 setBody("");
159178 setMention(null);
179+ if (text === "/collapse") {
180+ collapseAll();
181+ return;
182+ }
160183 try {
161184 await api(`/channels/${channel.id}/messages`, {
162185 method: "POST",
@@ -231,7 +254,11 @@ export default function ChatPane({
231254 </div>
232255 ) : (
233256 <div className={bodyClass}>
234- <Markdown text={m.body} />
257+ <Markdown
258+ text={m.body}
259+ collapsed={collapsed.has(m.id)}
260+ onToggle={() => toggleImage(m.id)}
261+ />
235262 </div>
236263 )}
237264 </div>
web/src/components/Markdown.tsx+40−6
@@ -1,12 +1,23 @@
1-import { Component, useState, type ReactNode } from "react";
1+import { Component, createContext, useContext, useState, type ReactNode } from "react";
22 import ReactMarkdown from "react-markdown";
33 import remarkBreaks from "remark-breaks";
44 import remarkGfm from "remark-gfm";
5+import { ChevronDown, ChevronRight } from "lucide-react";
6+
7+const Collapsed = createContext(false);
58
69 // Images render inline; clicking one opens it in a full-window focus dialog.
10+// When the message's header is collapsed, images shrink to an alt-text chip.
711 function Image({ src, alt }: { src?: string; alt?: string }) {
812 const [open, setOpen] = useState(false);
13+ const collapsed = useContext(Collapsed);
914 if (!src) return alt ? <span>{alt}</span> : null;
15+ if (collapsed)
16+ return (
17+ <span className="my-1 inline-block rounded border border-sepia/15 bg-sepia/5 px-2 py-0.5 text-xs text-sepia/60">
18+ {alt || "image"}
19+ </span>
20+ );
1021 return (
1122 <>
1223 <img
@@ -54,13 +65,36 @@ class Fallback extends Component<{ text: string; children: ReactNode }, { failed
5465
5566 // Markdown renders a message body as GFM. Single newlines become hard breaks so
5667 // multiline input shows as typed. If rendering ever throws, the raw text is
57-// shown instead.
58-export default function Markdown({ text }: { text: string }) {
68+// shown instead. Messages containing images get a header to collapse them; the
69+// header lines up with the username column.
70+export default function Markdown({
71+ text,
72+ collapsed,
73+ onToggle,
74+}: {
75+ text: string;
76+ collapsed: boolean;
77+ onToggle: () => void;
78+}) {
79+ const count = (text.match(/!\[[^\]]*\]\([^)]+\)/g) ?? []).length;
5980 return (
6081 <Fallback text={text}>
61- <ReactMarkdown remarkPlugins={[remarkGfm, remarkBreaks]} components={components}>
62- {text}
63- </ReactMarkdown>
82+ {count > 0 && (
83+ <button
84+ type="button"
85+ onClick={onToggle}
86+ title={collapsed ? "show images" : "hide images"}
87+ className="flex w-full items-center gap-1 text-xs leading-6 text-sepia/50 hover:text-sepia/80"
88+ >
89+ {collapsed ? <ChevronRight className="size-3" /> : <ChevronDown className="size-3" />}
90+ {count} image{count > 1 ? "s" : ""}
91+ </button>
92+ )}
93+ <Collapsed.Provider value={collapsed}>
94+ <ReactMarkdown remarkPlugins={[remarkGfm, remarkBreaks]} components={components}>
95+ {text}
96+ </ReactMarkdown>
97+ </Collapsed.Provider>
6498 </Fallback>
6599 );
66100 }