Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

31l long-log-folds: a long log in a reply folds to 16 lines (#924)

Oleksandr Ivanchenko00e97c39d5d4
diff --git a/frontend/src/components/MarkdownRenderer.tsx b/frontend/src/components/MarkdownRenderer.tsx
index c9d84ca4..93db1953 100644
--- a/frontend/src/components/MarkdownRenderer.tsx
+++ b/frontend/src/components/MarkdownRenderer.tsx
@@ -1,7 +1,7 @@
-import React, { useState } from 'react';
+import React, { useLayoutEffect, useRef, useState } from 'react';
import ReactMarkdown, { defaultUrlTransform } from 'react-markdown';
import remarkGfm from 'remark-gfm';
-import { Copy, Check, Terminal } from 'lucide-react';
+import { Copy, Check, ChevronDown, Terminal } from 'lucide-react';
import { playSfx } from '../audio/chiptune';
import { HastNode, blockAt, hastText, rehypeSmartIndex, rehypeSmartSection, resultBlocks, sectionBlock } from '../smartResults';
import type { ReplyPart, ReplySource } from './results/registry';
@@ -19,8 +19,23 @@ interface CodeBlockProps {
value: string;
}
+/** A block over FOLD_AFTER lines shows its first 16 (max-h-80: 320 px at text-xs leading-relaxed) until opened. */
+const FOLD_AFTER = 20;
+
const CodeBlock: React.FC<CodeBlockProps> = ({ language, value }) => {
const [copied, setCopied] = useState(false);
+ const [open, setOpen] = useState(false);
+ const lineCount = value.split('\n').length;
+ const long = lineCount > FOLD_AFTER;
+ const folded = long && !open;
+ // Folding a log you read to its end removes ~1,000 px above the toggle; bring the toggle back into view.
+ const toggleRef = useRef<HTMLButtonElement>(null);
+ const folding = useRef(false);
+ useLayoutEffect(() => {
+ if (!folding.current) return;
+ folding.current = false;
+ toggleRef.current?.scrollIntoView?.({ block: 'nearest' });
+ }, [open]);
const handleCopy = () => {
navigator.clipboard.writeText(value);
@@ -55,9 +70,29 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ language, value }) => {
)}
</button>
</div>
- <pre className="p-3.5 overflow-x-auto text-xs font-mono text-zinc-200 leading-relaxed">
- <code>{value}</code>
- </pre>
+ <div className="relative">
+ <pre className={`p-3.5 overflow-x-auto text-xs font-mono text-zinc-200 leading-relaxed${folded ? ' max-h-80 overflow-y-hidden' : ''}`}>
+ <code>{value}</code>
+ </pre>
+ {folded && (
+ <div aria-hidden="true" className="pointer-events-none absolute inset-x-0 bottom-0 h-10 bg-gradient-to-t from-zinc-950" />
+ )}
+ </div>
+ {long && (
+ <button
+ type="button"
+ ref={toggleRef}
+ aria-expanded={open}
+ onClick={() => {
+ folding.current = open;
+ setOpen(!open);
+ }}
+ className="flex w-full min-h-[44px] sm:min-h-[32px] items-center justify-center gap-1.5 border-t border-zinc-800 text-[11px] font-mono text-zinc-300 hover:bg-zinc-900 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-sky-400"
+ >
+ <ChevronDown size={12} aria-hidden="true" className={open ? 'rotate-180' : undefined} />
+ {open ? 'Show less' : `Show all ${lineCount} lines`}
+ </button>
+ )}
</div>
);
};
diff --git a/frontend/tests/codeBlockFold.test.mjs b/frontend/tests/codeBlockFold.test.mjs
new file mode 100644
index 00000000..5508bf45
--- /dev/null
+++ b/frontend/tests/codeBlockFold.test.mjs
@@ -0,0 +1,124 @@
+// 31l long-log-folds: a code or log block over 20 lines folds to 16 lines with one "Show all N lines" /
+// "Show less" toggle under it. Copy still copies every line; blocks of 20 lines or fewer render as before.
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { JSDOM } from 'jsdom';
+import { loadBundled } from './bundle.mjs';
+
+const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost/', pretendToBeVisual: true });
+const g = globalThis;
+for (const k of ['window', 'document', 'navigator', 'HTMLElement', 'Element', 'Node', 'Event', 'MouseEvent', 'requestAnimationFrame', 'cancelAnimationFrame', 'getComputedStyle', 'localStorage', 'sessionStorage']) {
+ if (!(k in dom.window)) continue;
+ Object.defineProperty(g, k, { value: dom.window[k], configurable: true, writable: true });
+}
+g.IS_REACT_ACT_ENVIRONMENT = false;
+const copied = [];
+Object.defineProperty(dom.window.navigator, 'clipboard', { value: { writeText: async (t) => void copied.push(t) }, configurable: true });
+
+const m = await loadBundled(`
+ export { default as React } from 'react';
+ export { createRoot } from 'react-dom/client';
+ export { flushSync } from 'react-dom';
+ export { MarkdownRenderer } from './src/components/MarkdownRenderer';
+`);
+const { React, createRoot, flushSync, MarkdownRenderer } = m;
+const h = React.createElement;
+
+const lines = (n, label = 'PASS test') => Array.from({ length: n }, (_, i) => `${label} ${i + 1}`).join('\n');
+const fence = (body, lang = 'text') => `Fixed it. Now all 418 pass.\n\n\`\`\`${lang}\n${body}\n\`\`\`\n\nAfter the log.\n`;
+
+const draw = (content) => {
+ const el = document.createElement('div');
+ document.body.appendChild(el);
+ const root = createRoot(el);
+ flushSync(() => root.render(h(MarkdownRenderer, { content })));
+ return { el, root, done: () => { root.unmount(); el.remove(); } };
+};
+const scrolled = [];
+dom.window.HTMLElement.prototype.scrollIntoView = function (opts) { scrolled.push({ el: this, opts }); };
+const toggleOf = (el) => [...el.querySelectorAll('button')].filter((b) => b.hasAttribute('aria-expanded'));
+const click = (b) => flushSync(() => b.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })));
+
+test('a 69-line log folds to 16 lines with "Show all 69 lines", and opens to the full log with "Show less"', () => {
+ const body = lines(69);
+ const { el, done } = draw(fence(body));
+ try {
+ const pre = el.querySelector('pre');
+ assert.equal(pre.querySelector('code').textContent, body, 'all 69 lines are in the DOM (folded by height only)');
+ assert.match(pre.className, /\bmax-h-80\b/, 'folded to 320 px = 16 lines');
+ assert.match(pre.className, /\boverflow-y-hidden\b/);
+ const fade = pre.parentElement.querySelector('[aria-hidden="true"]');
+ assert.ok(fade, 'a decorative fade sits at the cut');
+ const [btn, ...more] = toggleOf(el);
+ assert.ok(btn, 'one toggle under the block');
+ assert.equal(more.length, 0, 'only one toggle');
+ assert.equal(btn.getAttribute('type'), 'button');
+ assert.equal(btn.getAttribute('aria-expanded'), 'false');
+ assert.equal(btn.textContent.trim(), 'Show all 69 lines');
+
+ click(btn);
+ assert.equal(btn.getAttribute('aria-expanded'), 'true');
+ assert.equal(btn.textContent.trim(), 'Show less');
+ assert.doesNotMatch(el.querySelector('pre').className, /max-h-80|overflow-y-hidden/, 'open: full height');
+ assert.equal(el.querySelector('pre').parentElement.querySelector('[aria-hidden="true"]'), null, 'no fade when open');
+
+ click(btn);
+ assert.equal(btn.getAttribute('aria-expanded'), 'false');
+ assert.equal(btn.textContent.trim(), 'Show all 69 lines');
+ assert.match(el.querySelector('pre').className, /\bmax-h-80\b/);
+ } finally {
+ done();
+ }
+});
+
+test('Copy on a folded 69-line log copies all 69 lines', () => {
+ copied.length = 0;
+ const body = lines(69);
+ const { el, done } = draw(fence(body));
+ try {
+ const copy = [...el.querySelectorAll('button')].find((b) => b.getAttribute('title') === 'Copy code');
+ click(copy);
+ assert.equal(copied.length, 1);
+ assert.equal(copied[0], body);
+ assert.equal(copied[0].split('\n').length, 69);
+ } finally {
+ done();
+ }
+});
+
+test('a 9-line diff and a 20-line block have no toggle and no fold; 21 lines folds', () => {
+ for (const [n, lang] of [[9, 'diff'], [20, 'text'], [20, '']]) {
+ const { el, done } = draw(fence(lines(n, '+ line'), lang));
+ try {
+ assert.equal(toggleOf(el).length, 0, `${n} lines (${lang || 'no language'}): no toggle`);
+ assert.doesNotMatch(el.querySelector('pre').className, /max-h-80|overflow-y-hidden/);
+ assert.equal(el.querySelector('pre').parentElement.querySelector('[aria-hidden="true"]'), null);
+ } finally {
+ done();
+ }
+ }
+ const { el, done } = draw(fence(lines(21)));
+ try {
+ assert.equal(toggleOf(el)[0]?.textContent.trim(), 'Show all 21 lines');
+ } finally {
+ done();
+ }
+});
+
+test('"Show less" brings the toggle back into view (nearest); "Show all" does not scroll', () => {
+ const { el, done } = draw(fence(lines(69)));
+ try {
+ const [btn] = toggleOf(el);
+ scrolled.length = 0;
+ click(btn);
+ assert.equal(btn.getAttribute('aria-expanded'), 'true');
+ assert.equal(scrolled.length, 0, 'opening does not scroll');
+ click(btn);
+ assert.equal(btn.getAttribute('aria-expanded'), 'false');
+ assert.equal(scrolled.length, 1, 'folding scrolls once');
+ assert.equal(scrolled[0].el, btn);
+ assert.deepEqual(scrolled[0].opts, { block: 'nearest' });
+ } finally {
+ done();
+ }
+});