| 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 |
#![allow(clippy::needless_pass_by_value)] |
| 47 |
|
| 48 |
use quasi_declare::declare; |
| 49 |
use quasi_router::screen::Tag; |
| 50 |
use quasi_router::{Action, Node, RegionKind, Response, RouteError, Router, Screen, Slot}; |
| 51 |
|
| 52 |
use crate::commands::{SearchInput, SearchResultResponse, SearchResultsResponse}; |
| 53 |
use crate::state::AppState; |
| 54 |
|
| 55 |
#[cfg(test)] |
| 56 |
mod tests; |
| 57 |
|
| 58 |
|
| 59 |
const RESULTS: &str = "search-results"; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
fn destination(result: &SearchResultResponse) -> Option<Action> { |
| 67 |
let path = match result.result_type.as_str() { |
| 68 |
"task" => format!("/tasks/{}", result.id), |
| 69 |
"project" => format!("/projects/{}", result.id), |
| 70 |
"event" => format!("/events/{}", result.id), |
| 71 |
"contact" => format!("/contacts/{}", result.id), |
| 72 |
"email" => format!("/emails/{}", result.id), |
| 73 |
_ => return None, |
| 74 |
}; |
| 75 |
Some(Action::get(path)) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
fn kind_label(result_type: &str) -> &str { |
| 80 |
match result_type { |
| 81 |
"task" => "Task", |
| 82 |
"project" => "Project", |
| 83 |
"event" => "Event", |
| 84 |
"contact" => "Contact", |
| 85 |
"email" => "Email", |
| 86 |
other => other, |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
fn has_snippet(result: &SearchResultResponse) -> bool { |
| 92 |
!snippet(result).is_empty() |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
fn snippet(result: &SearchResultResponse) -> &str { |
| 97 |
result.snippet.as_deref().unwrap_or_default() |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
fn has_project(result: &SearchResultResponse) -> bool { |
| 102 |
!project(result).is_empty() |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
fn project(result: &SearchResultResponse) -> &str { |
| 107 |
result.project_name.as_deref().unwrap_or_default() |
| 108 |
} |
| 109 |
|
| 110 |
declare! { |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
shape row_for(result: &SearchResultResponse) -> Row; |
| 121 |
|
| 122 |
row &result.title { |
| 123 |
secondary snippet(result) when has_snippet(result); |
| 124 |
meta project(result) when has_project(result); |
| 125 |
token Tag::badge(kind_label(&result.result_type)); |
| 126 |
for opens in destination(result).into_iter() { |
| 127 |
activate to doing opens; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
fn text<'a>(params: &'a quasi_router::Params, name: &str) -> Option<&'a str> { |
| 135 |
params.get(name).map(str::trim).filter(|v| !v.is_empty()) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
fn look(state: &AppState, query: &str) -> Result<SearchResultsResponse, RouteError> { |
| 140 |
crate::commands::run_search( |
| 141 |
state, |
| 142 |
SearchInput { |
| 143 |
query: query.to_owned(), |
| 144 |
result_type: None, |
| 145 |
project_id: None, |
| 146 |
date_from: None, |
| 147 |
date_to: None, |
| 148 |
limit: None, |
| 149 |
offset: None, |
| 150 |
}, |
| 151 |
) |
| 152 |
.map_err(|error| RouteError::internal(error.to_string())) |
| 153 |
} |
| 154 |
|
| 155 |
declare! { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
shape grammar() -> Vec<Node>; |
| 162 |
|
| 163 |
text "Type to search across tasks, emails, events, projects and contacts. \ |
| 164 |
Words narrow by text; a colon filter narrows by fact."; |
| 165 |
text "is:overdue is:today is:tomorrow is:thisweek is:snoozed"; |
| 166 |
text "is:pending is:started is:completed is:waiting"; |
| 167 |
text "priority:high priority:medium priority:low"; |
| 168 |
text "type:task type:email type:event type:project type:contact"; |
| 169 |
text "in:ProjectName tag:name -tag:name"; |
| 170 |
text "after:2026-08-01 before:2026-09-01"; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
fn tally(found: &SearchResultsResponse) -> String { |
| 175 |
match found.total { |
| 176 |
1 => "1 result".to_owned(), |
| 177 |
total => format!("{total} results"), |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
fn asked(found: Option<&SearchResultsResponse>) -> bool { |
| 187 |
found.is_some() |
| 188 |
} |
| 189 |
|
| 190 |
declare! { |
| 191 |
|
| 192 |
shape results(found: Option<&SearchResultsResponse>) -> Slot; |
| 193 |
|
| 194 |
region RESULTS as Pane { |
| 195 |
for node in grammar() { |
| 196 |
include node unless asked(found); |
| 197 |
} |
| 198 |
|
| 199 |
for hit in found.into_iter() { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
for filter in hit.active_filters.iter() { |
| 205 |
badge filter; |
| 206 |
} |
| 207 |
|
| 208 |
empty "Nothing matches that." when hit.results.is_empty(); |
| 209 |
|
| 210 |
text tally(hit) unless hit.results.is_empty(); |
| 211 |
list { |
| 212 |
for result in hit.results.iter() { |
| 213 |
include row_for(result); |
| 214 |
} |
| 215 |
} unless hit.results.is_empty(); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
fn typed(query: Option<&str>) -> &str { |
| 222 |
query.unwrap_or_default() |
| 223 |
} |
| 224 |
|
| 225 |
declare! { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
shape box_for(query: Option<&str>) -> Field; |
| 232 |
|
| 233 |
field Text "q" "Search" { |
| 234 |
consults Action::get("/search/results").replacing(RESULTS); |
| 235 |
placeholder "invoice is:overdue in:Ledger"; |
| 236 |
value typed(query); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
fn index(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 242 |
let query = text(&request.carried, "q"); |
| 243 |
|
| 244 |
let band = Slot::new("search-band", RegionKind::Band) |
| 245 |
.with(Node::page("Search")) |
| 246 |
.with(Node::Field(Box::new(box_for(query)))); |
| 247 |
|
| 248 |
let found = query.map(|query| look(state, query)).transpose()?; |
| 249 |
|
| 250 |
Ok(Screen::list_detail("Search", false) |
| 251 |
.at_place(super::shell::SEARCH) |
| 252 |
.with(band) |
| 253 |
.with(results(found.as_ref())) |
| 254 |
.into()) |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
fn results_only(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 259 |
let query = text(&request.carried, "q"); |
| 260 |
let found = query.map(|query| look(state, query)).transpose()?; |
| 261 |
let slot = results(found.as_ref()); |
| 262 |
Ok(Response::fragment(RESULTS, Node::Region(slot))) |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
#[must_use] |
| 267 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 268 |
router |
| 269 |
.get("/search", index) |
| 270 |
.get("/search/results", results_only) |
| 271 |
} |
| 272 |
|