Skip to content

Commit cf23350

Browse files
committed
fix(es-app): make sidebar title search responsive (anomalyco#136)
1 parent cdcd1e0 commit cf23350

2 files changed

Lines changed: 109 additions & 7 deletions

File tree

‎packages/es-app/src/components/sidebar.tsx‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,24 @@ export default function Sidebar() {
4949
markViewed(item.session, true)
5050
}
5151

52-
// transcript search: ≥3 chars, debounced; null results = search inactive
52+
// Titles filter immediately; transcript search starts at three characters.
5353
const [query, setQuery] = createSignal("")
5454
const [results, setResults] = createSignal<SessionSearchResult[] | null>(null)
55-
const searchResults = () => (results() ?? []).filter((s) => !allProjects() || !s.parent_id)
55+
const searchResults = () => {
56+
const titleMatches = sessionRows().filter((s) => s.title.toLocaleLowerCase().includes(query().trim().toLocaleLowerCase()))
57+
.map((s) => ({ id: s.id, title: s.title, directory: s.directory, parent_id: null,
58+
updated: s.updated, snippet: "", matches: 0 }))
59+
const root = editspaces()?.editspaces.find((s) => s.name === current())?.root.replace(/\/\.editspace\/?$/, "")
60+
const rows = sessionRows()
61+
const directories = new Set(rows.map((s) => s.directory))
62+
const remote = (results() ?? []).filter((s) =>
63+
(!allProjects() || !s.parent_id) && (allProjects() || directories.has(s.directory) ||
64+
(s.parent_id !== null && rows.some((row) => row.id === s.parent_id)) ||
65+
(root !== undefined && (s.directory === root || s.directory.startsWith(`${root}/`)))))
66+
const byID = new Map<string, SessionSearchResult>(titleMatches.map((s) => [s.id, s]))
67+
remote.forEach((s) => byID.set(s.id, s))
68+
return [...byID.values()]
69+
}
5670
const [searching, setSearching] = createSignal(false)
5771
const [allTime, setAllTime] = createSignal(false)
5872
const [searchError, setSearchError] = createSignal("")
@@ -70,7 +84,7 @@ export default function Sidebar() {
7084
} catch {
7185
if (seq !== searchSeq) return
7286
setResults([])
73-
setSearchError("search unavailable — es-dashboard too old?")
87+
setSearchError("transcript search unavailable; showing matching titles")
7488
} finally {
7589
if (seq === searchSeq) setSearching(false)
7690
}
@@ -81,8 +95,9 @@ export default function Sidebar() {
8195
setAllTime(false)
8296
clearTimeout(searchTimer)
8397
searchSeq++ // invalidate in-flight responses
98+
setResults(null)
99+
setSearchError("")
84100
if (v.trim().length < 3) {
85-
setResults(null)
86101
setSearching(false)
87102
return
88103
}
@@ -220,20 +235,21 @@ export default function Sidebar() {
220235
/>
221236
</div>
222237
<Show
223-
when={results() === null}
238+
when={!query().trim()}
224239
fallback={
225240
<>
226241
<div class="nav-heading">
227-
results{searching() ? " ·" : ` (${searchResults().length})`}
242+
results ({searchResults().length})
228243
</div>
244+
<Show when={searching()}><div class="dim nav-empty">searching transcripts…</div></Show>
229245
<Show when={searchError()}>
230246
<div class="dim nav-empty">{searchError()}</div>
231247
</Show>
232248
<Show when={!searching() && !searchError() && !searchResults().length}>
233249
<div class="dim nav-empty">no matches</div>
234250
</Show>
235251
<For each={searchResults()}>{resultItem}</For>
236-
<Show when={!allTime() && !searching() && !searchError()}>
252+
<Show when={query().trim().length >= 3 && !allTime() && !searching() && !searchError()}>
237253
<button class="archived-toggle" onClick={searchAllTime}>
238254
⌕ all time (slow)
239255
</button>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { createRequire } from "node:module"
2+
import { mkdtemp, rm } from "node:fs/promises"
3+
import { tmpdir } from "node:os"
4+
import path from "node:path"
5+
import { startPanelPreview } from "./composer-panel-preview.mjs"
6+
import { directory, session } from "./composer-fixture.mjs"
7+
8+
const { chromium, _electron, expect } = createRequire(new URL("../../app/package.json", import.meta.url))("@playwright/test")
9+
const preview = await startPanelPreview()
10+
preview.fixture.archived.add("ses_b")
11+
preview.fixture.workspaceSessions.push({ ...session("ses_external"), title: "Other project title", directory: "/another/project" })
12+
const profile = process.env.ES_DESKTOP_EXECUTABLE ? await mkdtemp(path.join(tmpdir(), "sidebar-search-")) : undefined
13+
const app = profile ? await _electron.launch({ executablePath: process.env.ES_DESKTOP_EXECUTABLE,
14+
env: { ...process.env, ELECTRON_RUN_AS_NODE: "", ES_DESKTOP_USER_DATA: profile, ES_DESKTOP_RENDERER: "",
15+
OPENCODE_URL: preview.fixture.url, ES_DASHBOARD_URL: preview.fixture.url, OPENCODE_SERVER_PASSWORD: "" } }) : undefined
16+
const browser = app ? undefined : await chromium.launch({ channel: "chrome", headless: true })
17+
const page = app ? await app.firstWindow() : await browser.newPage()
18+
if (app) {
19+
await page.waitForURL("http://127.0.0.1:*/")
20+
preview.origin = new URL(page.url()).origin
21+
preview.url = `${preview.origin}/session/ses_a?directory=${encodeURIComponent(directory)}`
22+
}
23+
const projectState = await (await fetch(`${preview.fixture.url}/api/state`)).json()
24+
projectState.threads = projectState.threads.filter(thread => thread.key !== "ses_external")
25+
await page.route("**/es/api/state?*", route => route.fulfill({ json: projectState }))
26+
const replies = []
27+
const searches = []
28+
await page.route("**/es/api/search?*", route => {
29+
searches.push(new URL(route.request().url()).searchParams.get("q"))
30+
replies.push(route)
31+
})
32+
const result = (id, title, dir = directory, parent_id = null) => ({ id, title, directory: dir, parent_id, updated: 2, snippet: "transcript", matches: 1 })
33+
const rows = page.locator(".sidebar .search-result")
34+
const input = page.locator(".sidebar .search-input")
35+
try {
36+
await page.goto(preview.url)
37+
await expect(page.locator(".sidebar .nav-item", { hasText: "Composer fixture" })).toHaveCount(1)
38+
await input.fill("Com")
39+
await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(1, { timeout: 250 })
40+
await expect(page.locator(".sidebar .nav-item", { hasText: "Other session" })).toHaveCount(0)
41+
await expect(page.locator(".sidebar .nav-empty", { hasText: "searching transcripts" })).toHaveCount(1)
42+
await expect.poll(() => replies.length).toBe(1)
43+
await input.fill("Oth")
44+
await expect(rows.filter({ hasText: "Other session" })).toHaveCount(1)
45+
await replies.shift().fulfill({ json: { results: [result("ses_a", "Composer fixture")] } })
46+
await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(0)
47+
await expect.poll(() => replies.length).toBe(1)
48+
await replies.shift().fulfill({ json: { results: [result("ses_b", "Other session"), result("ses_x", "Transcript hit"), result("ses_child", "Child hit", directory, "ses_a"), result("ses_else", "Wrong project", "/another/project")] } })
49+
await expect(rows).toHaveCount(3)
50+
await expect(rows.filter({ hasText: "Transcript hit" })).toHaveCount(1)
51+
await expect(rows.filter({ hasText: "Child hit" })).toHaveCount(1)
52+
await expect(rows.filter({ hasText: "Wrong project" })).toHaveCount(0)
53+
await rows.filter({ hasText: "Other session" }).click()
54+
await expect(page).toHaveURL(/\/session\/ses_b\?/)
55+
expect(new URL(page.url()).searchParams.get("directory")).toBe(directory)
56+
await input.fill("Co")
57+
await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(1)
58+
await expect(page.locator(".sidebar .nav-empty", { hasText: "searching transcripts" })).toHaveCount(0)
59+
await input.fill("Com")
60+
await expect.poll(() => replies.length).toBe(1)
61+
await replies.shift().fulfill({ status: 503, json: { error: "unavailable" } })
62+
await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(1)
63+
await expect(page.locator(".sidebar .nav-empty", { hasText: "transcript search unavailable" })).toHaveCount(1)
64+
await page.locator(".es-switcher").selectOption("__all__")
65+
await input.fill("project title")
66+
await expect(rows.filter({ hasText: "Other project title" })).toHaveCount(1)
67+
await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(0)
68+
await input.fill("Chi")
69+
await expect.poll(() => replies.length).toBe(1)
70+
await replies.shift().fulfill({ json: { results: [result("ses_child", "Child hit", directory, "ses_a"), result("ses_x", "Transcript hit")] } })
71+
await expect(rows.filter({ hasText: "Child hit" })).toHaveCount(0)
72+
await expect(rows.filter({ hasText: "Transcript hit" })).toHaveCount(1)
73+
await input.press("Escape")
74+
await expect(rows).toHaveCount(0)
75+
await expect(page.locator(".sidebar .nav-item", { hasText: "Other session" })).toHaveCount(0)
76+
await page.locator(".sidebar .archived-toggle", { hasText: "archived" }).click()
77+
await expect(page.locator(".sidebar .nav-item.archived", { hasText: "Other session" })).toHaveCount(1)
78+
expect(searches).toEqual(["Com", "Oth", "Com", "Chi"])
79+
expect(preview.fixture.calls.filter(call => !["GET", "HEAD", "OPTIONS"].includes(call.method))).toEqual([])
80+
console.log(`PASS sidebar search ${app ? "native" : "browser"}: immediate titles, pending, stale result, transcript, scope, error, clear; zero mutations`)
81+
} finally {
82+
await app?.close()
83+
await browser?.close()
84+
await preview.close()
85+
if (profile) await rm(profile, { recursive: true, force: true })
86+
}

0 commit comments

Comments
 (0)