join_project_notification.rs

 1use client::User;
 2use gpui::{
 3    actions, ElementBox, Entity, ModelHandle, MutableAppContext, RenderContext, View, ViewContext,
 4};
 5use project::Project;
 6use std::sync::Arc;
 7use workspace::Notification;
 8
 9use crate::notifications::render_user_notification;
10
11pub fn init(cx: &mut MutableAppContext) {
12    cx.add_action(JoinProjectNotification::decline);
13    cx.add_action(JoinProjectNotification::accept);
14}
15
16pub enum Event {
17    Dismiss,
18}
19
20actions!(contacts_panel, [Accept, Decline]);
21
22pub struct JoinProjectNotification {
23    project: ModelHandle<Project>,
24    user: Arc<User>,
25}
26
27impl JoinProjectNotification {
28    pub fn new(project: ModelHandle<Project>, user: Arc<User>, cx: &mut ViewContext<Self>) -> Self {
29        cx.subscribe(&project, |this, _, event, cx| {
30            if let project::Event::ContactCancelledJoinRequest(user) = event {
31                if *user == this.user {
32                    cx.emit(Event::Dismiss);
33                }
34            }
35        })
36        .detach();
37        Self { project, user }
38    }
39
40    fn decline(&mut self, _: &Decline, cx: &mut ViewContext<Self>) {
41        self.project.update(cx, |project, cx| {
42            project.respond_to_join_request(self.user.id, false, cx)
43        });
44        cx.emit(Event::Dismiss)
45    }
46
47    fn accept(&mut self, _: &Accept, cx: &mut ViewContext<Self>) {
48        self.project.update(cx, |project, cx| {
49            project.respond_to_join_request(self.user.id, true, cx)
50        });
51        cx.emit(Event::Dismiss)
52    }
53}
54
55impl Entity for JoinProjectNotification {
56    type Event = Event;
57}
58
59impl View for JoinProjectNotification {
60    fn ui_name() -> &'static str {
61        "JoinProjectNotification"
62    }
63
64    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
65        render_user_notification(
66            self.user.clone(),
67            "wants to join your project",
68            None,
69            Decline,
70            vec![("Decline", Box::new(Decline)), ("Accept", Box::new(Accept))],
71            cx,
72        )
73    }
74}
75
76impl Notification for JoinProjectNotification {
77    fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
78        matches!(event, Event::Dismiss)
79    }
80}