feat: router crate is polished up

This commit is contained in:
Jonathan Kelley committed 2022-03-04 13:16:37 -05:00
1 parent 60b11081f2
commit cc83900334
12 files changed
+341 -260

No files matched your search

-5
View File
@@ -1,5 +0,0 @@
use dioxus_core::prelude::*;
pub fn use_param<T>(cx: &ScopeState) -> Option<T> {
todo!()
}
Whitespace-only changes.
+81 -8
View File
@@ -1,27 +1,100 @@
use crate::{ParsedRoute, RouteContext, RouterCore, RouterService};
use dioxus_core::{ScopeId, ScopeState};
use std::{rc::Rc, str::FromStr, sync::Arc};
use crate::{location::ParsedRoute, RouterCore, RouterService};
use std::{borrow::Cow, str::FromStr, sync::Arc};
use url::Url;
/// This hook provides access to information about the current location in the
/// context of a [`Router`]. If this function is called outside of a `Router`
/// component it will panic.
pub fn use_route(cx: &ScopeState) -> &ParsedRoute {
pub fn use_route(cx: &ScopeState) -> &UseRoute {
let handle = cx.use_hook(|_| {
let router = cx
.consume_context::<Arc<RouterCore>>()
.consume_context::<RouterService>()
.expect("Cannot call use_route outside the scope of a Router component");
let route_context = cx
.consume_context::<RouteContext>()
.expect("Cannot call use_route outside the scope of a Router component");
router.subscribe_onchange(cx.scope_id());
UseRouteListener {
route: router.current_location(),
state: UseRoute {
route_context,
route: router.current_location(),
},
router,
scope: cx.scope_id(),
}
});
&handle.route
handle.state.route = handle.router.current_location();
&handle.state
}
/// A handle to the current location of the router.
pub struct UseRoute {
pub(crate) route: Arc<ParsedRoute>,
pub(crate) route_context: RouteContext,
}
impl UseRoute {
/// Get the underlying [`Url`] of the current location.
pub fn url(&self) -> &Url {
&self.route.url
}
/// Get the first query parameter given the parameter name.
///
/// If you need to get more than one parameter, use [`query_pairs`] on the [`Url`] instead.
pub fn query(&self, param: &str) -> Option<Cow<str>> {
self.route
.url
.query_pairs()
.find(|(k, _)| k == param)
.map(|(_, v)| v)
}
/// Returns the nth segment in the path. Paths that end with a slash have
/// the slash removed before determining the segments. If the path has
/// fewer segments than `n` then this method returns `None`.
pub fn nth_segment(&self, n: usize) -> Option<&str> {
self.route.url.path_segments()?.nth(n)
}
/// Returns the last segment in the path. Paths that end with a slash have
/// the slash removed before determining the segments. The root path, `/`,
/// will return an empty string.
pub fn last_segment(&self) -> Option<&str> {
self.route.url.path_segments()?.last()
}
/// Get the named parameter from the path, as defined in your router. The
/// value will be parsed into the type specified by `T` by calling
/// `value.parse::<T>()`. This method returns `None` if the named
/// parameter does not exist in the current path.
pub fn segment(&self, name: &str) -> Option<&str> {
let index = self
.route_context
.total_route
.trim_start_matches('/')
.split('/')
.position(|segment| segment.starts_with(':') && &segment[1..] == name)?;
self.route.url.path_segments()?.nth(index)
}
/// Get the named parameter from the path, as defined in your router. The
/// value will be parsed into the type specified by `T` by calling
/// `value.parse::<T>()`. This method returns `None` if the named
/// parameter does not exist in the current path.
pub fn parse_segment<T>(&self, name: &str) -> Option<Result<T, T::Err>>
where
T: FromStr,
{
self.segment(name).map(|value| value.parse::<T>())
}
}
// The entire purpose of this struct is to unubscribe this component when it is unmounted.
@@ -29,7 +102,7 @@ pub fn use_route(cx: &ScopeState) -> &ParsedRoute {
// Instead, we hide the drop implementation on this private type exclusive to the hook,
// and reveal our cached version of UseRoute to the component.
struct UseRouteListener {
route: Arc<ParsedRoute>,
state: UseRoute,
router: Arc<RouterCore>,
scope: ScopeId,
}