File "route.js"

Full Path: /home/tekvhqgl/public_html/dev2024_old_skip/wp-content/plugins/presto-player/src/router/route.js
File size: 592 bytes
MIME-type: text/x-java
Charset: utf-8

import { RouterContext } from "./context";
const { useContext } = wp.element;
import { match } from "path-to-regexp";
let prev = "";

export function Route({ path, onRoute, children }) {
  // Extract route from RouterContext
  const { route } = useContext(RouterContext);

  const checkMatch = match(`${path}`);
  const matched = checkMatch(`${route.hash.substr(1)}`);

  if (!matched) {
    return null;
  }

  if (onRoute) {
    if (prev !== matched.path) {
      onRoute();
    }
    prev = matched.path;
  }

  return <div>{wp.element.cloneElement(children, { route: matched })}</div>;
}