waiting_room.rs

  1use crate::{
  2    sidebar::{Side, ToggleSidebarItem},
  3    AppState, ToggleFollow,
  4};
  5use anyhow::Result;
  6use client::{proto, Client, Contact};
  7use gpui::{
  8    elements::*, ElementBox, Entity, ImageData, MutableAppContext, RenderContext, Task, View,
  9    ViewContext,
 10};
 11use project::Project;
 12use settings::Settings;
 13use std::sync::Arc;
 14use util::ResultExt;
 15
 16pub struct WaitingRoom {
 17    project_id: u64,
 18    avatar: Option<Arc<ImageData>>,
 19    message: String,
 20    waiting: bool,
 21    client: Arc<Client>,
 22    _join_task: Task<Result<()>>,
 23}
 24
 25impl Entity for WaitingRoom {
 26    type Event = ();
 27
 28    fn release(&mut self, _: &mut MutableAppContext) {
 29        if self.waiting {
 30            self.client
 31                .send(proto::LeaveProject {
 32                    project_id: self.project_id,
 33                })
 34                .log_err();
 35        }
 36    }
 37}
 38
 39impl View for WaitingRoom {
 40    fn ui_name() -> &'static str {
 41        "WaitingRoom"
 42    }
 43
 44    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
 45        let theme = &cx.global::<Settings>().theme.workspace;
 46
 47        Flex::column()
 48            .with_children(self.avatar.clone().map(|avatar| {
 49                Image::new(avatar)
 50                    .with_style(theme.joining_project_avatar)
 51                    .aligned()
 52                    .boxed()
 53            }))
 54            .with_child(
 55                Text::new(
 56                    self.message.clone(),
 57                    theme.joining_project_message.text.clone(),
 58                )
 59                .contained()
 60                .with_style(theme.joining_project_message.container)
 61                .aligned()
 62                .boxed(),
 63            )
 64            .aligned()
 65            .contained()
 66            .with_background_color(theme.background)
 67            .boxed()
 68    }
 69}
 70
 71impl WaitingRoom {
 72    pub fn new(
 73        contact: Arc<Contact>,
 74        project_index: usize,
 75        app_state: Arc<AppState>,
 76        cx: &mut ViewContext<Self>,
 77    ) -> Self {
 78        let project_id = contact.projects[project_index].id;
 79        let client = app_state.client.clone();
 80        let _join_task = cx.spawn_weak({
 81            let contact = contact.clone();
 82            |this, mut cx| async move {
 83                let project = Project::remote(
 84                    project_id,
 85                    app_state.client.clone(),
 86                    app_state.user_store.clone(),
 87                    app_state.languages.clone(),
 88                    app_state.fs.clone(),
 89                    &mut cx,
 90                )
 91                .await;
 92
 93                if let Some(this) = this.upgrade(&cx) {
 94                    this.update(&mut cx, |this, cx| {
 95                        this.waiting = false;
 96                        match project {
 97                            Ok(project) => {
 98                                cx.replace_root_view(|cx| {
 99                                    let mut workspace = (app_state.build_workspace)(
100                                        project.clone(),
101                                        &app_state,
102                                        cx,
103                                    );
104                                    workspace.toggle_sidebar_item(
105                                        &ToggleSidebarItem {
106                                            side: Side::Left,
107                                            item_index: 0,
108                                        },
109                                        cx,
110                                    );
111                                    if let Some((host_peer_id, _)) = project
112                                        .read(cx)
113                                        .collaborators()
114                                        .iter()
115                                        .find(|(_, collaborator)| collaborator.replica_id == 0)
116                                    {
117                                        if let Some(follow) = workspace
118                                            .toggle_follow(&ToggleFollow(*host_peer_id), cx)
119                                        {
120                                            follow.detach_and_log_err(cx);
121                                        }
122                                    }
123                                    workspace
124                                });
125                            }
126                            Err(error @ _) => {
127                                let login = &contact.user.github_login;
128                                let message = match error {
129                                    project::JoinProjectError::HostDeclined => {
130                                        format!("@{} declined your request.", login)
131                                    }
132                                    project::JoinProjectError::HostClosedProject => {
133                                        format!(
134                                            "@{} closed their copy of {}.",
135                                            login,
136                                            humanize_list(
137                                                &contact.projects[project_index]
138                                                    .worktree_root_names
139                                            )
140                                        )
141                                    }
142                                    project::JoinProjectError::HostWentOffline => {
143                                        format!("@{} went offline.", login)
144                                    }
145                                    project::JoinProjectError::Other(error) => {
146                                        log::error!("error joining project: {}", error);
147                                        "An error occurred.".to_string()
148                                    }
149                                };
150                                this.message = message;
151                                cx.notify();
152                            }
153                        }
154                    })
155                }
156
157                Ok(())
158            }
159        });
160
161        Self {
162            project_id,
163            avatar: contact.user.avatar.clone(),
164            message: format!(
165                "Asking to join @{}'s copy of {}...",
166                contact.user.github_login,
167                humanize_list(&contact.projects[project_index].worktree_root_names)
168            ),
169            waiting: true,
170            client,
171            _join_task,
172        }
173    }
174}
175
176fn humanize_list<'a>(items: impl IntoIterator<Item = &'a String>) -> String {
177    let mut list = String::new();
178    let mut items = items.into_iter().enumerate().peekable();
179    while let Some((ix, item)) = items.next() {
180        if ix > 0 {
181            list.push_str(", ");
182            if items.peek().is_none() {
183                list.push_str("and ");
184            }
185        }
186
187        list.push_str(item);
188    }
189    list
190}