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

View file

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