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