| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#![allow(dead_code)] |
| 22 |
use quasi_declare::declare; |
| 23 |
use quasi_router::Choice; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
pub(crate) struct Theme { |
| 30 |
pub id: String, |
| 31 |
pub name: String, |
| 32 |
pub selected: bool, |
| 33 |
} |
| 34 |
|
| 35 |
impl Theme { |
| 36 |
fn new(id: &str, name: &str, selected: bool) -> Self { |
| 37 |
Self { |
| 38 |
id: id.into(), |
| 39 |
name: name.into(), |
| 40 |
selected, |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
pub(crate) fn themes(marked: Option<usize>) -> Vec<Theme> { |
| 47 |
["dark", "light", "paper"] |
| 48 |
.iter() |
| 49 |
.enumerate() |
| 50 |
.map(|(index, id)| Theme::new(id, id, marked == Some(index))) |
| 51 |
.collect() |
| 52 |
} |
| 53 |
|
| 54 |
declare! { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#[staged] |
| 62 |
pub(crate) shape ranges(chosen: &str) -> Vec<Node>; |
| 63 |
|
| 64 |
for range in RANGES { |
| 65 |
chip range.to_string() to get "/analytics?range={range}" navigating { |
| 66 |
latched when is_shown(range, chosen); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
declare! { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#[staged] |
| 79 |
pub(crate) shape range_bar(chosen: &str) -> Node; |
| 80 |
|
| 81 |
region "RANGES" as Pane { |
| 82 |
include each ranges(chosen); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
pub(crate) const RANGES: &[&str] = &["7d", "30d", "90d", "all"]; |
| 88 |
|
| 89 |
|
| 90 |
pub(crate) fn is_shown(range: &str, chosen: &str) -> bool { |
| 91 |
range == chosen |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub(crate) struct Prefs { |
| 99 |
pub themes: Vec<Theme>, |
| 100 |
pub advanced: bool, |
| 101 |
} |
| 102 |
|
| 103 |
declare! { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#[staged] |
| 110 |
pub(crate) shape settings(prefs: &Prefs) -> Node; |
| 111 |
|
| 112 |
form put "/api/users/me/console" awaiting { |
| 113 |
submit "Save"; |
| 114 |
|
| 115 |
field Select "theme_id" "Console theme" { |
| 116 |
for theme in prefs.themes.iter() { |
| 117 |
option Choice::new(theme.id.clone(), theme.name.clone()) { |
| 118 |
chosen when theme.selected; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
field Text "endpoint" "Server" when prefs.advanced; |
| 124 |
|
| 125 |
field Text "note" "Note"; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
declare! { |
| 130 |
|
| 131 |
#[staged] |
| 132 |
pub(crate) shape picker(themes: &[Theme]) -> Node; |
| 133 |
|
| 134 |
form put "/api/users/me/console-theme" awaiting { |
| 135 |
submit "Save Theme"; |
| 136 |
field Select "theme_id" "Console theme" { |
| 137 |
for theme in themes.iter() { |
| 138 |
option Choice::new(theme.id.clone(), theme.name.clone()) { |
| 139 |
chosen when theme.selected; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
#[cfg(test)] |
| 147 |
mod tests { |
| 148 |
use quasi_http::Serves as _; |
| 149 |
use quasi_router::stage::{Op, Plan, Residual}; |
| 150 |
use quasi_webview::Webview; |
| 151 |
|
| 152 |
use super::*; |
| 153 |
|
| 154 |
fn residual() -> Residual { |
| 155 |
quasi_webview::stage::derive(&Webview::new(), picker_staged) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
fn prefs(advanced: bool) -> Prefs { |
| 161 |
Prefs { |
| 162 |
themes: themes(Some(1)), |
| 163 |
advanced, |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
#[test] |
| 174 |
fn a_latched_chip_is_an_arm_inside_the_loop() { |
| 175 |
let webview = Webview::new(); |
| 176 |
let residual = quasi_webview::stage::derive(&Webview::new(), range_bar_staged); |
| 177 |
for chosen in ["7d", "all", "none of them"] { |
| 178 |
assert_eq!( |
| 179 |
webview.fragment(&range_bar(chosen)), |
| 180 |
range_bar_serve(&residual, chosen), |
| 181 |
"chosen {chosen}" |
| 182 |
); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn a_guarded_field_is_a_branch_in_the_form() { |
| 188 |
let webview = Webview::new(); |
| 189 |
let residual = quasi_webview::stage::derive(&Webview::new(), settings_staged); |
| 190 |
for advanced in [false, true] { |
| 191 |
let prefs = prefs(advanced); |
| 192 |
assert_eq!( |
| 193 |
webview.fragment(&settings(&prefs)), |
| 194 |
settings_serve(&residual, &prefs), |
| 195 |
"advanced {advanced}" |
| 196 |
); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn the_marked_option_is_a_branch_inside_the_row() { |
| 202 |
let residual = residual(); |
| 203 |
|
| 204 |
fn branch_under_loop(ops: &[Op], inside: bool) -> bool { |
| 205 |
ops.iter().any(|op| match op { |
| 206 |
Op::Lit(_) | Op::Hole { .. } => false, |
| 207 |
Op::Branch(body) => inside || branch_under_loop(body, inside), |
| 208 |
Op::Arms(arms) => inside || arms.iter().any(|arm| branch_under_loop(arm, inside)), |
| 209 |
Op::Loop(body) => branch_under_loop(body, true), |
| 210 |
}) |
| 211 |
} |
| 212 |
|
| 213 |
assert!( |
| 214 |
branch_under_loop(residual.ops(), false), |
| 215 |
"{:#?}", |
| 216 |
residual.ops() |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
#[test] |
| 223 |
fn replaying_the_residual_reproduces_the_staged_render() { |
| 224 |
let webview = Webview::new(); |
| 225 |
let residual = residual(); |
| 226 |
|
| 227 |
fn replay(ops: &[Op], rows: usize, out: &mut String) { |
| 228 |
for op in ops { |
| 229 |
match op { |
| 230 |
Op::Lit(text) => out.push_str(text), |
| 231 |
Op::Hole { scope, id } => { |
| 232 |
out.push_str(&quasi_router::stage::sentinel_at(*scope, *id)); |
| 233 |
} |
| 234 |
Op::Branch(body) => replay(body, rows, out), |
| 235 |
Op::Arms(arms) => replay(&arms[0], rows, out), |
| 236 |
Op::Loop(body) => { |
| 237 |
for _ in 0..rows { |
| 238 |
replay(body, rows, out); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
for rows in [1, 2, 5] { |
| 246 |
let mut replayed = String::new(); |
| 247 |
replay(residual.ops(), rows, &mut replayed); |
| 248 |
assert_eq!( |
| 249 |
webview.fragment(&picker_staged(&Plan::full(rows))), |
| 250 |
replayed, |
| 251 |
"the residual and the renderer disagree at {rows} rows" |
| 252 |
); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
#[test] |
| 262 |
fn a_filled_picker_is_what_the_renderer_would_have_produced() { |
| 263 |
let webview = Webview::new(); |
| 264 |
let residual = residual(); |
| 265 |
|
| 266 |
for marked in [Some(0), Some(1), Some(2), None] { |
| 267 |
let themes = themes(marked); |
| 268 |
let filled = picker_serve(&residual, &themes); |
| 269 |
assert_eq!( |
| 270 |
webview.fragment(&picker(&themes)), |
| 271 |
filled, |
| 272 |
"marked {marked:?}" |
| 273 |
); |
| 274 |
assert_eq!( |
| 275 |
filled.matches(" selected").count(), |
| 276 |
usize::from(marked.is_some()), |
| 277 |
"{filled}" |
| 278 |
); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
#[test] |
| 284 |
fn a_picker_with_no_themes_goes_through_the_same_residual() { |
| 285 |
let webview = Webview::new(); |
| 286 |
let residual = residual(); |
| 287 |
let themes = Vec::new(); |
| 288 |
|
| 289 |
assert_eq!( |
| 290 |
webview.fragment(&picker(&themes)), |
| 291 |
picker_serve(&residual, &themes) |
| 292 |
); |
| 293 |
} |
| 294 |
} |
| 295 |
|