chore: Refactor code to use HashSet for account search results (#222)

This commit is contained in:
XIAO YU
2024-07-03 09:33:16 +09:00
committed by GitHub
parent 72da83d648
commit 4c323b9daa
2 changed files with 7 additions and 15 deletions

View File

@@ -27,12 +27,9 @@ pub fn swizzle_to_menubar_panel(app_handle: &tauri::AppHandle) {
let handle = app_handle.clone(); let handle = app_handle.clone();
panel_delegate.set_listener(Box::new(move |delegate_name: String| { panel_delegate.set_listener(Box::new(move |delegate_name: String| {
match delegate_name.as_str() { if delegate_name.as_str() == "window_did_resign_key" {
"window_did_resign_key" => {
let _ = handle.emit("menubar_panel_did_resign_key", ()); let _ = handle.emit("menubar_panel_did_resign_key", ());
} }
_ => (),
}
})); }));
panel.set_level(NSMainMenuWindowLevel + 1); panel.set_level(NSMainMenuWindowLevel + 1);

View File

@@ -3,6 +3,7 @@ use keyring_search::{Limit, List, Search};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use serde::Serialize; use serde::Serialize;
use specta::Type; use specta::Type;
use std::collections::HashSet;
use std::time::Duration; use std::time::Duration;
use tauri::{EventTarget, Manager, State}; use tauri::{EventTarget, Manager, State};
use tauri_plugin_notification::NotificationExt; use tauri_plugin_notification::NotificationExt;
@@ -22,24 +23,18 @@ pub struct Account {
#[tauri::command] #[tauri::command]
#[specta::specta] #[specta::specta]
pub fn get_accounts() -> Result<Vec<String>, String> { pub fn get_accounts() -> Result<Vec<String>, String> {
let search = Search::new().unwrap(); let search = Search::new().map_err(|e| e.to_string())?;
let results = search.by("Account", "nostr_secret"); let results = search.by("Account", "nostr_secret");
match List::list_credentials(results, Limit::All) { match List::list_credentials(results, Limit::All) {
Ok(list) => { Ok(list) => {
let search: Vec<String> = list let accounts: HashSet<String> = list
.split_whitespace() .split_whitespace()
.map(|v| v.to_string())
.filter(|v| v.starts_with("npub1")) .filter(|v| v.starts_with("npub1"))
.map(String::from)
.collect(); .collect();
let accounts: Vec<String> = search Ok(accounts.into_iter().collect())
.into_iter()
.collect::<std::collections::HashSet<_>>()
.into_iter()
.collect();
Ok(accounts)
} }
Err(_) => Err("Empty.".into()), Err(_) => Err("Empty.".into()),
} }