| 1 | //! Modal met de details van één kaart en de knop «Openen in BC». |
| 2 | |
| 3 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 4 | use gpui_kit::component::tooltip::Tooltip; |
| 5 | use gpui_kit::component::{h_flex, v_flex}; |
| 6 | use gpui_kit::prelude::FluentBuilder as _; |
| 7 | use gpui_kit::*; |
| 8 | use kvt_sleutels::links::bc_url; |
| 9 | use kvt_sleutels::{Card, CardKind, CardRef, parse_card_link}; |
| 10 | |
| 11 | use super::card::title_from_anchor; |
| 12 | use super::{ |
| 13 | Link, Load, cached_card, close_card, connect, error_text, fetch, kind_label, kind_symbol, link, |
| 14 | notice, open_card, read, trusted_link, |
| 15 | }; |
| 16 | use crate::i18n::tr; |
| 17 | use crate::theme::{self, ElevatedSurface as _}; |
| 18 | use crate::views::widgets::symbol; |
| 19 | |
| 20 | pub const KVT_CARD_DIALOG_TAG: &str = "kvt-card-dialog"; |
| 21 | const BACKDROP_OPACITY: f32 = 0.55; |
| 22 | const PANEL_WIDTH: f32 = 500.; |
| 23 | const PANEL_MARGIN: f32 = 32.; |
| 24 | const PANEL_RADIUS: f32 = 10.; |
| 25 | const PANEL_PADDING: f32 = 16.; |
| 26 | const SECTION_GAP: f32 = 12.; |
| 27 | const CLOSE_SIZE: f32 = 28.; |
| 28 | const BADGE_SIZE: f32 = 36.; |
| 29 | const LABEL_WIDTH: f32 = 150.; |
| 30 | const BUTTON_HEIGHT: f32 = 32.; |
| 31 | const FIELDS_MAX_HEIGHT: f32 = 360.; |
| 32 | |
| 33 | pub fn render_modal(cx: &App) -> Option<AnyElement> { |
| 34 | let (reference, anchor) = read(cx, |kvt| kvt.modal.clone()).flatten()?; |
| 35 | Some( |
| 36 | div() |
| 37 | .id("kvt-card-layer") |
| 38 | .absolute() |
| 39 | .top_0() |
| 40 | .left_0() |
| 41 | .size_full() |
| 42 | .flex() |
| 43 | .items_center() |
| 44 | .justify_center() |
| 45 | .child( |
| 46 | div() |
| 47 | .id("kvt-card-backdrop") |
| 48 | .absolute() |
| 49 | .top_0() |
| 50 | .left_0() |
| 51 | .size_full() |
| 52 | .bg(black().opacity(BACKDROP_OPACITY)) |
| 53 | .occlude() |
| 54 | .on_click(|_, _, cx| close_card(cx)), |
| 55 | ) |
| 56 | .child(panel(reference, anchor, cx)) |
| 57 | .into_any_element(), |
| 58 | ) |
| 59 | } |
| 60 | |
| 61 | fn panel(reference: CardRef, anchor: String, cx: &App) -> Div { |
| 62 | let load = cached_card(&reference, cx); |
| 63 | let loaded = match &load { |
| 64 | Some(Load::Loaded(card)) => Some(card.clone()), |
| 65 | _ => None, |
| 66 | }; |
| 67 | v_flex() |
| 68 | .debug_selector(|| KVT_CARD_DIALOG_TAG.into()) |
| 69 | .w(px(PANEL_WIDTH)) |
| 70 | .max_w(relative(1.)) |
| 71 | .max_h(relative(1.)) |
| 72 | .m(px(PANEL_MARGIN)) |
| 73 | .p(px(PANEL_PADDING)) |
| 74 | .gap(px(SECTION_GAP)) |
| 75 | .rounded(px(PANEL_RADIUS)) |
| 76 | .dialog_surface() |
| 77 | .text_color(theme::text()) |
| 78 | .occlude() |
| 79 | .child(header(&reference, &anchor, loaded.as_ref())) |
| 80 | .child(body(&reference, load, cx)) |
| 81 | .child(actions(&reference, loaded.as_ref())) |
| 82 | } |
| 83 | |
| 84 | fn header(reference: &CardRef, anchor: &str, card: Option<&Card>) -> Div { |
| 85 | let (title, subtitle) = match card { |
| 86 | Some(card) => (card.title.clone(), card.subtitle.clone()), |
| 87 | None => (title_from_anchor(reference, anchor), String::new()), |
| 88 | }; |
| 89 | let detail = if subtitle.is_empty() { |
| 90 | reference.company.clone() |
| 91 | } else { |
| 92 | format!("{subtitle} · {}", reference.company) |
| 93 | }; |
| 94 | h_flex() |
| 95 | .w_full() |
| 96 | .gap(px(SECTION_GAP)) |
| 97 | .items_start() |
| 98 | .child( |
| 99 | div() |
| 100 | .size(px(BADGE_SIZE)) |
| 101 | .flex_none() |
| 102 | .flex() |
| 103 | .items_center() |
| 104 | .justify_center() |
| 105 | .rounded(px(8.)) |
| 106 | .bg(theme::accent()) |
| 107 | .child(symbol(kind_symbol(reference.kind), 20., theme::on_accent())), |
| 108 | ) |
| 109 | .child( |
| 110 | v_flex() |
| 111 | .flex_1() |
| 112 | .min_w_0() |
| 113 | .gap(px(2.)) |
| 114 | .child( |
| 115 | div() |
| 116 | .text_size(px(12.)) |
| 117 | .font_weight(FontWeight::SEMIBOLD) |
| 118 | .text_color(theme::text_muted()) |
| 119 | .child(format!("{} {}", kind_label(reference.kind), reference.no)), |
| 120 | ) |
| 121 | .when(!title.is_empty(), |column| { |
| 122 | column.child( |
| 123 | div() |
| 124 | .text_size(px(15.)) |
| 125 | .font_weight(FontWeight::SEMIBOLD) |
| 126 | .text_color(theme::text_strong()) |
| 127 | .child(title), |
| 128 | ) |
| 129 | }) |
| 130 | .child( |
| 131 | div() |
| 132 | .truncate() |
| 133 | .text_size(px(12.)) |
| 134 | .text_color(theme::text_muted()) |
| 135 | .child(detail), |
| 136 | ), |
| 137 | ) |
| 138 | .child( |
| 139 | icon_button("kvt-card-refresh", "refresh", tr("kvt.card.refresh")).on_click({ |
| 140 | let reference = reference.clone(); |
| 141 | move |_, _, cx| fetch(reference.clone(), cx) |
| 142 | }), |
| 143 | ) |
| 144 | .child( |
| 145 | div() |
| 146 | .id("kvt-card-close") |
| 147 | .size(px(CLOSE_SIZE)) |
| 148 | .flex_none() |
| 149 | .flex() |
| 150 | .items_center() |
| 151 | .justify_center() |
| 152 | .rounded_full() |
| 153 | .cursor_pointer() |
| 154 | .hover(|button| button.bg(theme::popup_hover())) |
| 155 | .on_click(|_, _, cx| close_card(cx)) |
| 156 | .child(symbol("close", 16., theme::text_muted())), |
| 157 | ) |
| 158 | } |
| 159 | |
| 160 | fn icon_button(id: &'static str, symbol_name: &'static str, label: String) -> Stateful<Div> { |
| 161 | div() |
| 162 | .id(id) |
| 163 | .size(px(CLOSE_SIZE)) |
| 164 | .flex_none() |
| 165 | .flex() |
| 166 | .items_center() |
| 167 | .justify_center() |
| 168 | .rounded_full() |
| 169 | .cursor_pointer() |
| 170 | .hover(|button| button.bg(theme::popup_hover())) |
| 171 | .tooltip(move |window, cx| Tooltip::new(label.clone()).build(window, cx)) |
| 172 | .child(symbol(symbol_name, 16., theme::text_muted())) |
| 173 | } |
| 174 | |
| 175 | fn message(text: String) -> Div { |
| 176 | div() |
| 177 | .w_full() |
| 178 | .py(px(8.)) |
| 179 | .text_size(px(13.)) |
| 180 | .text_color(theme::text_muted()) |
| 181 | .child(text) |
| 182 | } |
| 183 | |
| 184 | fn body(reference: &CardRef, load: Option<Load>, cx: &App) -> AnyElement { |
| 185 | match (link(cx), load) { |
| 186 | (_, Some(Load::Loaded(card))) => fields(reference, &card).into_any_element(), |
| 187 | (Link::Connecting, _) => message(tr("kvt.settings.connecting")).into_any_element(), |
| 188 | (Link::NotLinked | Link::Unknown, _) => v_flex() |
| 189 | .w_full() |
| 190 | .gap(px(8.)) |
| 191 | .child(message(tr("kvt.card.sign_in"))) |
| 192 | .child( |
| 193 | primary_button("kvt-card-connect", tr("kvt.settings.connect")) |
| 194 | .self_start() |
| 195 | .on_click(|_, _, cx| connect(cx)), |
| 196 | ) |
| 197 | .into_any_element(), |
| 198 | (_, Some(Load::Failed(error))) => message(error_text(&error)).into_any_element(), |
| 199 | (_, Some(Load::Loading) | None) => message(tr("kvt.card.loading")).into_any_element(), |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | fn fields(reference: &CardRef, card: &Card) -> impl IntoElement + use<> { |
| 204 | let rows: Vec<AnyElement> = card |
| 205 | .fields |
| 206 | .iter() |
| 207 | .filter(|field| !field.value.trim().is_empty()) |
| 208 | .enumerate() |
| 209 | .map(|(index, field)| { |
| 210 | let value = div().flex_1().min_w_0().text_size(px(13.)); |
| 211 | let value = match field.reference.as_ref().and_then(|target| { |
| 212 | let kind = CardKind::from_api_name(&target.kind)?; |
| 213 | let url = if trusted_link(&target.bc_url) { |
| 214 | target.bc_url.clone() |
| 215 | } else { |
| 216 | bc_url( |
| 217 | kind, |
| 218 | &reference.company, |
| 219 | &target.no, |
| 220 | reference.environment.as_deref(), |
| 221 | ) |
| 222 | }; |
| 223 | parse_card_link(&url) |
| 224 | }) { |
| 225 | Some(target) => { |
| 226 | let anchor = field.value.clone(); |
| 227 | value |
| 228 | .id(ElementId::Name(format!("kvt-card-ref-{index}").into())) |
| 229 | .text_color(theme::accent_text()) |
| 230 | .cursor_pointer() |
| 231 | .hover(|value| value.underline()) |
| 232 | .on_click(move |_, _, cx| open_card(target.clone(), anchor.clone(), cx)) |
| 233 | .child(field.value.clone()) |
| 234 | .into_any_element() |
| 235 | } |
| 236 | None => value |
| 237 | .text_color(theme::text()) |
| 238 | .child(field.value.clone()) |
| 239 | .into_any_element(), |
| 240 | }; |
| 241 | h_flex() |
| 242 | .w_full() |
| 243 | .gap(px(SECTION_GAP)) |
| 244 | .items_start() |
| 245 | .child( |
| 246 | div() |
| 247 | .w(px(LABEL_WIDTH)) |
| 248 | .flex_none() |
| 249 | .text_size(px(12.)) |
| 250 | .line_height(px(19.)) |
| 251 | .text_color(theme::text_muted()) |
| 252 | .child(field.label.clone()), |
| 253 | ) |
| 254 | .child(value) |
| 255 | .into_any_element() |
| 256 | }) |
| 257 | .collect(); |
| 258 | v_flex() |
| 259 | .id("kvt-card-fields") |
| 260 | .w_full() |
| 261 | .max_h(px(FIELDS_MAX_HEIGHT)) |
| 262 | .overflow_y_scrollbar() |
| 263 | .gap(px(6.)) |
| 264 | .children(rows) |
| 265 | } |
| 266 | |
| 267 | fn primary_button(id: &'static str, label: String) -> Stateful<Div> { |
| 268 | h_flex() |
| 269 | .id(id) |
| 270 | .h(px(BUTTON_HEIGHT)) |
| 271 | .flex_none() |
| 272 | .px(px(16.)) |
| 273 | .gap(px(6.)) |
| 274 | .items_center() |
| 275 | .rounded(px(6.)) |
| 276 | .bg(theme::accent()) |
| 277 | .text_color(theme::on_accent()) |
| 278 | .text_size(px(13.)) |
| 279 | .font_weight(FontWeight::SEMIBOLD) |
| 280 | .cursor_pointer() |
| 281 | .hover(|button| button.bg(theme::accent_text())) |
| 282 | .child(label) |
| 283 | } |
| 284 | |
| 285 | fn secondary_button(id: &'static str, symbol_name: &'static str, label: String) -> Stateful<Div> { |
| 286 | h_flex() |
| 287 | .id(id) |
| 288 | .h(px(BUTTON_HEIGHT)) |
| 289 | .flex_none() |
| 290 | .px(px(12.)) |
| 291 | .gap(px(6.)) |
| 292 | .items_center() |
| 293 | .rounded(px(6.)) |
| 294 | .bg(theme::popup_hover()) |
| 295 | .text_color(theme::text()) |
| 296 | .text_size(px(13.)) |
| 297 | .cursor_pointer() |
| 298 | .hover(|button| button.bg(theme::row_hover())) |
| 299 | .child(symbol(symbol_name, 14., theme::text_soft())) |
| 300 | .child(label) |
| 301 | } |
| 302 | |
| 303 | fn actions(reference: &CardRef, card: Option<&Card>) -> Div { |
| 304 | let bc = card |
| 305 | .map(|card| card.links.bc.clone()) |
| 306 | .filter(|url| trusted_link(url)) |
| 307 | .unwrap_or_else(|| { |
| 308 | bc_url( |
| 309 | reference.kind, |
| 310 | &reference.company, |
| 311 | &reference.no, |
| 312 | reference.environment.as_deref(), |
| 313 | ) |
| 314 | }); |
| 315 | let daedalus = card |
| 316 | .and_then(|card| card.links.daedalus.clone()) |
| 317 | .filter(|url| trusted_link(url)); |
| 318 | let no = reference.no.clone(); |
| 319 | h_flex() |
| 320 | .w_full() |
| 321 | .flex_wrap() |
| 322 | .gap(px(8.)) |
| 323 | .items_center() |
| 324 | .child( |
| 325 | primary_button("kvt-card-open-bc", tr("kvt.card.open_in_bc")) |
| 326 | .child(symbol("open_in_new", 14., theme::on_accent())) |
| 327 | .on_click(move |_, _, cx| app_ui_kit::render::hooks::open_link(&bc, cx)), |
| 328 | ) |
| 329 | .children(daedalus.map(|url| { |
| 330 | secondary_button( |
| 331 | "kvt-card-open-daedalus", |
| 332 | "open_in_new", |
| 333 | tr("kvt.card.open_daedalus"), |
| 334 | ) |
| 335 | .on_click(move |_, _, cx| app_ui_kit::render::hooks::open_link(&url, cx)) |
| 336 | })) |
| 337 | .child( |
| 338 | secondary_button("kvt-card-copy", "content_copy", tr("kvt.card.copy_no")).on_click( |
| 339 | move |_, _, cx| { |
| 340 | cx.write_to_clipboard(ClipboardItem::new_string(no.clone())); |
| 341 | notice(tr("kvt.notice.copied"), cx); |
| 342 | }, |
| 343 | ), |
| 344 | ) |
| 345 | } |