frontend: add param support to hash
component of URL
This commit is contained in:
parent
05fa47c9eb
commit
79bf537b3c
2 changed files with 11 additions and 6 deletions
|
@ -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</>;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue