BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / channel_admin_dialogs.rs

channel_admin_dialogs.rs

channel_admin_dialogs.rs 7.4 KiB Download Raw
1use gpui_kit::component::button::{Button, ButtonVariants as _};
2use gpui_kit::component::dialog::{Dialog, DialogFooter};
3use gpui_kit::component::input::{Input, InputEvent, InputState};
4use gpui_kit::component::{Disableable as _, WindowExt as _, v_flex};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::*;
7
8use super::new_meeting_dialog::labeled;
9use crate::app_state::AppState;
10use crate::team_admin::{can_create_channel, channel_name_problem};
11use crate::theme;
12
13const DIALOG_WIDTH: f32 = 480.;
14const FIELD_GAP: f32 = 12.;
15const ERROR_SIZE: f32 = 12.;
16
17struct EditChannelForm {
18 state: Entity<AppState>,
19 channel_id: String,
20 name: Entity<InputState>,
21 description: Entity<InputState>,
22 description_loaded: bool,
23 _subscriptions: Vec<Subscription>,
24}
25
26impl EditChannelForm {
27 fn new(
28 state: Entity<AppState>,
29 channel_id: String,
30 current_name: &str,
31 window: &mut Window,
32 cx: &mut Context<Self>,
33 ) -> Self {
34 let name = cx.new(|cx| {
35 InputState::new(window, cx)
36 .placeholder("Letters, numbers, and spaces are allowed")
37 .default_value(current_name.to_owned())
38 });
39 let description = cx.new(|cx| {
40 InputState::new(window, cx)
41 .placeholder("Help others find the right channel by providing a description")
42 });
43 let subscriptions = vec![
44 cx.subscribe_in(&name, window, Self::on_input),
45 cx.subscribe_in(&description, window, Self::on_input),
46 ];
47 let loading = state.update(cx, |app, cx| app.load_channel_description(&channel_id, cx));
48 cx.spawn_in(window, async move |form, cx| {
49 let loaded = loading.await;
50 form.update_in(cx, |form, window, cx| {
51 form.fill_description(loaded, window, cx)
52 })
53 .ok();
54 })
55 .detach();
56 EditChannelForm {
57 state,
58 channel_id,
59 name,
60 description,
61 description_loaded: false,
62 _subscriptions: subscriptions,
63 }
64 }
65
66 fn fill_description(
67 &mut self,
68 loaded: Option<String>,
69 window: &mut Window,
70 cx: &mut Context<Self>,
71 ) {
72 let Some(text) = loaded else {
73 return;
74 };
75 self.description_loaded = true;
76 if self.description.read(cx).value().is_empty() {
77 self.description
78 .update(cx, |input, cx| input.set_value(text, window, cx));
79 }
80 cx.notify();
81 }
82
83 fn on_input(
84 &mut self,
85 _: &Entity<InputState>,
86 event: &InputEvent,
87 window: &mut Window,
88 cx: &mut Context<Self>,
89 ) {
90 if matches!(event, InputEvent::PressEnter { .. }) && self.submit(cx) {
91 window.close_dialog(cx);
92 }
93 cx.notify();
94 }
95
96 fn can_save(&self, cx: &App) -> bool {
97 can_create_channel(&self.name.read(cx).value())
98 }
99
100 fn submit(&mut self, cx: &mut Context<Self>) -> bool {
101 if !self.can_save(cx) {
102 return false;
103 }
104 let name = self.name.read(cx).value().to_string();
105 let typed = self.description.read(cx).value().to_string();
106 let description = (self.description_loaded || !typed.is_empty()).then_some(typed);
107 let channel_id = self.channel_id.clone();
108 self.state.update(cx, |state, cx| {
109 state.edit_channel(&channel_id, &name, description.as_deref(), cx)
110 });
111 true
112 }
113}
114
115impl Render for EditChannelForm {
116 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
117 let problem = channel_name_problem(&self.name.read(cx).value());
118 v_flex()
119 .debug_selector(|| "edit-channel".into())
120 .gap(px(FIELD_GAP))
121 .child(labeled("Channel name", Input::new(&self.name)))
122 .when_some(problem, |form, problem| {
123 form.child(
124 div()
125 .text_size(px(ERROR_SIZE))
126 .text_color(theme::red_soft())
127 .child(problem),
128 )
129 })
130 .child(labeled("Description", Input::new(&self.description)))
131 }
132}
133
134pub fn open_edit_channel_dialog(
135 state: Entity<AppState>,
136 channel_id: &str,
137 window: &mut Window,
138 cx: &mut App,
139) {
140 let (channel_name, team_name) = {
141 let app = state.read(cx);
142 let channel_name = app.channel_name(channel_id).unwrap_or_default();
143 let team_name = app
144 .channel_team_id(channel_id)
145 .and_then(|team_id| app.team_name(&team_id))
146 .unwrap_or_default();
147 (channel_name, team_name)
148 };
149 let title = format!("Edit \"{channel_name}\" channel in \"{team_name}\" team");
150 let channel_id = channel_id.to_owned();
151 let form = cx.new(|cx| EditChannelForm::new(state, channel_id, &channel_name, window, cx));
152 let field = form.read(cx).name.clone();
153 window.open_dialog(cx, move |dialog: Dialog, _, cx| {
154 let submit_form = form.clone();
155 let can_save = form.read(cx).can_save(cx);
156 let footer = DialogFooter::new()
157 .child(
158 Button::new("edit-channel-cancel")
159 .label("Cancel")
160 .on_click(|_, window, cx| window.close_dialog(cx)),
161 )
162 .child(
163 Button::new("edit-channel-save")
164 .primary()
165 .label("Save")
166 .disabled(!can_save)
167 .on_click(move |_, window, cx| {
168 if submit_form.update(cx, |form, cx| form.submit(cx)) {
169 window.close_dialog(cx);
170 }
171 }),
172 );
173 dialog
174 .title(title.clone())
175 .w(px(DIALOG_WIDTH))
176 .child(form.clone())
177 .footer(footer)
178 });
179 window.defer(cx, move |window, cx| {
180 field.update(cx, |input, cx| input.focus(window, cx));
181 });
182}
183
184pub fn confirm_delete_channel(
185 state: Entity<AppState>,
186 channel_id: &str,
187 window: &mut Window,
188 cx: &mut App,
189) {
190 let (channel_name, private) = {
191 let app = state.read(cx);
192 (
193 app.channel_name(channel_id).unwrap_or_default(),
194 app.channel_is_private(channel_id),
195 )
196 };
197 let description = if private {
198 format!(
199 "Are you sure you want to delete the {channel_name} channel? All files and conversations will be deleted."
200 )
201 } else {
202 format!(
203 "Are you sure you want to delete the {channel_name} channel? All conversations will be deleted."
204 )
205 };
206 let channel_id = channel_id.to_owned();
207 window.open_alert_dialog(cx, move |alert, _, _| {
208 let (state, channel_id) = (state.clone(), channel_id.clone());
209 let cancel = Button::new("delete-channel-cancel")
210 .label("Cancel")
211 .on_click(|_, window, cx| window.close_dialog(cx));
212 let delete = Button::new("delete-channel-confirm")
213 .label("Delete")
214 .danger()
215 .on_click(move |_, window, cx| {
216 state.update(cx, |state, cx| state.delete_channel(&channel_id, cx));
217 window.close_dialog(cx);
218 });
219 alert
220 .title("Delete channel")
221 .description(description.clone())
222 .footer(DialogFooter::new().child(cancel).child(delete))
223 .on_ok(|_, _, _| false)
224 });
225}