chore: fix high cpu

This commit is contained in:
2025-03-25 15:04:41 +07:00
parent 00cf7792e5
commit 4c9533bfe4

View File

@@ -1,12 +1,12 @@
use std::time::Duration; use std::{sync::Arc, time::Duration};
use account::Account; use account::Account;
use common::utils::create_qr; use common::utils::create_qr;
use global::get_client_keys; use global::get_client_keys;
use gpui::{ use gpui::{
div, img, prelude::FluentBuilder, relative, AnyElement, App, AppContext, Context, Entity, div, img, prelude::FluentBuilder, relative, AnyElement, App, AppContext, Context, Entity,
EventEmitter, FocusHandle, Focusable, IntoElement, ParentElement, Render, SharedString, Styled, EventEmitter, FocusHandle, Focusable, Image, IntoElement, ParentElement, Render, SharedString,
Subscription, Window, Styled, Subscription, Window,
}; };
use nostr_connect::prelude::*; use nostr_connect::prelude::*;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
@@ -36,6 +36,7 @@ pub struct Login {
error_message: Entity<Option<SharedString>>, error_message: Entity<Option<SharedString>>,
is_logging_in: bool, is_logging_in: bool,
// Nostr Connect // Nostr Connect
qr: Option<Arc<Image>>,
connect_relay: Entity<TextInput>, connect_relay: Entity<TextInput>,
connect_client: Entity<Option<NostrConnectURI>>, connect_client: Entity<Option<NostrConnectURI>>,
// Panel // Panel
@@ -90,12 +91,17 @@ impl Login {
)); ));
subscriptions.push( subscriptions.push(
cx.observe_in(&connect_client, window, |_, this, window, cx| { cx.observe_in(&connect_client, window, |this, uri, window, cx| {
let keys = get_client_keys().to_owned(); let keys = get_client_keys().to_owned();
let account = Account::global(cx); let account = Account::global(cx);
if let Some(uri) = this.read(cx) { if let Some(uri) = uri.read(cx).clone() {
match NostrConnect::new(uri.to_owned(), keys, Duration::from_secs(300), None) { if let Ok(qr) = create_qr(uri.to_string().as_str()) {
this.qr = Some(qr);
cx.notify();
}
match NostrConnect::new(uri, keys, Duration::from_secs(300), None) {
Ok(signer) => { Ok(signer) => {
account.update(cx, |this, cx| { account.update(cx, |this, cx| {
this.login(signer, window, cx); this.login(signer, window, cx);
@@ -141,6 +147,7 @@ impl Login {
connect_client, connect_client,
subscriptions, subscriptions,
error_message, error_message,
qr: None,
is_logging_in: false, is_logging_in: false,
name: "Login".into(), name: "Login".into(),
closable: true, closable: true,
@@ -371,8 +378,7 @@ impl Render for Login {
.child("Use Nostr Connect apps to scan the code"), .child("Use Nostr Connect apps to scan the code"),
), ),
) )
.when_some(self.connect_client.read(cx).as_ref(), |this, uri| { .when_some(self.qr.clone(), |this, qr| {
if let Ok(qr) = create_qr(uri.to_string().as_ref()) {
this.child( this.child(
div() div()
.mb_2() .mb_2()
@@ -393,9 +399,6 @@ impl Render for Login {
.bg(cx.theme().background) .bg(cx.theme().background)
.child(img(qr).h_56()), .child(img(qr).h_56()),
) )
} else {
this
}
}) })
.child( .child(
div() div()