feat(library): search field over titles and bodies (#144)
CI / ci (push) Canceled after 0s

PostsLibraryColumn gains the live search under its PaneHeader: plain
text field + clear button + bottom hairline; searching replaces the
sectioned library with a flat ranked list (title hits first via
LibrarySearch), nil matches = in-flight ProgressView, empty = the
'No matches' state naming the query; clearing restores sections.
Scan is debounced 250 ms by .task(id: query) (Task(id:) cancels the
in-flight wave on every keystroke — one scan per settled query) and
runs off-main through Task.detached over PostStore.readBody
(rescan's posture; frontmatter never searched). State is per-view —
both library surfaces get the field from the one shared column.
This commit is contained in:
AlexKucera committed 2026-11-03 04:11:14 +01:00
1 parent 7e75d53d79
commit 105814cfb2
1 file changed
+106 -1
+106 -1
View File
@@ -205,6 +205,14 @@ struct PostsLibraryColumn: View {
/// #108: collapsed project sections — per-view state, not persisted (a
/// fresh open shows everything; the prototype behaved the same).
@State private var collapsed: Set<String> = []
/// #144 (FEAT-004): the library search — live title+body filter over
/// the non-archived pieces. nil = not searching (sections render).
@State private var query = ""
@State private var matches: [Post]?
private var isSearching: Bool {
!query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
/// Non-archived pieces keyed by project (scan order = title order holds
/// inside each group — filter preserves it).
@@ -225,7 +233,19 @@ struct PostsLibraryColumn: View {
.help("New Post")
.accessibilityLabel("New Post")
}
if activeByProject.isEmpty {
searchField
if isSearching {
if let matches {
if matches.isEmpty {
searchEmptyState
} else {
searchResults(matches)
}
} else {
ProgressView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
} else if activeByProject.isEmpty {
emptyState
} else {
ScrollView {
@@ -292,6 +312,91 @@ struct PostsLibraryColumn: View {
model.moveToProject(post, project: project, home: home)
}
}
// #144 (FEAT-004): keystroke-debounced, off-main body scan — Task(id:)
// restarts (and cancels) the in-flight wave on every query change, so
// fast typing coalesces into ONE scan per settled query; disk I/O
// never touches MainActor (rescan's posture).
.task(id: query) {
let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else {
matches = nil
return
}
try? await Task.sleep(for: .milliseconds(250))
guard !Task.isCancelled else { return }
let home = home
let candidates = model.posts.filter { !$0.isArchived }
let found = await Task.detached(priority: .userInitiated) {
LibrarySearch.results(for: trimmed, in: candidates) {
PostStore.readBody(id: $0.id, home: home)
}
}.value
guard !Task.isCancelled else { return }
matches = found
}
}
/// #144 (FEAT-004): the field row + bottom hairline (the pane-head voice).
private var searchField: some View {
HStack(spacing: 6) {
Image(systemName: "magnifyingglass")
.font(.system(size: 11))
.foregroundStyle(DesignTokens.muted)
TextField("Search posts", text: $query)
.textFieldStyle(.plain)
.font(.system(size: 12))
if isSearching {
Button {
query = ""
} label: {
Image(systemName: "xmark.circle.fill")
}
.buttonStyle(.plain)
.foregroundStyle(DesignTokens.muted)
.accessibilityLabel("Clear search")
}
}
.padding(.horizontal, 12)
.padding(.vertical, 7)
.overlay(alignment: .bottom) {
Rectangle().fill(DesignTokens.border).frame(height: 1)
}
}
/// Flat results, title matches first (stable partition — both halves
/// keep the scan's title order). Same row the sections render.
private func searchResults(_ found: [Post]) -> some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 4) {
Text("\(found.count) \(found.count == 1 ? "match" : "matches")")
.font(.system(size: 10, design: DesignTokens.mono))
.kerning(0.6)
.foregroundStyle(DesignTokens.muted.opacity(0.8))
.padding(.horizontal, 14)
.padding(.top, 12)
.padding(.bottom, 5)
ForEach(found) { post in
LibraryPostRow(post: post, selected: post.id == model.selectedPostID) {
model.select(post.id, home: home)
}
}
}
.padding(.bottom, 12)
}
}
/// #144 (FEAT-004): honest no-hit state — names the query, suggests scope.
private var searchEmptyState: some View {
VStack(spacing: 8) {
Text("No matches")
.font(.system(size: 16, weight: .semibold, design: DesignTokens.serif))
Text("No piece's title or body contains “\(query.trimmingCharacters(in: .whitespacesAndNewlines))”.")
.font(.callout)
.foregroundStyle(DesignTokens.muted)
.multilineTextAlignment(.center)
}
.padding(38)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
/// Pass-State groups inside one project (`.vB .group-label`: smaller,