waiting_room.rs

  1use crate::{
  2    sidebar::{Side, ToggleSidebarItem},
  3    AppState, ToggleFollow, Workspace,
  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 =
 81            cx.spawn_weak({
 82                let contact = contact.clone();
 83                |this, mut cx| async move {
 84                    let project = Project::remote(
 85                        project_id,
 86                        app_state.client.clone(),
 87                        app_state.user_store.clone(),
 88                        app_state.languages.clone(),
 89                        app_state.fs.clone(),
 90                        &mut cx,
 91                    )
 92                    .await;
 93
 94                    if let Some(this) = this.upgrade(&cx) {
 95                        this.update(&mut cx, |this, cx| {
 96                            this.waiting = false;
 97                            match project {
 98                                Ok(project) => {
 99                                    cx.replace_root_view(|cx| {
100                                        let mut workspace = Workspace::new(project, cx);
101                                        (app_state.initialize_workspace)(
102                                            &mut workspace,
103                                            &app_state,
104                                            cx,
105                                        );
106                                        workspace.toggle_sidebar_item(
107                                            &ToggleSidebarItem {
108                                                side: Side::Left,
109                                                item_index: 0,
110                                            },
111                                            cx,
112                                        );
113                                        if let Some((host_peer_id, _)) =
114                                            workspace.project.read(cx).collaborators().iter().find(
115                                                |(_, collaborator)| collaborator.replica_id == 0,
116                                            )
117                                        {
118                                            if let Some(follow) = workspace
119                                                .toggle_follow(&ToggleFollow(*host_peer_id), cx)
120                                            {
121                                                follow.detach_and_log_err(cx);
122                                            }
123                                        }
124                                        workspace
125                                    });
126                                }
127                                Err(error @ _) => {
128                                    let login = &contact.user.github_login;
129                                    let message = match error {
130                                        project::JoinProjectError::HostDeclined => {
131                                            format!("@{} declined your request.", login)
132                                        }
133                                        project::JoinProjectError::HostClosedProject => {
134                                            format!(
135                                                "@{} closed their copy of {}.",
136                                                login,
137                                                humanize_list(
138                                                    &contact.projects[project_index]
139                                                        .worktree_root_names
140                                                )
141                                            )
142                                        }
143                                        project::JoinProjectError::HostWentOffline => {
144                                            format!("@{} went offline.", login)
145                                        }
146                                        project::JoinProjectError::Other(error) => {
147                                            log::error!("error joining project: {}", error);
148                                            "An error occurred.".to_string()
149                                        }
150                                    };
151                                    this.message = message;
152                                    cx.notify();
153                                }
154                            }
155                        })
156                    }
157
158                    Ok(())
159                }
160            });
161
162        Self {
163            project_id,
164            avatar: contact.user.avatar.clone(),
165            message: format!(
166                "Asking to join @{}'s copy of {}...",
167                contact.user.github_login,
168                humanize_list(&contact.projects[project_index].worktree_root_names)
169            ),
170            waiting: true,
171            client,
172            _join_task,
173        }
174    }
175}
176
177fn humanize_list<'a>(items: impl IntoIterator<Item = &'a String>) -> String {
178    let mut list = String::new();
179    let mut items = items.into_iter().enumerate().peekable();
180    while let Some((ix, item)) = items.next() {
181        if ix > 0 {
182            list.push_str(", ");
183            if items.peek().is_none() {
184                list.push_str("and ");
185            }
186        }
187
188        list.push_str(item);
189    }
190    list
191}