From d9020a15d4089073524c861a2cab33de53c241d3 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 14 Nov 2024 11:15:02 +0100 Subject: [PATCH] Move api client into static --- src/frontend/api.rs | 6 ++++++ src/frontend/app.rs | 21 ++----------------- src/frontend/components/article_nav.rs | 4 ++-- src/frontend/components/connect.rs | 5 ++--- .../components/instance_follow_button.rs | 4 ++-- src/frontend/components/nav.rs | 12 +++++------ src/frontend/pages/article/actions.rs | 6 +++--- src/frontend/pages/article/create.rs | 6 +++--- src/frontend/pages/article/edit.rs | 7 +++---- src/frontend/pages/article/list.rs | 6 +++--- src/frontend/pages/instance/details.rs | 7 +++---- src/frontend/pages/instance/list.rs | 6 +++--- src/frontend/pages/login.rs | 4 ++-- src/frontend/pages/mod.rs | 9 +++----- src/frontend/pages/notifications.rs | 4 ++-- src/frontend/pages/register.rs | 4 ++-- src/frontend/pages/search.rs | 11 +++++----- src/frontend/pages/user_profile.rs | 6 +++--- 18 files changed, 55 insertions(+), 73 deletions(-) diff --git a/src/frontend/api.rs b/src/frontend/api.rs index 401795d..ee85f48 100644 --- a/src/frontend/api.rs +++ b/src/frontend/api.rs @@ -1,3 +1,5 @@ +use std::sync::LazyLock; + use crate::{ common::{ newtypes::ArticleId, @@ -32,6 +34,10 @@ use reqwest::{Client, RequestBuilder, StatusCode}; use serde::{Deserialize, Serialize}; use url::Url; +pub static CLIENT: LazyLock = LazyLock::new(|| { + ApiClient::new(Client::new(), None) + }); + #[derive(Clone)] pub struct ApiClient { client: Client, diff --git a/src/frontend/app.rs b/src/frontend/app.rs index e5b8dae..6f97bc8 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -1,7 +1,6 @@ use crate::{ common::LocalUserView, frontend::{ - api::ApiClient, components::nav::Nav, dark_mode::DarkMode, pages::{ @@ -35,40 +34,25 @@ use leptos::{ IntoView, RwSignal, SignalGet, - SignalGetUntracked, SignalUpdate, }; use leptos_meta::{provide_meta_context, *}; use leptos_router::{Route, Router, Routes}; -use reqwest::Client; -use std::{thread::sleep, time::Duration}; +use crate::frontend::api::CLIENT; // https://book.leptos.dev/15_global_state.html #[derive(Clone)] pub struct GlobalState { - api_client: ApiClient, pub(crate) my_profile: Option, } impl GlobalState { - pub fn api_client() -> ApiClient { - let mut global_state = use_context::>(); - // Wait for global state to be populated (only needed on instance_details for some reason) - while global_state.is_none() { - sleep(Duration::from_millis(10)); - global_state = use_context::>(); - } - global_state - .expect("global state is provided") - .get_untracked() - .api_client - } pub fn update_my_profile() { create_local_resource( move || (), |_| async move { - let my_profile = GlobalState::api_client().my_profile().await.ok(); + let my_profile = CLIENT.my_profile().await.ok(); expect_context::>() .update(|state| state.my_profile = my_profile.clone()); }, @@ -91,7 +75,6 @@ impl GlobalState { pub fn App() -> impl IntoView { provide_meta_context(); let global_state = GlobalState { - api_client: ApiClient::new(Client::new(), None), my_profile: None, }; // Load user profile in case we are already logged in diff --git a/src/frontend/components/article_nav.rs b/src/frontend/components/article_nav.rs index 6ee5523..10553a0 100644 --- a/src/frontend/components/article_nav.rs +++ b/src/frontend/components/article_nav.rs @@ -6,7 +6,7 @@ use crate::{ article_title, components::instance_follow_button::InstanceFollowButton, }, -}; +};use crate::frontend::api::CLIENT; use leptos::*; use leptos_router::*; @@ -37,7 +37,7 @@ pub fn ArticleNav( let form = GetInstance { id: Some(instance_id), }; - GlobalState::api_client().get_instance(&form).await.unwrap() + CLIENT.get_instance(&form).await.unwrap() }, ); let global_state = use_context::>().unwrap(); diff --git a/src/frontend/components/connect.rs b/src/frontend/components/connect.rs index fc28d96..73eddf4 100644 --- a/src/frontend/components/connect.rs +++ b/src/frontend/components/connect.rs @@ -1,11 +1,10 @@ -use crate::frontend::app::GlobalState; use leptos::{component, *}; -use url::Url; +use url::Url;use crate::frontend::api::CLIENT; #[component] pub fn ConnectView(res: Resource) -> impl IntoView { let connect_ibis_wiki = create_action(move |_: &()| async move { - GlobalState::api_client() + CLIENT .resolve_instance(Url::parse("https://ibis.wiki").unwrap()) .await .unwrap(); diff --git a/src/frontend/components/instance_follow_button.rs b/src/frontend/components/instance_follow_button.rs index 504f107..c02b502 100644 --- a/src/frontend/components/instance_follow_button.rs +++ b/src/frontend/components/instance_follow_button.rs @@ -1,7 +1,7 @@ use crate::{ common::{newtypes::InstanceId, DbInstance, FollowInstance}, frontend::app::GlobalState, -}; +};use crate::frontend::api::CLIENT; use leptos::{component, *}; #[component] @@ -11,7 +11,7 @@ pub fn InstanceFollowButton(instance: DbInstance) -> impl IntoView { let instance_id = *instance_id; async move { let form = FollowInstance { id: instance_id }; - GlobalState::api_client() + CLIENT .follow_instance(form) .await .unwrap(); diff --git a/src/frontend/components/nav.rs b/src/frontend/components/nav.rs index ee69596..755b6ff 100644 --- a/src/frontend/components/nav.rs +++ b/src/frontend/components/nav.rs @@ -1,18 +1,18 @@ use crate::frontend::{app::GlobalState, dark_mode::DarkMode}; use leptos::{component, use_context, view, IntoView, RwSignal, SignalWith, *}; -use leptos_router::*; +use leptos_router::*;use crate::frontend::api::CLIENT; #[component] pub fn Nav() -> impl IntoView { let global_state = use_context::>().unwrap(); let logout_action = create_action(move |_| async move { - GlobalState::api_client().logout().await.unwrap(); + CLIENT.logout().await.unwrap(); GlobalState::update_my_profile(); }); let registration_open = create_local_resource( || (), move |_| async move { - GlobalState::api_client() + CLIENT .get_local_instance() .await .map(|i| i.registration_open) @@ -22,7 +22,7 @@ pub fn Nav() -> impl IntoView { let notification_count = create_resource( || (), move |_| async move { - GlobalState::api_client() + CLIENT .notifications_count() .await .unwrap_or_default() @@ -39,7 +39,7 @@ pub fn Nav() -> impl IntoView { >

- {GlobalState::api_client().hostname} + {CLIENT.hostname.clone()}