BlueGit

ict rusty-teams

Files

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

channel_admin_dialogs.rs

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