fix link component

This commit is contained in:
Evan Almloff committed 2023-07-26 10:18:39 -07:00
1 parent 5f0dd3af3e
commit e7a9161066
6 files changed
+42 -39

No files matched your search

+1 -1
View File
@@ -48,7 +48,7 @@ fn app(cx: Scope) -> Element {
let read = div_element.read(); let read = div_element.read();
let client_rect = read.as_ref().map(|el| el.get_client_rect()); let client_rect = read.as_ref().map(|el| el.get_client_rect());
if let Some(client_rect) = client_rect { if let Some(client_rect) = client_rect {
if let Ok(rect) = dbg!(client_rect.await) { if let Ok(rect) = client_rect.await {
dimentions.set(rect); dimentions.set(rect);
} }
} }
+6 -2
View File
@@ -106,12 +106,16 @@ async fn check_files_and_report(files_to_check: Vec<PathBuf>) -> Result<()> {
} }
fn collect_rs_files(folder: &Path, files: &mut Vec<PathBuf>) { fn collect_rs_files(folder: &Path, files: &mut Vec<PathBuf>) {
let Ok(folder) = folder.read_dir() else { return }; let Ok(folder) = folder.read_dir() else {
return;
};
// load the gitignore // load the gitignore
for entry in folder { for entry in folder {
let Ok(entry) = entry else { continue; }; let Ok(entry) = entry else {
continue;
};
let path = entry.path(); let path = entry.path();
+8 -6
View File
@@ -5,7 +5,7 @@ use dioxus::prelude::*;
use log::error; use log::error;
use crate::navigation::NavigationTarget; use crate::navigation::NavigationTarget;
use crate::prelude::{AnyDisplay, Routable}; use crate::prelude::Routable;
use crate::utils::use_router_internal::use_router_internal; use crate::utils::use_router_internal::use_router_internal;
/// Something that can be converted into a [`NavigationTarget`]. /// Something that can be converted into a [`NavigationTarget`].
@@ -13,19 +13,21 @@ pub enum IntoRoutable {
/// A raw string target. /// A raw string target.
FromStr(String), FromStr(String),
/// A internal target. /// A internal target.
Route(Box<dyn AnyDisplay>), Route(Box<dyn Any>),
} }
impl<R: Routable> From<R> for IntoRoutable { impl<R: Routable> From<R> for IntoRoutable {
fn from(value: R) -> Self { fn from(value: R) -> Self {
IntoRoutable::Route(Box::new(value)) IntoRoutable::Route(Box::new(value) as Box<dyn Any>)
} }
} }
impl<R: Routable> From<NavigationTarget<R>> for IntoRoutable { impl<R: Routable> From<NavigationTarget<R>> for IntoRoutable {
fn from(value: NavigationTarget<R>) -> Self { fn from(value: NavigationTarget<R>) -> Self {
match value { match value {
NavigationTarget::Internal(route) => IntoRoutable::Route(Box::new(route)), NavigationTarget::Internal(route) => {
IntoRoutable::Route(Box::new(route) as Box<dyn Any>)
}
NavigationTarget::External(url) => IntoRoutable::FromStr(url), NavigationTarget::External(url) => IntoRoutable::FromStr(url),
} }
} }
@@ -193,7 +195,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
let current_url = router.current_route_string(); let current_url = router.current_route_string();
let href = match to { let href = match to {
IntoRoutable::FromStr(url) => url.to_string(), IntoRoutable::FromStr(url) => url.to_string(),
IntoRoutable::Route(route) => route.to_string(), IntoRoutable::Route(route) => router.any_route_to_string(&**route),
}; };
let parsed_route: NavigationTarget<Box<dyn Any>> = match router.route_from_str(&href) { let parsed_route: NavigationTarget<Box<dyn Any>> = match router.route_from_str(&href) {
Ok(route) => NavigationTarget::Internal(route.into()), Ok(route) => NavigationTarget::Internal(route.into()),
@@ -219,7 +221,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
if do_default && is_router_nav { if do_default && is_router_nav {
let href = match to { let href = match to {
IntoRoutable::FromStr(url) => url.to_string(), IntoRoutable::FromStr(url) => url.to_string(),
IntoRoutable::Route(route) => route.to_string(), IntoRoutable::Route(route) => router.any_route_to_string(&**route),
}; };
let parsed_route: NavigationTarget<Box<dyn Any>> = match router.route_from_str(&href) { let parsed_route: NavigationTarget<Box<dyn Any>> = match router.route_from_str(&href) {
Ok(route) => NavigationTarget::Internal(route.into()), Ok(route) => NavigationTarget::Internal(route.into()),
+23 -7
View File
@@ -37,6 +37,8 @@ pub struct RouterContext {
routing_callback: Option<Arc<dyn Fn(RouterContext) -> Option<NavigationTarget<Box<dyn Any>>>>>, routing_callback: Option<Arc<dyn Fn(RouterContext) -> Option<NavigationTarget<Box<dyn Any>>>>>,
failure_external_navigation: fn(Scope) -> Element, failure_external_navigation: fn(Scope) -> Element,
any_route_to_string: fn(&dyn Any) -> String,
} }
impl RouterContext { impl RouterContext {
@@ -79,6 +81,20 @@ impl RouterContext {
}), }),
failure_external_navigation: cfg.failure_external_navigation, failure_external_navigation: cfg.failure_external_navigation,
any_route_to_string: |route| {
route
.downcast_ref::<R>()
.unwrap_or_else(|| {
panic!(
"Route is not of the expected type: {}\n found typeid: {:?}\n expected typeid: {:?}",
std::any::type_name::<R>(),
route.type_id(),
std::any::TypeId::of::<R>()
)
})
.to_string()
},
}; };
// set the updater // set the updater
@@ -189,7 +205,8 @@ impl RouterContext {
/// The route that is currently active. /// The route that is currently active.
pub fn current<R: Routable>(&self) -> R { pub fn current<R: Routable>(&self) -> R {
self.state *self
.state
.read() .read()
.unwrap() .unwrap()
.history .history
@@ -200,12 +217,11 @@ impl RouterContext {
/// The route that is currently active. /// The route that is currently active.
pub fn current_route_string(&self) -> String { pub fn current_route_string(&self) -> String {
self.state self.any_route_to_string(&*self.state.read().unwrap().history.current_route())
.read() }
.unwrap()
.history pub(crate) fn any_route_to_string(&self, route: &dyn Any) -> String {
.current_route() (self.any_route_to_string)(route)
.to_string()
} }
/// The prefix that is currently active. /// The prefix that is currently active.
+4 -22
View File
@@ -10,7 +10,7 @@
//! 1) [`MemoryHistory`] for desktop/mobile/ssr platforms //! 1) [`MemoryHistory`] for desktop/mobile/ssr platforms
//! 2) [`WebHistory`] for web platforms //! 2) [`WebHistory`] for web platforms
use std::{any::Any, fmt::Display, sync::Arc}; use std::{any::Any, sync::Arc};
mod memory; mod memory;
pub use memory::*; pub use memory::*;
@@ -278,25 +278,6 @@ pub trait HistoryProvider<R: Routable> {
fn updater(&mut self, callback: Arc<dyn Fn() + Send + Sync>) {} fn updater(&mut self, callback: Arc<dyn Fn() + Send + Sync>) {}
} }
/// Something that can be displayed and is also an [`Any`]
pub trait AnyDisplay: Display + Any {
/// Get a reference to the inner [`Any`] object.
fn as_any(&self) -> &dyn Any;
}
impl dyn AnyDisplay {
/// Try to downcast the inner [`Any`] object to a concrete type.
pub fn downcast<T: Any + Clone>(self: Box<dyn AnyDisplay>) -> Option<T> {
self.as_any().downcast_ref::<T>().cloned()
}
}
impl<T: Display + Any> AnyDisplay for T {
fn as_any(&self) -> &dyn Any {
self
}
}
pub(crate) trait AnyHistoryProvider { pub(crate) trait AnyHistoryProvider {
#[must_use] #[must_use]
fn parse_route(&self, route: &str) -> Result<Box<dyn Any>, String>; fn parse_route(&self, route: &str) -> Result<Box<dyn Any>, String>;
@@ -305,7 +286,7 @@ pub(crate) trait AnyHistoryProvider {
fn accepts_type_id(&self, type_id: &std::any::TypeId) -> bool; fn accepts_type_id(&self, type_id: &std::any::TypeId) -> bool;
#[must_use] #[must_use]
fn current_route(&self) -> Box<dyn AnyDisplay>; fn current_route(&self) -> Box<dyn Any>;
#[must_use] #[must_use]
fn current_prefix(&self) -> Option<String> { fn current_prefix(&self) -> Option<String> {
@@ -375,9 +356,10 @@ where
type_id == &std::any::TypeId::of::<R>() type_id == &std::any::TypeId::of::<R>()
} }
fn current_route(&self) -> Box<dyn AnyDisplay> { fn current_route(&self) -> Box<dyn Any> {
let route = self.inner.current_route(); let route = self.inner.current_route();
println!("current_route {route}"); println!("current_route {route}");
println!("current_route type {}", std::any::type_name::<R>());
Box::new(route) Box::new(route)
} }
-1
View File
@@ -141,7 +141,6 @@ pub trait Routable: std::fmt::Display + std::str::FromStr + Clone + 'static {
let self_segments = self_str.split('/'); let self_segments = self_str.split('/');
let other_segments = other_str.split('/'); let other_segments = other_str.split('/');
for (self_seg, other_seg) in self_segments.zip(other_segments) { for (self_seg, other_seg) in self_segments.zip(other_segments) {
dbg!(self_seg, other_seg);
if self_seg != other_seg { if self_seg != other_seg {
return false; return false;
} }