feat: add global state
This commit is contained in:
@@ -6,14 +6,6 @@ resolver = "2"
|
|||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
coop = { path = "crates/*" }
|
coop = { path = "crates/*" }
|
||||||
|
|
||||||
# Account
|
|
||||||
keyring-search = "1.2.0"
|
|
||||||
keyring = { version = "3", features = [
|
|
||||||
"apple-native",
|
|
||||||
"windows-native",
|
|
||||||
"linux-native",
|
|
||||||
] }
|
|
||||||
|
|
||||||
# UI
|
# UI
|
||||||
gpui = { git = "https://github.com/zed-industries/zed" }
|
gpui = { git = "https://github.com/zed-industries/zed" }
|
||||||
components = { package = "ui", git = "https://github.com/longbridgeapp/gpui-component", version = "0.1.0" }
|
components = { package = "ui", git = "https://github.com/longbridgeapp/gpui-component", version = "0.1.0" }
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
gpui.workspace = true
|
||||||
nostr-sdk.workspace = true
|
nostr-sdk.workspace = true
|
||||||
dirs.workspace = true
|
dirs.workspace = true
|
||||||
tokio.workspace = true
|
tokio.workspace = true
|
||||||
|
|
||||||
|
keyring-search = "1.2.0"
|
||||||
|
keyring-lib = { version = "1.0.2", features = ["tokio", "derive"] }
|
||||||
|
|||||||
@@ -1 +1,39 @@
|
|||||||
|
use gpui::Global;
|
||||||
|
use keyring_search::{Limit, List, Search};
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
use state::get_client;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
|
||||||
|
pub struct Nostr {
|
||||||
|
pub client: &'static Client,
|
||||||
|
pub accounts: HashSet<PublicKey>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Nostr as Global State
|
||||||
|
impl Global for Nostr {}
|
||||||
|
|
||||||
|
impl Nostr {
|
||||||
|
pub async fn init() -> Self {
|
||||||
|
// Initialize nostr client
|
||||||
|
let client = get_client().await;
|
||||||
|
// Get all accounts
|
||||||
|
let accounts = Self::get_accounts();
|
||||||
|
|
||||||
|
Self { client, accounts }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_accounts() -> HashSet<PublicKey> {
|
||||||
|
let search = Search::new().expect("Keyring not working.");
|
||||||
|
let results = search.by_service("coop");
|
||||||
|
let list = List::list_credentials(&results, Limit::All);
|
||||||
|
let accounts: HashSet<PublicKey> = list
|
||||||
|
.split_whitespace()
|
||||||
|
.filter(|v| v.starts_with("npub1") && !v.ends_with("coop"))
|
||||||
|
.filter_map(|i| PublicKey::from_bech32(i).ok())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
accounts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use gpui::*;
|
use gpui::*;
|
||||||
use nostr::state::get_client;
|
use nostr::Nostr;
|
||||||
|
|
||||||
struct HelloWorld {
|
struct HelloWorld {
|
||||||
text: SharedString,
|
text: SharedString,
|
||||||
@@ -19,10 +19,13 @@ impl Render for HelloWorld {
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let _client = get_client().await;
|
let nostr = Nostr::init().await;
|
||||||
|
|
||||||
App::new().run(|cx: &mut AppContext| {
|
App::new().run(|cx: &mut AppContext| {
|
||||||
|
// Set window size
|
||||||
let bounds = Bounds::centered(None, size(px(860.0), px(650.0)), cx);
|
let bounds = Bounds::centered(None, size(px(860.0), px(650.0)), cx);
|
||||||
|
// Set global state
|
||||||
|
cx.set_global(nostr);
|
||||||
|
|
||||||
cx.open_window(
|
cx.open_window(
|
||||||
WindowOptions {
|
WindowOptions {
|
||||||
|
|||||||
Reference in New Issue
Block a user