| 1 | //! Het kaartje onder een bericht met een BC- of sleutels-link. |
| 2 | |
| 3 | use gpui_kit::component::{h_flex, v_flex}; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | use kvt_sleutels::{CardRef, MAX_CARDS_PER_MESSAGE, parse_card_link}; |
| 7 | |
| 8 | use super::{Load, cached_card, kind_label, kind_symbol, open_card}; |
| 9 | use crate::render::Block; |
| 10 | use crate::theme; |
| 11 | use crate::views::widgets::symbol; |
| 12 | |
| 13 | const CARD_WIDTH: f32 = 360.; |
| 14 | const BADGE_SIZE: f32 = 32.; |
| 15 | const BADGE_ICON_SIZE: f32 = 18.; |
| 16 | |
| 17 | /// Kaart-verwijzingen met hun linktekst, in volgorde, zonder dubbele en zonder |
| 18 | /// links in een geciteerd bericht. Hooguit [`MAX_CARDS_PER_MESSAGE`]. |
| 19 | pub(crate) fn card_links(blocks: &[Block]) -> Vec<(CardRef, String)> { |
| 20 | let mut found: Vec<(CardRef, String)> = Vec::new(); |
| 21 | collect(blocks, &mut found); |
| 22 | found |
| 23 | } |
| 24 | |
| 25 | fn collect(blocks: &[Block], found: &mut Vec<(CardRef, String)>) { |
| 26 | for block in blocks { |
| 27 | if found.len() >= MAX_CARDS_PER_MESSAGE { |
| 28 | return; |
| 29 | } |
| 30 | match block { |
| 31 | Block::Paragraph(inline) | Block::Heading { inline, .. } => { |
| 32 | collect_inline(inline, found) |
| 33 | } |
| 34 | Block::Quote(children) | Block::Indent { children, .. } => collect(children, found), |
| 35 | Block::List { items, .. } => { |
| 36 | for item in items { |
| 37 | collect(item, found); |
| 38 | } |
| 39 | } |
| 40 | Block::Table { rows, .. } => { |
| 41 | for cell in rows.iter().flatten() { |
| 42 | collect_inline(&cell.inline, found); |
| 43 | } |
| 44 | } |
| 45 | Block::Reply { .. } |
| 46 | | Block::Code { .. } |
| 47 | | Block::CodeLine { .. } |
| 48 | | Block::Rule |
| 49 | | Block::Image { .. } => {} |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn collect_inline(inline: &crate::render::blocks::Inline, found: &mut Vec<(CardRef, String)>) { |
| 55 | for segment in &inline.segments { |
| 56 | if found.len() >= MAX_CARDS_PER_MESSAGE { |
| 57 | return; |
| 58 | } |
| 59 | let Some(reference) = segment.link.as_deref().and_then(parse_card_link) else { |
| 60 | continue; |
| 61 | }; |
| 62 | let text = inline |
| 63 | .text |
| 64 | .get(segment.range.clone()) |
| 65 | .unwrap_or_default() |
| 66 | .trim(); |
| 67 | match found.iter_mut().find(|(known, _)| { |
| 68 | known.kind == reference.kind |
| 69 | && known.company == reference.company |
| 70 | && known.no == reference.no |
| 71 | }) { |
| 72 | // Eén link kan in meerdere opgemaakte stukken staan: tekst aanvullen. |
| 73 | Some((_, known_text)) if !text.is_empty() && !known_text.contains(text) => { |
| 74 | known_text.push(' '); |
| 75 | known_text.push_str(text); |
| 76 | } |
| 77 | Some(_) => {} |
| 78 | None => found.push((reference, text.to_owned())), |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Omschrijving uit de linktekst: «Werkorder WO2699001 - Onderhoud» → «Onderhoud». |
| 84 | /// Een kale URL als tekst geeft niets. |
| 85 | pub(crate) fn title_from_anchor(reference: &CardRef, text: &str) -> String { |
| 86 | let text = text.trim(); |
| 87 | if text.contains("://") { |
| 88 | return String::new(); |
| 89 | } |
| 90 | let prefix = format!("{} {}", reference.kind.label_nl(), reference.no); |
| 91 | match text.strip_prefix(&prefix) { |
| 92 | Some(rest) => rest.trim_start_matches([' ', '-', '–']).trim().to_owned(), |
| 93 | None if text == reference.no => String::new(), |
| 94 | None => text.to_owned(), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn message_cards(blocks: &[Block], id: &str, cx: &App) -> Vec<AnyElement> { |
| 99 | if !cx.has_global::<super::Kvt>() { |
| 100 | return Vec::new(); |
| 101 | } |
| 102 | card_links(blocks) |
| 103 | .into_iter() |
| 104 | .enumerate() |
| 105 | .map(|(index, (reference, text))| { |
| 106 | card_chip(reference, text, format!("{id}-kvt-{index}"), cx) |
| 107 | }) |
| 108 | .collect() |
| 109 | } |
| 110 | |
| 111 | fn card_chip(reference: CardRef, text: String, id: String, cx: &App) -> AnyElement { |
| 112 | let (title, subtitle) = match cached_card(&reference, cx) { |
| 113 | Some(Load::Loaded(card)) => (card.title.clone(), card.subtitle.clone()), |
| 114 | _ => (title_from_anchor(&reference, &text), String::new()), |
| 115 | }; |
| 116 | let heading = format!("{} {}", kind_label(reference.kind), reference.no); |
| 117 | let detail = if subtitle.is_empty() { |
| 118 | reference.company.clone() |
| 119 | } else { |
| 120 | format!("{subtitle} · {}", reference.company) |
| 121 | }; |
| 122 | let kind = reference.kind; |
| 123 | h_flex() |
| 124 | .id(ElementId::Name(id.into())) |
| 125 | .w_full() |
| 126 | .max_w(px(CARD_WIDTH)) |
| 127 | .gap(px(10.)) |
| 128 | .p(px(8.)) |
| 129 | .items_center() |
| 130 | .rounded(px(10.)) |
| 131 | .bg(theme::surface()) |
| 132 | .cursor_pointer() |
| 133 | .hover(|card| card.bg(theme::row_hover())) |
| 134 | .child( |
| 135 | div() |
| 136 | .size(px(BADGE_SIZE)) |
| 137 | .flex_none() |
| 138 | .flex() |
| 139 | .items_center() |
| 140 | .justify_center() |
| 141 | .rounded(px(8.)) |
| 142 | .bg(theme::accent()) |
| 143 | .child(symbol( |
| 144 | kind_symbol(kind), |
| 145 | BADGE_ICON_SIZE, |
| 146 | theme::on_accent(), |
| 147 | )), |
| 148 | ) |
| 149 | .child( |
| 150 | v_flex() |
| 151 | .flex_1() |
| 152 | .min_w_0() |
| 153 | .gap(px(1.)) |
| 154 | .child( |
| 155 | div() |
| 156 | .truncate() |
| 157 | .text_size(px(11.)) |
| 158 | .font_weight(FontWeight::SEMIBOLD) |
| 159 | .text_color(theme::text_muted()) |
| 160 | .child(heading), |
| 161 | ) |
| 162 | .when(!title.is_empty(), |column| { |
| 163 | column.child( |
| 164 | div() |
| 165 | .truncate() |
| 166 | .text_size(px(13.)) |
| 167 | .font_weight(FontWeight::SEMIBOLD) |
| 168 | .text_color(theme::text_strong()) |
| 169 | .child(title), |
| 170 | ) |
| 171 | }) |
| 172 | .child( |
| 173 | div() |
| 174 | .truncate() |
| 175 | .text_size(px(11.)) |
| 176 | .text_color(theme::text_muted()) |
| 177 | .child(detail), |
| 178 | ), |
| 179 | ) |
| 180 | .child(symbol("open_in_new", 14., theme::text_muted())) |
| 181 | .on_click(move |_, _, cx| { |
| 182 | cx.stop_propagation(); |
| 183 | open_card(reference.clone(), text.clone(), cx); |
| 184 | }) |
| 185 | .into_any_element() |
| 186 | } |
| 187 | |
| 188 | #[cfg(test)] |
| 189 | mod tests { |
| 190 | use super::{card_links, title_from_anchor}; |
| 191 | use crate::render::Block; |
| 192 | use crate::render::blocks::Inline; |
| 193 | use kvt_sleutels::CardKind; |
| 194 | use kvt_sleutels::links::bc_url; |
| 195 | |
| 196 | fn linked(text: &str, url: &str) -> Inline { |
| 197 | let mut inline = Inline::plain(text); |
| 198 | inline.segments[0].link = Some(url.to_owned()); |
| 199 | inline |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn finds_card_links_but_not_in_quotes_of_replies() { |
| 204 | let wo = bc_url(CardKind::Werkorder, "Testbedrijf", "WO2699001", None); |
| 205 | let ct = bc_url(CardKind::Contract, "Testbedrijf", "CT2699001", None); |
| 206 | let blocks = vec![ |
| 207 | Block::Reply { |
| 208 | message_id: None, |
| 209 | children: vec![Block::Paragraph(linked("oud", &ct))], |
| 210 | }, |
| 211 | Block::Paragraph(linked("Werkorder WO2699001 - Onderhoud", &wo)), |
| 212 | Block::List { |
| 213 | ordered: false, |
| 214 | start: 1, |
| 215 | items: vec![vec![Block::Paragraph(linked("nog eens", &wo))]], |
| 216 | }, |
| 217 | Block::Paragraph(linked("site", "https://example.com/")), |
| 218 | ]; |
| 219 | let found = card_links(&blocks); |
| 220 | assert_eq!(found.len(), 1); |
| 221 | assert_eq!(found[0].0.no, "WO2699001"); |
| 222 | assert_eq!(found[0].1, "Werkorder WO2699001 - Onderhoud nog eens"); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn title_comes_from_readable_anchor_text() { |
| 227 | let reference = kvt_sleutels::parse_card_link(&bc_url( |
| 228 | CardKind::Project, |
| 229 | "Testbedrijf", |
| 230 | "PRJ2699001", |
| 231 | None, |
| 232 | )) |
| 233 | .unwrap(); |
| 234 | assert_eq!( |
| 235 | title_from_anchor(&reference, "Project PRJ2699001 - Nieuwbouw"), |
| 236 | "Nieuwbouw" |
| 237 | ); |
| 238 | assert_eq!(title_from_anchor(&reference, "Project PRJ2699001"), ""); |
| 239 | assert_eq!(title_from_anchor(&reference, "PRJ2699001"), ""); |
| 240 | assert_eq!( |
| 241 | title_from_anchor(&reference, "https://kvtmd365.kvt.nl/x"), |
| 242 | "" |
| 243 | ); |
| 244 | assert_eq!(title_from_anchor(&reference, "dit project"), "dit project"); |
| 245 | } |
| 246 | } |