use freya_core::prelude::*;
use crate::{
prelude::{
Outlet,
OutletContext,
RouterContext,
},
routable::Routable,
router_cfg::RouterConfig,
};
pub struct Router<R: Routable + Clone>(NoArgCallback<RouterConfig<R>>);
impl<R: Routable + Clone> PartialEq for Router<R> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<R: Routable + Clone> Router<R> {
pub fn new(init: impl Into<NoArgCallback<RouterConfig<R>>>) -> Self {
Self(init.into())
}
}
impl<R: Routable + Clone> Component for Router<R> {
fn render(&self) -> impl IntoElement {
use_hook(|| {
if try_consume_own_context::<RouterContext>().is_none() {
provide_context(RouterContext::create::<R>(self.0.call()));
}
if try_consume_own_context::<OutletContext<R>>().is_none() {
provide_context(OutletContext::<R>::new());
}
});
Outlet::<R>::new()
}
}