| 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 |
use makeover_layout as layout; |
| 57 |
use quasi_router::{Action, Node, RegionKind, Slot}; |
| 58 |
use quasi_webview::Webview; |
| 59 |
|
| 60 |
use crate::config::QuasiScreens; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
const STRIP: &str = "settings-sections"; |
| 68 |
|
| 69 |
|
| 70 |
#[derive(PartialEq, Eq)] |
| 71 |
enum Gate { |
| 72 |
|
| 73 |
Always, |
| 74 |
|
| 75 |
Media, |
| 76 |
|
| 77 |
Git, |
| 78 |
|
| 79 |
Forums, |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
struct Section { |
| 84 |
label: &'static str, |
| 85 |
|
| 86 |
name: &'static str, |
| 87 |
gate: Gate, |
| 88 |
|
| 89 |
|
| 90 |
panel: &'static str, |
| 91 |
route: &'static str, |
| 92 |
|
| 93 |
screen: Option<&'static str>, |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
const SECTIONS: &[Section] = &[ |
| 98 |
Section { |
| 99 |
label: "Profile", |
| 100 |
name: "profile", |
| 101 |
gate: Gate::Always, |
| 102 |
panel: "settings-profile", |
| 103 |
route: "/dashboard/tabs/profile", |
| 104 |
screen: None, |
| 105 |
}, |
| 106 |
Section { |
| 107 |
label: "Account", |
| 108 |
name: "account", |
| 109 |
gate: Gate::Always, |
| 110 |
panel: "settings-account", |
| 111 |
route: "/dashboard/tabs/account", |
| 112 |
screen: None, |
| 113 |
}, |
| 114 |
Section { |
| 115 |
label: "Creator Plan", |
| 116 |
name: "creator", |
| 117 |
gate: Gate::Always, |
| 118 |
panel: "settings-creator", |
| 119 |
route: "/dashboard/tabs/creator", |
| 120 |
screen: None, |
| 121 |
}, |
| 122 |
Section { |
| 123 |
label: "Media", |
| 124 |
name: "media", |
| 125 |
gate: Gate::Media, |
| 126 |
panel: "settings-media", |
| 127 |
route: "/dashboard/tabs/media", |
| 128 |
screen: None, |
| 129 |
}, |
| 130 |
Section { |
| 131 |
label: "SSH Keys", |
| 132 |
name: "ssh-keys", |
| 133 |
gate: Gate::Git, |
| 134 |
panel: super::ssh_keys::REGION, |
| 135 |
route: super::ssh_keys::PATH, |
| 136 |
screen: Some(super::ssh_keys::SCREEN), |
| 137 |
}, |
| 138 |
Section { |
| 139 |
label: "Forums", |
| 140 |
name: "forums", |
| 141 |
gate: Gate::Forums, |
| 142 |
panel: super::forum_memberships::SETTINGS_REGION, |
| 143 |
route: super::forum_memberships::SETTINGS_PATH, |
| 144 |
screen: Some(super::forum_memberships::SETTINGS_SCREEN), |
| 145 |
}, |
| 146 |
]; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
const FILLABLE: &[&str] = &["profile", "creator", "ssh-keys"]; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
fn visible(has_media: bool, git_enabled: bool, has_mt_memberships: bool) -> Vec<&'static Section> { |
| 161 |
SECTIONS |
| 162 |
.iter() |
| 163 |
.filter(|section| match section.gate { |
| 164 |
Gate::Always => true, |
| 165 |
Gate::Media => has_media, |
| 166 |
Gate::Git => git_enabled, |
| 167 |
Gate::Forums => has_mt_memberships, |
| 168 |
}) |
| 169 |
.collect() |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
#[must_use] |
| 185 |
pub fn shown_at( |
| 186 |
asked: Option<&str>, |
| 187 |
screens: &QuasiScreens, |
| 188 |
has_media: bool, |
| 189 |
git_enabled: bool, |
| 190 |
has_mt_memberships: bool, |
| 191 |
) -> usize { |
| 192 |
let Some(asked) = asked else { return 0 }; |
| 193 |
if !FILLABLE.contains(&asked) { |
| 194 |
return 0; |
| 195 |
} |
| 196 |
visible(has_media, git_enabled, has_mt_memberships) |
| 197 |
.iter() |
| 198 |
.position(|section| { |
| 199 |
section.name == asked && !section.screen.is_some_and(|name| screens.enabled(name)) |
| 200 |
}) |
| 201 |
.unwrap_or(0) |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
#[must_use] |
| 206 |
pub fn section_at( |
| 207 |
shown: usize, |
| 208 |
has_media: bool, |
| 209 |
git_enabled: bool, |
| 210 |
has_mt_memberships: bool, |
| 211 |
) -> &'static str { |
| 212 |
let sections = visible(has_media, git_enabled, has_mt_memberships); |
| 213 |
sections |
| 214 |
.get(shown) |
| 215 |
.map_or(sections[0].name, |section| section.name) |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
#[must_use] |
| 224 |
pub fn html( |
| 225 |
screens: &QuasiScreens, |
| 226 |
shown: usize, |
| 227 |
section: &str, |
| 228 |
has_media: bool, |
| 229 |
git_enabled: bool, |
| 230 |
has_mt_memberships: bool, |
| 231 |
) -> String { |
| 232 |
let sections = visible(has_media, git_enabled, has_mt_memberships); |
| 233 |
let shown = shown.min(sections.len() - 1); |
| 234 |
|
| 235 |
let mut strip = Slot::new(STRIP, RegionKind::TabGroup) |
| 236 |
.across(layout::Fallback::Menu) |
| 237 |
.showing_one(shown); |
| 238 |
|
| 239 |
for (at, section) in sections.iter().enumerate() { |
| 240 |
let mut region = Slot::bespoke(section.panel, "settings-panel").label(section.label); |
| 241 |
if at != shown { |
| 242 |
let mut call = Action::get(section.route).awaiting(); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if !section.screen.is_some_and(|name| screens.enabled(name)) { |
| 248 |
call = call.replacing(section.panel); |
| 249 |
} |
| 250 |
region = region.fed_by(call); |
| 251 |
} |
| 252 |
strip = strip.with(Node::Region(region)); |
| 253 |
} |
| 254 |
|
| 255 |
use quasi_axum::Serves as _; |
| 256 |
|
| 257 |
Webview::new() |
| 258 |
.with_fill(sections[shown].panel, section) |
| 259 |
.fragment(&Node::Region(strip)) |
| 260 |
} |
| 261 |
|
| 262 |
#[cfg(test)] |
| 263 |
mod tests { |
| 264 |
use super::*; |
| 265 |
|
| 266 |
fn strip(media: bool, git: bool, forums: bool) -> String { |
| 267 |
html( |
| 268 |
&QuasiScreens::default(), |
| 269 |
0, |
| 270 |
"<p>your profile</p>", |
| 271 |
media, |
| 272 |
git, |
| 273 |
forums, |
| 274 |
) |
| 275 |
} |
| 276 |
|
| 277 |
#[test] |
| 278 |
fn the_settings_tab_still_opens_without_fetching() { |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
let html = strip(true, true, true); |
| 283 |
|
| 284 |
assert!(!html.contains("hx-trigger=\"load\""), "{html}"); |
| 285 |
assert!(html.contains("<p>your profile</p>"), "{html}"); |
| 286 |
assert_eq!(html.matches("hx-get=").count(), 5, "{html}"); |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn every_unshown_section_says_where_its_answer_lands() { |
| 291 |
let html = strip(true, true, true); |
| 292 |
|
| 293 |
for panel in [ |
| 294 |
"settings-account", |
| 295 |
"settings-creator", |
| 296 |
"settings-media", |
| 297 |
"settings-ssh-keys", |
| 298 |
"settings-forums", |
| 299 |
] { |
| 300 |
assert!(html.contains(&format!("hx-target=\"#{panel}\"")), "{html}"); |
| 301 |
assert!(html.contains(&format!("id=\"{panel}\"")), "{html}"); |
| 302 |
} |
| 303 |
assert!(!html.contains("hx-target=\"#settings-profile\""), "{html}"); |
| 304 |
} |
| 305 |
|
| 306 |
#[test] |
| 307 |
fn a_described_section_is_left_to_name_its_own_region() { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
let screens = QuasiScreens::parse(super::super::ssh_keys::SCREEN); |
| 312 |
let html = html(&screens, 0, "<p>your profile</p>", true, true, true); |
| 313 |
|
| 314 |
assert!( |
| 315 |
!html.contains("hx-target=\"#settings-ssh-keys\""), |
| 316 |
"a described section retargets its own answer:\n{html}" |
| 317 |
); |
| 318 |
|
| 319 |
assert!(html.contains("hx-target=\"#settings-forums\""), "{html}"); |
| 320 |
} |
| 321 |
|
| 322 |
#[test] |
| 323 |
fn the_two_screens_answer_into_the_frame_that_is_theirs() { |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
assert_eq!(super::super::ssh_keys::REGION, "settings-ssh-keys"); |
| 328 |
assert_eq!( |
| 329 |
super::super::forum_memberships::SETTINGS_REGION, |
| 330 |
"settings-forums" |
| 331 |
); |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn a_deep_link_arrives_showing_the_section_it_asked_for() { |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
let screens = QuasiScreens::default(); |
| 340 |
let shown = shown_at(Some("creator"), &screens, true, true, true); |
| 341 |
assert_eq!(shown, 2); |
| 342 |
assert_eq!(section_at(shown, true, true, true), "creator"); |
| 343 |
|
| 344 |
let html = html(&screens, shown, "<p>your plan</p>", true, true, true); |
| 345 |
assert!(html.contains("<p>your plan</p>"), "{html}"); |
| 346 |
assert!(!html.contains("hx-target=\"#settings-creator\""), "{html}"); |
| 347 |
|
| 348 |
assert!(html.contains("hx-target=\"#settings-profile\""), "{html}"); |
| 349 |
assert!(html.contains("data-shows=\"2\""), "{html}"); |
| 350 |
} |
| 351 |
|
| 352 |
#[test] |
| 353 |
fn a_section_the_page_cannot_fill_answers_profile() { |
| 354 |
let screens = QuasiScreens::default(); |
| 355 |
|
| 356 |
assert_eq!(shown_at(Some("account"), &screens, true, true, true), 0); |
| 357 |
assert_eq!(shown_at(Some("media"), &screens, true, true, true), 0); |
| 358 |
assert_eq!(shown_at(Some("forums"), &screens, true, true, true), 0); |
| 359 |
|
| 360 |
assert_eq!(shown_at(Some("nonsense"), &screens, true, true, true), 0); |
| 361 |
assert_eq!(shown_at(None, &screens, true, true, true), 0); |
| 362 |
|
| 363 |
|
| 364 |
assert_eq!(shown_at(Some("ssh-keys"), &screens, true, false, true), 0); |
| 365 |
assert_eq!(section_at(0, true, false, true), "profile"); |
| 366 |
assert_eq!(shown_at(Some("ssh-keys"), &screens, false, true, false), 3); |
| 367 |
assert_eq!(section_at(3, false, true, false), "ssh-keys"); |
| 368 |
} |
| 369 |
|
| 370 |
#[test] |
| 371 |
fn a_switched_on_screen_is_not_fillable() { |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
let screens = QuasiScreens::parse(super::super::ssh_keys::SCREEN); |
| 377 |
assert_eq!(shown_at(Some("ssh-keys"), &screens, true, true, true), 0); |
| 378 |
|
| 379 |
assert_eq!( |
| 380 |
shown_at(Some("ssh-keys"), &QuasiScreens::default(), true, true, true), |
| 381 |
4 |
| 382 |
); |
| 383 |
} |
| 384 |
|
| 385 |
#[test] |
| 386 |
fn a_shown_index_past_the_end_cannot_panic() { |
| 387 |
|
| 388 |
|
| 389 |
let html = html( |
| 390 |
&QuasiScreens::default(), |
| 391 |
99, |
| 392 |
"<p>your profile</p>", |
| 393 |
false, |
| 394 |
false, |
| 395 |
false, |
| 396 |
); |
| 397 |
assert!(html.contains("<p>your profile</p>"), "{html}"); |
| 398 |
assert!(html.contains("data-shows=\"1\""), "{html}"); |
| 399 |
} |
| 400 |
|
| 401 |
#[test] |
| 402 |
fn the_three_gated_sections_leave_when_their_test_fails() { |
| 403 |
let all = strip(true, true, true); |
| 404 |
for label in [">Media</button>", ">SSH Keys</button>", ">Forums</button>"] { |
| 405 |
assert!(all.contains(label), "{all}"); |
| 406 |
} |
| 407 |
|
| 408 |
let none = strip(false, false, false); |
| 409 |
for label in [">Media</button>", ">SSH Keys</button>", ">Forums</button>"] { |
| 410 |
assert!(!none.contains(label), "{none}"); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
assert_eq!(none.matches("hx-get=").count(), 2, "{none}"); |
| 415 |
assert!(none.contains("<p>your profile</p>"), "{none}"); |
| 416 |
assert!(none.contains("role=\"tablist\""), "{none}"); |
| 417 |
} |
| 418 |
} |
| 419 |
|