| 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 |
use makeover_layout as layout; |
| 75 |
use quasi_router::{Action, Node, RegionKind, Slot}; |
| 76 |
use quasi_webview::Webview; |
| 77 |
|
| 78 |
|
| 79 |
const STRIP: &str = "tab-content"; |
| 80 |
|
| 81 |
|
| 82 |
#[derive(PartialEq, Eq)] |
| 83 |
enum Gate { |
| 84 |
|
| 85 |
Live, |
| 86 |
|
| 87 |
Creator, |
| 88 |
|
| 89 |
Always, |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
struct Tab { |
| 94 |
label: &'static str, |
| 95 |
|
| 96 |
|
| 97 |
panel: &'static str, |
| 98 |
|
| 99 |
route: &'static str, |
| 100 |
gate: Gate, |
| 101 |
|
| 102 |
screen: Option<&'static str>, |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
const TABS: &[Tab] = &[ |
| 107 |
Tab { |
| 108 |
label: "Projects", |
| 109 |
panel: super::user_projects::REGION, |
| 110 |
route: "projects", |
| 111 |
gate: Gate::Creator, |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
screen: None, |
| 116 |
}, |
| 117 |
Tab { |
| 118 |
label: "Payments", |
| 119 |
panel: "user-payments", |
| 120 |
route: "payments", |
| 121 |
gate: Gate::Live, |
| 122 |
screen: None, |
| 123 |
}, |
| 124 |
Tab { |
| 125 |
label: "Analytics", |
| 126 |
panel: super::user_analytics::REGION, |
| 127 |
route: "analytics", |
| 128 |
gate: Gate::Creator, |
| 129 |
screen: Some(super::user_analytics::SCREEN), |
| 130 |
}, |
| 131 |
Tab { |
| 132 |
label: "Settings", |
| 133 |
panel: "user-settings", |
| 134 |
route: "settings", |
| 135 |
gate: Gate::Live, |
| 136 |
screen: None, |
| 137 |
}, |
| 138 |
Tab { |
| 139 |
label: "Support", |
| 140 |
panel: super::user_support::REGION, |
| 141 |
route: "support", |
| 142 |
gate: Gate::Always, |
| 143 |
|
| 144 |
|
| 145 |
screen: None, |
| 146 |
}, |
| 147 |
]; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
const FILLABLE: &[&str] = &["projects", "payments", "settings", "support"]; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
#[must_use] |
| 164 |
pub fn shown_at(asked: Option<&str>, deactivated: bool, can_create_projects: bool) -> usize { |
| 165 |
let Some(asked) = asked else { return 0 }; |
| 166 |
if !FILLABLE.contains(&asked) { |
| 167 |
return 0; |
| 168 |
} |
| 169 |
visible(deactivated, can_create_projects) |
| 170 |
.iter() |
| 171 |
.position(|tab| tab.route == asked) |
| 172 |
.unwrap_or(0) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
#[must_use] |
| 177 |
pub fn route_at(shown: usize, deactivated: bool, can_create_projects: bool) -> &'static str { |
| 178 |
let tabs = visible(deactivated, can_create_projects); |
| 179 |
tabs.get(shown).map_or(tabs[0].route, |tab| tab.route) |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
fn visible(deactivated: bool, can_create_projects: bool) -> Vec<&'static Tab> { |
| 186 |
TABS.iter() |
| 187 |
.filter(|tab| match tab.gate { |
| 188 |
Gate::Always => true, |
| 189 |
Gate::Live => !deactivated, |
| 190 |
Gate::Creator => !deactivated && can_create_projects, |
| 191 |
}) |
| 192 |
.collect() |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
#[must_use] |
| 202 |
pub fn html(shown: usize, panel: &str, deactivated: bool, can_create_projects: bool) -> String { |
| 203 |
let tabs = visible(deactivated, can_create_projects); |
| 204 |
let shown = shown.min(tabs.len() - 1); |
| 205 |
|
| 206 |
let mut strip = Slot::new(STRIP, RegionKind::TabGroup) |
| 207 |
.across(layout::Fallback::Menu) |
| 208 |
.showing_one(shown); |
| 209 |
|
| 210 |
for (at, tab) in tabs.iter().enumerate() { |
| 211 |
let mut region = Slot::handover(tab.panel, "user-panel"); |
| 212 |
if at != shown { |
| 213 |
let mut call = Action::get(format!("/dashboard/tabs/{}", tab.route)).awaiting(); |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if tab.screen.is_none() { |
| 220 |
call = call.replacing(tab.panel); |
| 221 |
} |
| 222 |
region = region.fed_by(call); |
| 223 |
} |
| 224 |
strip = strip.frame(tab.label, Node::Region(region)); |
| 225 |
} |
| 226 |
|
| 227 |
use quasi_axum::Serves as _; |
| 228 |
|
| 229 |
|
| 230 |
Webview::new() |
| 231 |
.with_fill(tabs[shown].panel, panel) |
| 232 |
.fragment(&Node::Region(strip)) |
| 233 |
} |
| 234 |
|
| 235 |
#[cfg(test)] |
| 236 |
mod tests { |
| 237 |
use super::*; |
| 238 |
|
| 239 |
fn strip(deactivated: bool, creator: bool) -> String { |
| 240 |
html( |
| 241 |
shown_at(None, deactivated, creator), |
| 242 |
"<p>the panel</p>", |
| 243 |
deactivated, |
| 244 |
creator, |
| 245 |
) |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn the_page_asks_for_nothing_on_load() { |
| 250 |
|
| 251 |
let html = strip(false, true); |
| 252 |
|
| 253 |
assert!(!html.contains("hx-trigger=\"load\""), "{html}"); |
| 254 |
assert!(html.contains("<p>the panel</p>"), "{html}"); |
| 255 |
assert_eq!(html.matches("hx-get=").count(), 4, "{html}"); |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn every_unshown_tab_says_where_its_answer_lands() { |
| 260 |
let html = strip(false, true); |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
for panel in ["user-payments", "user-settings", "user-support"] { |
| 269 |
assert!(html.contains(&format!("hx-target=\"#{panel}\"")), "{html}"); |
| 270 |
assert!(html.contains(&format!("id=\"{panel}\"")), "{html}"); |
| 271 |
} |
| 272 |
|
| 273 |
assert!(html.contains("id=\"user-analytics\""), "{html}"); |
| 274 |
assert!(!html.contains("hx-target=\"#user-projects\""), "{html}"); |
| 275 |
} |
| 276 |
|
| 277 |
#[test] |
| 278 |
fn a_creator_opens_on_projects_and_everyone_else_on_payments() { |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
let creator = strip(false, true); |
| 283 |
assert!(creator.contains(">Projects</button>"), "{creator}"); |
| 284 |
assert!( |
| 285 |
!creator.contains("hx-target=\"#user-projects\""), |
| 286 |
"{creator}" |
| 287 |
); |
| 288 |
|
| 289 |
let fan = strip(false, false); |
| 290 |
assert!(!fan.contains(">Projects</button>"), "{fan}"); |
| 291 |
assert!(!fan.contains(">Analytics</button>"), "{fan}"); |
| 292 |
assert!(!fan.contains("hx-target=\"#user-payments\""), "{fan}"); |
| 293 |
assert_eq!(fan.matches("hx-get=").count(), 2, "{fan}"); |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn a_deactivated_account_sees_support_and_nothing_else() { |
| 298 |
let html = strip(true, true); |
| 299 |
|
| 300 |
assert!(html.contains(">Support</button>"), "{html}"); |
| 301 |
for label in [ |
| 302 |
">Projects</button>", |
| 303 |
">Payments</button>", |
| 304 |
">Analytics</button>", |
| 305 |
">Settings</button>", |
| 306 |
] { |
| 307 |
assert!(!html.contains(label), "{html}"); |
| 308 |
} |
| 309 |
|
| 310 |
assert!(html.contains("role=\"tablist\""), "{html}"); |
| 311 |
assert!(html.contains("<p>the panel</p>"), "{html}"); |
| 312 |
assert_eq!(html.matches("hx-get=").count(), 0, "{html}"); |
| 313 |
} |
| 314 |
|
| 315 |
#[test] |
| 316 |
fn a_deep_link_arrives_showing_what_it_asked_for() { |
| 317 |
let shown = shown_at(Some("settings"), false, true); |
| 318 |
assert_eq!(shown, 3); |
| 319 |
assert_eq!(route_at(shown, false, true), "settings"); |
| 320 |
|
| 321 |
let html = html(shown, "<p>your settings</p>", false, true); |
| 322 |
assert!(html.contains("<p>your settings</p>"), "{html}"); |
| 323 |
assert!(!html.contains("hx-target=\"#user-settings\""), "{html}"); |
| 324 |
assert!(html.contains("hx-target=\"#user-projects\""), "{html}"); |
| 325 |
assert!(html.contains("data-shows=\"3\""), "{html}"); |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn a_tab_the_page_cannot_fill_answers_the_first_one() { |
| 330 |
|
| 331 |
|
| 332 |
assert_eq!(shown_at(Some("analytics"), false, true), 0); |
| 333 |
assert_eq!(shown_at(Some("nonsense"), false, true), 0); |
| 334 |
assert_eq!(shown_at(None, false, true), 0); |
| 335 |
|
| 336 |
assert_eq!(shown_at(Some("projects"), false, false), 0); |
| 337 |
assert_eq!( |
| 338 |
route_at(shown_at(Some("projects"), false, false), false, false), |
| 339 |
"payments" |
| 340 |
); |
| 341 |
|
| 342 |
assert_eq!( |
| 343 |
route_at(shown_at(Some("settings"), true, true), true, true), |
| 344 |
"support" |
| 345 |
); |
| 346 |
} |
| 347 |
|
| 348 |
#[test] |
| 349 |
fn the_described_screen_is_left_to_name_its_own_region() { |
| 350 |
let html = html(0, "<p>the panel</p>", false, true); |
| 351 |
|
| 352 |
assert!( |
| 353 |
!html.contains("hx-target=\"#user-analytics\""), |
| 354 |
"a described screen retargets its own answer:\n{html}" |
| 355 |
); |
| 356 |
|
| 357 |
assert!(html.contains("hx-target=\"#user-settings\""), "{html}"); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn the_screen_answers_into_the_frame_that_is_its_own() { |
| 362 |
|
| 363 |
|
| 364 |
assert_eq!(super::super::user_analytics::REGION, "user-analytics"); |
| 365 |
} |
| 366 |
|
| 367 |
#[test] |
| 368 |
fn the_strip_says_what_it_does_when_it_runs_out_of_room() { |
| 369 |
assert!(strip(false, true).contains("run-menu")); |
| 370 |
} |
| 371 |
|
| 372 |
#[test] |
| 373 |
fn a_shown_index_past_the_end_cannot_panic() { |
| 374 |
|
| 375 |
|
| 376 |
let html = html(99, "<p>the panel</p>", true, true); |
| 377 |
assert!(html.contains("<p>the panel</p>"), "{html}"); |
| 378 |
assert!(html.contains("data-shows=\"0\""), "{html}"); |
| 379 |
} |
| 380 |
} |
| 381 |
|