aboutsummaryrefslogtreecommitdiffstats
path: root/src/Router.tsx
blob: eb4cf21ba5b254972d6d23e5e224b507e3ec469d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { useEffect, useState } from "react";
import Search from "./Search";
import Table from "./Table";

const startPath =
  location.pathname.at(-1) === "/"
    ? location.pathname
    : location.pathname + "/";
export default function Router() {
  const [path, setPath] = useState("");
  useEffect(() => {
    setPath(location.pathname);
  }, []);
  window.addEventListener("popstate", () => {
    setPath(location.pathname);
  });

  function updatePath(newPath: string) {
    console.log({ newPath, startPath });
    const path = startPath + newPath;
    history.pushState({}, "", path);
    setPath(path);
  }

  if (path.includes("/nagrody/")) {
    return <Table />;
  } else {
    return <Search setPath={updatePath} />;
  }
}