threads_archive_view.rs

   1use std::collections::HashSet;
   2use std::sync::Arc;
   3
   4use crate::agent_connection_store::AgentConnectionStore;
   5
   6use crate::thread_metadata_store::{ThreadMetadata, ThreadMetadataStore};
   7use crate::{Agent, RemoveSelectedThread};
   8
   9use agent::ThreadStore;
  10use agent_client_protocol as acp;
  11use agent_settings::AgentSettings;
  12use chrono::{DateTime, Datelike as _, Local, NaiveDate, TimeDelta, Utc};
  13use editor::Editor;
  14use fs::Fs;
  15use fuzzy::{StringMatch, StringMatchCandidate};
  16use gpui::{
  17    AnyElement, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
  18    ListState, Render, SharedString, Subscription, Task, WeakEntity, Window, list, prelude::*, px,
  19};
  20use itertools::Itertools as _;
  21use menu::{Confirm, SelectFirst, SelectLast, SelectNext, SelectPrevious};
  22use picker::{
  23    Picker, PickerDelegate,
  24    highlighted_match_with_paths::{HighlightedMatch, HighlightedMatchWithPaths},
  25};
  26use project::{AgentId, AgentServerStore};
  27use settings::Settings as _;
  28use theme::ActiveTheme;
  29use ui::{AgentThreadStatus, ThreadItem};
  30use ui::{
  31    Divider, KeyBinding, ListItem, ListItemSpacing, ListSubHeader, Tooltip, WithScrollbar,
  32    prelude::*, utils::platform_title_bar_height,
  33};
  34use ui_input::ErasedEditor;
  35use util::ResultExt;
  36use util::paths::PathExt;
  37use workspace::{
  38    ModalView, PathList, SerializedWorkspaceLocation, Workspace, WorkspaceDb, WorkspaceId,
  39    resolve_worktree_workspaces,
  40};
  41
  42use zed_actions::agents_sidebar::FocusSidebarFilter;
  43use zed_actions::editor::{MoveDown, MoveUp};
  44
  45#[derive(Clone)]
  46enum ArchiveListItem {
  47    BucketSeparator(TimeBucket),
  48    Entry {
  49        thread: ThreadMetadata,
  50        highlight_positions: Vec<usize>,
  51    },
  52}
  53
  54#[derive(Clone, Copy, Debug, PartialEq, Eq)]
  55enum TimeBucket {
  56    Today,
  57    Yesterday,
  58    ThisWeek,
  59    PastWeek,
  60    Older,
  61}
  62
  63impl TimeBucket {
  64    fn from_dates(reference: NaiveDate, date: NaiveDate) -> Self {
  65        if date == reference {
  66            return TimeBucket::Today;
  67        }
  68        if date == reference - TimeDelta::days(1) {
  69            return TimeBucket::Yesterday;
  70        }
  71        let week = date.iso_week();
  72        if reference.iso_week() == week {
  73            return TimeBucket::ThisWeek;
  74        }
  75        let last_week = (reference - TimeDelta::days(7)).iso_week();
  76        if week == last_week {
  77            return TimeBucket::PastWeek;
  78        }
  79        TimeBucket::Older
  80    }
  81
  82    fn label(&self) -> &'static str {
  83        match self {
  84            TimeBucket::Today => "Today",
  85            TimeBucket::Yesterday => "Yesterday",
  86            TimeBucket::ThisWeek => "This Week",
  87            TimeBucket::PastWeek => "Past Week",
  88            TimeBucket::Older => "Older",
  89        }
  90    }
  91}
  92
  93fn fuzzy_match_positions(query: &str, text: &str) -> Option<Vec<usize>> {
  94    let mut positions = Vec::new();
  95    let mut query_chars = query.chars().peekable();
  96    for (byte_idx, candidate_char) in text.char_indices() {
  97        if let Some(&query_char) = query_chars.peek() {
  98            if candidate_char.eq_ignore_ascii_case(&query_char) {
  99                positions.push(byte_idx);
 100                query_chars.next();
 101            }
 102        } else {
 103            break;
 104        }
 105    }
 106    if query_chars.peek().is_none() {
 107        Some(positions)
 108    } else {
 109        None
 110    }
 111}
 112
 113pub enum ThreadsArchiveViewEvent {
 114    Close,
 115    Unarchive { thread: ThreadMetadata },
 116    CancelRestore { session_id: acp::SessionId },
 117}
 118
 119impl EventEmitter<ThreadsArchiveViewEvent> for ThreadsArchiveView {}
 120
 121pub struct ThreadsArchiveView {
 122    _history_subscription: Subscription,
 123    focus_handle: FocusHandle,
 124    list_state: ListState,
 125    items: Vec<ArchiveListItem>,
 126    selection: Option<usize>,
 127    hovered_index: Option<usize>,
 128    preserve_selection_on_next_update: bool,
 129    filter_editor: Entity<Editor>,
 130    _subscriptions: Vec<gpui::Subscription>,
 131    _refresh_history_task: Task<()>,
 132    workspace: WeakEntity<Workspace>,
 133    agent_connection_store: WeakEntity<AgentConnectionStore>,
 134    agent_server_store: WeakEntity<AgentServerStore>,
 135    restoring: HashSet<acp::SessionId>,
 136}
 137
 138impl ThreadsArchiveView {
 139    pub fn new(
 140        workspace: WeakEntity<Workspace>,
 141        agent_connection_store: WeakEntity<AgentConnectionStore>,
 142        agent_server_store: WeakEntity<AgentServerStore>,
 143        window: &mut Window,
 144        cx: &mut Context<Self>,
 145    ) -> Self {
 146        let focus_handle = cx.focus_handle();
 147
 148        let filter_editor = cx.new(|cx| {
 149            let mut editor = Editor::single_line(window, cx);
 150            editor.set_placeholder_text("Search archive…", window, cx);
 151            editor
 152        });
 153
 154        let filter_editor_subscription =
 155            cx.subscribe(&filter_editor, |this: &mut Self, _, event, cx| {
 156                if let editor::EditorEvent::BufferEdited = event {
 157                    this.update_items(cx);
 158                }
 159            });
 160
 161        let filter_focus_handle = filter_editor.read(cx).focus_handle(cx);
 162        cx.on_focus_in(
 163            &filter_focus_handle,
 164            window,
 165            |this: &mut Self, _window, cx| {
 166                if this.selection.is_some() {
 167                    this.selection = None;
 168                    cx.notify();
 169                }
 170            },
 171        )
 172        .detach();
 173
 174        let thread_metadata_store_subscription = cx.observe(
 175            &ThreadMetadataStore::global(cx),
 176            |this: &mut Self, _, cx| {
 177                this.update_items(cx);
 178            },
 179        );
 180
 181        cx.on_focus_out(&focus_handle, window, |this: &mut Self, _, _window, cx| {
 182            this.selection = None;
 183            cx.notify();
 184        })
 185        .detach();
 186
 187        let mut this = Self {
 188            _history_subscription: Subscription::new(|| {}),
 189            focus_handle,
 190            list_state: ListState::new(0, gpui::ListAlignment::Top, px(1000.)),
 191            items: Vec::new(),
 192            selection: None,
 193            hovered_index: None,
 194            preserve_selection_on_next_update: false,
 195            filter_editor,
 196            _subscriptions: vec![
 197                filter_editor_subscription,
 198                thread_metadata_store_subscription,
 199            ],
 200            _refresh_history_task: Task::ready(()),
 201            workspace,
 202            agent_connection_store,
 203            agent_server_store,
 204            restoring: HashSet::default(),
 205        };
 206
 207        this.update_items(cx);
 208        this
 209    }
 210
 211    pub fn has_selection(&self) -> bool {
 212        self.selection.is_some()
 213    }
 214
 215    pub fn clear_selection(&mut self) {
 216        self.selection = None;
 217    }
 218
 219    pub fn mark_restoring(&mut self, session_id: &acp::SessionId, cx: &mut Context<Self>) {
 220        self.restoring.insert(session_id.clone());
 221        cx.notify();
 222    }
 223
 224    pub fn clear_restoring(&mut self, session_id: &acp::SessionId, cx: &mut Context<Self>) {
 225        self.restoring.remove(session_id);
 226        cx.notify();
 227    }
 228
 229    pub fn focus_filter_editor(&self, window: &mut Window, cx: &mut App) {
 230        let handle = self.filter_editor.read(cx).focus_handle(cx);
 231        handle.focus(window, cx);
 232    }
 233
 234    pub fn is_filter_editor_focused(&self, window: &Window, cx: &App) -> bool {
 235        self.filter_editor
 236            .read(cx)
 237            .focus_handle(cx)
 238            .is_focused(window)
 239    }
 240
 241    fn update_items(&mut self, cx: &mut Context<Self>) {
 242        let sessions = ThreadMetadataStore::global(cx)
 243            .read(cx)
 244            .archived_entries()
 245            .sorted_by_cached_key(|t| t.created_at.unwrap_or(t.updated_at))
 246            .rev()
 247            .cloned()
 248            .collect::<Vec<_>>();
 249
 250        let query = self.filter_editor.read(cx).text(cx).to_lowercase();
 251        let today = Local::now().naive_local().date();
 252
 253        let mut items = Vec::with_capacity(sessions.len() + 5);
 254        let mut current_bucket: Option<TimeBucket> = None;
 255
 256        for session in sessions {
 257            let highlight_positions = if !query.is_empty() {
 258                match fuzzy_match_positions(&query, &session.title) {
 259                    Some(positions) => positions,
 260                    None => continue,
 261                }
 262            } else {
 263                Vec::new()
 264            };
 265
 266            let entry_bucket = {
 267                let entry_date = session
 268                    .created_at
 269                    .unwrap_or(session.updated_at)
 270                    .with_timezone(&Local)
 271                    .naive_local()
 272                    .date();
 273                TimeBucket::from_dates(today, entry_date)
 274            };
 275
 276            if Some(entry_bucket) != current_bucket {
 277                current_bucket = Some(entry_bucket);
 278                items.push(ArchiveListItem::BucketSeparator(entry_bucket));
 279            }
 280
 281            items.push(ArchiveListItem::Entry {
 282                thread: session,
 283                highlight_positions,
 284            });
 285        }
 286
 287        let preserve = self.preserve_selection_on_next_update;
 288        self.preserve_selection_on_next_update = false;
 289
 290        let saved_scroll = if preserve {
 291            Some(self.list_state.logical_scroll_top())
 292        } else {
 293            None
 294        };
 295
 296        self.list_state.reset(items.len());
 297        self.items = items;
 298
 299        if !preserve {
 300            self.hovered_index = None;
 301        } else if let Some(ix) = self.hovered_index {
 302            if ix >= self.items.len() || !self.is_selectable_item(ix) {
 303                self.hovered_index = None;
 304            }
 305        }
 306
 307        if let Some(scroll_top) = saved_scroll {
 308            self.list_state.scroll_to(scroll_top);
 309
 310            if let Some(ix) = self.selection {
 311                let next = self.find_next_selectable(ix).or_else(|| {
 312                    ix.checked_sub(1)
 313                        .and_then(|i| self.find_previous_selectable(i))
 314                });
 315                self.selection = next;
 316                if let Some(next) = next {
 317                    self.list_state.scroll_to_reveal_item(next);
 318                }
 319            }
 320        } else {
 321            self.selection = None;
 322        }
 323
 324        cx.notify();
 325    }
 326
 327    fn reset_filter_editor_text(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 328        self.filter_editor.update(cx, |editor, cx| {
 329            editor.set_text("", window, cx);
 330        });
 331    }
 332
 333    fn unarchive_thread(
 334        &mut self,
 335        thread: ThreadMetadata,
 336        window: &mut Window,
 337        cx: &mut Context<Self>,
 338    ) {
 339        if self.restoring.contains(&thread.session_id) {
 340            return;
 341        }
 342
 343        if thread.folder_paths.is_empty() {
 344            self.show_project_picker_for_thread(thread, window, cx);
 345            return;
 346        }
 347
 348        self.mark_restoring(&thread.session_id, cx);
 349        self.selection = None;
 350        self.reset_filter_editor_text(window, cx);
 351        cx.emit(ThreadsArchiveViewEvent::Unarchive { thread });
 352    }
 353
 354    fn show_project_picker_for_thread(
 355        &mut self,
 356        thread: ThreadMetadata,
 357        window: &mut Window,
 358        cx: &mut Context<Self>,
 359    ) {
 360        let Some(workspace) = self.workspace.upgrade() else {
 361            return;
 362        };
 363
 364        let archive_view = cx.weak_entity();
 365        let fs = workspace.read(cx).app_state().fs.clone();
 366        let current_workspace_id = workspace.read(cx).database_id();
 367        let sibling_workspace_ids: HashSet<WorkspaceId> = workspace
 368            .read(cx)
 369            .multi_workspace()
 370            .and_then(|mw| mw.upgrade())
 371            .map(|mw| {
 372                mw.read(cx)
 373                    .workspaces()
 374                    .filter_map(|ws| ws.read(cx).database_id())
 375                    .collect()
 376            })
 377            .unwrap_or_default();
 378
 379        workspace.update(cx, |workspace, cx| {
 380            workspace.toggle_modal(window, cx, |window, cx| {
 381                ProjectPickerModal::new(
 382                    thread,
 383                    fs,
 384                    archive_view,
 385                    current_workspace_id,
 386                    sibling_workspace_ids,
 387                    window,
 388                    cx,
 389                )
 390            });
 391        });
 392    }
 393
 394    fn is_selectable_item(&self, ix: usize) -> bool {
 395        matches!(self.items.get(ix), Some(ArchiveListItem::Entry { .. }))
 396    }
 397
 398    fn find_next_selectable(&self, start: usize) -> Option<usize> {
 399        (start..self.items.len()).find(|&i| self.is_selectable_item(i))
 400    }
 401
 402    fn find_previous_selectable(&self, start: usize) -> Option<usize> {
 403        (0..=start).rev().find(|&i| self.is_selectable_item(i))
 404    }
 405
 406    fn editor_move_down(&mut self, _: &MoveDown, window: &mut Window, cx: &mut Context<Self>) {
 407        self.select_next(&SelectNext, window, cx);
 408        if self.selection.is_some() {
 409            self.focus_handle.focus(window, cx);
 410        }
 411    }
 412
 413    fn editor_move_up(&mut self, _: &MoveUp, window: &mut Window, cx: &mut Context<Self>) {
 414        self.select_previous(&SelectPrevious, window, cx);
 415        if self.selection.is_some() {
 416            self.focus_handle.focus(window, cx);
 417        }
 418    }
 419
 420    fn select_next(&mut self, _: &SelectNext, _window: &mut Window, cx: &mut Context<Self>) {
 421        let next = match self.selection {
 422            Some(ix) => self.find_next_selectable(ix + 1),
 423            None => self.find_next_selectable(0),
 424        };
 425        if let Some(next) = next {
 426            self.selection = Some(next);
 427            self.list_state.scroll_to_reveal_item(next);
 428            cx.notify();
 429        }
 430    }
 431
 432    fn select_previous(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context<Self>) {
 433        match self.selection {
 434            Some(ix) => {
 435                if let Some(prev) = (ix > 0)
 436                    .then(|| self.find_previous_selectable(ix - 1))
 437                    .flatten()
 438                {
 439                    self.selection = Some(prev);
 440                    self.list_state.scroll_to_reveal_item(prev);
 441                } else {
 442                    self.selection = None;
 443                    self.focus_filter_editor(window, cx);
 444                }
 445                cx.notify();
 446            }
 447            None => {
 448                let last = self.items.len().saturating_sub(1);
 449                if let Some(prev) = self.find_previous_selectable(last) {
 450                    self.selection = Some(prev);
 451                    self.list_state.scroll_to_reveal_item(prev);
 452                    cx.notify();
 453                }
 454            }
 455        }
 456    }
 457
 458    fn select_first(&mut self, _: &SelectFirst, _window: &mut Window, cx: &mut Context<Self>) {
 459        if let Some(first) = self.find_next_selectable(0) {
 460            self.selection = Some(first);
 461            self.list_state.scroll_to_reveal_item(first);
 462            cx.notify();
 463        }
 464    }
 465
 466    fn select_last(&mut self, _: &SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
 467        let last = self.items.len().saturating_sub(1);
 468        if let Some(last) = self.find_previous_selectable(last) {
 469            self.selection = Some(last);
 470            self.list_state.scroll_to_reveal_item(last);
 471            cx.notify();
 472        }
 473    }
 474
 475    fn confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
 476        let Some(ix) = self.selection else { return };
 477        let Some(ArchiveListItem::Entry { thread, .. }) = self.items.get(ix) else {
 478            return;
 479        };
 480
 481        self.unarchive_thread(thread.clone(), window, cx);
 482    }
 483
 484    fn render_list_entry(
 485        &mut self,
 486        ix: usize,
 487        _window: &mut Window,
 488        cx: &mut Context<Self>,
 489    ) -> AnyElement {
 490        let Some(item) = self.items.get(ix) else {
 491            return div().into_any_element();
 492        };
 493
 494        match item {
 495            ArchiveListItem::BucketSeparator(bucket) => div()
 496                .w_full()
 497                .px_2p5()
 498                .pt_3()
 499                .pb_1()
 500                .child(
 501                    Label::new(bucket.label())
 502                        .size(LabelSize::Small)
 503                        .color(Color::Muted),
 504                )
 505                .into_any_element(),
 506            ArchiveListItem::Entry {
 507                thread,
 508                highlight_positions,
 509            } => {
 510                let id = SharedString::from(format!("archive-entry-{}", ix));
 511
 512                let is_focused = self.selection == Some(ix);
 513                let is_hovered = self.hovered_index == Some(ix);
 514
 515                let focus_handle = self.focus_handle.clone();
 516
 517                let timestamp =
 518                    format_history_entry_timestamp(thread.created_at.unwrap_or(thread.updated_at));
 519
 520                let icon_from_external_svg = self
 521                    .agent_server_store
 522                    .upgrade()
 523                    .and_then(|store| store.read(cx).agent_icon(&thread.agent_id));
 524
 525                let icon = if thread.agent_id.as_ref() == agent::ZED_AGENT_ID.as_ref() {
 526                    IconName::ZedAgent
 527                } else {
 528                    IconName::Sparkle
 529                };
 530
 531                let is_restoring = self.restoring.contains(&thread.session_id);
 532
 533                let base = ThreadItem::new(id, thread.title.clone())
 534                    .icon(icon)
 535                    .when_some(icon_from_external_svg, |this, svg| {
 536                        this.custom_icon_from_external_svg(svg)
 537                    })
 538                    .timestamp(timestamp)
 539                    .highlight_positions(highlight_positions.clone())
 540                    .project_paths(thread.folder_paths.paths_owned())
 541                    .focused(is_focused)
 542                    .hovered(is_hovered)
 543                    .on_hover(cx.listener(move |this, is_hovered, _window, cx| {
 544                        if *is_hovered {
 545                            this.hovered_index = Some(ix);
 546                        } else if this.hovered_index == Some(ix) {
 547                            this.hovered_index = None;
 548                        }
 549                        cx.notify();
 550                    }));
 551
 552                if is_restoring {
 553                    base.status(AgentThreadStatus::Running)
 554                        .action_slot(
 555                            IconButton::new("cancel-restore", IconName::Close)
 556                                .style(ButtonStyle::Filled)
 557                                .icon_size(IconSize::Small)
 558                                .icon_color(Color::Muted)
 559                                .tooltip(Tooltip::text("Cancel Restore"))
 560                                .on_click({
 561                                    let session_id = thread.session_id.clone();
 562                                    cx.listener(move |this, _, _, cx| {
 563                                        this.clear_restoring(&session_id, cx);
 564                                        cx.emit(ThreadsArchiveViewEvent::CancelRestore {
 565                                            session_id: session_id.clone(),
 566                                        });
 567                                        cx.stop_propagation();
 568                                    })
 569                                }),
 570                        )
 571                        .tooltip(Tooltip::text("Restoring\u{2026}"))
 572                        .into_any_element()
 573                } else {
 574                    base.action_slot(
 575                        IconButton::new("delete-thread", IconName::Trash)
 576                            .style(ButtonStyle::Filled)
 577                            .icon_size(IconSize::Small)
 578                            .icon_color(Color::Muted)
 579                            .tooltip({
 580                                move |_window, cx| {
 581                                    Tooltip::for_action_in(
 582                                        "Delete Thread",
 583                                        &RemoveSelectedThread,
 584                                        &focus_handle,
 585                                        cx,
 586                                    )
 587                                }
 588                            })
 589                            .on_click({
 590                                let agent = thread.agent_id.clone();
 591                                let session_id = thread.session_id.clone();
 592                                cx.listener(move |this, _, _, cx| {
 593                                    this.preserve_selection_on_next_update = true;
 594                                    this.delete_thread(session_id.clone(), agent.clone(), cx);
 595                                    cx.stop_propagation();
 596                                })
 597                            }),
 598                    )
 599                    .tooltip(move |_, cx| Tooltip::for_action("Restore Thread", &menu::Confirm, cx))
 600                    .on_click({
 601                        let thread = thread.clone();
 602                        cx.listener(move |this, _, window, cx| {
 603                            this.unarchive_thread(thread.clone(), window, cx);
 604                        })
 605                    })
 606                    .into_any_element()
 607                }
 608            }
 609        }
 610    }
 611
 612    fn remove_selected_thread(
 613        &mut self,
 614        _: &RemoveSelectedThread,
 615        _window: &mut Window,
 616        cx: &mut Context<Self>,
 617    ) {
 618        let Some(ix) = self.selection else { return };
 619        let Some(ArchiveListItem::Entry { thread, .. }) = self.items.get(ix) else {
 620            return;
 621        };
 622
 623        self.preserve_selection_on_next_update = true;
 624        self.delete_thread(thread.session_id.clone(), thread.agent_id.clone(), cx);
 625    }
 626
 627    fn delete_thread(
 628        &mut self,
 629        session_id: acp::SessionId,
 630        agent: AgentId,
 631        cx: &mut Context<Self>,
 632    ) {
 633        ThreadMetadataStore::global(cx)
 634            .update(cx, |store, cx| store.delete(session_id.clone(), cx));
 635
 636        let agent = Agent::from(agent);
 637
 638        let Some(agent_connection_store) = self.agent_connection_store.upgrade() else {
 639            return;
 640        };
 641        let fs = <dyn Fs>::global(cx);
 642
 643        let task = agent_connection_store.update(cx, |store, cx| {
 644            store
 645                .request_connection(agent.clone(), agent.server(fs, ThreadStore::global(cx)), cx)
 646                .read(cx)
 647                .wait_for_connection()
 648        });
 649        cx.spawn(async move |_this, cx| {
 650            crate::thread_worktree_archive::cleanup_thread_archived_worktrees(&session_id, cx)
 651                .await;
 652
 653            let state = task.await?;
 654            let task = cx.update(|cx| {
 655                if let Some(list) = state.connection.session_list(cx) {
 656                    list.delete_session(&session_id, cx)
 657                } else {
 658                    Task::ready(Ok(()))
 659                }
 660            });
 661            task.await
 662        })
 663        .detach_and_log_err(cx);
 664    }
 665
 666    fn render_header(&self, window: &Window, cx: &mut Context<Self>) -> impl IntoElement {
 667        let has_query = !self.filter_editor.read(cx).text(cx).is_empty();
 668        let sidebar_on_left = matches!(
 669            AgentSettings::get_global(cx).sidebar_side(),
 670            settings::SidebarSide::Left
 671        );
 672        let traffic_lights =
 673            cfg!(target_os = "macos") && !window.is_fullscreen() && sidebar_on_left;
 674        let header_height = platform_title_bar_height(window);
 675        let show_focus_keybinding =
 676            self.selection.is_some() && !self.filter_editor.focus_handle(cx).is_focused(window);
 677
 678        h_flex()
 679            .h(header_height)
 680            .mt_px()
 681            .pb_px()
 682            .map(|this| {
 683                if traffic_lights {
 684                    this.pl(px(ui::utils::TRAFFIC_LIGHT_PADDING))
 685                } else {
 686                    this.pl_1p5()
 687                }
 688            })
 689            .pr_1p5()
 690            .gap_1()
 691            .justify_between()
 692            .border_b_1()
 693            .border_color(cx.theme().colors().border)
 694            .when(traffic_lights, |this| {
 695                this.child(Divider::vertical().color(ui::DividerColor::Border))
 696            })
 697            .child(
 698                h_flex()
 699                    .ml_1()
 700                    .min_w_0()
 701                    .w_full()
 702                    .gap_1()
 703                    .child(
 704                        Icon::new(IconName::MagnifyingGlass)
 705                            .size(IconSize::Small)
 706                            .color(Color::Muted),
 707                    )
 708                    .child(self.filter_editor.clone()),
 709            )
 710            .when(show_focus_keybinding, |this| {
 711                this.child(KeyBinding::for_action(&FocusSidebarFilter, cx))
 712            })
 713            .when(has_query, |this| {
 714                this.child(
 715                    IconButton::new("clear-filter", IconName::Close)
 716                        .icon_size(IconSize::Small)
 717                        .tooltip(Tooltip::text("Clear Search"))
 718                        .on_click(cx.listener(|this, _, window, cx| {
 719                            this.reset_filter_editor_text(window, cx);
 720                            this.update_items(cx);
 721                        })),
 722                )
 723            })
 724    }
 725}
 726
 727pub fn format_history_entry_timestamp(entry_time: DateTime<Utc>) -> String {
 728    let now = Utc::now();
 729    let duration = now.signed_duration_since(entry_time);
 730
 731    let minutes = duration.num_minutes();
 732    let hours = duration.num_hours();
 733    let days = duration.num_days();
 734    let weeks = days / 7;
 735    let months = days / 30;
 736
 737    if minutes < 60 {
 738        format!("{}m", minutes.max(1))
 739    } else if hours < 24 {
 740        format!("{}h", hours.max(1))
 741    } else if days < 7 {
 742        format!("{}d", days.max(1))
 743    } else if weeks < 4 {
 744        format!("{}w", weeks.max(1))
 745    } else {
 746        format!("{}mo", months.max(1))
 747    }
 748}
 749
 750impl Focusable for ThreadsArchiveView {
 751    fn focus_handle(&self, _cx: &App) -> FocusHandle {
 752        self.focus_handle.clone()
 753    }
 754}
 755
 756impl Render for ThreadsArchiveView {
 757    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 758        let is_empty = self.items.is_empty();
 759        let has_query = !self.filter_editor.read(cx).text(cx).is_empty();
 760
 761        let content = if is_empty {
 762            let message = if has_query {
 763                "No threads match your search."
 764            } else {
 765                "No archived or hidden threads yet."
 766            };
 767
 768            v_flex()
 769                .flex_1()
 770                .justify_center()
 771                .items_center()
 772                .child(
 773                    Label::new(message)
 774                        .size(LabelSize::Small)
 775                        .color(Color::Muted),
 776                )
 777                .into_any_element()
 778        } else {
 779            v_flex()
 780                .flex_1()
 781                .overflow_hidden()
 782                .child(
 783                    list(
 784                        self.list_state.clone(),
 785                        cx.processor(Self::render_list_entry),
 786                    )
 787                    .flex_1()
 788                    .size_full(),
 789                )
 790                .vertical_scrollbar_for(&self.list_state, window, cx)
 791                .into_any_element()
 792        };
 793
 794        v_flex()
 795            .key_context("ThreadsArchiveView")
 796            .track_focus(&self.focus_handle)
 797            .on_action(cx.listener(Self::select_next))
 798            .on_action(cx.listener(Self::select_previous))
 799            .on_action(cx.listener(Self::editor_move_down))
 800            .on_action(cx.listener(Self::editor_move_up))
 801            .on_action(cx.listener(Self::select_first))
 802            .on_action(cx.listener(Self::select_last))
 803            .on_action(cx.listener(Self::confirm))
 804            .on_action(cx.listener(Self::remove_selected_thread))
 805            .size_full()
 806            .child(self.render_header(window, cx))
 807            .child(content)
 808    }
 809}
 810
 811struct ProjectPickerModal {
 812    picker: Entity<Picker<ProjectPickerDelegate>>,
 813    _subscription: Subscription,
 814}
 815
 816impl ProjectPickerModal {
 817    fn new(
 818        thread: ThreadMetadata,
 819        fs: Arc<dyn Fs>,
 820        archive_view: WeakEntity<ThreadsArchiveView>,
 821        current_workspace_id: Option<WorkspaceId>,
 822        sibling_workspace_ids: HashSet<WorkspaceId>,
 823        window: &mut Window,
 824        cx: &mut Context<Self>,
 825    ) -> Self {
 826        let delegate = ProjectPickerDelegate {
 827            thread,
 828            archive_view,
 829            workspaces: Vec::new(),
 830            filtered_entries: Vec::new(),
 831            selected_index: 0,
 832            current_workspace_id,
 833            sibling_workspace_ids,
 834            focus_handle: cx.focus_handle(),
 835        };
 836
 837        let picker = cx.new(|cx| {
 838            Picker::list(delegate, window, cx)
 839                .list_measure_all()
 840                .modal(false)
 841        });
 842
 843        let picker_focus_handle = picker.focus_handle(cx);
 844        picker.update(cx, |picker, _| {
 845            picker.delegate.focus_handle = picker_focus_handle;
 846        });
 847
 848        let _subscription =
 849            cx.subscribe(&picker, |_this: &mut Self, _, _event: &DismissEvent, cx| {
 850                cx.emit(DismissEvent);
 851            });
 852
 853        let db = WorkspaceDb::global(cx);
 854        cx.spawn_in(window, async move |this, cx| {
 855            let workspaces = db
 856                .recent_workspaces_on_disk(fs.as_ref())
 857                .await
 858                .log_err()
 859                .unwrap_or_default();
 860            let workspaces = resolve_worktree_workspaces(workspaces, fs.as_ref()).await;
 861            this.update_in(cx, move |this, window, cx| {
 862                this.picker.update(cx, move |picker, cx| {
 863                    picker.delegate.workspaces = workspaces;
 864                    picker.update_matches(picker.query(cx), window, cx)
 865                })
 866            })
 867            .ok();
 868        })
 869        .detach();
 870
 871        picker.focus_handle(cx).focus(window, cx);
 872
 873        Self {
 874            picker,
 875            _subscription,
 876        }
 877    }
 878}
 879
 880impl EventEmitter<DismissEvent> for ProjectPickerModal {}
 881
 882impl Focusable for ProjectPickerModal {
 883    fn focus_handle(&self, cx: &App) -> FocusHandle {
 884        self.picker.focus_handle(cx)
 885    }
 886}
 887
 888impl ModalView for ProjectPickerModal {}
 889
 890impl Render for ProjectPickerModal {
 891    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 892        v_flex()
 893            .key_context("ProjectPickerModal")
 894            .elevation_3(cx)
 895            .w(rems(34.))
 896            .on_action(cx.listener(|this, _: &workspace::Open, window, cx| {
 897                this.picker.update(cx, |picker, cx| {
 898                    picker.delegate.open_local_folder(window, cx)
 899                })
 900            }))
 901            .child(self.picker.clone())
 902    }
 903}
 904
 905enum ProjectPickerEntry {
 906    Header(SharedString),
 907    Workspace(StringMatch),
 908}
 909
 910struct ProjectPickerDelegate {
 911    thread: ThreadMetadata,
 912    archive_view: WeakEntity<ThreadsArchiveView>,
 913    current_workspace_id: Option<WorkspaceId>,
 914    sibling_workspace_ids: HashSet<WorkspaceId>,
 915    workspaces: Vec<(
 916        WorkspaceId,
 917        SerializedWorkspaceLocation,
 918        PathList,
 919        DateTime<Utc>,
 920    )>,
 921    filtered_entries: Vec<ProjectPickerEntry>,
 922    selected_index: usize,
 923    focus_handle: FocusHandle,
 924}
 925
 926impl ProjectPickerDelegate {
 927    fn update_working_directories_and_unarchive(
 928        &mut self,
 929        paths: PathList,
 930        window: &mut Window,
 931        cx: &mut Context<Picker<Self>>,
 932    ) {
 933        self.thread.folder_paths = paths.clone();
 934        ThreadMetadataStore::global(cx).update(cx, |store, cx| {
 935            store.update_working_directories(&self.thread.session_id, paths, cx);
 936        });
 937
 938        self.archive_view
 939            .update(cx, |view, cx| {
 940                view.selection = None;
 941                view.reset_filter_editor_text(window, cx);
 942                cx.emit(ThreadsArchiveViewEvent::Unarchive {
 943                    thread: self.thread.clone(),
 944                });
 945            })
 946            .log_err();
 947    }
 948
 949    fn is_current_workspace(&self, workspace_id: WorkspaceId) -> bool {
 950        self.current_workspace_id == Some(workspace_id)
 951    }
 952
 953    fn is_sibling_workspace(&self, workspace_id: WorkspaceId) -> bool {
 954        self.sibling_workspace_ids.contains(&workspace_id)
 955            && !self.is_current_workspace(workspace_id)
 956    }
 957
 958    fn selected_match(&self) -> Option<&StringMatch> {
 959        match self.filtered_entries.get(self.selected_index)? {
 960            ProjectPickerEntry::Workspace(hit) => Some(hit),
 961            ProjectPickerEntry::Header(_) => None,
 962        }
 963    }
 964
 965    fn open_local_folder(&mut self, window: &mut Window, cx: &mut Context<Picker<Self>>) {
 966        let paths_receiver = cx.prompt_for_paths(gpui::PathPromptOptions {
 967            files: false,
 968            directories: true,
 969            multiple: false,
 970            prompt: None,
 971        });
 972        cx.spawn_in(window, async move |this, cx| {
 973            let Ok(Ok(Some(paths))) = paths_receiver.await else {
 974                return;
 975            };
 976            if paths.is_empty() {
 977                return;
 978            }
 979
 980            let work_dirs = PathList::new(&paths);
 981
 982            this.update_in(cx, |this, window, cx| {
 983                this.delegate
 984                    .update_working_directories_and_unarchive(work_dirs, window, cx);
 985                cx.emit(DismissEvent);
 986            })
 987            .log_err();
 988        })
 989        .detach();
 990    }
 991}
 992
 993impl EventEmitter<DismissEvent> for ProjectPickerDelegate {}
 994
 995impl PickerDelegate for ProjectPickerDelegate {
 996    type ListItem = AnyElement;
 997
 998    fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
 999        format!("Associate the \"{}\" thread with...", self.thread.title).into()
1000    }
1001
1002    fn render_editor(
1003        &self,
1004        editor: &Arc<dyn ErasedEditor>,
1005        window: &mut Window,
1006        cx: &mut Context<Picker<Self>>,
1007    ) -> Div {
1008        h_flex()
1009            .flex_none()
1010            .h_9()
1011            .px_2p5()
1012            .justify_between()
1013            .border_b_1()
1014            .border_color(cx.theme().colors().border_variant)
1015            .child(editor.render(window, cx))
1016    }
1017
1018    fn match_count(&self) -> usize {
1019        self.filtered_entries.len()
1020    }
1021
1022    fn selected_index(&self) -> usize {
1023        self.selected_index
1024    }
1025
1026    fn set_selected_index(
1027        &mut self,
1028        ix: usize,
1029        _window: &mut Window,
1030        _cx: &mut Context<Picker<Self>>,
1031    ) {
1032        self.selected_index = ix;
1033    }
1034
1035    fn can_select(&self, ix: usize, _window: &mut Window, _cx: &mut Context<Picker<Self>>) -> bool {
1036        matches!(
1037            self.filtered_entries.get(ix),
1038            Some(ProjectPickerEntry::Workspace(_))
1039        )
1040    }
1041
1042    fn update_matches(
1043        &mut self,
1044        query: String,
1045        _window: &mut Window,
1046        cx: &mut Context<Picker<Self>>,
1047    ) -> Task<()> {
1048        let query = query.trim_start();
1049        let smart_case = query.chars().any(|c| c.is_uppercase());
1050        let is_empty_query = query.is_empty();
1051
1052        let sibling_candidates: Vec<_> = self
1053            .workspaces
1054            .iter()
1055            .enumerate()
1056            .filter(|(_, (id, _, _, _))| self.is_sibling_workspace(*id))
1057            .map(|(id, (_, _, paths, _))| {
1058                let combined_string = paths
1059                    .ordered_paths()
1060                    .map(|path| path.compact().to_string_lossy().into_owned())
1061                    .collect::<Vec<_>>()
1062                    .join("");
1063                StringMatchCandidate::new(id, &combined_string)
1064            })
1065            .collect();
1066
1067        let mut sibling_matches = smol::block_on(fuzzy::match_strings(
1068            &sibling_candidates,
1069            query,
1070            smart_case,
1071            true,
1072            100,
1073            &Default::default(),
1074            cx.background_executor().clone(),
1075        ));
1076
1077        sibling_matches.sort_unstable_by(|a, b| {
1078            b.score
1079                .partial_cmp(&a.score)
1080                .unwrap_or(std::cmp::Ordering::Equal)
1081                .then_with(|| a.candidate_id.cmp(&b.candidate_id))
1082        });
1083
1084        let recent_candidates: Vec<_> = self
1085            .workspaces
1086            .iter()
1087            .enumerate()
1088            .filter(|(_, (id, _, _, _))| {
1089                !self.is_current_workspace(*id) && !self.is_sibling_workspace(*id)
1090            })
1091            .map(|(id, (_, _, paths, _))| {
1092                let combined_string = paths
1093                    .ordered_paths()
1094                    .map(|path| path.compact().to_string_lossy().into_owned())
1095                    .collect::<Vec<_>>()
1096                    .join("");
1097                StringMatchCandidate::new(id, &combined_string)
1098            })
1099            .collect();
1100
1101        let mut recent_matches = smol::block_on(fuzzy::match_strings(
1102            &recent_candidates,
1103            query,
1104            smart_case,
1105            true,
1106            100,
1107            &Default::default(),
1108            cx.background_executor().clone(),
1109        ));
1110
1111        recent_matches.sort_unstable_by(|a, b| {
1112            b.score
1113                .partial_cmp(&a.score)
1114                .unwrap_or(std::cmp::Ordering::Equal)
1115                .then_with(|| a.candidate_id.cmp(&b.candidate_id))
1116        });
1117
1118        let mut entries = Vec::new();
1119
1120        let has_siblings_to_show = if is_empty_query {
1121            !sibling_candidates.is_empty()
1122        } else {
1123            !sibling_matches.is_empty()
1124        };
1125
1126        if has_siblings_to_show {
1127            entries.push(ProjectPickerEntry::Header("This Window".into()));
1128
1129            if is_empty_query {
1130                for (id, (workspace_id, _, _, _)) in self.workspaces.iter().enumerate() {
1131                    if self.is_sibling_workspace(*workspace_id) {
1132                        entries.push(ProjectPickerEntry::Workspace(StringMatch {
1133                            candidate_id: id,
1134                            score: 0.0,
1135                            positions: Vec::new(),
1136                            string: String::new(),
1137                        }));
1138                    }
1139                }
1140            } else {
1141                for m in sibling_matches {
1142                    entries.push(ProjectPickerEntry::Workspace(m));
1143                }
1144            }
1145        }
1146
1147        let has_recent_to_show = if is_empty_query {
1148            !recent_candidates.is_empty()
1149        } else {
1150            !recent_matches.is_empty()
1151        };
1152
1153        if has_recent_to_show {
1154            entries.push(ProjectPickerEntry::Header("Recent Projects".into()));
1155
1156            if is_empty_query {
1157                for (id, (workspace_id, _, _, _)) in self.workspaces.iter().enumerate() {
1158                    if !self.is_current_workspace(*workspace_id)
1159                        && !self.is_sibling_workspace(*workspace_id)
1160                    {
1161                        entries.push(ProjectPickerEntry::Workspace(StringMatch {
1162                            candidate_id: id,
1163                            score: 0.0,
1164                            positions: Vec::new(),
1165                            string: String::new(),
1166                        }));
1167                    }
1168                }
1169            } else {
1170                for m in recent_matches {
1171                    entries.push(ProjectPickerEntry::Workspace(m));
1172                }
1173            }
1174        }
1175
1176        self.filtered_entries = entries;
1177
1178        self.selected_index = self
1179            .filtered_entries
1180            .iter()
1181            .position(|e| matches!(e, ProjectPickerEntry::Workspace(_)))
1182            .unwrap_or(0);
1183
1184        Task::ready(())
1185    }
1186
1187    fn confirm(&mut self, _secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
1188        let candidate_id = match self.filtered_entries.get(self.selected_index) {
1189            Some(ProjectPickerEntry::Workspace(hit)) => hit.candidate_id,
1190            _ => return,
1191        };
1192        let Some((_workspace_id, _location, paths, _)) = self.workspaces.get(candidate_id) else {
1193            return;
1194        };
1195
1196        self.update_working_directories_and_unarchive(paths.clone(), window, cx);
1197        cx.emit(DismissEvent);
1198    }
1199
1200    fn dismissed(&mut self, _window: &mut Window, _cx: &mut Context<Picker<Self>>) {}
1201
1202    fn no_matches_text(&self, _window: &mut Window, _cx: &mut App) -> Option<SharedString> {
1203        let text = if self.workspaces.is_empty() {
1204            "No recent projects found"
1205        } else {
1206            "No matches"
1207        };
1208        Some(text.into())
1209    }
1210
1211    fn render_match(
1212        &self,
1213        ix: usize,
1214        selected: bool,
1215        window: &mut Window,
1216        cx: &mut Context<Picker<Self>>,
1217    ) -> Option<Self::ListItem> {
1218        match self.filtered_entries.get(ix)? {
1219            ProjectPickerEntry::Header(title) => Some(
1220                v_flex()
1221                    .w_full()
1222                    .gap_1()
1223                    .when(ix > 0, |this| this.mt_1().child(Divider::horizontal()))
1224                    .child(ListSubHeader::new(title.clone()).inset(true))
1225                    .into_any_element(),
1226            ),
1227            ProjectPickerEntry::Workspace(hit) => {
1228                let (_, location, paths, _) = self.workspaces.get(hit.candidate_id)?;
1229
1230                let ordered_paths: Vec<_> = paths
1231                    .ordered_paths()
1232                    .map(|p| p.compact().to_string_lossy().to_string())
1233                    .collect();
1234
1235                let tooltip_path: SharedString = ordered_paths.join("\n").into();
1236
1237                let mut path_start_offset = 0;
1238                let match_labels: Vec<_> = paths
1239                    .ordered_paths()
1240                    .map(|p| p.compact())
1241                    .map(|path| {
1242                        let path_string = path.to_string_lossy();
1243                        let path_text = path_string.to_string();
1244                        let path_byte_len = path_text.len();
1245
1246                        let path_positions: Vec<usize> = hit
1247                            .positions
1248                            .iter()
1249                            .copied()
1250                            .skip_while(|pos| *pos < path_start_offset)
1251                            .take_while(|pos| *pos < path_start_offset + path_byte_len)
1252                            .map(|pos| pos - path_start_offset)
1253                            .collect();
1254
1255                        let file_name_match = path.file_name().map(|file_name| {
1256                            let file_name_text = file_name.to_string_lossy().into_owned();
1257                            let file_name_start = path_byte_len - file_name_text.len();
1258                            let highlight_positions: Vec<usize> = path_positions
1259                                .iter()
1260                                .copied()
1261                                .skip_while(|pos| *pos < file_name_start)
1262                                .take_while(|pos| *pos < file_name_start + file_name_text.len())
1263                                .map(|pos| pos - file_name_start)
1264                                .collect();
1265                            HighlightedMatch {
1266                                text: file_name_text,
1267                                highlight_positions,
1268                                color: Color::Default,
1269                            }
1270                        });
1271
1272                        path_start_offset += path_byte_len;
1273                        file_name_match
1274                    })
1275                    .collect();
1276
1277                let highlighted_match = HighlightedMatchWithPaths {
1278                    prefix: match location {
1279                        SerializedWorkspaceLocation::Remote(options) => {
1280                            Some(SharedString::from(options.display_name()))
1281                        }
1282                        _ => None,
1283                    },
1284                    match_label: HighlightedMatch::join(match_labels.into_iter().flatten(), ", "),
1285                    paths: Vec::new(),
1286                    active: false,
1287                };
1288
1289                Some(
1290                    ListItem::new(ix)
1291                        .toggle_state(selected)
1292                        .inset(true)
1293                        .spacing(ListItemSpacing::Sparse)
1294                        .child(
1295                            h_flex()
1296                                .gap_3()
1297                                .flex_grow()
1298                                .child(highlighted_match.render(window, cx)),
1299                        )
1300                        .tooltip(Tooltip::text(tooltip_path))
1301                        .into_any_element(),
1302                )
1303            }
1304        }
1305    }
1306
1307    fn render_footer(&self, _: &mut Window, cx: &mut Context<Picker<Self>>) -> Option<AnyElement> {
1308        let has_selection = self.selected_match().is_some();
1309        let focus_handle = self.focus_handle.clone();
1310
1311        Some(
1312            h_flex()
1313                .flex_1()
1314                .p_1p5()
1315                .gap_1()
1316                .justify_end()
1317                .border_t_1()
1318                .border_color(cx.theme().colors().border_variant)
1319                .child(
1320                    Button::new("open_local_folder", "Choose from Local Folders")
1321                        .key_binding(KeyBinding::for_action_in(
1322                            &workspace::Open::default(),
1323                            &focus_handle,
1324                            cx,
1325                        ))
1326                        .on_click(cx.listener(|this, _, window, cx| {
1327                            this.delegate.open_local_folder(window, cx);
1328                        })),
1329                )
1330                .child(
1331                    Button::new("select_project", "Select")
1332                        .disabled(!has_selection)
1333                        .key_binding(KeyBinding::for_action_in(&menu::Confirm, &focus_handle, cx))
1334                        .on_click(cx.listener(move |picker, _, window, cx| {
1335                            picker.delegate.confirm(false, window, cx);
1336                        })),
1337                )
1338                .into_any(),
1339        )
1340    }
1341}
1342
1343#[cfg(test)]
1344mod tests {
1345    use super::*;
1346
1347    #[test]
1348    fn test_fuzzy_match_positions_returns_byte_indices() {
1349        // "🔥abc" — the fire emoji is 4 bytes, so 'a' starts at byte 4, 'b' at 5, 'c' at 6.
1350        let text = "🔥abc";
1351        let positions = fuzzy_match_positions("ab", text).expect("should match");
1352        assert_eq!(positions, vec![4, 5]);
1353
1354        // Verify positions are valid char boundaries (this is the assertion that
1355        // panicked before the fix).
1356        for &pos in &positions {
1357            assert!(
1358                text.is_char_boundary(pos),
1359                "position {pos} is not a valid UTF-8 boundary in {text:?}"
1360            );
1361        }
1362    }
1363
1364    #[test]
1365    fn test_fuzzy_match_positions_ascii_still_works() {
1366        let positions = fuzzy_match_positions("he", "hello").expect("should match");
1367        assert_eq!(positions, vec![0, 1]);
1368    }
1369
1370    #[test]
1371    fn test_fuzzy_match_positions_case_insensitive() {
1372        let positions = fuzzy_match_positions("HE", "hello").expect("should match");
1373        assert_eq!(positions, vec![0, 1]);
1374    }
1375
1376    #[test]
1377    fn test_fuzzy_match_positions_no_match() {
1378        assert!(fuzzy_match_positions("xyz", "hello").is_none());
1379    }
1380
1381    #[test]
1382    fn test_fuzzy_match_positions_multi_byte_interior() {
1383        // "café" — 'é' is 2 bytes (0xC3 0xA9), so 'f' starts at byte 4, 'é' at byte 5.
1384        let text = "café";
1385        let positions = fuzzy_match_positions("", text).expect("should match");
1386        // 'c'=0, 'a'=1, 'f'=2, 'é'=3..4 — wait, let's verify:
1387        // Actually: c=1 byte, a=1 byte, f=1 byte, é=2 bytes
1388        // So byte positions: c=0, a=1, f=2, é=3
1389        assert_eq!(positions, vec![2, 3]);
1390        for &pos in &positions {
1391            assert!(
1392                text.is_char_boundary(pos),
1393                "position {pos} is not a valid UTF-8 boundary in {text:?}"
1394            );
1395        }
1396    }
1397}