| 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 |
use quasi_declare::declare; |
| 46 |
use quasi_router::Action; |
| 47 |
use quasi_router::screen::Choice; |
| 48 |
|
| 49 |
use crate::git::{Breadcrumb, RefInfo}; |
| 50 |
|
| 51 |
|
| 52 |
pub const REGION: &str = "git-nav"; |
| 53 |
|
| 54 |
|
| 55 |
pub const CHOOSE_PATH: &str = "/git/{owner}/{repo}/ref"; |
| 56 |
|
| 57 |
|
| 58 |
pub const CHOSEN: &str = "ref"; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pub const NO_REFS: &[RefInfo] = &[]; |
| 64 |
|
| 65 |
|
| 66 |
pub struct Nav<'a> { |
| 67 |
pub owner: &'a str, |
| 68 |
pub repo: &'a str, |
| 69 |
|
| 70 |
pub current_ref: &'a str, |
| 71 |
|
| 72 |
pub active_tab: &'a str, |
| 73 |
pub open_issue_count: i64, |
| 74 |
|
| 75 |
pub is_owner: bool, |
| 76 |
pub refs: &'a [RefInfo], |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct Tab { |
| 87 |
|
| 88 |
label: String, |
| 89 |
|
| 90 |
route: String, |
| 91 |
|
| 92 |
current: bool, |
| 93 |
} |
| 94 |
|
| 95 |
impl Nav<'_> { |
| 96 |
|
| 97 |
fn base(&self) -> String { |
| 98 |
format!("/git/{}/{}", self.owner, self.repo) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
fn ref_path(&self) -> String { |
| 103 |
format!("{}/ref", self.base()) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
fn has_refs(&self) -> bool { |
| 108 |
!self.refs.is_empty() |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
fn tabs(&self) -> Vec<Tab> { |
| 113 |
let base = self.base(); |
| 114 |
let current = |
| 115 |
|key: &str| self.active_tab == key || (key == "commits" && self.active_tab == "commit"); |
| 116 |
|
| 117 |
let mut tabs = vec![ |
| 118 |
Tab { |
| 119 |
label: "Files".to_owned(), |
| 120 |
route: format!("{base}/tree/{}", self.current_ref), |
| 121 |
current: current("files"), |
| 122 |
}, |
| 123 |
Tab { |
| 124 |
label: "Commits".to_owned(), |
| 125 |
route: format!("{base}/commits/{}", self.current_ref), |
| 126 |
current: current("commits"), |
| 127 |
}, |
| 128 |
Tab { |
| 129 |
label: "Tags".to_owned(), |
| 130 |
route: format!("{base}/tags"), |
| 131 |
current: current("tags"), |
| 132 |
}, |
| 133 |
Tab { |
| 134 |
label: "Notes".to_owned(), |
| 135 |
route: format!("{base}/notes"), |
| 136 |
current: current("notes"), |
| 137 |
}, |
| 138 |
Tab { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
label: if self.open_issue_count > 0 { |
| 143 |
format!("Issues ({})", self.open_issue_count) |
| 144 |
} else { |
| 145 |
"Issues".to_owned() |
| 146 |
}, |
| 147 |
route: format!("{base}/issues"), |
| 148 |
current: current("issues"), |
| 149 |
}, |
| 150 |
]; |
| 151 |
if self.is_owner { |
| 152 |
tabs.push(Tab { |
| 153 |
label: "Settings".to_owned(), |
| 154 |
route: format!("{base}/settings"), |
| 155 |
current: current("settings"), |
| 156 |
}); |
| 157 |
} |
| 158 |
tabs |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
fn ref_label(info: &RefInfo) -> String { |
| 172 |
if info.is_branch { |
| 173 |
info.name.clone() |
| 174 |
} else { |
| 175 |
format!("tag: {}", info.name) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
declare! { |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
#[must_use] |
| 192 |
pub shape region(nav: &Nav<'_>) -> Node; |
| 193 |
|
| 194 |
region REGION as Group { |
| 195 |
across Wrap { |
| 196 |
beside Essential include chooser(nav) when nav.has_refs(); |
| 197 |
beside Essential include strip(nav); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
#[must_use] |
| 204 |
pub fn html(nav: &Nav<'_>) -> String { |
| 205 |
use quasi_axum::Serves as _; |
| 206 |
|
| 207 |
|
| 208 |
quasi_webview::Webview::new().fragment(®ion(nav)) |
| 209 |
} |
| 210 |
|
| 211 |
declare! { |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
shape chooser(nav: &Nav<'_>) -> Field; |
| 223 |
|
| 224 |
field Select CHOSEN "Branch or tag" { |
| 225 |
for info in nav.refs.iter() { |
| 226 |
option Choice::new(&info.name, ref_label(info)); |
| 227 |
} |
| 228 |
value nav.current_ref; |
| 229 |
writes Action::get(nav.ref_path()).navigating(); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
declare! { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
shape strip(nav: &Nav<'_>) -> Node; |
| 242 |
|
| 243 |
list { |
| 244 |
for tab in nav.tabs() { |
| 245 |
row tab.label { |
| 246 |
current tab.current; |
| 247 |
activate to get tab.route navigating; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
declare! { |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
#[must_use] |
| 262 |
pub shape heading(owner: &str, repo: &str) -> Node; |
| 263 |
|
| 264 |
region "git-repo-name" as Group { |
| 265 |
named "{owner} / {repo}"; |
| 266 |
across Wrap { |
| 267 |
beside Essential link owner to get "/git/{owner}" navigating; |
| 268 |
beside Essential link repo to get "/git/{owner}/{repo}" navigating; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
declare! { |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
#[must_use] |
| 280 |
pub shape breadcrumb( |
| 281 |
owner: &str, |
| 282 |
repo: &str, |
| 283 |
current_ref: &str, |
| 284 |
crumbs: &[Breadcrumb], |
| 285 |
) -> Node; |
| 286 |
|
| 287 |
let tree = "/git/{owner}/{repo}/tree/{current_ref}"; |
| 288 |
|
| 289 |
region "git-breadcrumb" as Group { |
| 290 |
named "Path"; |
| 291 |
across Wrap { |
| 292 |
beside Essential link repo to get tree.clone() navigating; |
| 293 |
for crumb in crumbs.iter() { |
| 294 |
beside Essential given crumb.is_link { |
| 295 |
true -> link crumb.name.clone() to get "{tree}/{crumb.path}" navigating; |
| 296 |
otherwise -> text crumb.name.clone(); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
#[cfg(test)] |
| 304 |
mod tests { |
| 305 |
use super::*; |
| 306 |
|
| 307 |
fn refs() -> Vec<RefInfo> { |
| 308 |
vec![ |
| 309 |
RefInfo { |
| 310 |
name: "main".into(), |
| 311 |
is_branch: true, |
| 312 |
}, |
| 313 |
RefInfo { |
| 314 |
name: "v1.0.0".into(), |
| 315 |
is_branch: false, |
| 316 |
}, |
| 317 |
] |
| 318 |
} |
| 319 |
|
| 320 |
fn nav<'a>(refs: &'a [RefInfo], active: &'a str, is_owner: bool, issues: i64) -> Nav<'a> { |
| 321 |
Nav { |
| 322 |
owner: "ada", |
| 323 |
repo: "engine", |
| 324 |
current_ref: "main", |
| 325 |
active_tab: active, |
| 326 |
open_issue_count: issues, |
| 327 |
is_owner, |
| 328 |
refs, |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
#[test] |
| 334 |
fn the_current_tab_is_the_current_row() { |
| 335 |
let refs = refs(); |
| 336 |
let html = html(&nav(&refs, "tags", false, 0)); |
| 337 |
|
| 338 |
let tags = html |
| 339 |
.split("<li") |
| 340 |
.find(|row| row.contains("/git/ada/engine/tags")) |
| 341 |
.expect("a Tags row"); |
| 342 |
assert!(tags.contains("row-current"), "{html}"); |
| 343 |
assert_eq!(html.matches("row-current").count(), 1, "{html}"); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
#[test] |
| 349 |
fn one_commit_marks_the_commits_tab() { |
| 350 |
let refs = refs(); |
| 351 |
let html = html(&nav(&refs, "commit", false, 0)); |
| 352 |
|
| 353 |
let commits = html |
| 354 |
.split("<li") |
| 355 |
.find(|row| row.contains("/git/ada/engine/commits/")) |
| 356 |
.expect("a Commits row"); |
| 357 |
assert!(commits.contains("row-current"), "{html}"); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
#[test] |
| 363 |
fn only_the_owner_is_offered_settings() { |
| 364 |
let refs = refs(); |
| 365 |
|
| 366 |
assert!(html(&nav(&refs, "files", true, 0)).contains("/settings")); |
| 367 |
assert!(!html(&nav(&refs, "files", false, 0)).contains("/settings")); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
#[test] |
| 373 |
fn the_issue_count_is_shown_only_when_there_is_one() { |
| 374 |
let refs = refs(); |
| 375 |
|
| 376 |
assert!(html(&nav(&refs, "files", false, 3)).contains("Issues (3)")); |
| 377 |
let none = html(&nav(&refs, "files", false, 0)); |
| 378 |
assert!(none.contains("Issues"), "{none}"); |
| 379 |
assert!(!none.contains("Issues ("), "{none}"); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
#[test] |
| 386 |
fn a_tag_reads_as_a_tag_and_submits_as_its_name() { |
| 387 |
let refs = refs(); |
| 388 |
let html = html(&nav(&refs, "files", false, 0)); |
| 389 |
|
| 390 |
assert!( |
| 391 |
html.contains("<option value=\"v1.0.0\">tag: v1.0.0</option>"), |
| 392 |
"{html}" |
| 393 |
); |
| 394 |
assert!( |
| 395 |
html.contains("<option value=\"main\" selected>main</option>"), |
| 396 |
"{html}" |
| 397 |
); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
#[test] |
| 403 |
fn choosing_a_ref_asks_the_join_route() { |
| 404 |
let refs = refs(); |
| 405 |
let html = html(&nav(&refs, "files", false, 0)); |
| 406 |
|
| 407 |
assert!(html.contains("/git/ada/engine/ref"), "{html}"); |
| 408 |
assert!(html.contains("name=\"ref\""), "{html}"); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
#[test] |
| 414 |
fn a_page_with_no_refs_still_carries_the_strip() { |
| 415 |
let html = html(&nav(NO_REFS, "issues", true, 1)); |
| 416 |
|
| 417 |
assert!(!html.contains("<select"), "{html}"); |
| 418 |
assert!(html.contains("/git/ada/engine/tags"), "{html}"); |
| 419 |
assert!(html.contains("/git/ada/engine/notes"), "{html}"); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
#[test] |
| 425 |
fn a_branch_name_cannot_smuggle_markup() { |
| 426 |
let refs = vec![RefInfo { |
| 427 |
name: "<script>alert(1)</script>".into(), |
| 428 |
is_branch: true, |
| 429 |
}]; |
| 430 |
let html = html(&nav(&refs, "files", false, 0)); |
| 431 |
|
| 432 |
assert!(!html.contains("<script>"), "{html}"); |
| 433 |
assert!(html.contains("<script>"), "{html}"); |
| 434 |
} |
| 435 |
} |
| 436 |
|