BlueGit

ict rusty-teams

Files

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

recap_tab.rs

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