BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / kvt / modal.rs

modal.rs

modal.rs 11.5 KiB Download Raw
1//! Modal met de details van één kaart en de knop «Openen in BC».
2
3use gpui_kit::component::scroll::ScrollableElement as _;
4use gpui_kit::component::tooltip::Tooltip;
5use gpui_kit::component::{h_flex, v_flex};
6use gpui_kit::prelude::FluentBuilder as _;
7use gpui_kit::*;
8use kvt_sleutels::links::bc_url;
9use kvt_sleutels::{Card, CardKind, CardRef, parse_card_link};
10
11use super::card::title_from_anchor;
12use 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};
16use crate::i18n::tr;
17use crate::theme::{self, ElevatedSurface as _};
18use crate::views::widgets::symbol;
19
20pub const KVT_CARD_DIALOG_TAG: &str = "kvt-card-dialog";
21const BACKDROP_OPACITY: f32 = 0.55;
22const PANEL_WIDTH: f32 = 500.;
23const PANEL_MARGIN: f32 = 32.;
24const PANEL_RADIUS: f32 = 10.;
25const PANEL_PADDING: f32 = 16.;
26const SECTION_GAP: f32 = 12.;
27const CLOSE_SIZE: f32 = 28.;
28const BADGE_SIZE: f32 = 36.;
29const LABEL_WIDTH: f32 = 150.;
30const BUTTON_HEIGHT: f32 = 32.;
31const FIELDS_MAX_HEIGHT: f32 = 360.;
32
33pub 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
61fn 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
84fn 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
160fn 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
175fn 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
184fn 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
203fn 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
267pub(super) 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
285pub(super) fn secondary_button(
286 id: &'static str,
287 symbol_name: &'static str,
288 label: String,
289) -> Stateful<Div> {
290 h_flex()
291 .id(id)
292 .h(px(BUTTON_HEIGHT))
293 .flex_none()
294 .px(px(12.))
295 .gap(px(6.))
296 .items_center()
297 .rounded(px(6.))
298 .bg(theme::popup_hover())
299 .text_color(theme::text())
300 .text_size(px(13.))
301 .cursor_pointer()
302 .hover(|button| button.bg(theme::row_hover()))
303 .child(symbol(symbol_name, 14., theme::text_soft()))
304 .child(label)
305}
306
307fn actions(reference: &CardRef, card: Option<&Card>) -> Div {
308 let bc = card
309 .map(|card| card.links.bc.clone())
310 .filter(|url| trusted_link(url))
311 .unwrap_or_else(|| {
312 bc_url(
313 reference.kind,
314 &reference.company,
315 &reference.no,
316 reference.environment.as_deref(),
317 )
318 });
319 let daedalus = card
320 .and_then(|card| card.links.daedalus.clone())
321 .filter(|url| trusted_link(url));
322 let no = reference.no.clone();
323 h_flex()
324 .w_full()
325 .flex_wrap()
326 .gap(px(8.))
327 .items_center()
328 .child(
329 primary_button("kvt-card-open-bc", tr("kvt.card.open_in_bc"))
330 .child(symbol("open_in_new", 14., theme::on_accent()))
331 .on_click(move |_, _, cx| app_ui_kit::render::hooks::open_link(&bc, cx)),
332 )
333 .children(daedalus.map(|url| {
334 secondary_button(
335 "kvt-card-open-daedalus",
336 "open_in_new",
337 tr("kvt.card.open_daedalus"),
338 )
339 .on_click(move |_, _, cx| app_ui_kit::render::hooks::open_link(&url, cx))
340 }))
341 .child(
342 secondary_button("kvt-card-copy", "content_copy", tr("kvt.card.copy_no")).on_click(
343 move |_, _, cx| {
344 cx.write_to_clipboard(ClipboardItem::new_string(no.clone()));
345 notice(tr("kvt.notice.copied"), cx);
346 },
347 ),
348 )
349}