yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·7.2 MiB·4b67e80·Tahinli·1 hour ago·400 commits
| 1 | use common::model::Id; |
| 2 | use common::{ |
| 3 | call::{CreateInviteCode, DeleteInviteCode, ListInviteCodes, ListServerBans, RemoveServerBan}, |
| 4 | model::{InviteCode, ServerBan, ServerBanKind}, |
| 5 | }; |
| 6 | use gpui_kit::{ |
| 7 | AnyElement, ClipboardItem, Context, Div, Entity, FontWeight, IntoElement, ParentElement, |
| 8 | Styled, Window, assets::IconName, div, px, |
| 9 | }; |
| 10 | |
| 11 | use super::ui::{ |
| 12 | card, card_header, confirmed, delete_icon_button, fixed, icon_button, list_row, pill, toolbar, |
| 13 | }; |
| 14 | use crate::gui::style::{size, space, text}; |
| 15 | use crate::gui::{Palette, Yumush, entity_action, millis_time, notify_error, text, widget::field}; |
| 16 | use crate::lang::w; |
| 17 | |
| 18 | fn list_card(header: Div, rows: Vec<Div>, none: &str, palette: Palette) -> Div { |
| 19 | let body = if rows.is_empty() { |
| 20 | crate::gui::parts::empty(none, palette.tokens) |
| 21 | } else { |
| 22 | div().flex().flex_col().children(rows) |
| 23 | }; |
| 24 | |
| 25 | card(palette).child(header).child(body) |
| 26 | } |
| 27 | |
| 28 | fn invite_row( |
| 29 | invite: &InviteCode, |
| 30 | pending: bool, |
| 31 | entity: &Entity<Yumush>, |
| 32 | palette: Palette, |
| 33 | ) -> Div { |
| 34 | let copy_code = invite.code.clone(); |
| 35 | let delete_code = invite.code.clone(); |
| 36 | let expiry = invite.expires_at.map_or_else( |
| 37 | || w().builder_access.never.to_string(), |
| 38 | |at| { |
| 39 | w().builder_access |
| 40 | .expires(millis_time(at, crate::lang::w().common.stamp)) |
| 41 | }, |
| 42 | ); |
| 43 | let left = match invite.uses_left { |
| 44 | 1 => w().builder_access.one_use_left.to_string(), |
| 45 | uses => w().builder_access.uses_left(uses), |
| 46 | }; |
| 47 | |
| 48 | list_row() |
| 49 | .px(px(space::LG)) |
| 50 | .child( |
| 51 | div() |
| 52 | .flex_1() |
| 53 | .min_w_0() |
| 54 | .flex() |
| 55 | .flex_col() |
| 56 | .child( |
| 57 | div() |
| 58 | .font_weight(FontWeight::MEDIUM) |
| 59 | .truncate() |
| 60 | .child(invite.code.clone()), |
| 61 | ) |
| 62 | .child( |
| 63 | div() |
| 64 | .text_size(text(text::CAPTION)) |
| 65 | .text_color(palette.muted) |
| 66 | .truncate() |
| 67 | .child(format!("{left} · {expiry}")), |
| 68 | ), |
| 69 | ) |
| 70 | .child(icon_button( |
| 71 | format!("copy_invite_{}", invite.code), |
| 72 | IconName::Copy, |
| 73 | w().builder_access.copy, |
| 74 | palette.muted, |
| 75 | palette, |
| 76 | entity_action(entity, move |yumush, _, cx| { |
| 77 | yumush.copy_invite(©_code, cx) |
| 78 | }), |
| 79 | )) |
| 80 | .child(delete_icon_button( |
| 81 | format!("delete_invite_{}", invite.code), |
| 82 | pending, |
| 83 | palette, |
| 84 | entity_action(entity, move |yumush, window, cx| { |
| 85 | yumush.delete_invite(&delete_code, window, cx) |
| 86 | }), |
| 87 | )) |
| 88 | } |
| 89 | |
| 90 | fn ban_row(ban: &ServerBan, entity: &Entity<Yumush>, palette: Palette) -> Div { |
| 91 | let remove_id = ban.id; |
| 92 | let kind = match ban.kind { |
| 93 | ServerBanKind::Ip => "IP", |
| 94 | ServerBanKind::Username => w().builder_access.username, |
| 95 | }; |
| 96 | |
| 97 | list_row() |
| 98 | .px(px(space::LG)) |
| 99 | .child(pill(kind, palette.text, palette)) |
| 100 | .child( |
| 101 | div() |
| 102 | .flex_1() |
| 103 | .min_w_0() |
| 104 | .flex() |
| 105 | .flex_col() |
| 106 | .child( |
| 107 | div() |
| 108 | .font_weight(FontWeight::MEDIUM) |
| 109 | .truncate() |
| 110 | .child(ban.value.clone()), |
| 111 | ) |
| 112 | .children(ban.reason.as_ref().map(|reason| { |
| 113 | div() |
| 114 | .text_size(text(text::CAPTION)) |
| 115 | .text_color(palette.muted) |
| 116 | .min_w_0() |
| 117 | .child(super::super::glyph::line( |
| 118 | reason.clone().into(), |
| 119 | super::super::text(super::super::style::text::CAPTION), |
| 120 | )) |
| 121 | })), |
| 122 | ) |
| 123 | .child(icon_button( |
| 124 | format!("remove_ban_{}", ban.id), |
| 125 | IconName::X, |
| 126 | w().builder_access.remove, |
| 127 | palette.muted, |
| 128 | palette, |
| 129 | entity_action(entity, move |yumush, window, cx| { |
| 130 | yumush.remove_ban(remove_id, window, cx) |
| 131 | }), |
| 132 | )) |
| 133 | } |
| 134 | |
| 135 | impl Yumush { |
| 136 | pub(super) fn load_access(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 137 | self.load_invites(window, cx); |
| 138 | self.load_server_bans(window, cx); |
| 139 | } |
| 140 | |
| 141 | pub(super) fn load_invites(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 142 | self.fetch_builder( |
| 143 | window, |
| 144 | cx, |
| 145 | |network| async move { network.call(ListInviteCodes).await }, |
| 146 | |yumush, codes, cx| { |
| 147 | yumush.chat.builder_invites = codes; |
| 148 | cx.notify(); |
| 149 | }, |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | pub(super) fn load_server_bans(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 154 | self.fetch_builder( |
| 155 | window, |
| 156 | cx, |
| 157 | |network| async move { network.call(ListServerBans).await }, |
| 158 | |yumush, bans, cx| { |
| 159 | yumush.chat.builder_bans = bans; |
| 160 | cx.notify(); |
| 161 | }, |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | pub(super) fn create_invite(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 166 | let uses = self.chat.invite_uses.read(cx).text().to_string(); |
| 167 | let Ok(uses) = uses.trim().parse::<u32>() else { |
| 168 | return; |
| 169 | }; |
| 170 | let hours = self.chat.invite_hours.read(cx).text().to_string(); |
| 171 | let hours = match hours.trim() { |
| 172 | "" => None, |
| 173 | text => match text.parse::<u32>() { |
| 174 | Ok(hours) => Some(hours), |
| 175 | Err(_) => return, |
| 176 | }, |
| 177 | }; |
| 178 | let network = self.network.clone(); |
| 179 | |
| 180 | cx.spawn_in(window, async move |this, cx| { |
| 181 | match network.call(CreateInviteCode { uses, hours }).await { |
| 182 | Ok(_) => { |
| 183 | let _ = |
| 184 | this.update_in(cx, |yumush, window, cx| yumush.load_invites(window, cx)); |
| 185 | } |
| 186 | Err(error) => notify_error(&this, cx, error), |
| 187 | } |
| 188 | }) |
| 189 | .detach(); |
| 190 | } |
| 191 | |
| 192 | pub(super) fn delete_invite( |
| 193 | &mut self, |
| 194 | code: &str, |
| 195 | window: &mut Window, |
| 196 | cx: &mut Context<Self>, |
| 197 | ) { |
| 198 | if !confirmed(&mut self.chat.pending_invite_delete, code.to_string()) { |
| 199 | cx.notify(); |
| 200 | return; |
| 201 | } |
| 202 | let network = self.network.clone(); |
| 203 | let code = code.to_string(); |
| 204 | |
| 205 | cx.spawn_in(window, async move |this, cx| { |
| 206 | match network.call(DeleteInviteCode { code }).await { |
| 207 | Ok(()) => { |
| 208 | let _ = |
| 209 | this.update_in(cx, |yumush, window, cx| yumush.load_invites(window, cx)); |
| 210 | } |
| 211 | Err(error) => notify_error(&this, cx, error), |
| 212 | } |
| 213 | }) |
| 214 | .detach(); |
| 215 | } |
| 216 | |
| 217 | pub(super) fn copy_invite(&self, code: &str, cx: &mut Context<Self>) { |
| 218 | cx.write_to_clipboard(ClipboardItem::new_string(code.to_string())); |
| 219 | } |
| 220 | |
| 221 | pub(super) fn remove_ban(&mut self, id: Id, window: &mut Window, cx: &mut Context<Self>) { |
| 222 | let network = self.network.clone(); |
| 223 | |
| 224 | cx.spawn_in(window, async move |this, cx| { |
| 225 | match network.call(RemoveServerBan { id }).await { |
| 226 | Ok(()) => { |
| 227 | let _ = this |
| 228 | .update_in(cx, |yumush, window, cx| yumush.load_server_bans(window, cx)); |
| 229 | } |
| 230 | Err(error) => notify_error(&this, cx, error), |
| 231 | } |
| 232 | }) |
| 233 | .detach(); |
| 234 | } |
| 235 | |
| 236 | pub(super) fn access_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { |
| 237 | let controls = toolbar(vec![ |
| 238 | fixed(80., field(palette, &self.chat.invite_uses)), |
| 239 | fixed(size::THUMB, field(palette, &self.chat.invite_hours)), |
| 240 | crate::gui::parts::button( |
| 241 | "create_invite", |
| 242 | w().builder_access.create, |
| 243 | crate::gui::parts::Press::Plain, |
| 244 | palette.tokens, |
| 245 | entity_action(entity, |yumush, window, cx| { |
| 246 | yumush.create_invite(window, cx) |
| 247 | }), |
| 248 | ) |
| 249 | .into_any_element(), |
| 250 | ]) |
| 251 | .into_any_element(); |
| 252 | let invite_header = card_header(w().builder_access.invite_codes, vec![controls], palette); |
| 253 | let invites = self.chat.builder_invites.iter().map(|invite| { |
| 254 | let pending = self.chat.pending_invite_delete.as_deref() == Some(invite.code.as_str()); |
| 255 | invite_row(invite, pending, entity, palette) |
| 256 | }); |
| 257 | let bans = self |
| 258 | .chat |
| 259 | .builder_bans |
| 260 | .iter() |
| 261 | .map(|ban| ban_row(ban, entity, palette)); |
| 262 | let ban_header = card_header(w().builder_access.bans, Vec::new(), palette); |
| 263 | |
| 264 | vec![ |
| 265 | list_card( |
| 266 | invite_header, |
| 267 | invites.collect(), |
| 268 | w().builder_access.no_invite_codes, |
| 269 | palette, |
| 270 | ) |
| 271 | .into_any_element(), |
| 272 | list_card(ban_header, bans.collect(), w().common.no_bans, palette).into_any_element(), |
| 273 | ] |
| 274 | } |
| 275 | } |