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