feat: improve ui

This commit is contained in:
reya
2024-07-26 08:25:58 +07:00
parent b6d3f1f24a
commit ccc5d85fc2
10 changed files with 121 additions and 96 deletions

11
src-tauri/Cargo.lock generated
View File

@@ -929,6 +929,7 @@ dependencies = [
"tauri-plugin-devtools",
"tauri-plugin-dialog",
"tauri-plugin-os",
"tauri-plugin-prevent-default",
"tauri-plugin-shell",
"tauri-specta",
]
@@ -5092,6 +5093,16 @@ dependencies = [
"thiserror",
]
[[package]]
name = "tauri-plugin-prevent-default"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38be0ac8fcc5fa03422409fc506015b01dc29cc8a3a72572c68d41e6f10f4491"
dependencies = [
"bitflags 2.6.0",
"tauri",
]
[[package]]
name = "tauri-plugin-shell"
version = "2.0.0-beta.9"

View File

@@ -12,16 +12,18 @@ tauri-build = { version = "2.0.0-beta", features = [] }
[dependencies]
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = [
"sqlite",
"sqlite",
] }
tauri = { version = "2.0.0-beta", features = [
"tray-icon",
"macos-private-api",
"protocol-asset",
"tray-icon",
"macos-private-api",
"protocol-asset",
] }
tauri-specta = { git = "https://github.com/reyamir/tauri-specta", branch = "feat/tauri-v2", features = [
"typescript",
"typescript",
] }
tauri-plugin-devtools = "2.0.0-beta"
tauri-plugin-prevent-default = "0.1"
tauri-plugin-os = "2.0.0-beta"
tauri-plugin-clipboard-manager = "2.0.0-beta"
tauri-plugin-dialog = "2.0.0-beta"
@@ -30,15 +32,14 @@ tauri-plugin-decorum = "0.1.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
keyring = { version = "3", features = [
"apple-native",
"windows-native",
"linux-native",
"apple-native",
"windows-native",
"linux-native",
] }
keyring-search = "1.2.0"
itertools = "0.13.0"
futures = "0.3.30"
specta = "^2.0.0-rc.12"
tauri-plugin-devtools = "2.0.0-beta"
[target.'cfg(target_os = "macos")'.dependencies]
border = { git = "https://github.com/ahkohd/tauri-toolkit", branch = "v2" }

View File

@@ -155,55 +155,6 @@ pub async fn login(
let hex = public_key.to_hex();
let keyring = Entry::new(&id, "nostr_secret").expect("Unexpected.");
tauri::async_runtime::spawn(async move {
let window = app.get_webview_window("main").expect("Window is terminated.");
let state = window.state::<Nostr>();
let client = &state.client;
client
.handle_notifications(|notification| async {
if let RelayPoolNotification::Message { message, relay_url } = notification {
if let RelayMessage::Event { event, .. } = message {
if event.kind == Kind::GiftWrap {
if let Ok(UnwrappedGift { rumor, sender }) =
client.unwrap_gift_wrap(&event).await
{
window
.emit(
"event",
Payload { event: rumor.as_json(), sender: sender.to_hex() },
)
.unwrap();
}
}
} else if let RelayMessage::Auth { challenge } = message {
match client.auth(challenge, relay_url.clone()).await {
Ok(..) => {
println!("Authenticated to {} relay.", relay_url);
if let Ok(relay) = client.relay(relay_url).await {
let opts = RelaySendOptions::new().skip_send_confirmation(true);
if let Err(e) = relay.resubscribe(opts).await {
println!(
"Impossible to resubscribe to '{}': {e}",
relay.url()
);
}
}
}
Err(e) => {
println!("Can't authenticate to '{relay_url}' relay: {e}");
}
}
} else {
println!("relay message: {}", message.as_json());
}
}
Ok(false)
})
.await
});
let password = match keyring.get_password() {
Ok(pw) => pw,
Err(_) => return Err("Cancelled".into()),
@@ -256,12 +207,41 @@ pub async fn login(
if client.reconcile_with(relays.clone(), old, NegentropyOptions::default()).await.is_ok() {
handle.emit("synchronized", ()).unwrap();
println!("synchronized");
};
if client.subscribe_to(relays, vec![new], None).await.is_ok() {
println!("Waiting for new message...")
};
tauri::async_runtime::spawn(async move {
let window = app.get_webview_window("main").expect("Window is terminated.");
let state = window.state::<Nostr>();
let client = &state.client;
// Workaround for https://github.com/rust-nostr/nostr/issues/509
// TODO: remove
let _ = client.get_events_of(vec![Filter::new().kind(Kind::TextNote).limit(0)], None).await;
client
.handle_notifications(|notification| async {
if let RelayPoolNotification::Event { event, .. } = notification {
if event.kind == Kind::GiftWrap {
if let Ok(UnwrappedGift { rumor, sender }) =
client.unwrap_gift_wrap(&event).await
{
window
.emit(
"event",
Payload { event: rumor.as_json(), sender: sender.to_hex() },
)
.unwrap();
}
}
}
Ok(false)
})
.await
});
Ok(hex)
}

View File

@@ -32,9 +32,9 @@ pub async fn get_chats(state: State<'_, Nostr>) -> Result<Vec<String>, String> {
let uniqs = rumors
.into_iter()
.sorted_by_key(|ev| Reverse(ev.created_at))
.filter(|ev| ev.pubkey != public_key)
.unique_by(|ev| ev.pubkey)
.sorted_by_key(|ev| Reverse(ev.created_at))
.map(|ev| ev.as_json())
.collect::<Vec<_>>();

View File

@@ -76,9 +76,8 @@ fn main() {
// Config
let opts = Options::new()
.autoconnect(true)
.automatic_authentication(false)
.timeout(Duration::from_secs(5))
.send_timeout(Some(Duration::from_secs(50)))
.send_timeout(Some(Duration::from_secs(5)))
.connection_timeout(Some(Duration::from_secs(20)));
// Setup nostr client
@@ -98,6 +97,7 @@ fn main() {
Ok(())
})
.enable_macos_default_menu(false)
.plugin(tauri_plugin_prevent_default::init())
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_dialog::init())