Possibility to search instances in public tab (#16)

This commit is contained in:
Lorenzo B Gomez 2023-07-09 00:50:29 -05:00 committed by GitHub
parent 5e0c84aec7
commit 2442843ce3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 9 deletions

View File

@ -4,7 +4,7 @@ use lemmy_api_common::{
site::{GetFederatedInstances, GetFederatedInstancesResponse},
};
pub fn fetch_instances() -> std::result::Result<Vec<Instance>, reqwest::Error> {
pub fn fetch_instances(query_filter: &str) -> std::result::Result<Vec<Instance>, reqwest::Error> {
// TODO: Update code to use the Instance views from lemmy 0.18.0
let params = GetFederatedInstances {
auth: settings::get_current_account().jwt,
@ -17,11 +17,12 @@ pub fn fetch_instances() -> std::result::Result<Vec<Instance>, reqwest::Error> {
.send()?
.json::<GetFederatedInstancesResponse>()?;
let lowercase_query_filter = query_filter.to_lowercase();
match instances.federated_instances {
Some(instances) => Ok(instances
.linked
.iter()
.filter(|instance| instance.software == Some("lemmy".to_owned()))
.filter(|instance| instance.software == Some("lemmy".to_owned()) && instance.domain.clone().contains(&lowercase_query_filter))
.map(|instance| instance.clone())
.collect::<Vec<Instance>>()),
None => Ok(vec![]),

View File

@ -8,6 +8,7 @@ use super::instance_row::InstanceRow;
pub struct InstancesPage {
instances: FactoryVecDeque<InstanceRow>,
instances_search_buffer: gtk::EntryBuffer
}
#[derive(Debug)]
@ -40,12 +41,32 @@ impl SimpleComponent for InstancesPage {
#[name(stack)]
gtk::Stack {
add_child = &gtk::ScrolledWindow {
set_vexpand: true,
set_hexpand: true,
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 10,
set_margin_all: 10,
gtk::Box {
set_spacing: 10,
gtk::Entry {
set_hexpand: true,
set_tooltip_text: Some("Search"),
set_buffer: &model.instances_search_buffer,
},
gtk::Button {
set_label: "Filter",
connect_clicked => InstancesPageInput::FetchInstances,
}
},
#[local_ref]
instances -> gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 5,
set_vexpand: true,
},
}
} -> {
set_title: "Public",
},
@ -71,6 +92,7 @@ impl SimpleComponent for InstancesPage {
} -> {
set_title: "Custom",
},
}
}
}
@ -81,7 +103,8 @@ impl SimpleComponent for InstancesPage {
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let instances = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender());
let model = Self { instances };
let instances_search_buffer = gtk::EntryBuffer::builder().build();
let model = Self { instances, instances_search_buffer };
let instances = model.instances.widget();
let widgets = view_output!();
ComponentParts { model, widgets }
@ -90,8 +113,9 @@ impl SimpleComponent for InstancesPage {
fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>) {
match msg {
InstancesPageInput::FetchInstances => {
let filter = self.instances_search_buffer.text().as_str().to_owned();
std::thread::spawn(move || {
let message = match api::instances::fetch_instances() {
let message = match api::instances::fetch_instances(&filter) {
Ok(instances) => Some(InstancesPageInput::DoneFetchInstances(instances)),
Err(_err) => None,
};