1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Search page — FTS5 full-text search across sessions
use leptos::prelude::*;
use serde::{Deserialize, Serialize};
const API_BASE_URL: &str = "";
// ─── API types ────────────────────────────────────────────────────────────────
#[derive(Debug, Clone, Serialize, Deserialize)]
struct SearchResultItem {
session_id: String,
project: Option<String>,
first_user_message: Option<String>,
snippet: Option<String>,
rank: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct SearchResponse {
results: Vec<SearchResultItem>,
total: usize,
query: String,
}
// ─── Fetch helper ─────────────────────────────────────────────────────────────
async fn fetch_search(query: String, limit: usize) -> Result<SearchResponse, String> {
let encoded = js_sys::encode_uri_component(&query);
let url = format!("{}/api/search?q={}&limit={}", API_BASE_URL, encoded, limit);
let response = gloo_net::http::Request::get(&url)
.send()
.await
.map_err(|e| format!("Network error: {}", e))?;
if !response.ok() {
return Err(format!("HTTP error: {}", response.status()));
}
let resp: SearchResponse = response
.json()
.await
.map_err(|e| format!("Parse error: {}", e))?;
Ok(resp)
}
// ─── Main page component ──────────────────────────────────────────────────────
/// Search page with FTS5 full-text search
#[component]
pub fn SearchPage() -> impl IntoView {
let query = RwSignal::new(String::new());
let submitted_query: RwSignal<Option<String>> = RwSignal::new(None);
// Reactive resource — only fetches when submitted_query is Some (user pressed Enter/button)
let search_resource = LocalResource::new(move || {
let q = submitted_query.get();
async move {
match q {
Some(q) if q.trim().len() >= 2 => fetch_search(q, 50).await,
Some(_) => Err("Query must be at least 2 characters".to_string()),
None => Ok(SearchResponse {
results: Vec::new(),
total: 0,
query: String::new(),
}),
}
}
});
let do_search = move || {
let q = query.get();
if q.trim().len() >= 2 {
submitted_query.set(Some(q));
}
};
view! {
<div class="page">
<div class="page-header">
<h1 class="page-title">"Search Sessions"</h1>
<p class="page-subtitle">"Full-text search across all session messages"</p>
</div>
// Search input
<div class="card" style="margin-bottom: 1.5rem;">
<div style="display: flex; gap: 1rem; align-items: center;">
<input
type="text"
placeholder="Search sessions... (min 2 chars)"
style="flex: 1; padding: 0.75rem 1rem; background: var(--bg-tertiary); border: 1px solid var(--border); border-radius: 6px; color: var(--text-primary); font-size: 1rem;"
prop:value=query
on:input=move |ev| {
query.set(event_target_value(&ev));
}
on:keydown=move |ev| {
if ev.key() == "Enter" {
do_search();
}
}
/>
<button
style="padding: 0.75rem 1.5rem; background: var(--accent); color: white; border: none; border-radius: 6px; cursor: pointer;"
on:click=move |_| {
do_search();
}
>
"Search"
</button>
</div>
</div>
// Results
<Suspense fallback=|| view! { <div class="card" style="text-align: center; color: var(--text-muted);">"Searching..."</div> }>
{move || {
search_resource.get().map(|result| {
match result.as_ref() {
Ok(response) => {
if submitted_query.get().is_none() {
return view! {
<div class="card" style="text-align: center; color: var(--text-muted); padding: 3rem;">
"Enter a query and press Enter or click Search."
</div>
}.into_any();
}
if response.results.is_empty() {
return view! {
<div class="card" style="text-align: center; color: var(--text-muted); padding: 3rem;">
"No results found. Try searching for session content, project names, or model names."
</div>
}.into_any();
}
let results = response.results.clone();
let total = response.total;
view! {
<div>
<div style="margin-bottom: 1rem; color: var(--text-muted); font-size: 0.9rem;">
{format!("{} result(s)", total)}
</div>
<div>
{results.into_iter().map(|item| {
let snippet = item.snippet
.clone()
.or_else(|| item.first_user_message.clone())
.unwrap_or_else(|| "(no preview)".to_string());
let project = item.project.clone().unwrap_or_else(|| "unknown".to_string());
let session_id = item.session_id.clone();
let id_len = session_id.len().min(8);
let session_id_short = session_id[..id_len].to_string();
let href = format!("/sessions?id={}", session_id);
view! {
<a
href=href
style="display: block; text-decoration: none; padding: 1rem; margin-bottom: 0.5rem; background: var(--bg-secondary); border-radius: 8px; border: 1px solid var(--border);"
>
<div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
<span style="color: var(--accent); font-weight: 600;">{project}</span>
<span style="color: var(--text-muted); font-size: 0.85rem;">{session_id_short}</span>
</div>
<div style="color: var(--text-secondary); font-size: 0.9rem;">{snippet}</div>
</a>
}
}).collect::<Vec<_>>()}
</div>
</div>
}.into_any()
}
Err(e) => view! {
<div style="color: var(--error, #f87171); padding: 1rem;">
{format!("Error: {}", e)}
</div>
}.into_any(),
}
})
}}
</Suspense>
</div>
}
}