update relay list panel
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 2m0s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 2m23s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
Rust / build (macos-latest, stable) (pull_request) Has been cancelled
Rust / build (windows-latest, stable) (pull_request) Has been cancelled

This commit is contained in:
2026-02-24 09:08:31 +07:00
parent ebf0e86828
commit a7f9a7ceeb

View File

@@ -4,23 +4,36 @@ use std::time::Duration;
use anyhow::{anyhow, Context as AnyhowContext, Error}; use anyhow::{anyhow, Context as AnyhowContext, Error};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, relative, uniform_list, AnyElement, App, AppContext, Context, Entity, EventEmitter, div, px, rems, Action, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle,
FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render, SharedString, Focusable, InteractiveElement, IntoElement, ParentElement, Render, SharedString, Styled,
Styled, Subscription, Task, TextAlign, UniformList, Window, Subscription, Task, TextAlign, Window,
}; };
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use serde::Deserialize;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::{NostrRegistry, BOOTSTRAP_RELAYS}; use state::NostrRegistry;
use theme::ActiveTheme; use theme::ActiveTheme;
use ui::button::{Button, ButtonVariants}; use ui::button::{Button, ButtonVariants};
use ui::dock_area::panel::{Panel, PanelEvent}; use ui::dock_area::panel::{Panel, PanelEvent};
use ui::input::{InputEvent, InputState, TextInput}; use ui::input::{InputEvent, InputState, TextInput};
use ui::{divider, h_flex, v_flex, IconName, Sizable, StyledExt}; use ui::menu::DropdownMenu;
use ui::{divider, h_flex, v_flex, Disableable, IconName, Sizable, StyledExt, WindowExtension};
const MSG: &str = "Relay List (or Gossip Relays) are a set of relays \
where you will publish all your events. Others also publish events \
related to you here.";
pub fn init(window: &mut Window, cx: &mut App) -> Entity<RelayListPanel> { pub fn init(window: &mut Window, cx: &mut App) -> Entity<RelayListPanel> {
cx.new(|cx| RelayListPanel::new(window, cx)) cx.new(|cx| RelayListPanel::new(window, cx))
} }
#[derive(Action, Clone, PartialEq, Eq, Deserialize)]
#[action(namespace = relay, no_json)]
enum SetMetadata {
Read,
Write,
}
#[derive(Debug)] #[derive(Debug)]
pub struct RelayListPanel { pub struct RelayListPanel {
name: SharedString, name: SharedString,
@@ -29,6 +42,9 @@ pub struct RelayListPanel {
/// Relay URL input /// Relay URL input
input: Entity<InputState>, input: Entity<InputState>,
/// Whether the panel is updating
updating: bool,
/// Relay metadata input /// Relay metadata input
metadata: Entity<Option<RelayMetadata>>, metadata: Entity<Option<RelayMetadata>>,
@@ -42,7 +58,7 @@ pub struct RelayListPanel {
_subscriptions: SmallVec<[Subscription; 1]>, _subscriptions: SmallVec<[Subscription; 1]>,
// Background tasks // Background tasks
_tasks: SmallVec<[Task<()>; 1]>, tasks: Vec<Task<Result<(), Error>>>,
} }
impl RelayListPanel { impl RelayListPanel {
@@ -50,28 +66,7 @@ impl RelayListPanel {
let input = cx.new(|cx| InputState::new(window, cx).placeholder("wss://example.com")); let input = cx.new(|cx| InputState::new(window, cx).placeholder("wss://example.com"));
let metadata = cx.new(|_| None); let metadata = cx.new(|_| None);
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
let mut tasks = smallvec![];
tasks.push(
// Load user's relays in the local database
cx.spawn_in(window, async move |this, cx| {
let result = cx
.background_spawn(async move { Self::load(&client).await })
.await;
if let Ok(relays) = result {
this.update(cx, |this, cx| {
this.relays.extend(relays);
cx.notify();
})
.ok();
}
}),
);
subscriptions.push( subscriptions.push(
// Subscribe to user's input events // Subscribe to user's input events
@@ -82,19 +77,31 @@ impl RelayListPanel {
}), }),
); );
// Run at the end of current cycle
cx.defer_in(window, |this, window, cx| {
this.load(window, cx);
});
Self { Self {
name: "Update Relay List".into(), name: "Update Relay List".into(),
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
input, input,
updating: false,
metadata, metadata,
relays: HashSet::new(), relays: HashSet::new(),
error: None, error: None,
_subscriptions: subscriptions, _subscriptions: subscriptions,
_tasks: tasks, tasks: vec![],
} }
} }
async fn load(client: &Client) -> Result<Vec<(RelayUrl, Option<RelayMetadata>)>, Error> { #[allow(clippy::type_complexity)]
fn load(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let task: Task<Result<Vec<(RelayUrl, Option<RelayMetadata>)>, Error>> = cx
.background_spawn(async move {
let signer = client.signer().context("Signer not found")?; let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?; let public_key = signer.get_public_key().await?;
@@ -108,6 +115,19 @@ impl RelayListPanel {
} else { } else {
Err(anyhow!("Not found.")) Err(anyhow!("Not found."))
} }
});
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
let relays = task.await?;
// Update state
this.update(cx, |this, cx| {
this.relays.extend(relays);
cx.notify();
})?;
Ok(())
}));
} }
fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@@ -120,7 +140,7 @@ impl RelayListPanel {
} }
if let Ok(url) = RelayUrl::parse(&value) { if let Ok(url) = RelayUrl::parse(&value) {
if !self.relays.insert((url, metadata.to_owned())) { if self.relays.insert((url, metadata.to_owned())) {
self.input.update(cx, |this, cx| { self.input.update(cx, |this, cx| {
this.set_value("", window, cx); this.set_value("", window, cx);
}); });
@@ -155,7 +175,29 @@ impl RelayListPanel {
.detach(); .detach();
} }
pub fn set_relays(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn set_updating(&mut self, updating: bool, cx: &mut Context<Self>) {
self.updating = updating;
cx.notify();
}
fn set_metadata(&mut self, ev: &SetMetadata, _window: &mut Window, cx: &mut Context<Self>) {
match ev {
SetMetadata::Read => {
self.metadata.update(cx, |this, cx| {
*this = Some(RelayMetadata::Read);
cx.notify();
});
}
SetMetadata::Write => {
self.metadata.update(cx, |this, cx| {
*this = Some(RelayMetadata::Write);
cx.notify();
});
}
}
}
fn set_relays(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.relays.is_empty() { if self.relays.is_empty() {
self.set_error("You need to add at least 1 relay", window, cx); self.set_error("You need to add at least 1 relay", window, cx);
return; return;
@@ -163,79 +205,81 @@ impl RelayListPanel {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
// Get all relays
let relays = self.relays.clone(); let relays = self.relays.clone();
// Set updating state
self.set_updating(true, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let builder = EventBuilder::relay_list(relays); let builder = EventBuilder::relay_list(relays);
let event = client.sign_event_builder(builder).await?; let event = client.sign_event_builder(builder).await?;
// Set relay list for current user // Set relay list for current user
client.send_event(&event).to(BOOTSTRAP_RELAYS).await?; client.send_event(&event).await?;
Ok(()) Ok(())
}); });
cx.spawn_in(window, async move |this, cx| { self.tasks.push(cx.spawn_in(window, async move |this, cx| {
match task.await { match task.await {
Ok(_) => { Ok(_) => {
// TODO this.update_in(cx, |this, window, cx| {
this.set_updating(false, cx);
this.load(window, cx);
window.push_notification("Update successful", cx);
})?;
} }
Err(e) => { Err(e) => {
this.update_in(cx, |this, window, cx| { this.update_in(cx, |this, window, cx| {
this.set_error(e.to_string(), window, cx); this.set_error(e.to_string(), window, cx);
}) })?;
.ok();
} }
}; };
})
.detach(); Ok(())
}));
} }
fn render_list(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> UniformList { fn render_list_items(&mut self, cx: &mut Context<Self>) -> Vec<impl IntoElement> {
let relays = self.relays.clone();
let total = relays.len();
uniform_list(
"relays",
total,
cx.processor(move |_v, range, _window, cx| {
let mut items = Vec::new(); let mut items = Vec::new();
for ix in range { for (url, metadata) in self.relays.iter() {
let Some((url, metadata)) = relays.iter().nth(ix) else {
continue;
};
items.push( items.push(
div() h_flex()
.id(SharedString::from(url.to_string())) .id(SharedString::from(url.to_string()))
.group("") .group("")
.flex_1()
.w_full() .w_full()
.h_9() .h_8()
.py_0p5()
.child(
h_flex()
.px_2() .px_2()
.flex()
.justify_between() .justify_between()
.rounded(cx.theme().radius) .rounded(cx.theme().radius)
.bg(cx.theme().elevated_surface_background) .bg(cx.theme().secondary_background)
.child( .text_color(cx.theme().secondary_foreground)
div().text_sm().child(SharedString::from(url.to_string())),
)
.child( .child(
h_flex() h_flex()
.gap_1() .gap_1()
.text_xs() .text_sm()
.child(SharedString::from(url.to_string()))
.child(
div()
.p_0p5()
.rounded_xs()
.font_semibold()
.text_size(px(8.))
.text_color(cx.theme().secondary_foreground)
.map(|this| { .map(|this| {
if let Some(metadata) = metadata { if let Some(metadata) = metadata {
this.child(SharedString::from( this.child(SharedString::from(metadata.to_string()))
metadata.to_string(),
))
} else { } else {
this.child(SharedString::from("Read+Write")) this.child("Read and Write")
} }
}) }),
),
)
.child( .child(
Button::new("remove_{ix}") Button::new("remove_{ix}")
.icon(IconName::Close) .icon(IconName::Close)
@@ -245,27 +289,19 @@ impl RelayListPanel {
.group_hover("", |this| this.visible()) .group_hover("", |this| this.visible())
.on_click({ .on_click({
let url = url.to_owned(); let url = url.to_owned();
cx.listener( cx.listener(move |this, _ev, _window, cx| {
move |this, _ev, _window, cx| {
this.remove(&url, cx); this.remove(&url, cx);
}, })
)
}), }),
), ),
),
),
) )
} }
items items
}),
)
.h_full()
} }
fn render_empty(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render_empty(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
h_flex() h_flex()
.mt_2()
.h_20() .h_20()
.justify_center() .justify_center()
.border_2() .border_2()
@@ -299,36 +335,67 @@ impl Focusable for RelayListPanel {
impl Render for RelayListPanel { impl Render for RelayListPanel {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex() v_flex()
.size_full() .on_action(cx.listener(Self::set_metadata))
.items_center() .p_3()
.justify_center() .gap_2()
.p_2() .w_full()
.gap_10()
.child( .child(
div() div()
.text_center() .text_xs()
.text_color(cx.theme().text_muted)
.child(SharedString::from(MSG)),
)
.child(divider(cx))
.child(
v_flex()
.gap_2()
.flex_1()
.w_full()
.text_sm()
.child(
div()
.text_xs()
.font_semibold() .font_semibold()
.line_height(relative(1.25)) .text_color(cx.theme().text_muted)
.child(SharedString::from("Update Relay List")), .child(SharedString::from("Relays:")),
) )
.child( .child(
v_flex() v_flex()
.w_112() .gap_1()
.gap_2()
.text_sm()
.child(
v_flex()
.gap_1p5()
.child( .child(
h_flex() h_flex()
.gap_1() .gap_1()
.w_full() .w_full()
.child(TextInput::new(&self.input).small()) .child(
TextInput::new(&self.input)
.small()
.bordered(false)
.cleanable(),
)
.child(
Button::new("metadata")
.map(|this| {
if let Some(metadata) = self.metadata.read(cx) {
this.label(metadata.to_string())
} else {
this.label("R & W")
}
})
.tooltip("Relay metadata")
.ghost()
.h(rems(2.))
.text_xs()
.dropdown_menu(|this, _window, _cx| {
this.menu("Read", Box::new(SetMetadata::Read))
.menu("Write", Box::new(SetMetadata::Write))
}),
)
.child( .child(
Button::new("add") Button::new("add")
.icon(IconName::Plus) .icon(IconName::Plus)
.label("Add") .tooltip("Add relay")
.ghost() .ghost()
.size(rems(2.))
.on_click(cx.listener(move |this, _, window, cx| { .on_click(cx.listener(move |this, _, window, cx| {
this.add(window, cx); this.add(window, cx);
})), })),
@@ -345,17 +412,25 @@ impl Render for RelayListPanel {
}), }),
) )
.map(|this| { .map(|this| {
if !self.relays.is_empty() { if self.relays.is_empty() {
this.child(self.render_list(window, cx))
} else {
this.child(self.render_empty(window, cx)) this.child(self.render_empty(window, cx))
} else {
this.child(
v_flex()
.gap_1()
.flex_1()
.w_full()
.children(self.render_list_items(cx)),
)
} }
}) })
.child(divider(cx))
.child( .child(
Button::new("submit") Button::new("submit")
.label("Update") .label("Update")
.primary() .primary()
.small()
.loading(self.updating)
.disabled(self.updating)
.on_click(cx.listener(move |this, _ev, window, cx| { .on_click(cx.listener(move |this, _ev, window, cx| {
this.set_relays(window, cx); this.set_relays(window, cx);
})), })),