| Server IP : 193.146.211.90 / Your IP : 10.85.6.55 Web Server : Apache System : Linux wordpress-gti-69dcdb54f7-n4wdq 6.11.8-arch1-2 #1 SMP PREEMPT_DYNAMIC Fri, 15 Nov 2024 15:35:07 +0000 x86_64 User : 1001 ( 1001) PHP Version : 8.2.25 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /bitnami/wordpress/wp-content/plugins/presto-player/src/router/ |
Upload File : |
import { Component } from "@wordpress/element";
import { locationToRoute } from "./utils";
import { history, RouterContext } from "./context";
import { Route } from "./route";
import { Link } from "./link";
import { match } from "path-to-regexp";
class Router extends Component {
constructor(props) {
super(props);
// Convert our routes into an array for easy 404 checking
this.routes = Object.keys(props.routes).map(
(key) => props.routes[key].path
);
// Listen for path changes from the history API
this.unlisten = history.listen(this.handleRouteChange);
const route = locationToRoute(history.location);
const { search } = history.location;
// Define the initial RouterContext value
this.state = {
route,
defaultRoute: props?.defaultRoute
? `${search}#${props?.defaultRoute}`
: `${search}#/`,
};
}
componentWillUnmount() {
// Stop listening for changes if the Router component unmounts
this.unlisten();
}
handleRouteChange = (location) => {
const route = locationToRoute(location?.location);
this.setState({ route: route });
};
render() {
// Define our variables
const { children, NotFound } = this.props;
const { route, defaultRoute } = this.state;
if (!route.hash) {
history.push(defaultRoute);
return <div></div>;
}
let matched = false;
// match route
(this.routes || []).forEach((name) => {
const checkMatch = match(route.hash.substr(1));
const isMatched = checkMatch(`${route.hash.substr(1)}`);
if (!isMatched) {
return;
}
matched = {
name,
data: isMatched,
};
});
const routerContextValue = { route, matched };
// Check if 404 if no route matched
const is404 = !matched;
return (
<RouterContext.Provider value={routerContextValue}>
{is404 ? <div>Not found</div> : children}
</RouterContext.Provider>
);
}
}
export { history, RouterContext, Router, Route, Link };