| 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 |
use makeover_layout as layout; |
| 62 |
use quasi_router::{Action, Node, RegionKind, Slot}; |
| 63 |
use quasi_webview::Webview; |
| 64 |
|
| 65 |
use crate::config::QuasiScreens; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
const STRIP: &str = "tab-content"; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#[derive(PartialEq, Eq)] |
| 77 |
enum Gate { |
| 78 |
|
| 79 |
Always, |
| 80 |
|
| 81 |
Communities, |
| 82 |
|
| 83 |
Creators, |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
struct Tab { |
| 88 |
label: &'static str, |
| 89 |
|
| 90 |
|
| 91 |
gate: Gate, |
| 92 |
|
| 93 |
|
| 94 |
panel: &'static str, |
| 95 |
route: &'static str, |
| 96 |
|
| 97 |
|
| 98 |
screen: Option<&'static str>, |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
const TABS: &[Tab] = &[ |
| 108 |
Tab { |
| 109 |
label: "Purchases", |
| 110 |
gate: Gate::Always, |
| 111 |
panel: "library-purchases", |
| 112 |
route: "/library/tabs/purchases", |
| 113 |
screen: None, |
| 114 |
}, |
| 115 |
Tab { |
| 116 |
label: "Feed", |
| 117 |
gate: Gate::Always, |
| 118 |
panel: "library-feed", |
| 119 |
route: "/library/tabs/feed", |
| 120 |
screen: None, |
| 121 |
}, |
| 122 |
Tab { |
| 123 |
label: "Collections", |
| 124 |
gate: Gate::Always, |
| 125 |
panel: "library-collections", |
| 126 |
route: "/library/tabs/collections", |
| 127 |
screen: None, |
| 128 |
}, |
| 129 |
Tab { |
| 130 |
label: "Communities", |
| 131 |
gate: Gate::Communities, |
| 132 |
panel: super::forum_memberships::LIBRARY_REGION, |
| 133 |
route: super::forum_memberships::LIBRARY_PATH, |
| 134 |
screen: Some(super::forum_memberships::LIBRARY_SCREEN), |
| 135 |
}, |
| 136 |
Tab { |
| 137 |
label: "Contacts", |
| 138 |
gate: Gate::Creators, |
| 139 |
panel: super::library_contacts::REGION, |
| 140 |
route: super::library_contacts::PATH, |
| 141 |
screen: Some(super::library_contacts::SCREEN), |
| 142 |
}, |
| 143 |
]; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
#[must_use] |
| 151 |
pub fn html( |
| 152 |
screens: &QuasiScreens, |
| 153 |
purchases: &str, |
| 154 |
has_mt_memberships: bool, |
| 155 |
can_create_projects: bool, |
| 156 |
) -> String { |
| 157 |
let shown: Vec<&Tab> = TABS |
| 158 |
.iter() |
| 159 |
.filter(|tab| match tab.gate { |
| 160 |
Gate::Always => true, |
| 161 |
Gate::Communities => has_mt_memberships, |
| 162 |
Gate::Creators => can_create_projects, |
| 163 |
}) |
| 164 |
.collect(); |
| 165 |
|
| 166 |
let mut strip = Slot::new(STRIP, RegionKind::TabGroup) |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
.across(layout::Fallback::Menu) |
| 174 |
.showing_one(0); |
| 175 |
|
| 176 |
for (at, tab) in shown.iter().enumerate() { |
| 177 |
let mut panel = Slot::bespoke(tab.panel, "library-panel").label(tab.label); |
| 178 |
if at > 0 { |
| 179 |
let mut call = Action::get(tab.route).awaiting(); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if !tab.screen.is_some_and(|name| screens.enabled(name)) { |
| 184 |
call = call.replacing(tab.panel); |
| 185 |
} |
| 186 |
panel = panel.fed_by(call); |
| 187 |
} |
| 188 |
strip = strip.with(Node::Region(panel)); |
| 189 |
} |
| 190 |
|
| 191 |
use quasi_axum::Serves as _; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
Webview::new() |
| 196 |
.with_fill(TABS[0].panel, purchases) |
| 197 |
.fragment(&Node::Region(strip)) |
| 198 |
} |
| 199 |
|
| 200 |
#[cfg(test)] |
| 201 |
mod tests { |
| 202 |
use super::*; |
| 203 |
|
| 204 |
fn strip(has_mt: bool, can_create: bool) -> String { |
| 205 |
html( |
| 206 |
&QuasiScreens::default(), |
| 207 |
"<p>your purchases</p>", |
| 208 |
has_mt, |
| 209 |
can_create, |
| 210 |
) |
| 211 |
} |
| 212 |
|
| 213 |
#[test] |
| 214 |
fn the_page_makes_no_more_requests_than_it_did_before() { |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
let html = strip(true, true); |
| 220 |
|
| 221 |
assert!(!html.contains("hx-trigger=\"load\""), "{html}"); |
| 222 |
assert!(html.contains("<p>your purchases</p>"), "{html}"); |
| 223 |
assert_eq!(html.matches("hx-get=").count(), 4, "{html}"); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn every_tab_but_the_shown_one_says_where_its_answer_lands() { |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
let html = strip(true, true); |
| 233 |
|
| 234 |
for panel in [ |
| 235 |
"library-feed", |
| 236 |
"library-collections", |
| 237 |
"library-communities", |
| 238 |
"library-contacts", |
| 239 |
] { |
| 240 |
assert!(html.contains(&format!("hx-target=\"#{panel}\"")), "{html}"); |
| 241 |
assert!(html.contains(&format!("id=\"{panel}\"")), "{html}"); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
assert!(!html.contains("hx-target=\"#library-purchases\""), "{html}"); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn a_described_panel_is_left_to_name_its_own_region() { |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
let screens = QuasiScreens::parse(super::super::library_contacts::SCREEN); |
| 255 |
let html = html(&screens, "<p>your purchases</p>", true, true); |
| 256 |
|
| 257 |
assert!( |
| 258 |
!html.contains("hx-target=\"#library-contacts\""), |
| 259 |
"a described panel retargets its own answer:\n{html}" |
| 260 |
); |
| 261 |
|
| 262 |
assert!(html.contains("hx-target=\"#library-feed\""), "{html}"); |
| 263 |
assert!( |
| 264 |
html.contains("hx-target=\"#library-communities\""), |
| 265 |
"{html}" |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn the_two_gated_tabs_leave_when_their_test_fails() { |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
let both = strip(true, true); |
| 275 |
assert!(both.contains(">Communities</button>"), "{both}"); |
| 276 |
assert!(both.contains(">Contacts</button>"), "{both}"); |
| 277 |
|
| 278 |
let neither = strip(false, false); |
| 279 |
assert!(!neither.contains(">Communities</button>"), "{neither}"); |
| 280 |
assert!(!neither.contains(">Contacts</button>"), "{neither}"); |
| 281 |
|
| 282 |
assert!(neither.contains("role=\"tablist\""), "{neither}"); |
| 283 |
assert!(neither.contains("<p>your purchases</p>"), "{neither}"); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn the_strip_says_what_it_does_when_it_runs_out_of_room() { |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
assert!(strip(true, true).contains("run-menu")); |
| 292 |
} |
| 293 |
} |
| 294 |
|