| 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 |
use makeover_layout::FieldKind; |
| 53 |
use quasi_router::{Action, Candidate, Consult, Field, Node}; |
| 54 |
|
| 55 |
use super::discover_typeahead::FILTERS; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
pub const FIELD: &str = "q"; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
const RESULTS: &str = "results-container"; |
| 72 |
|
| 73 |
|
| 74 |
const RESULTS_ROUTE: &str = "/discover/results"; |
| 75 |
|
| 76 |
|
| 77 |
const SUGGEST_ROUTE: &str = "/discover/suggestions"; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
const SUGGEST_WAIT: std::time::Duration = std::time::Duration::from_millis(200); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
const RESULTS_WAIT: std::time::Duration = std::time::Duration::from_millis(150); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
const FLOOR: usize = 2; |
| 97 |
|
| 98 |
|
| 99 |
#[derive(Debug, Clone)] |
| 100 |
pub struct Hit { |
| 101 |
|
| 102 |
pub label: String, |
| 103 |
|
| 104 |
|
| 105 |
pub category: String, |
| 106 |
|
| 107 |
pub url: String, |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
#[must_use] |
| 119 |
pub fn search_box(mode: &str, value: &str) -> String { |
| 120 |
use quasi_axum::Serves as _; |
| 121 |
|
| 122 |
let noun = if mode == "projects" { |
| 123 |
"projects" |
| 124 |
} else { |
| 125 |
"items" |
| 126 |
}; |
| 127 |
let mut field = Field::new(FieldKind::Text, FIELD, format!("Search {noun}")) |
| 128 |
.suggesting( |
| 129 |
Consult::new(Action::get(SUGGEST_ROUTE)) |
| 130 |
.after(SUGGEST_WAIT) |
| 131 |
.at_least(FLOOR), |
| 132 |
) |
| 133 |
.consulting( |
| 134 |
Consult::new(Action::get(RESULTS_ROUTE).replacing(RESULTS)) |
| 135 |
.after(RESULTS_WAIT) |
| 136 |
.sending(FILTERS), |
| 137 |
); |
| 138 |
field.placeholder = Some(format!("Search {noun}...")); |
| 139 |
if !value.is_empty() { |
| 140 |
field = field.value(value); |
| 141 |
} |
| 142 |
|
| 143 |
quasi_webview::Webview::new().fragment(&Node::field(field)) |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#[must_use] |
| 149 |
pub fn suggestions(hits: &[Hit]) -> String { |
| 150 |
use quasi_axum::Serves as _; |
| 151 |
|
| 152 |
let candidates: Vec<Candidate> = hits.iter().map(candidate).collect(); |
| 153 |
quasi_webview::Webview::new().suggestions(FIELD, &candidates) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
fn candidate(hit: &Hit) -> Candidate { |
| 164 |
let mut candidate = |
| 165 |
Candidate::new(&hit.url, &hit.label).picking(Action::get(&hit.url).navigating()); |
| 166 |
if !hit.category.is_empty() { |
| 167 |
candidate = candidate.detailed(&hit.category); |
| 168 |
} |
| 169 |
candidate |
| 170 |
} |
| 171 |
|
| 172 |
#[cfg(test)] |
| 173 |
mod tests { |
| 174 |
use super::*; |
| 175 |
|
| 176 |
fn hit(label: &str, category: &str, url: &str) -> Hit { |
| 177 |
Hit { |
| 178 |
label: label.to_owned(), |
| 179 |
category: category.to_owned(), |
| 180 |
url: url.to_owned(), |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
#[test] |
| 187 |
fn the_box_says_where_its_list_comes_from_and_what_it_waits_for() { |
| 188 |
let html = search_box("items", ""); |
| 189 |
|
| 190 |
assert!(html.contains(r#"hx-get="/discover/suggestions""#), "{html}"); |
| 191 |
assert!(html.contains("delay:200ms"), "{html}"); |
| 192 |
assert!(html.contains("value.length>=2"), "{html}"); |
| 193 |
|
| 194 |
|
| 195 |
assert!(html.contains(r#"role="combobox""#), "{html}"); |
| 196 |
assert!(html.contains(r#"aria-controls="q-suggestions""#), "{html}"); |
| 197 |
assert!(html.contains(r#"id="q-suggestions""#), "{html}"); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
#[test] |
| 203 |
fn the_same_value_re_reads_the_results_under_the_current_filters() { |
| 204 |
let html = search_box("items", ""); |
| 205 |
|
| 206 |
assert!(html.contains(r#"hx-get="/discover/results""#), "{html}"); |
| 207 |
assert!(html.contains("delay:150ms"), "{html}"); |
| 208 |
assert!( |
| 209 |
html.contains(r##"hx-target="#results-container""##), |
| 210 |
"{html}" |
| 211 |
); |
| 212 |
for name in FILTERS { |
| 213 |
assert!(html.contains(&format!("[name='{name}']")), "{html}"); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
#[test] |
| 219 |
fn the_box_holds_the_search_it_was_drawn_under() { |
| 220 |
let html = search_box("projects", "ambient pads"); |
| 221 |
|
| 222 |
assert!(html.contains(r#"value="ambient pads""#), "{html}"); |
| 223 |
assert!(html.contains("Search projects"), "{html}"); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
#[test] |
| 231 |
fn picking_a_candidate_navigates_rather_than_swapping() { |
| 232 |
let html = suggestions(&[hit("Slow Reader", "Project", "/p/slow-reader")]); |
| 233 |
|
| 234 |
assert!(html.contains(r#"href="/p/slow-reader""#), "{html}"); |
| 235 |
assert!(!html.contains("hx-get"), "no htmx on a navigation: {html}"); |
| 236 |
assert!(!html.contains("hx-swap"), "{html}"); |
| 237 |
|
| 238 |
|
| 239 |
assert!(!html.contains("data-value"), "{html}"); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
#[test] |
| 246 |
fn the_kind_of_page_is_the_second_line() { |
| 247 |
let html = suggestions(&[hit("Slow Reader", "Creator", "/u/slowreader")]); |
| 248 |
|
| 249 |
assert!(html.contains("form-suggestion-detail"), "{html}"); |
| 250 |
assert!(html.contains(">Creator<"), "{html}"); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
#[test] |
| 255 |
fn a_title_a_creator_chose_cannot_smuggle_markup() { |
| 256 |
let html = suggestions(&[hit("<script>x()</script>", "Item", "/i/1")]); |
| 257 |
|
| 258 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 259 |
} |
| 260 |
} |
| 261 |
|