Create GlobalState

This commit is contained in:
Ethan Girouard 2024-11-15 18:35:06 -05:00
parent 47fc35814d
commit d42737f856
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146
2 changed files with 51 additions and 0 deletions

View File

@ -5,3 +5,5 @@ cfg_if! {
pub mod audio; pub mod audio;
} }
} }
pub mod state;

49
src/util/state.rs Normal file
View File

@ -0,0 +1,49 @@
use leptos::*;
use leptos::logging::*;
use crate::playstatus::PlayStatus;
use crate::models::User;
use crate::auth::get_logged_in_user;
/// Global front-end state
/// Contains anything frequently needed across multiple components
/// Behaves like a singleton, in that provide/expect_context will
/// always return the same instance
#[derive(Clone)]
pub struct GlobalState {
/// A resource that fetches the logged in user
/// This will not automatically refetch, so any login/logout related code
/// should call `refetch` on this resource
pub logged_in_user: Resource<(), Option<User>>,
/// The current play status
pub play_status: RwSignal<PlayStatus>,
}
impl GlobalState {
pub fn new() -> Self {
let play_status = create_rw_signal(PlayStatus::default());
let logged_in_user = create_resource(|| (), |_| async {
get_logged_in_user().await
.inspect_err(|e| {
error!("Error getting logged in user: {:?}", e);
})
.ok()
.flatten()
});
Self {
logged_in_user,
play_status,
}
}
pub fn logged_in_user() -> Resource<(), Option<User>> {
expect_context::<Self>().logged_in_user
}
pub fn play_status() -> RwSignal<PlayStatus> {
expect_context::<Self>().play_status
}
}