BlueGit

ict rusty-teams

Files

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

new_meeting_dialog.rs

new_meeting_dialog.rs 10.7 KiB Download Raw
1use chatsvc::Repeat;
2use chrono::{Local, NaiveDate, NaiveDateTime, NaiveTime, Utc};
3use gpui_kit::assets::IconName;
4use gpui_kit::component::button::{Button, ButtonVariants as _};
5use gpui_kit::component::date_picker::DatePicker;
6use gpui_kit::component::date_picker::DatePickerState;
7use gpui_kit::component::dialog::{Dialog, DialogFooter};
8use gpui_kit::component::input::{Input, InputState, Textarea, TextareaState};
9use gpui_kit::component::menu::{DropdownMenu as _, PopupMenuItem};
10use gpui_kit::component::time_field::{TimeField, TimeFieldState};
11use gpui_kit::component::{WindowExt as _, h_flex, v_flex};
12use gpui_kit::prelude::FluentBuilder as _;
13use gpui_kit::*;
14
15use super::new_chat::{NewChatDraft, Pick};
16use super::widgets::icon;
17use crate::app_state::AppState;
18use crate::call::{FormValues, Invitee, default_window, draft_from_form};
19use crate::i18n::tr;
20use crate::theme;
21
22const DIALOG_WIDTH: f32 = 520.;
23const FIELD_GAP: f32 = 12.;
24const LABEL_SIZE: f32 = 12.;
25const DATE_FORMAT: &str = "%a, %b %-d, %Y";
26const DETAILS_MIN_ROWS: usize = 3;
27const DETAILS_MAX_ROWS: usize = 6;
28const TIME_FIELD_WIDTH: f32 = 96.;
29const DATE_FIELD_WIDTH: f32 = 176.;
30const REPEAT_WIDTH: f32 = 180.;
31
32struct MeetingForm {
33 state: Entity<AppState>,
34 title: Entity<InputState>,
35 people: Entity<NewChatDraft>,
36 date: Entity<DatePickerState>,
37 start: Entity<TimeFieldState>,
38 end: Entity<TimeFieldState>,
39 details: Entity<TextareaState>,
40 repeat: Repeat,
41 error: Option<String>,
42 sending: bool,
43}
44
45impl MeetingForm {
46 fn new(
47 state: Entity<AppState>,
48 when: Option<(NaiveDateTime, NaiveDateTime)>,
49 window: &mut Window,
50 cx: &mut Context<Self>,
51 ) -> Self {
52 let (start_moment, end_moment) = when.unwrap_or_else(|| default_window(Utc::now(), &Local));
53 let title = cx.new(|cx| {
54 InputState::new(window, cx).placeholder(tr("calls.meeting.title_placeholder"))
55 });
56 let people_state = state.clone();
57 let people = cx.new(|cx| NewChatDraft::new(people_state, window, cx).compact());
58 let date = cx.new(|cx| {
59 let mut picker = DatePickerState::new(window, cx).date_format(DATE_FORMAT);
60 picker.set_date(start_moment.date(), window, cx);
61 picker
62 });
63 let start = cx.new(|cx| {
64 let mut field = TimeFieldState::new(window, cx);
65 field.set_time(start_moment.time(), window, cx);
66 field
67 });
68 let end = cx.new(|cx| {
69 let mut field = TimeFieldState::new(window, cx);
70 field.set_time(end_moment.time(), window, cx);
71 field
72 });
73 let details = cx.new(|cx| {
74 TextareaState::new(window, cx)
75 .auto_grow(DETAILS_MIN_ROWS, DETAILS_MAX_ROWS)
76 .placeholder(tr("calls.meeting.details_placeholder"))
77 });
78 MeetingForm {
79 state,
80 title,
81 people,
82 date,
83 start,
84 end,
85 details,
86 repeat: Repeat::Never,
87 error: None,
88 sending: false,
89 }
90 }
91
92 fn set_repeat(&mut self, repeat: Repeat, cx: &mut Context<Self>) {
93 self.repeat = repeat;
94 cx.notify();
95 }
96
97 fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
98 if self.sending {
99 return;
100 }
101 let people = self
102 .people
103 .read(cx)
104 .chips()
105 .iter()
106 .map(|pick| Invitee {
107 user_id: pick.user_id.clone(),
108 name: pick.name.clone(),
109 mail: pick.mail.clone(),
110 })
111 .collect();
112 let values = FormValues {
113 subject: self.title.read(cx).value().to_string(),
114 date: self.date.read(cx).date().start(),
115 start: self.start.read(cx).time(),
116 end: self.end.read(cx).time(),
117 repeat: self.repeat,
118 details: self.details.read(cx).value().to_string(),
119 people,
120 };
121 let time_zone_name = self.state.read(cx).own_time_zone.clone();
122 let draft = match draft_from_form(values, &Local, time_zone_name) {
123 Ok(draft) => draft,
124 Err(message) => {
125 self.error = Some(message);
126 cx.notify();
127 return;
128 }
129 };
130 self.error = None;
131 self.sending = true;
132 let created = self
133 .state
134 .update(cx, |state, cx| state.create_meeting(draft, cx));
135 cx.notify();
136 cx.spawn_in(window, async move |this, cx| {
137 let outcome = created
138 .await
139 .unwrap_or_else(|_| Err("The request was cancelled.".to_owned()));
140 this.update_in(cx, |form, window, cx| match outcome {
141 Ok(()) => window.close_dialog(cx),
142 Err(message) => {
143 form.sending = false;
144 form.error = Some(message);
145 cx.notify();
146 }
147 })
148 .ok();
149 })
150 .detach();
151 }
152}
153
154pub(super) fn labeled(label: &str, field: impl IntoElement) -> Div {
155 v_flex()
156 .gap(px(4.))
157 .child(
158 div()
159 .text_size(px(LABEL_SIZE))
160 .text_color(theme::text_muted())
161 .child(label.to_owned()),
162 )
163 .child(field)
164}
165
166impl Render for MeetingForm {
167 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
168 let form = cx.entity();
169 let current = self.repeat;
170 let repeat_button = Button::new("meeting-repeat")
171 .outline()
172 .w(px(REPEAT_WIDTH))
173 .label(current.label())
174 .icon(IconName::ChevronDown)
175 .dropdown_menu(move |menu, _, _| {
176 Repeat::ALL.into_iter().fold(menu, |menu, repeat| {
177 let form = form.clone();
178 menu.item(
179 PopupMenuItem::new(repeat.label())
180 .checked(repeat == current)
181 .on_click(move |_, _, cx| {
182 form.update(cx, |form, cx| form.set_repeat(repeat, cx))
183 }),
184 )
185 })
186 })
187 .listbox();
188 v_flex()
189 .debug_selector(|| "new-meeting".into())
190 .gap(px(FIELD_GAP))
191 .child(labeled(
192 &tr("calls.meeting.title_label"),
193 Input::new(&self.title),
194 ))
195 .child(labeled(
196 &tr("calls.meeting.people"),
197 div()
198 .debug_selector(|| "new-meeting/people".into())
199 .rounded(px(6.))
200 .border_1()
201 .border_color(theme::border())
202 .child(self.people.clone()),
203 ))
204 .child(
205 h_flex()
206 .debug_selector(|| "new-meeting/when".into())
207 .gap(px(8.))
208 .items_end()
209 .child(div().w(px(DATE_FIELD_WIDTH)).child(labeled(
210 &tr("calls.meeting.date"),
211 DatePicker::new(&self.date).cleanable(false),
212 )))
213 .child(div().w(px(TIME_FIELD_WIDTH)).child(labeled(
214 &tr("calls.meeting.start"),
215 TimeField::new(&self.start),
216 )))
217 .child(
218 div()
219 .w(px(TIME_FIELD_WIDTH))
220 .child(labeled(&tr("calls.meeting.end"), TimeField::new(&self.end))),
221 ),
222 )
223 .child(labeled(&tr("calls.meeting.repeat"), repeat_button))
224 .child(labeled(
225 &tr("calls.meeting.details"),
226 Textarea::new(&self.details),
227 ))
228 .child(
229 h_flex()
230 .gap(px(6.))
231 .items_center()
232 .text_size(px(LABEL_SIZE))
233 .text_color(theme::text_muted())
234 .child(icon(IconName::Video, 14., theme::text_muted()))
235 .child(tr("calls.meeting.teams_link_added")),
236 )
237 .when_some(self.error.clone(), |form, error| {
238 form.child(
239 div()
240 .text_size(px(LABEL_SIZE))
241 .text_color(theme::red_soft())
242 .child(error),
243 )
244 })
245 }
246}
247
248pub fn open_new_meeting(state: Entity<AppState>, window: &mut Window, cx: &mut App) {
249 open_new_meeting_with(state, Vec::new(), window, cx);
250}
251
252/// New meeting with the date and times already set, e.g. from a block picked on the grid.
253pub fn open_new_meeting_at(
254 state: Entity<AppState>,
255 date: NaiveDate,
256 start: NaiveTime,
257 end: NaiveTime,
258 window: &mut Window,
259 cx: &mut App,
260) {
261 let when = (date.and_time(start), date.and_time(end));
262 open_meeting_form(state, Vec::new(), Some(when), window, cx);
263}
264
265pub fn open_new_meeting_with(
266 state: Entity<AppState>,
267 people: Vec<Pick>,
268 window: &mut Window,
269 cx: &mut App,
270) {
271 open_meeting_form(state, people, None, window, cx);
272}
273
274fn open_meeting_form(
275 state: Entity<AppState>,
276 people: Vec<Pick>,
277 when: Option<(NaiveDateTime, NaiveDateTime)>,
278 window: &mut Window,
279 cx: &mut App,
280) {
281 let form = cx.new(|cx| MeetingForm::new(state, when, window, cx));
282 if !people.is_empty() {
283 let draft = form.read(cx).people.clone();
284 draft.update(cx, |draft, cx| draft.set_chips(people, cx));
285 }
286 let first_field = form.read(cx).title.clone();
287 window.open_dialog(cx, move |dialog: Dialog, _, cx| {
288 let submit_form = form.clone();
289 let sending = form.read(cx).sending;
290 let footer = DialogFooter::new()
291 .child(
292 Button::new("meeting-cancel")
293 .label(tr("common.cancel"))
294 .on_click(|_, window, cx| window.close_dialog(cx)),
295 )
296 .child(
297 Button::new("meeting-send")
298 .primary()
299 .label(if sending {
300 tr("common.sending")
301 } else {
302 tr("common.send")
303 })
304 .on_click(move |_, window, cx| {
305 submit_form.update(cx, |form, cx| form.submit(window, cx));
306 }),
307 );
308 dialog
309 .title(tr("calls.meeting.new_title"))
310 .w(px(DIALOG_WIDTH))
311 .child(form.clone())
312 .footer(footer)
313 });
314 window.defer(cx, move |window, cx| {
315 first_field.update(cx, |input, cx| input.focus(window, cx));
316 });
317}