cosmic_applet_avestruz/views/
relays.rs1use cosmic::Element;
9use cosmic::iced::widget::text::Wrapping;
10use cosmic::iced::{Alignment, Length};
11use cosmic::widget::{self, button, container, text, text_input};
12
13use nostr_sdk::prelude::RelayUrl;
14
15use crate::fl;
16use crate::handlers::auth::sign_error_text;
17use crate::helpers::readable_size;
18use crate::messages::Message;
19use crate::nostr::relay_lists::{Hindrance, Kept, NIP_AUTH, NIP_SEARCH, Section};
20use crate::state::AppModel;
21use crate::views::components::note_row::relay_name;
22
23fn section_texts(section: Section) -> (String, String) {
25 match section {
26 Section::Outbox => (fl!("relays-outbox"), fl!("relays-outbox-description")),
27 Section::Inbox => (fl!("relays-inbox"), fl!("relays-inbox-description")),
28 Section::Messages => (fl!("relays-messages"), fl!("relays-messages-description")),
29 Section::Search => (fl!("relays-search"), fl!("relays-search-description")),
30 Section::Indexer => (fl!("relays-indexer"), fl!("relays-indexer-description")),
31 Section::Trusted => (fl!("relays-trusted"), fl!("relays-trusted-description")),
32 Section::Blocked => (fl!("relays-blocked"), fl!("relays-blocked-description")),
33 }
34}
35
36pub fn section_title(section: Section) -> String {
38 section_texts(section).0
39}
40
41fn hindrance_text(hindrance: Hindrance) -> String {
44 match hindrance {
45 Hindrance::Lacks(NIP_SEARCH) => fl!("relays-lacks-search"),
46 Hindrance::Lacks(NIP_AUTH) => fl!("relays-lacks-auth"),
47 Hindrance::Lacks(nip) => fl!("relays-lacks-nip", nip = nip),
48 Hindrance::RestrictedWrites => fl!("relays-restricted-writes"),
49 Hindrance::PaymentRequired => fl!("relays-payment-required"),
50 Hindrance::AuthToWrite => fl!("relays-auth-to-write"),
51 Hindrance::AuthToRead => fl!("relays-auth-to-read"),
52 }
53}
54
55pub(crate) fn small<'a>(words: String) -> Element<'a, Message> {
57 text(words).size(11).wrapping(Wrapping::WordOrGlyph).width(Length::Fill).into()
58}
59
60impl AppModel {
61 fn view_relays_header(&self) -> Element<'_, Message> {
63 let back = button::icon(widget::icon::from_name("go-previous-symbolic"))
64 .tooltip(fl!("relays-back"))
65 .on_press(Message::CloseRelays);
66 let label = if self.relays.saving { fl!("relays-saving") } else { fl!("relays-save") };
67 let save = button::suggested(label).on_press_maybe(self.relays_can_save().then_some(Message::SaveRelays));
68 widget::Row::new()
69 .push(back)
70 .push(text(fl!("relays-title")).size(16).font(cosmic::font::bold()).width(Length::Fill))
71 .push(save)
72 .spacing(8)
73 .align_y(Alignment::Center)
74 .padding([4, 12])
75 .into()
76 }
77
78 fn view_relay_use(&self, relay: &RelayUrl) -> Option<Element<'_, Message>> {
81 let doing = self.relays.in_use.get(relay)?;
82 let mut said = vec![if doing.connected { fl!("relays-use-connected") } else { fl!("relays-use-disconnected") }];
83 if doing.received > 0 || doing.sent > 0 {
84 said.push(fl!("relays-use-traffic", down = readable_size(doing.received), up = readable_size(doing.sent)));
85 }
86 if doing.attempts > 1 {
87 said.push(fl!("relays-use-attempts", worked = doing.succeeded, all = doing.attempts));
88 }
89 if let Some(latency) = doing.latency {
90 said.push(fl!("relays-use-latency", ms = latency.as_millis()));
91 }
92 Some(text(said.join(" · ")).size(11).wrapping(Wrapping::None).into())
93 }
94
95 fn view_relay_hindrances(&self, section: Section, relay: &RelayUrl) -> Vec<Element<'_, Message>> {
99 let Some(Some(info)) = self.relays.said.get(relay) else {
100 return Vec::new();
101 };
102 let line = |hindrance| {
103 text(hindrance_text(hindrance)).size(11).class(cosmic::theme::Text::Accent).wrapping(Wrapping::WordOrGlyph).into()
104 };
105 section.hindrances(info).into_iter().map(line).collect()
106 }
107
108 fn view_relay_section(&self, section: Section) -> Element<'_, Message> {
111 let (title, description) = section_texts(section);
112 let can_change = self.relays_can_change(section);
113 let mut shown =
114 widget::Column::new().push(text(title).size(14).font(cosmic::font::bold())).push(small(description)).spacing(6);
115
116 let relays = self.relays.edited.of(section);
117 if relays.is_empty() && self.relays.lists.is_some() {
118 shown = shown.push(small(fl!("relays-none")));
119 }
120 let last = relays.len().saturating_sub(1);
121 for (place, relay) in relays.iter().enumerate() {
122 let moving =
123 |up: bool, at_end: bool| (can_change && !at_end).then(|| Message::ShiftRelay(section, relay.clone(), up));
124 let up = button::icon(widget::icon::from_name("go-up-symbolic"))
125 .tooltip(fl!("relays-up"))
126 .on_press_maybe(moving(true, place == 0));
127 let down = button::icon(widget::icon::from_name("go-down-symbolic"))
128 .tooltip(fl!("relays-down"))
129 .on_press_maybe(moving(false, place == last));
130 let remove = button::icon(widget::icon::from_name("edit-delete-symbolic"))
131 .tooltip(fl!("relays-remove"))
132 .on_press_maybe(can_change.then(|| Message::RemoveRelay(section, relay.clone())));
133 let name = container(text(relay_name(relay)).size(13).wrapping(Wrapping::None)).clip(true);
135 let named = widget::Column::new()
136 .push(name)
137 .push_maybe(self.view_relay_use(relay))
138 .extend(self.view_relay_hindrances(section, relay))
139 .spacing(2)
140 .width(Length::Fill);
141 shown =
142 shown.push(widget::Row::new().push(named).push(up).push(down).push(remove).spacing(4).align_y(Alignment::Center));
143 }
144
145 let kept = self.relays.lists.as_ref().map(|lists| lists.of(section).kept().clone());
147 match kept.filter(|_| Section::LISTS.contains(§ion)) {
148 Some(Kept::Unreadable(why)) => {
149 shown = shown.push(small(fl!("relays-list-unreadable", error = sign_error_text(why))));
150 }
151 Some(Kept::Unread) => shown = shown.push(small(fl!("relays-list-unread"))),
152 Some(Kept::Read(_)) | None => {}
153 }
154 if can_change {
155 let typed = text_input(fl!("relays-add-placeholder"), self.relays.typed(section))
156 .on_input(move |typed| Message::RelayTyped(section, typed))
157 .on_submit(move |_| Message::AddRelay(section))
158 .width(Length::Fill);
159 let add = button::standard(fl!("relays-add"))
160 .on_press_maybe((!self.relays.typed(section).trim().is_empty()).then_some(Message::AddRelay(section)));
161 shown = shown.push(widget::Row::new().push(typed).push(add).spacing(8).align_y(Alignment::Center));
162 }
163 shown.into()
164 }
165
166 pub fn view_relays(&self) -> Element<'_, Message> {
168 let mut body = widget::Column::new().spacing(16);
169 if !self.can_publish() {
170 body = body.push(small(fl!("relays-read-only")));
171 } else if !self.relays.from_relays {
172 body = body.push(small(fl!("relays-reading")));
173 }
174 for section in Section::ALL {
175 body = body.push(self.view_relay_section(section));
176 }
177
178 widget::Column::new()
179 .push(self.view_relays_header())
180 .push(widget::divider::horizontal::default())
181 .push(widget::scrollable(container(body).padding([8, 12]).width(Length::Fill)))
182 .into()
183 }
184}