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