| 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 |
use makeover_layout as layout; |
| 64 |
use quasi_router::{Action, Node, RegionKind, Slot}; |
| 65 |
use quasi_webview::Webview; |
| 66 |
|
| 67 |
|
| 68 |
const STRIP: &str = "tab-content"; |
| 69 |
|
| 70 |
|
| 71 |
#[derive(PartialEq, Eq)] |
| 72 |
enum Gate { |
| 73 |
|
| 74 |
Always, |
| 75 |
|
| 76 |
Git, |
| 77 |
|
| 78 |
SyncKit, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
struct Tab { |
| 83 |
label: &'static str, |
| 84 |
|
| 85 |
panel: &'static str, |
| 86 |
|
| 87 |
route: &'static str, |
| 88 |
gate: Gate, |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
const TABS: &[Tab] = &[ |
| 93 |
Tab { |
| 94 |
label: "Overview", |
| 95 |
panel: super::project_overview::REGION, |
| 96 |
route: "overview", |
| 97 |
gate: Gate::Always, |
| 98 |
}, |
| 99 |
Tab { |
| 100 |
label: "Content", |
| 101 |
panel: "project-content", |
| 102 |
route: "content", |
| 103 |
gate: Gate::Always, |
| 104 |
}, |
| 105 |
Tab { |
| 106 |
label: "Analytics", |
| 107 |
panel: super::project_analytics::REGION, |
| 108 |
route: "analytics", |
| 109 |
gate: Gate::Always, |
| 110 |
}, |
| 111 |
Tab { |
| 112 |
label: "Monetization", |
| 113 |
panel: "project-monetization", |
| 114 |
route: "monetization", |
| 115 |
gate: Gate::Always, |
| 116 |
}, |
| 117 |
Tab { |
| 118 |
label: "Code", |
| 119 |
panel: "project-code", |
| 120 |
route: "code", |
| 121 |
gate: Gate::Git, |
| 122 |
}, |
| 123 |
Tab { |
| 124 |
label: "Cloud Sync", |
| 125 |
panel: "project-synckit", |
| 126 |
route: "synckit", |
| 127 |
gate: Gate::SyncKit, |
| 128 |
}, |
| 129 |
Tab { |
| 130 |
label: "Settings", |
| 131 |
panel: "project-settings", |
| 132 |
route: "settings", |
| 133 |
gate: Gate::Always, |
| 134 |
}, |
| 135 |
]; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
const FILLABLE: &[&str] = &["overview", "content", "synckit"]; |
| 143 |
|
| 144 |
|
| 145 |
#[must_use] |
| 146 |
pub fn shown_at(asked: Option<&str>, git: bool, synckit: bool) -> usize { |
| 147 |
let Some(asked) = asked else { return 0 }; |
| 148 |
if !FILLABLE.contains(&asked) { |
| 149 |
return 0; |
| 150 |
} |
| 151 |
visible(git, synckit) |
| 152 |
.iter() |
| 153 |
.position(|tab| tab.route == asked) |
| 154 |
.unwrap_or(0) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
#[must_use] |
| 159 |
pub fn route_at(shown: usize, git: bool, synckit: bool) -> &'static str { |
| 160 |
visible(git, synckit) |
| 161 |
.get(shown) |
| 162 |
.map_or(TABS[0].route, |tab| tab.route) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
fn visible(git: bool, synckit: bool) -> Vec<&'static Tab> { |
| 167 |
TABS.iter() |
| 168 |
.filter(|tab| match tab.gate { |
| 169 |
Gate::Always => true, |
| 170 |
Gate::Git => git, |
| 171 |
Gate::SyncKit => synckit, |
| 172 |
}) |
| 173 |
.collect() |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
#[must_use] |
| 178 |
pub fn html(slug: &str, shown: usize, panel: &str, git: bool, synckit: bool) -> String { |
| 179 |
let tabs = visible(git, synckit); |
| 180 |
let shown = shown.min(tabs.len().saturating_sub(1)); |
| 181 |
|
| 182 |
let mut strip = Slot::new(STRIP, RegionKind::TabGroup) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
.across(layout::Fallback::Menu) |
| 188 |
.showing_one(shown); |
| 189 |
|
| 190 |
for (at, tab) in tabs.iter().enumerate() { |
| 191 |
let mut region = Slot::handover(tab.panel, "project-panel"); |
| 192 |
if at != shown { |
| 193 |
|
| 194 |
|
| 195 |
region = region.fed_by( |
| 196 |
Action::get(format!("/dashboard/project/{slug}/tabs/{}", tab.route)) |
| 197 |
.awaiting() |
| 198 |
.replacing(tab.panel), |
| 199 |
); |
| 200 |
} |
| 201 |
strip = strip.frame(tab.label, Node::Region(region)); |
| 202 |
} |
| 203 |
|
| 204 |
use quasi_axum::Serves as _; |
| 205 |
|
| 206 |
Webview::new() |
| 207 |
.with_fill(tabs[shown].panel, panel) |
| 208 |
.fragment(&Node::Region(strip)) |
| 209 |
} |
| 210 |
|
| 211 |
#[cfg(test)] |
| 212 |
mod tests { |
| 213 |
use super::*; |
| 214 |
|
| 215 |
fn strip(git: bool, synckit: bool) -> String { |
| 216 |
html("a-project", 0, "<p>the overview</p>", git, synckit) |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn the_page_asks_for_nothing_on_load() { |
| 221 |
let html = strip(true, true); |
| 222 |
|
| 223 |
assert!(!html.contains("hx-trigger=\"load\""), "{html}"); |
| 224 |
assert!(html.contains("<p>the overview</p>"), "{html}"); |
| 225 |
|
| 226 |
assert_eq!(html.matches("hx-get=").count(), 6, "{html}"); |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn every_unshown_tab_says_where_its_answer_lands() { |
| 231 |
let html = strip(true, true); |
| 232 |
|
| 233 |
for panel in [ |
| 234 |
"project-content", |
| 235 |
"project-analytics", |
| 236 |
"project-monetization", |
| 237 |
"project-code", |
| 238 |
"project-synckit", |
| 239 |
"project-settings", |
| 240 |
] { |
| 241 |
assert!(html.contains(&format!("hx-target=\"#{panel}\"")), "{html}"); |
| 242 |
assert!(html.contains(&format!("id=\"{panel}\"")), "{html}"); |
| 243 |
} |
| 244 |
assert!(!html.contains("hx-target=\"#project-overview\""), "{html}"); |
| 245 |
} |
| 246 |
|
| 247 |
#[test] |
| 248 |
fn the_two_routes_that_are_not_tabs_stay_out() { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
let html = strip(true, true); |
| 263 |
|
| 264 |
for route in ["/tabs/subscriptions", "/tabs/members"] { |
| 265 |
assert!(!html.contains(route), "{route} is not a tab:\n{html}"); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn the_two_gated_tabs_leave_when_their_test_fails() { |
| 271 |
let both = strip(true, true); |
| 272 |
assert!(both.contains(">Code</button>"), "{both}"); |
| 273 |
assert!(both.contains(">Cloud Sync</button>"), "{both}"); |
| 274 |
|
| 275 |
let neither = strip(false, false); |
| 276 |
assert!(!neither.contains(">Code</button>"), "{neither}"); |
| 277 |
assert!(!neither.contains(">Cloud Sync</button>"), "{neither}"); |
| 278 |
assert!(neither.contains("role=\"tablist\""), "{neither}"); |
| 279 |
assert_eq!(neither.matches("hx-get=").count(), 4, "{neither}"); |
| 280 |
} |
| 281 |
|
| 282 |
#[test] |
| 283 |
fn a_deep_link_arrives_showing_what_it_asked_for() { |
| 284 |
|
| 285 |
let shown = shown_at(Some("synckit"), true, true); |
| 286 |
assert_eq!(shown, 5); |
| 287 |
assert_eq!(route_at(shown, true, true), "synckit"); |
| 288 |
|
| 289 |
let html = html("a-project", shown, "<p>the apps</p>", true, true); |
| 290 |
assert!(html.contains("<p>the apps</p>"), "{html}"); |
| 291 |
assert!(!html.contains("hx-target=\"#project-synckit\""), "{html}"); |
| 292 |
assert!(html.contains("hx-target=\"#project-overview\""), "{html}"); |
| 293 |
} |
| 294 |
|
| 295 |
#[test] |
| 296 |
fn a_tab_the_page_cannot_fill_is_not_opened() { |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
for asked in ["analytics", "monetization", "code", "settings"] { |
| 301 |
assert_eq!(shown_at(Some(asked), true, true), 0, "{asked}"); |
| 302 |
} |
| 303 |
|
| 304 |
assert_eq!(shown_at(Some("overview"), true, true), 0); |
| 305 |
assert_eq!(shown_at(Some("content"), true, true), 1); |
| 306 |
} |
| 307 |
|
| 308 |
#[test] |
| 309 |
fn a_closed_gate_moves_the_index_of_what_follows_it() { |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
assert_eq!(shown_at(Some("synckit"), false, true), 4); |
| 314 |
|
| 315 |
assert_eq!(shown_at(Some("synckit"), true, false), 0); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn an_unknown_or_absent_tab_is_the_first_one() { |
| 320 |
assert_eq!(shown_at(None, true, true), 0); |
| 321 |
assert_eq!(shown_at(Some("nonsense"), true, true), 0); |
| 322 |
} |
| 323 |
|
| 324 |
#[test] |
| 325 |
fn a_shown_index_past_the_end_cannot_panic() { |
| 326 |
let html = html("a-project", 99, "<p>the overview</p>", false, false); |
| 327 |
assert!(html.contains("<p>the overview</p>"), "{html}"); |
| 328 |
} |
| 329 |
|
| 330 |
#[test] |
| 331 |
fn the_strip_says_what_it_does_when_it_runs_out_of_room() { |
| 332 |
assert!(strip(true, true).contains("run-menu")); |
| 333 |
} |
| 334 |
} |
| 335 |
|