| 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 |
use makeover_layout as layout; |
| 67 |
use quasi_router::{Action, Node, RegionKind, Slot}; |
| 68 |
use quasi_webview::Webview; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
const STRIP: &str = "settings-sections"; |
| 76 |
|
| 77 |
|
| 78 |
#[derive(PartialEq, Eq)] |
| 79 |
enum Gate { |
| 80 |
|
| 81 |
Always, |
| 82 |
|
| 83 |
Media, |
| 84 |
|
| 85 |
Git, |
| 86 |
|
| 87 |
Forums, |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
SyncKit, |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#[derive(Clone, Copy)] |
| 101 |
pub struct Gates { |
| 102 |
|
| 103 |
pub has_media: bool, |
| 104 |
|
| 105 |
pub git_enabled: bool, |
| 106 |
|
| 107 |
pub has_mt_memberships: bool, |
| 108 |
|
| 109 |
pub has_sync_apps: bool, |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
struct Section { |
| 114 |
label: &'static str, |
| 115 |
|
| 116 |
name: &'static str, |
| 117 |
gate: Gate, |
| 118 |
|
| 119 |
|
| 120 |
panel: &'static str, |
| 121 |
route: &'static str, |
| 122 |
|
| 123 |
screen: Option<&'static str>, |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
const SECTIONS: &[Section] = &[ |
| 128 |
Section { |
| 129 |
label: "Profile", |
| 130 |
name: "profile", |
| 131 |
gate: Gate::Always, |
| 132 |
panel: "settings-profile", |
| 133 |
route: "/dashboard/tabs/profile", |
| 134 |
screen: None, |
| 135 |
}, |
| 136 |
Section { |
| 137 |
label: "Account", |
| 138 |
name: "account", |
| 139 |
gate: Gate::Always, |
| 140 |
panel: "settings-account", |
| 141 |
route: "/dashboard/tabs/account", |
| 142 |
screen: None, |
| 143 |
}, |
| 144 |
Section { |
| 145 |
label: "Creator Plan", |
| 146 |
name: "creator", |
| 147 |
gate: Gate::Always, |
| 148 |
panel: "settings-creator", |
| 149 |
route: "/dashboard/tabs/creator", |
| 150 |
screen: None, |
| 151 |
}, |
| 152 |
Section { |
| 153 |
label: "Media", |
| 154 |
name: "media", |
| 155 |
gate: Gate::Media, |
| 156 |
panel: "settings-media", |
| 157 |
route: "/dashboard/tabs/media", |
| 158 |
screen: None, |
| 159 |
}, |
| 160 |
Section { |
| 161 |
label: "SSH Keys", |
| 162 |
name: "ssh-keys", |
| 163 |
gate: Gate::Git, |
| 164 |
panel: super::ssh_keys::REGION, |
| 165 |
route: super::ssh_keys::PATH, |
| 166 |
screen: Some(super::ssh_keys::SCREEN), |
| 167 |
}, |
| 168 |
Section { |
| 169 |
label: "Forums", |
| 170 |
name: "forums", |
| 171 |
gate: Gate::Forums, |
| 172 |
panel: super::forum_memberships::SETTINGS_REGION, |
| 173 |
route: super::forum_memberships::SETTINGS_PATH, |
| 174 |
screen: Some(super::forum_memberships::SETTINGS_SCREEN), |
| 175 |
}, |
| 176 |
Section { |
| 177 |
label: "Cloud Sync", |
| 178 |
name: "synckit", |
| 179 |
gate: Gate::SyncKit, |
| 180 |
panel: "settings-synckit", |
| 181 |
route: "/dashboard/tabs/synckit", |
| 182 |
screen: None, |
| 183 |
}, |
| 184 |
]; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
const FILLABLE: &[&str] = &["profile", "creator", "ssh-keys", "synckit"]; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
fn visible(gates: Gates) -> Vec<&'static Section> { |
| 202 |
SECTIONS |
| 203 |
.iter() |
| 204 |
.filter(|section| match section.gate { |
| 205 |
Gate::Always => true, |
| 206 |
Gate::Media => gates.has_media, |
| 207 |
Gate::Git => gates.git_enabled, |
| 208 |
Gate::Forums => gates.has_mt_memberships, |
| 209 |
Gate::SyncKit => gates.has_sync_apps, |
| 210 |
}) |
| 211 |
.collect() |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
#[must_use] |
| 227 |
pub fn shown_at(asked: Option<&str>, gates: Gates) -> usize { |
| 228 |
let Some(asked) = asked else { return 0 }; |
| 229 |
if !FILLABLE.contains(&asked) { |
| 230 |
return 0; |
| 231 |
} |
| 232 |
visible(gates) |
| 233 |
.iter() |
| 234 |
.position(|section| section.name == asked && section.screen.is_none()) |
| 235 |
.unwrap_or(0) |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
#[must_use] |
| 240 |
pub fn section_at(shown: usize, gates: Gates) -> &'static str { |
| 241 |
let sections = visible(gates); |
| 242 |
sections |
| 243 |
.get(shown) |
| 244 |
.map_or(sections[0].name, |section| section.name) |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
#[must_use] |
| 253 |
pub fn html(shown: usize, section: &str, gates: Gates) -> String { |
| 254 |
let sections = visible(gates); |
| 255 |
let shown = shown.min(sections.len() - 1); |
| 256 |
|
| 257 |
let mut strip = Slot::new(STRIP, RegionKind::TabGroup) |
| 258 |
.across(layout::Fallback::Menu) |
| 259 |
.showing_one(shown); |
| 260 |
|
| 261 |
for (at, section) in sections.iter().enumerate() { |
| 262 |
let mut region = Slot::handover(section.panel, "settings-panel"); |
| 263 |
if at != shown { |
| 264 |
let mut call = Action::get(section.route).awaiting(); |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if section.screen.is_none() { |
| 270 |
call = call.replacing(section.panel); |
| 271 |
} |
| 272 |
region = region.fed_by(call); |
| 273 |
} |
| 274 |
strip = strip.frame(section.label, Node::Region(region)); |
| 275 |
} |
| 276 |
|
| 277 |
use quasi_axum::Serves as _; |
| 278 |
|
| 279 |
Webview::new() |
| 280 |
.with_fill(sections[shown].panel, section) |
| 281 |
.fragment(&Node::Region(strip)) |
| 282 |
} |
| 283 |
|
| 284 |
#[cfg(test)] |
| 285 |
mod tests { |
| 286 |
use super::*; |
| 287 |
|
| 288 |
|
| 289 |
const ALL: Gates = Gates { |
| 290 |
has_media: true, |
| 291 |
git_enabled: true, |
| 292 |
has_mt_memberships: true, |
| 293 |
has_sync_apps: true, |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
const NONE: Gates = Gates { |
| 298 |
has_media: false, |
| 299 |
git_enabled: false, |
| 300 |
has_mt_memberships: false, |
| 301 |
has_sync_apps: false, |
| 302 |
}; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
const NO_SYNC: Gates = Gates { |
| 307 |
has_sync_apps: false, |
| 308 |
..ALL |
| 309 |
}; |
| 310 |
|
| 311 |
fn strip(gates: Gates) -> String { |
| 312 |
html(0, "<p>your profile</p>", gates) |
| 313 |
} |
| 314 |
|
| 315 |
#[test] |
| 316 |
fn the_settings_tab_still_opens_without_fetching() { |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
let html = strip(NO_SYNC); |
| 321 |
|
| 322 |
assert!(!html.contains("hx-trigger=\"load\""), "{html}"); |
| 323 |
assert!(html.contains("<p>your profile</p>"), "{html}"); |
| 324 |
assert_eq!(html.matches("hx-get=").count(), 5, "{html}"); |
| 325 |
} |
| 326 |
|
| 327 |
#[test] |
| 328 |
fn every_unshown_section_says_where_its_answer_lands() { |
| 329 |
let html = strip(NO_SYNC); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
for panel in ["settings-account", "settings-creator", "settings-media"] { |
| 336 |
assert!(html.contains(&format!("hx-target=\"#{panel}\"")), "{html}"); |
| 337 |
assert!(html.contains(&format!("id=\"{panel}\"")), "{html}"); |
| 338 |
} |
| 339 |
|
| 340 |
for panel in ["settings-ssh-keys", "settings-forums"] { |
| 341 |
assert!(html.contains(&format!("id=\"{panel}\"")), "{html}"); |
| 342 |
assert!( |
| 343 |
!html.contains(&format!("hx-target=\"#{panel}\"")), |
| 344 |
"{panel} is described and names its own region:\n{html}" |
| 345 |
); |
| 346 |
} |
| 347 |
assert!(!html.contains("hx-target=\"#settings-profile\""), "{html}"); |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn a_described_section_is_left_to_name_its_own_region() { |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
let html = strip(NO_SYNC); |
| 357 |
|
| 358 |
for panel in ["settings-ssh-keys", "settings-forums"] { |
| 359 |
assert!( |
| 360 |
!html.contains(&format!("hx-target=\"#{panel}\"")), |
| 361 |
"a described section retargets its own answer:\n{html}" |
| 362 |
); |
| 363 |
} |
| 364 |
|
| 365 |
assert!(html.contains("hx-target=\"#settings-account\""), "{html}"); |
| 366 |
} |
| 367 |
|
| 368 |
#[test] |
| 369 |
fn the_two_screens_answer_into_the_frame_that_is_theirs() { |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
assert_eq!(super::super::ssh_keys::REGION, "settings-ssh-keys"); |
| 374 |
assert_eq!( |
| 375 |
super::super::forum_memberships::SETTINGS_REGION, |
| 376 |
"settings-forums" |
| 377 |
); |
| 378 |
} |
| 379 |
|
| 380 |
#[test] |
| 381 |
fn a_deep_link_arrives_showing_the_section_it_asked_for() { |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
let all = NO_SYNC; |
| 386 |
let shown = shown_at(Some("creator"), all); |
| 387 |
assert_eq!(shown, 2); |
| 388 |
assert_eq!(section_at(shown, all), "creator"); |
| 389 |
|
| 390 |
let html = html(shown, "<p>your plan</p>", all); |
| 391 |
assert!(html.contains("<p>your plan</p>"), "{html}"); |
| 392 |
assert!(!html.contains("hx-target=\"#settings-creator\""), "{html}"); |
| 393 |
|
| 394 |
assert!(html.contains("hx-target=\"#settings-profile\""), "{html}"); |
| 395 |
assert!(html.contains("data-shows=\"2\""), "{html}"); |
| 396 |
} |
| 397 |
|
| 398 |
#[test] |
| 399 |
fn a_section_the_page_cannot_fill_answers_profile() { |
| 400 |
|
| 401 |
let all = ALL; |
| 402 |
assert_eq!(shown_at(Some("account"), all), 0); |
| 403 |
assert_eq!(shown_at(Some("media"), all), 0); |
| 404 |
assert_eq!(shown_at(Some("forums"), all), 0); |
| 405 |
|
| 406 |
assert_eq!(shown_at(Some("nonsense"), all), 0); |
| 407 |
assert_eq!(shown_at(None, all), 0); |
| 408 |
|
| 409 |
|
| 410 |
assert_eq!( |
| 411 |
shown_at( |
| 412 |
Some("ssh-keys"), |
| 413 |
Gates { |
| 414 |
git_enabled: false, |
| 415 |
..NO_SYNC |
| 416 |
} |
| 417 |
), |
| 418 |
0 |
| 419 |
); |
| 420 |
assert_eq!( |
| 421 |
section_at( |
| 422 |
0, |
| 423 |
Gates { |
| 424 |
git_enabled: false, |
| 425 |
..NO_SYNC |
| 426 |
} |
| 427 |
), |
| 428 |
"profile" |
| 429 |
); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
let git_only = Gates { |
| 435 |
git_enabled: true, |
| 436 |
..NONE |
| 437 |
}; |
| 438 |
assert_eq!(shown_at(Some("ssh-keys"), git_only), 0); |
| 439 |
assert_eq!(section_at(3, git_only), "ssh-keys"); |
| 440 |
} |
| 441 |
|
| 442 |
#[test] |
| 443 |
fn a_described_section_is_not_fillable() { |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
assert_eq!(shown_at(Some("ssh-keys"), NO_SYNC), 0); |
| 454 |
} |
| 455 |
|
| 456 |
#[test] |
| 457 |
fn a_shown_index_past_the_end_cannot_panic() { |
| 458 |
|
| 459 |
|
| 460 |
let html = html(99, "<p>your profile</p>", NONE); |
| 461 |
assert!(html.contains("<p>your profile</p>"), "{html}"); |
| 462 |
assert!(html.contains("data-shows=\"1\""), "{html}"); |
| 463 |
} |
| 464 |
|
| 465 |
#[test] |
| 466 |
fn the_four_gated_sections_leave_when_their_test_fails() { |
| 467 |
const GATED: [&str; 4] = [ |
| 468 |
">Media</button>", |
| 469 |
">SSH Keys</button>", |
| 470 |
">Forums</button>", |
| 471 |
">Cloud Sync</button>", |
| 472 |
]; |
| 473 |
|
| 474 |
let all = strip(ALL); |
| 475 |
for label in GATED { |
| 476 |
assert!(all.contains(label), "{all}"); |
| 477 |
} |
| 478 |
|
| 479 |
let none = strip(NONE); |
| 480 |
for label in GATED { |
| 481 |
assert!(!none.contains(label), "{none}"); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
assert_eq!(none.matches("hx-get=").count(), 2, "{none}"); |
| 486 |
assert!(none.contains("<p>your profile</p>"), "{none}"); |
| 487 |
assert!(none.contains("role=\"tablist\""), "{none}"); |
| 488 |
} |
| 489 |
|
| 490 |
#[test] |
| 491 |
fn cloud_sync_is_absent_for_a_reader_who_owns_no_sync_app() { |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
let without = strip(NO_SYNC); |
| 497 |
assert!(!without.contains(">Cloud Sync</button>"), "{without}"); |
| 498 |
assert!(!without.contains("id=\"settings-synckit\""), "{without}"); |
| 499 |
|
| 500 |
let with = strip(ALL); |
| 501 |
assert!(with.contains(">Cloud Sync</button>"), "{with}"); |
| 502 |
assert!(with.contains("id=\"settings-synckit\""), "{with}"); |
| 503 |
|
| 504 |
assert!(with.contains("hx-target=\"#settings-synckit\""), "{with}"); |
| 505 |
assert!( |
| 506 |
with.contains("hx-get=\"/dashboard/tabs/synckit\""), |
| 507 |
"{with}" |
| 508 |
); |
| 509 |
} |
| 510 |
|
| 511 |
#[test] |
| 512 |
fn the_stripe_return_lands_on_cloud_sync_filled() { |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
let owner = ALL; |
| 517 |
let shown = shown_at(Some("synckit"), owner); |
| 518 |
assert_eq!(section_at(shown, owner), "synckit"); |
| 519 |
|
| 520 |
let html = html(shown, "<p>your apps</p>", owner); |
| 521 |
assert!(html.contains("<p>your apps</p>"), "{html}"); |
| 522 |
assert!(!html.contains("hx-target=\"#settings-synckit\""), "{html}"); |
| 523 |
} |
| 524 |
|
| 525 |
#[test] |
| 526 |
fn asking_for_cloud_sync_without_one_answers_profile() { |
| 527 |
|
| 528 |
|
| 529 |
let stranger = NO_SYNC; |
| 530 |
assert_eq!(shown_at(Some("synckit"), stranger), 0); |
| 531 |
assert_eq!( |
| 532 |
section_at(shown_at(Some("synckit"), stranger), stranger), |
| 533 |
"profile" |
| 534 |
); |
| 535 |
} |
| 536 |
} |
| 537 |
|