| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | use topcoat::{ |
| 20 | Result, |
| 21 | view::{component, view}, |
| 22 | }; |
| 23 | |
| 24 | |
| 25 | const WINDOW: usize = 1; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | pub(super) struct Step { |
| 32 | pub label: String, |
| 33 | pub href: Option<String>, |
| 34 | pub current: bool, |
| 35 | } |
| 36 | |
| 37 | impl Step { |
| 38 | fn link(label: impl Into<String>, href: String) -> Self { |
| 39 | Self { |
| 40 | label: label.into(), |
| 41 | href: Some(href), |
| 42 | current: false, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn here(label: impl Into<String>) -> Self { |
| 47 | Self { |
| 48 | label: label.into(), |
| 49 | href: None, |
| 50 | current: true, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn gap() -> Self { |
| 55 | Self { |
| 56 | label: "…".to_owned(), |
| 57 | href: None, |
| 58 | current: false, |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | pub(super) struct Pages { |
| 68 | pub steps: Vec<Step>, |
| 69 | } |
| 70 | |
| 71 | impl Pages { |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | pub fn numbered(current: usize, total: usize, url: impl Fn(usize) -> String) -> Self { |
| 81 | if total <= 1 { |
| 82 | return Self { steps: Vec::new() }; |
| 83 | } |
| 84 | |
| 85 | let mut steps = Vec::new(); |
| 86 | |
| 87 | if current > 1 { |
| 88 | steps.push(Step::link("← Previous", url(current - 1))); |
| 89 | } |
| 90 | |
| 91 | let mut last: Option<usize> = None; |
| 92 | |
| 93 | for page in 1..=total { |
| 94 | let near = page.abs_diff(current) <= WINDOW; |
| 95 | let end = page == 1 || page == total; |
| 96 | |
| 97 | if !near && !end { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if last.is_some_and(|last| page > last + 1) { |
| 102 | steps.push(Step::gap()); |
| 103 | } |
| 104 | |
| 105 | steps.push(match page == current { |
| 106 | true => Step::here(page.to_string()), |
| 107 | false => Step::link(page.to_string(), url(page)), |
| 108 | }); |
| 109 | |
| 110 | last = Some(page); |
| 111 | } |
| 112 | |
| 113 | if current < total { |
| 114 | steps.push(Step::link("Next →", url(current + 1))); |
| 115 | } |
| 116 | |
| 117 | Self { steps } |
| 118 | } |
| 119 | |
| 120 | pub fn is_empty(&self) -> bool { |
| 121 | self.steps.is_empty() |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | pub(super) fn requested_page(value: Option<&str>) -> usize { |
| 130 | value |
| 131 | .and_then(|value| value.trim().parse::<usize>().ok()) |
| 132 | .filter(|page| *page > 0) |
| 133 | .unwrap_or(1) |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | pub(super) fn showing_at_most( |
| 146 | offset: usize, |
| 147 | shown: usize, |
| 148 | total: usize, |
| 149 | capped: bool, |
| 150 | plural: &str, |
| 151 | ) -> String { |
| 152 | if !capped || shown == 0 { |
| 153 | return showing(offset, shown, total, plural); |
| 154 | } |
| 155 | |
| 156 | format!( |
| 157 | "Showing {}–{} of {total}+ {plural}", |
| 158 | offset + 1, |
| 159 | offset + shown |
| 160 | ) |
| 161 | } |
| 162 | |
| 163 | pub(super) fn showing(offset: usize, shown: usize, total: usize, plural: &str) -> String { |
| 164 | if shown == 0 { |
| 165 | return format!("No {plural}."); |
| 166 | } |
| 167 | |
| 168 | format!( |
| 169 | "Showing {}–{} of {total} {plural}", |
| 170 | offset + 1, |
| 171 | offset + shown |
| 172 | ) |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | #[component] |
| 181 | pub(super) async fn list_pages(summary: &str, pages: &Pages) -> Result { |
| 182 | view! { |
| 183 | if !pages.is_empty() { |
| 184 | <div class="mt-2 space-y-1"> |
| 185 | <p class="text-xs text-muted-foreground">(summary)</p> |
| 186 | pager(pages: pages) |
| 187 | </div> |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | #[component] |
| 199 | pub(super) async fn pager(pages: &Pages) -> Result { |
| 200 | view! { |
| 201 | if !pages.is_empty() { |
| 202 | <nav |
| 203 | aria-label="Pagination" |
| 204 | class="mt-2 flex flex-wrap items-center justify-end gap-x-2 gap-y-1 font-mono text-xs text-muted-foreground" |
| 205 | > |
| 206 | for step in &pages.steps { |
| 207 | match (step.href.as_deref(), step.current) { |
| 208 | (Some(href), _) => <a href=(href) class="hover:text-foreground">(step.label.as_str())</a>, |
| 209 | (None, true) => <span class="text-foreground" aria-current="page">(step.label.as_str())</span>, |
| 210 | (None, false) => <span aria-hidden="true">(step.label.as_str())</span>, |
| 211 | } |
| 212 | } |
| 213 | </nav> |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #[cfg(test)] |
| 219 | mod tests { |
| 220 | use super::*; |
| 221 | |
| 222 | fn labels(pages: &Pages) -> Vec<&str> { |
| 223 | pages.steps.iter().map(|step| step.label.as_str()).collect() |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn one_page_renders_nothing() { |
| 228 | assert!(Pages::numbered(1, 1, |page| format!("?page={page}")).is_empty()); |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn the_first_page_has_no_previous() { |
| 233 | let pages = Pages::numbered(1, 3, |page| format!("?page={page}")); |
| 234 | |
| 235 | assert_eq!(labels(&pages), ["1", "2", "3", "Next →"]); |
| 236 | } |
| 237 | |
| 238 | #[test] |
| 239 | fn the_last_page_has_no_next() { |
| 240 | let pages = Pages::numbered(3, 3, |page| format!("?page={page}")); |
| 241 | |
| 242 | assert_eq!(labels(&pages), ["← Previous", "1", "2", "3"]); |
| 243 | } |
| 244 | |
| 245 | #[test] |
| 246 | fn the_current_page_is_not_a_link() { |
| 247 | let pages = Pages::numbered(2, 3, |page| format!("?page={page}")); |
| 248 | let current: Vec<_> = pages |
| 249 | .steps |
| 250 | .iter() |
| 251 | .filter(|step| step.current) |
| 252 | .map(|step| step.label.as_str()) |
| 253 | .collect(); |
| 254 | |
| 255 | assert_eq!(current, ["2"]); |
| 256 | assert!( |
| 257 | pages |
| 258 | .steps |
| 259 | .iter() |
| 260 | .all(|step| !step.current || step.href.is_none()) |
| 261 | ); |
| 262 | } |
| 263 | |
| 264 | #[test] |
| 265 | fn a_long_run_collapses_around_the_current_page() { |
| 266 | let pages = Pages::numbered(5, 12, |page| format!("?page={page}")); |
| 267 | |
| 268 | assert_eq!( |
| 269 | labels(&pages), |
| 270 | ["← Previous", "1", "…", "4", "5", "6", "…", "12", "Next →"] |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn a_gap_of_one_is_shown_rather_than_elided() { |
| 276 | // Page 3 of 6 windows to 2..4, so page 1 is adjacent and there is nothing to |
| 277 | // hide — an ellipsis standing in for a single number is longer than the number. |
| 278 | let pages = Pages::numbered(3, 6, |page| format!("?page={page}")); |
| 279 | |
| 280 | assert_eq!( |
| 281 | labels(&pages), |
| 282 | ["← Previous", "1", "2", "3", "4", "…", "6", "Next →"] |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | #[test] |
| 287 | fn links_carry_whatever_the_caller_puts_in_the_url() { |
| 288 | let pages = Pages::numbered(1, 2, |page| format!("/issues?state=closed&page={page}")); |
| 289 | let next = pages.steps.last().expect("a next link"); |
| 290 | |
| 291 | assert_eq!(next.href.as_deref(), Some("/issues?state=closed&page=2")); |
| 292 | } |
| 293 | |
| 294 | #[test] |
| 295 | fn an_unreadable_page_number_is_the_first_page() { |
| 296 | assert_eq!(requested_page(Some("400")), 400); |
| 297 | assert_eq!(requested_page(Some("0")), 1); |
| 298 | assert_eq!(requested_page(Some("banana")), 1); |
| 299 | assert_eq!(requested_page(Some("-2")), 1); |
| 300 | assert_eq!(requested_page(None), 1); |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn the_sentence_names_the_range_and_the_whole() { |
| 305 | assert_eq!( |
| 306 | showing(50, 50, 312, "commits"), |
| 307 | "Showing 51–100 of 312 commits" |
| 308 | ); |
| 309 | assert_eq!(showing(0, 3, 3, "issues"), "Showing 1–3 of 3 issues"); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn a_capped_total_says_so_in_the_sentence() { |
| 314 | // "500" and "at least 500" are different statements about a file's history. |
| 315 | assert_eq!( |
| 316 | showing_at_most(0, 50, 500, true, "commits"), |
| 317 | "Showing 1–50 of 500+ commits" |
| 318 | ); |
| 319 | assert_eq!( |
| 320 | showing_at_most(0, 50, 500, false, "commits"), |
| 321 | "Showing 1–50 of 500 commits" |
| 322 | ); |
| 323 | } |
| 324 | } |