BlueGit

ict rusty-teams

Files

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

create_channel_dialog.rs

fc95684c History
create_channel_dialog.rs 6.3 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::radio::Radio;
5use gpui_kit::component::{Disableable as _, WindowExt as _, v_flex};
6use gpui_kit::prelude::FluentBuilder as _;
7use gpui_kit::*;
8use teams_core::ChannelPrivacy;
9
10use super::new_meeting_dialog::labeled;
11use crate::app_state::AppState;
12use crate::team_admin::{can_create_channel, channel_name_problem};
13use crate::theme;
14
15const DIALOG_WIDTH: f32 = 480.;
16const FIELD_GAP: f32 = 12.;
17const OPTION_GAP: f32 = 8.;
18const ERROR_SIZE: f32 = 12.;
19
20struct CreateChannelForm {
21 state: Entity<AppState>,
22 team_id: String,
23 name: Entity<InputState>,
24 description: Entity<InputState>,
25 privacy: ChannelPrivacy,
26 private_allowed: bool,
27 _subscriptions: Vec<Subscription>,
28}
29
30impl CreateChannelForm {
31 fn new(
32 state: Entity<AppState>,
33 team_id: String,
34 private_allowed: bool,
35 window: &mut Window,
36 cx: &mut Context<Self>,
37 ) -> Self {
38 let name = cx.new(|cx| {
39 InputState::new(window, cx).placeholder("Letters, numbers, and spaces are allowed")
40 });
41 let description = cx.new(|cx| {
42 InputState::new(window, cx)
43 .placeholder("Help others find the right channel by providing a description")
44 });
45 let subscriptions = vec![
46 cx.subscribe_in(&name, window, Self::on_input),
47 cx.subscribe_in(&description, window, Self::on_input),
48 ];
49 CreateChannelForm {
50 state,
51 team_id,
52 name,
53 description,
54 privacy: ChannelPrivacy::Standard,
55 private_allowed,
56 _subscriptions: subscriptions,
57 }
58 }
59
60 fn on_input(
61 &mut self,
62 _: &Entity<InputState>,
63 event: &InputEvent,
64 window: &mut Window,
65 cx: &mut Context<Self>,
66 ) {
67 if matches!(event, InputEvent::PressEnter { .. }) && self.submit(cx) {
68 window.close_dialog(cx);
69 }
70 cx.notify();
71 }
72
73 fn can_create(&self, cx: &App) -> bool {
74 can_create_channel(&self.name.read(cx).value())
75 }
76
77 fn submit(&mut self, cx: &mut Context<Self>) -> bool {
78 if !self.can_create(cx) {
79 return false;
80 }
81 let name = self.name.read(cx).value().to_string();
82 let description = self.description.read(cx).value().to_string();
83 let (team_id, privacy) = (self.team_id.clone(), self.privacy);
84 self.state.update(cx, |state, cx| {
85 state.create_channel(&team_id, &name, &description, privacy, cx)
86 });
87 true
88 }
89
90 fn choose(&mut self, privacy: ChannelPrivacy, cx: &mut Context<Self>) {
91 self.privacy = privacy;
92 cx.notify();
93 }
94
95 fn privacy_options(&self, cx: &mut Context<Self>) -> Div {
96 let form = cx.entity();
97 let option = |id: &'static str, label: &'static str, privacy: ChannelPrivacy| {
98 let form = form.clone();
99 Radio::new(id)
100 .label(label)
101 .checked(self.privacy == privacy)
102 .on_click(move |_, _, cx| form.update(cx, |form, cx| form.choose(privacy, cx)))
103 };
104 v_flex()
105 .debug_selector(|| "create-channel/type".into())
106 .gap(px(OPTION_GAP))
107 .child(option(
108 "create-channel-standard",
109 "Standard: Everyone on the team has access",
110 ChannelPrivacy::Standard,
111 ))
112 .when(self.private_allowed, |options| {
113 options.child(option(
114 "create-channel-private",
115 "Private: Specific teammates have access",
116 ChannelPrivacy::Private,
117 ))
118 })
119 }
120}
121
122impl Render for CreateChannelForm {
123 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
124 let problem = channel_name_problem(&self.name.read(cx).value());
125 v_flex()
126 .debug_selector(|| "create-channel".into())
127 .gap(px(FIELD_GAP))
128 .child(labeled("Channel name", Input::new(&self.name)))
129 .when_some(problem, |form, problem| {
130 form.child(
131 div()
132 .text_size(px(ERROR_SIZE))
133 .text_color(theme::red_soft())
134 .child(problem),
135 )
136 })
137 .child(labeled(
138 "Description (optional)",
139 Input::new(&self.description),
140 ))
141 .child(labeled("Choose a channel type", self.privacy_options(cx)))
142 }
143}
144
145pub fn open_create_channel_dialog(
146 state: Entity<AppState>,
147 team_id: &str,
148 window: &mut Window,
149 cx: &mut App,
150) {
151 let (team_name, rights) = {
152 let app = state.read(cx);
153 (
154 app.team_name(team_id).unwrap_or_default(),
155 app.team_rights(team_id),
156 )
157 };
158 let title = format!("Create a channel for \"{team_name}\" team");
159 let team_id = team_id.to_owned();
160 let form =
161 cx.new(|cx| CreateChannelForm::new(state, team_id, rights.add_private_channel, window, cx));
162 let field = form.read(cx).name.clone();
163 window.open_dialog(cx, move |dialog: Dialog, _, cx| {
164 let submit_form = form.clone();
165 let can_create = form.read(cx).can_create(cx);
166 let footer = DialogFooter::new()
167 .child(
168 Button::new("create-channel-cancel")
169 .label("Cancel")
170 .on_click(|_, window, cx| window.close_dialog(cx)),
171 )
172 .child(
173 Button::new("create-channel-confirm")
174 .primary()
175 .label("Create")
176 .disabled(!can_create)
177 .on_click(move |_, window, cx| {
178 if submit_form.update(cx, |form, cx| form.submit(cx)) {
179 window.close_dialog(cx);
180 }
181 }),
182 );
183 dialog
184 .title(title.clone())
185 .w(px(DIALOG_WIDTH))
186 .child(form.clone())
187 .footer(footer)
188 });
189 window.defer(cx, move |window, cx| {
190 field.update(cx, |input, cx| input.focus(window, cx));
191 });
192}