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