frontend: add param support to hash component of URL

This commit is contained in:
0x1eef 2023-12-21 02:57:51 -03:00
parent 05fa47c9eb
commit 79bf537b3c
2 changed files with 11 additions and 6 deletions

View file

@ -3,7 +3,7 @@ import { Issue } from "/types/schema";
import { useLocationHash } from "/hooks/useLocationHash";
export function ReadIssue() {
const [id] = useLocationHash();
const { id } = useLocationHash();
const [issue, setIssue] = useState<Issue | null>(null);
useEffect(() => {
@ -13,7 +13,7 @@ export function ReadIssue() {
.then(res => setIssue(res.issue));
}, [id]);
if (id.length && issue) {
if (id?.length && issue) {
return <>{issue.title}</>;
} else {
return <>No issue</>;

View file

@ -1,11 +1,16 @@
import { useEffect, useState } from "react";
export function useLocationHash() {
const [hash, setHash] = useState<string>("");
const [params, setParams] = useState<Record<string, string>>({});
function set() {
const normalized = location.hash.substring(1, location.hash.length);
setHash(normalized);
const entries = Object.fromEntries(
location.hash
.substring(1, location.hash.length)
.split(",")
.map(e => e.split("=")),
);
setParams(entries);
}
useEffect(set, [location.hash]);
@ -14,5 +19,5 @@ export function useLocationHash() {
return () => window.removeEventListener("hashchange", set);
}, []);
return [hash];
return params;
}