| 1 | use std::fmt::Display; |
| 2 | |
| 3 | use app_base::ui_locale::tr_with; |
| 4 | use chrono::{DateTime, Datelike, TimeZone, Utc}; |
| 5 | |
| 6 | use crate::rows::ReactionChip; |
| 7 | |
| 8 | const FACE_LIMIT: usize = 3; |
| 9 | const TOOLTIP_NAME_LIMIT: usize = 3; |
| 10 | const TOOLTIP_LEAD_NAMES: usize = 2; |
| 11 | pub const UNKNOWN_REACTOR: &str = "Unknown"; |
| 12 | |
| 13 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 14 | pub struct Reactor { |
| 15 | pub user_id: Option<String>, |
| 16 | pub name: String, |
| 17 | pub created_at: Option<DateTime<Utc>>, |
| 18 | } |
| 19 | |
| 20 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 21 | pub struct ReactionTab { |
| 22 | pub glyph: Option<String>, |
| 23 | pub label: String, |
| 24 | pub count: usize, |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 28 | pub struct ReactionEntry { |
| 29 | pub reactor: Reactor, |
| 30 | pub glyph: String, |
| 31 | pub label: String, |
| 32 | } |
| 33 | |
| 34 | pub fn shows_faces(count: usize) -> bool { |
| 35 | (1..=FACE_LIMIT).contains(&count) |
| 36 | } |
| 37 | |
| 38 | pub fn tooltip_text(names: &[&str]) -> String { |
| 39 | match names { |
| 40 | [] => String::new(), |
| 41 | [only] => (*only).to_owned(), |
| 42 | names if names.len() <= TOOLTIP_NAME_LIMIT => { |
| 43 | let (last, lead) = names.split_last().expect("not empty"); |
| 44 | format!("{} and {last}", lead.join(", ")) |
| 45 | } |
| 46 | names => format!( |
| 47 | "{} and {} others", |
| 48 | names[..TOOLTIP_LEAD_NAMES].join(", "), |
| 49 | names.len() - TOOLTIP_LEAD_NAMES |
| 50 | ), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn chip_tooltip(chip: &ReactionChip) -> String { |
| 55 | let names: Vec<&str> = chip |
| 56 | .reactors |
| 57 | .iter() |
| 58 | .map(|reactor| reactor.name.as_str()) |
| 59 | .collect(); |
| 60 | tooltip_text(&names) |
| 61 | } |
| 62 | |
| 63 | pub fn reaction_time_label<Zone>(time: DateTime<Utc>, now: &DateTime<Zone>) -> String |
| 64 | where |
| 65 | Zone: TimeZone, |
| 66 | Zone::Offset: Display, |
| 67 | { |
| 68 | let local = time.with_timezone(&now.timezone()); |
| 69 | let day = local.date_naive(); |
| 70 | let today = now.date_naive(); |
| 71 | let clock = local.format(app_base::format::clock_format()).to_string(); |
| 72 | let clock = ("clock", clock.as_str()); |
| 73 | if day == today { |
| 74 | tr_with("regional.today_at", &[clock]) |
| 75 | } else if today.pred_opt() == Some(day) { |
| 76 | tr_with("regional.yesterday_at", &[clock]) |
| 77 | } else { |
| 78 | let date = if day.year() == today.year() { |
| 79 | app_base::format::day_month_label(day) |
| 80 | } else { |
| 81 | app_base::format::date_label(day) |
| 82 | }; |
| 83 | tr_with("regional.date_at", &[("date", &date), clock]) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub fn reaction_tabs(chips: &[ReactionChip]) -> Vec<ReactionTab> { |
| 88 | if chips.len() < 2 { |
| 89 | return Vec::new(); |
| 90 | } |
| 91 | let total = chips.iter().map(|chip| chip.reactors.len()).sum(); |
| 92 | std::iter::once(ReactionTab { |
| 93 | glyph: None, |
| 94 | label: "All".to_owned(), |
| 95 | count: total, |
| 96 | }) |
| 97 | .chain(chips.iter().map(|chip| ReactionTab { |
| 98 | glyph: Some(chip.glyph()), |
| 99 | label: chip.label.clone(), |
| 100 | count: chip.reactors.len(), |
| 101 | })) |
| 102 | .collect() |
| 103 | } |
| 104 | |
| 105 | pub fn reaction_entries(chips: &[ReactionChip], tab: Option<&str>) -> Vec<ReactionEntry> { |
| 106 | let mut entries: Vec<ReactionEntry> = chips |
| 107 | .iter() |
| 108 | .filter(|chip| tab.is_none_or(|glyph| chip.glyph() == glyph)) |
| 109 | .flat_map(|chip| { |
| 110 | chip.reactors.iter().map(|reactor| ReactionEntry { |
| 111 | reactor: reactor.clone(), |
| 112 | glyph: chip.glyph(), |
| 113 | label: chip.label.clone(), |
| 114 | }) |
| 115 | }) |
| 116 | .collect(); |
| 117 | entries.sort_by_key(|entry| std::cmp::Reverse(entry.reactor.created_at)); |
| 118 | entries |
| 119 | } |
| 120 | |
| 121 | #[cfg(test)] |
| 122 | mod tests { |
| 123 | use chrono::{Duration, FixedOffset}; |
| 124 | |
| 125 | use super::*; |
| 126 | |
| 127 | fn reactor(name: &str, created_at: Option<DateTime<Utc>>) -> Reactor { |
| 128 | Reactor { |
| 129 | user_id: None, |
| 130 | name: name.to_owned(), |
| 131 | created_at, |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fn chip(reaction_type: &str, reactors: Vec<Reactor>) -> ReactionChip { |
| 136 | ReactionChip { |
| 137 | reaction_type: reaction_type.to_owned(), |
| 138 | label: crate::rows::reaction_label(reaction_type), |
| 139 | click_glyph: crate::rows::reaction_glyph(reaction_type), |
| 140 | count: reactors.len(), |
| 141 | mine: false, |
| 142 | reactors, |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fn utc(day: u32, hour: u32, minute: u32) -> DateTime<Utc> { |
| 147 | Utc.with_ymd_and_hms(2026, 10, day, hour, minute, 0) |
| 148 | .unwrap() |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn faces_show_up_to_three_people_then_a_count() { |
| 153 | assert!(!shows_faces(0)); |
| 154 | assert!(shows_faces(1)); |
| 155 | assert!(shows_faces(3)); |
| 156 | assert!(!shows_faces(4)); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn tooltip_lists_names_then_others() { |
| 161 | assert_eq!(tooltip_text(&["Priya Nair"]), "Priya Nair"); |
| 162 | assert_eq!(tooltip_text(&["Priya Nair", "Lea"]), "Priya Nair and Lea"); |
| 163 | assert_eq!(tooltip_text(&["A", "B", "C"]), "A, B and C"); |
| 164 | assert_eq!( |
| 165 | tooltip_text(&["Jonas Ortega", "Tobias Klein", "C", "D", "E"]), |
| 166 | "Jonas Ortega, Tobias Klein and 3 others" |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn time_label_reads_today_yesterday_or_date() { |
| 172 | let now = FixedOffset::east_opt(0) |
| 173 | .unwrap() |
| 174 | .with_ymd_and_hms(2026, 10, 8, 12, 0, 0) |
| 175 | .unwrap(); |
| 176 | assert_eq!(reaction_time_label(utc(8, 1, 5), &now), "today at 01:05"); |
| 177 | assert_eq!( |
| 178 | reaction_time_label(utc(7, 17, 31), &now), |
| 179 | "yesterday at 17:31" |
| 180 | ); |
| 181 | assert_eq!(reaction_time_label(utc(4, 17, 31), &now), "Oct 4 at 17:31"); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn tabs_appear_only_for_several_kinds() { |
| 186 | let one = vec![chip("like", vec![reactor("A", None)])]; |
| 187 | assert!(reaction_tabs(&one).is_empty()); |
| 188 | let two = vec![ |
| 189 | chip("like", vec![reactor("A", None), reactor("B", None)]), |
| 190 | chip("heart", vec![reactor("C", None)]), |
| 191 | ]; |
| 192 | let tabs = reaction_tabs(&two); |
| 193 | assert_eq!(tabs.len(), 3); |
| 194 | assert_eq!((tabs[0].glyph.clone(), tabs[0].count), (None, 3)); |
| 195 | assert_eq!(tabs[1].count, 2); |
| 196 | assert_eq!(tabs[2].count, 1); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn entries_are_newest_first_and_filtered_by_tab() { |
| 201 | let chips = vec![ |
| 202 | chip( |
| 203 | "like", |
| 204 | vec![ |
| 205 | reactor("old", Some(utc(1, 9, 0))), |
| 206 | reactor("none", None), |
| 207 | reactor("new", Some(utc(1, 9, 0) + Duration::hours(2))), |
| 208 | ], |
| 209 | ), |
| 210 | chip("heart", vec![reactor("mid", Some(utc(1, 10, 0)))]), |
| 211 | ]; |
| 212 | let names = |entries: Vec<ReactionEntry>| -> Vec<String> { |
| 213 | entries |
| 214 | .into_iter() |
| 215 | .map(|entry| entry.reactor.name) |
| 216 | .collect() |
| 217 | }; |
| 218 | assert_eq!( |
| 219 | names(reaction_entries(&chips, None)), |
| 220 | ["new", "mid", "old", "none"] |
| 221 | ); |
| 222 | let like = chips[0].glyph(); |
| 223 | assert_eq!( |
| 224 | names(reaction_entries(&chips, Some(&like))), |
| 225 | ["new", "old", "none"] |
| 226 | ); |
| 227 | } |
| 228 | } |