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";
|
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</>;
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue