feat: history/favorites panel, image preview, context strip

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:50:53 +01:00
1 parent 6360c75ff8
commit 7116af9434
9 files changed
+930 -79

No files matched your search

+93
View File
@@ -141,3 +141,96 @@ function freshId(list, now) {
while (taken.has(id)) id = `f${now}-${(n += 1)}`;
return id;
}
// --- what the panel shows ---------------------------------------------------
//
// The panel (panel.js) only draws. What it draws - which rows, which part of a
// row is lit, the line under the text, which empty state - is decided here, so
// it is tested in plain Node.
/**
* A text cut into the parts that match the query and the parts that do not.
*
* @param {string} text
* @param {[number, number][]} ranges - as `search` returns them
* @returns {{text:string, hit:boolean}[]}
*/
export function segments(text, ranges) {
const source = String(text ?? "");
const out = [];
let at = 0;
for (const [start, end] of Array.isArray(ranges) ? ranges : []) {
const from = Math.max(at, Math.min(start, source.length));
const to = Math.min(end, source.length);
if (to <= from) continue;
if (from > at) out.push({ text: source.slice(at, from), hit: false });
out.push({ text: source.slice(from, to), hit: true });
at = to;
}
if (at < source.length) out.push({ text: source.slice(at), hit: false });
return out;
}
/**
* A moment as `DD.MM HH:MM`, in the time of this computer.
*
* @param {number} ms
* @returns {string}
*/
export function formatMoment(ms) {
if (!Number.isFinite(ms)) return "";
const date = new Date(ms);
const two = (n) => String(n).padStart(2, "0");
return `${two(date.getDate())}.${two(date.getMonth() + 1)} ${two(date.getHours())}:${two(date.getMinutes())}`;
}
/**
* The line under the text of a row: when, in which space, to which agent - or,
* for a favourite, when it was saved.
*
* @param {HistoryEntry|Favorite} item
* @param {"history"|"favorites"} tab
* @returns {string}
*/
export function entryMeta(item, tab) {
if (tab === "favorites") return `zapisano ${formatMoment(item.createdAt)}`;
const space = item.spaceName || "?";
const agent = item.tabName || item.agentName || "?";
return `${formatMoment(item.sentAt)} · ${space} · ${agent}`;
}
/**
* Everything the panel draws, for one tab and one query.
*
* `empty` says which empty state to show: the whole list is empty
* (`"history"` / `"favorites"`), or only the query found nothing (`"query"`).
*
* @param {{history:HistoryEntry[], favorites:Favorite[], tab:"history"|"favorites", query:string}} input
* @returns {{tab:string, counts:{history:number, favorites:number}, hits:Hit[], empty:null|"history"|"favorites"|"query"}}
*/
export function panelState({ history, favorites, tab, query }) {
const source = tab === "favorites" ? favorites : history;
const hits = search(source, query);
let empty = null;
if (!source || source.length === 0) empty = tab === "favorites" ? "favorites" : "history";
else if (hits.length === 0) empty = "query";
return { tab, counts: { history: history.length, favorites: favorites.length }, hits, empty };
}
/**
* The row after a step up or down - past the end it comes round.
*
* @param {number} index
* @param {number} step
* @param {number} count
* @returns {number}
*/
export function moveIndex(index, step, count) {
if (count <= 0) return 0;
return (index + step + count) % count;
}
/** The other tab. */
export function otherTab(tab) {
return tab === "history" ? "favorites" : "history";
}