BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / recap_tab.rs

recap_tab.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
recap_tab.rs 16.7 KiB Download Raw
1use std::rc::Rc;
2
3use chatsvc::{
4 Attendance, AttendanceRecord, Occurrence, RecordingDetails, TranscriptFormat, TranscriptTurn,
5 days_until_expiry,
6};
7use chrono::{DateTime, FixedOffset, Local, NaiveDate, Offset as _, Utc};
8use gpui_kit::assets::IconName;
9use gpui_kit::component::scroll::ScrollableElement as _;
10use gpui_kit::component::{
11 button::{Button, ButtonVariants as _},
12 h_flex,
13 input::{Input, InputState},
14 v_flex,
15};
16use gpui_kit::prelude::FluentBuilder as _;
17use gpui_kit::*;
18
19use super::shared_tab::{SharedRun, download_label, item_row};
20use super::widgets::icon;
21use crate::channel_files::SharedItem;
22use crate::downloads::DownloadState;
23use crate::recap::{
24 ATTENDANCE_FAILED, ATTENDANCE_OFF, EMPTY_RECAP, Load, NO_TRANSCRIPT, clock_label,
25 expiry_is_urgent, expiry_label, highlight_segments, matching_turns, occurrence_label,
26 shows_recording, span_label, time_label,
27};
28use crate::theme;
29
30const SIDE_PADDING: f32 = 20.;
31const SECTION_GAP: f32 = 24.;
32const CARD_WIDTH: f32 = 360.;
33const CARD_HEIGHT: f32 = 202.;
34const CARD_RADIUS: f32 = 10.;
35const PLAY_SIZE: f32 = 52.;
36const PLAY_GLYPH: f32 = 24.;
37const SEARCH_WIDTH: f32 = 260.;
38const TIME_WIDTH: f32 = 48.;
39const ATTENDANCE_COLUMN: f32 = 90.;
40const NAME_COLUMN: f32 = 220.;
41
42pub const RECAP_REGION: &str = "shell/islands/main/recap";
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45pub enum RecapDownload {
46 Recording,
47 Transcript(TranscriptFormat),
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum RecapCommand {
52 Occurrence(usize),
53 Play,
54 Download(RecapDownload),
55 AskDelete,
56 Retry,
57}
58
59pub type RecapRun = Rc<dyn Fn(RecapCommand, &mut Window, &mut App)>;
60
61pub struct RecapView<'a> {
62 pub occurrences: &'a [Occurrence],
63 pub selected: usize,
64 pub details: &'a Load<RecordingDetails>,
65 pub transcript: &'a Load<Vec<TranscriptTurn>>,
66 pub attendance: &'a Load<Attendance>,
67 pub search: &'a Entity<InputState>,
68 pub query: &'a str,
69 pub can_delete: bool,
70 pub recording_state: Option<&'a DownloadState>,
71 pub vtt_state: Option<&'a DownloadState>,
72 pub docx_state: Option<&'a DownloadState>,
73 pub files: &'a [SharedItem],
74 pub file_labels: Vec<Option<String>>,
75 pub file_run: SharedRun,
76 pub now: DateTime<Utc>,
77}
78
79fn section(title: &'static str) -> Div {
80 v_flex().w_full().flex_none().gap(px(10.)).child(
81 div()
82 .text_size(px(13.))
83 .font_weight(FontWeight::SEMIBOLD)
84 .text_color(theme::text())
85 .child(title),
86 )
87}
88
89fn muted(text: impl Into<SharedString>) -> Div {
90 div()
91 .text_size(px(12.5))
92 .text_color(theme::text_muted())
93 .child(text.into())
94}
95
96fn action(
97 id: &'static str,
98 label: String,
99 glyph: IconName,
100 command: RecapCommand,
101 run: &RecapRun,
102) -> Button {
103 let run = run.clone();
104 Button::new(id)
105 .ghost()
106 .compact()
107 .icon(glyph)
108 .label(label)
109 .on_click(move |_, window, cx| run(command, window, cx))
110}
111
112fn download_action(
113 id: &'static str,
114 label: &'static str,
115 download: RecapDownload,
116 state: Option<&DownloadState>,
117 run: &RecapRun,
118) -> Button {
119 let text = download_label(state).map_or_else(
120 || label.to_owned(),
121 |progress| format!("{label} - {progress}"),
122 );
123 action(
124 id,
125 text,
126 IconName::Download,
127 RecapCommand::Download(download),
128 run,
129 )
130}
131
132fn picker(view: &RecapView, offset: FixedOffset, today: NaiveDate, run: &RecapRun) -> Div {
133 h_flex()
134 .w_full()
135 .flex_none()
136 .gap(px(6.))
137 .flex_wrap()
138 .children(
139 view.occurrences
140 .iter()
141 .enumerate()
142 .map(|(index, occurrence)| {
143 let run = run.clone();
144 let active = index == view.selected;
145 div()
146 .id(ElementId::NamedInteger(
147 "recap-occurrence".into(),
148 index as u64,
149 ))
150 .px(px(12.))
151 .py(px(4.))
152 .rounded_full()
153 .cursor_pointer()
154 .text_size(px(12.))
155 .bg(if active {
156 theme::accent()
157 } else {
158 theme::surface_raised()
159 })
160 .text_color(if active {
161 theme::on_accent()
162 } else {
163 theme::text_soft()
164 })
165 .on_click(move |_, window, cx| {
166 run(RecapCommand::Occurrence(index), window, cx)
167 })
168 .child(occurrence_label(occurrence, today, offset))
169 }),
170 )
171}
172
173fn recording_card(details: &RecordingDetails) -> Div {
174 let labels = h_flex()
175 .absolute()
176 .left(px(12.))
177 .right(px(12.))
178 .bottom(px(10.))
179 .justify_between()
180 .text_size(px(12.))
181 .text_color(gpui_kit::white())
182 .child(
183 div().child(
184 details
185 .duration_seconds
186 .map(clock_label)
187 .unwrap_or_default(),
188 ),
189 )
190 .when(details.audio_only, |row| {
191 row.child(div().child("Audio only"))
192 });
193 div()
194 .relative()
195 .w(px(CARD_WIDTH))
196 .h(px(CARD_HEIGHT))
197 .flex_none()
198 .flex()
199 .items_center()
200 .justify_center()
201 .rounded(px(CARD_RADIUS))
202 .bg(hsla(0., 0., 0.07, 1.))
203 .child(
204 div()
205 .size(px(PLAY_SIZE))
206 .flex()
207 .items_center()
208 .justify_center()
209 .rounded_full()
210 .bg(gpui_kit::white().opacity(0.18))
211 .child(icon(IconName::Play, PLAY_GLYPH, gpui_kit::white())),
212 )
213 .child(labels)
214}
215
216fn recording_section(view: &RecapView, run: &RecapRun) -> Div {
217 let body = match view.details {
218 Load::Loading => muted("Loading recording ..").into_any_element(),
219 Load::Failed(reason) => failure(format!("Recording not available: {reason}"), run),
220 Load::Ready(details) => {
221 let days = details
222 .expires_at
223 .map(|expires_at| days_until_expiry(expires_at, view.now));
224 let expiry = days.map(|days| {
225 div()
226 .text_size(px(12.))
227 .text_color(if expiry_is_urgent(days) {
228 theme::amber()
229 } else {
230 theme::text_muted()
231 })
232 .child(expiry_label(days))
233 });
234 h_flex()
235 .gap(px(16.))
236 .items_start()
237 .child(recording_card(details))
238 .child(
239 v_flex()
240 .gap(px(8.))
241 .items_start()
242 .children(expiry)
243 .child(action(
244 "recap-play",
245 "Play".to_owned(),
246 IconName::Play,
247 RecapCommand::Play,
248 run,
249 ))
250 .child(download_action(
251 "recap-download",
252 "Download",
253 RecapDownload::Recording,
254 view.recording_state,
255 run,
256 ))
257 .when(view.can_delete, |column| {
258 column.child(
259 action(
260 "recap-delete",
261 "Delete".to_owned(),
262 IconName::Trash,
263 RecapCommand::AskDelete,
264 run,
265 )
266 .danger(),
267 )
268 }),
269 )
270 .into_any_element()
271 }
272 };
273 section("Recording").child(body)
274}
275
276fn failure(reason: String, run: &RecapRun) -> AnyElement {
277 let run = run.clone();
278 h_flex()
279 .gap(px(8.))
280 .items_center()
281 .child(muted(reason))
282 .child(
283 Button::new("recap-retry")
284 .ghost()
285 .compact()
286 .label("Retry")
287 .on_click(move |_, window, cx| run(RecapCommand::Retry, window, cx)),
288 )
289 .into_any_element()
290}
291
292fn highlighted(text: &str, query: &str) -> StyledText {
293 let mut start = 0;
294 let mut ranges = Vec::new();
295 for (segment, hit) in highlight_segments(text, query) {
296 let end = start + segment.len();
297 if hit {
298 ranges.push((
299 start..end,
300 HighlightStyle {
301 background_color: Some(theme::amber().opacity(0.35)),
302 ..HighlightStyle::default()
303 },
304 ));
305 }
306 start = end;
307 }
308 StyledText::new(text.to_owned()).with_highlights(ranges)
309}
310
311fn turn_row(turn: &TranscriptTurn, query: &str) -> Div {
312 h_flex()
313 .w_full()
314 .flex_none()
315 .gap(px(12.))
316 .items_start()
317 .text_size(px(13.))
318 .child(
319 div()
320 .w(px(TIME_WIDTH))
321 .flex_none()
322 .text_size(px(12.))
323 .text_color(theme::text_faint())
324 .child(clock_label(turn.start_seconds as u64)),
325 )
326 .child(
327 v_flex()
328 .flex_1()
329 .min_w_0()
330 .gap(px(2.))
331 .child(
332 div()
333 .font_weight(FontWeight::BOLD)
334 .text_color(theme::text())
335 .child(highlighted(&turn.speaker, query)),
336 )
337 .child(
338 div()
339 .text_color(theme::text_soft())
340 .child(highlighted(&turn.text, query)),
341 ),
342 )
343}
344
345fn transcript_section(view: &RecapView, run: &RecapRun) -> Div {
346 let downloads = h_flex()
347 .gap(px(4.))
348 .child(download_action(
349 "recap-vtt",
350 "Download .vtt",
351 RecapDownload::Transcript(TranscriptFormat::Vtt),
352 view.vtt_state,
353 run,
354 ))
355 .child(download_action(
356 "recap-docx",
357 "Download .docx",
358 RecapDownload::Transcript(TranscriptFormat::Docx),
359 view.docx_state,
360 run,
361 ));
362 let body = match view.transcript {
363 Load::Loading => muted("Loading transcript ..").into_any_element(),
364 Load::Failed(reason) => failure(format!("Transcript not available: {reason}"), run),
365 Load::Ready(turns) if turns.is_empty() => muted(NO_TRANSCRIPT).into_any_element(),
366 Load::Ready(turns) => {
367 let matches = matching_turns(turns, view.query);
368 let list = if matches.is_empty() {
369 muted("No lines match.").into_any_element()
370 } else {
371 v_flex()
372 .w_full()
373 .gap(px(10.))
374 .children(
375 matches
376 .into_iter()
377 .map(|index| turn_row(&turns[index], view.query)),
378 )
379 .into_any_element()
380 };
381 v_flex()
382 .w_full()
383 .gap(px(12.))
384 .child(
385 h_flex()
386 .w(px(SEARCH_WIDTH))
387 .h(px(30.))
388 .px(px(8.))
389 .gap(px(6.))
390 .items_center()
391 .rounded(px(8.))
392 .bg(theme::surface_raised())
393 .child(icon(IconName::Search, 14., theme::text_muted()))
394 .child(Input::new(view.search).appearance(false).bordered(false)),
395 )
396 .child(list)
397 .into_any_element()
398 }
399 };
400 section("Transcript").child(downloads).child(body)
401}
402
403fn attendance_row(record: &AttendanceRecord, offset: FixedOffset) -> Div {
404 let cell = |text: String, width: f32| {
405 div()
406 .w(px(width))
407 .flex_none()
408 .truncate()
409 .text_color(theme::text_muted())
410 .child(text)
411 };
412 h_flex()
413 .w_full()
414 .flex_none()
415 .gap(px(12.))
416 .text_size(px(12.5))
417 .child(
418 div()
419 .w(px(NAME_COLUMN))
420 .flex_none()
421 .truncate()
422 .text_color(theme::text())
423 .child(record.name.clone()),
424 )
425 .child(cell(
426 time_label(record.joined_at, offset),
427 ATTENDANCE_COLUMN,
428 ))
429 .child(cell(time_label(record.left_at, offset), ATTENDANCE_COLUMN))
430 .child(cell(span_label(record.duration_seconds), ATTENDANCE_COLUMN))
431}
432
433fn attendance_header() -> Div {
434 let cell = |text: &'static str, width: f32| div().w(px(width)).flex_none().child(text);
435 h_flex()
436 .w_full()
437 .flex_none()
438 .gap(px(12.))
439 .text_size(px(11.5))
440 .text_color(theme::text_faint())
441 .child(cell("Name", NAME_COLUMN))
442 .child(cell("Joined", ATTENDANCE_COLUMN))
443 .child(cell("Left", ATTENDANCE_COLUMN))
444 .child(cell("Duration", ATTENDANCE_COLUMN))
445}
446
447fn attendance_section(view: &RecapView, offset: FixedOffset) -> Div {
448 let body = match view.attendance {
449 Load::Loading => muted("Loading attendance ..").into_any_element(),
450 Load::Failed(_) => muted(ATTENDANCE_FAILED).into_any_element(),
451 Load::Ready(Attendance::TurnedOff) => muted(ATTENDANCE_OFF).into_any_element(),
452 Load::Ready(Attendance::Report(records)) if records.is_empty() => {
453 muted("No attendees recorded.").into_any_element()
454 }
455 Load::Ready(Attendance::Report(records)) => v_flex()
456 .w_full()
457 .gap(px(6.))
458 .child(attendance_header())
459 .children(records.iter().map(|record| attendance_row(record, offset)))
460 .into_any_element(),
461 };
462 section("Attendance").child(body)
463}
464
465fn files_section(view: &RecapView) -> Option<Div> {
466 (!view.files.is_empty()).then(|| {
467 v_flex()
468 .w_full()
469 .flex_none()
470 .gap(px(10.))
471 .child(
472 div()
473 .px(px(SIDE_PADDING))
474 .text_size(px(13.))
475 .font_weight(FontWeight::SEMIBOLD)
476 .text_color(theme::text())
477 .child("Files"),
478 )
479 .children(view.files.iter().enumerate().map(|(index, item)| {
480 let label = view.file_labels.get(index).cloned().flatten();
481 item_row(index, item, label, &view.file_run)
482 }))
483 })
484}
485
486pub fn render_recap(view: RecapView, run: &RecapRun) -> AnyElement {
487 let now_local = Local::now();
488 let (offset, today) = (now_local.offset().fix(), now_local.date_naive());
489 let body = if view.occurrences.is_empty() && view.files.is_empty() {
490 div()
491 .w_full()
492 .flex_1()
493 .flex()
494 .items_center()
495 .justify_center()
496 .text_size(px(13.))
497 .text_color(theme::text_muted())
498 .child(EMPTY_RECAP)
499 .into_any_element()
500 } else {
501 v_flex()
502 .id("recap-scroll")
503 .w_full()
504 .flex_1()
505 .min_h_0()
506 .overflow_y_scrollbar()
507 .py(px(16.))
508 .gap(px(SECTION_GAP))
509 .when(!view.occurrences.is_empty(), |column| {
510 column.child(
511 v_flex()
512 .w_full()
513 .flex_none()
514 .px(px(SIDE_PADDING))
515 .gap(px(SECTION_GAP))
516 .child(picker(&view, offset, today, run))
517 .when(shows_recording(view.details), |column| {
518 column.child(recording_section(&view, run))
519 })
520 .child(transcript_section(&view, run))
521 .child(attendance_section(&view, offset)),
522 )
523 })
524 .children(files_section(&view))
525 .into_any_element()
526 };
527 v_flex()
528 .size_full()
529 .debug_selector(|| RECAP_REGION.into())
530 .child(body)
531 .into_any_element()
532}