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