rustpad/src/useHash.ts

30 lines
724 B
TypeScript

import { useEffect, useState } from "react";
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const idLen = 6;
function getHash() {
if (!window.location.hash) {
let id = "";
for (let i = 0; i < idLen; i++) {
id += chars[Math.floor(Math.random() * chars.length)];
}
window.history.replaceState(null, "", "#" + id);
}
return window.location.hash.slice(1);
}
function useHash() {
const [hash, setHash] = useState(getHash);
useEffect(() => {
const handler = () => setHash(getHash());
window.addEventListener("hashchange", handler);
return () => window.removeEventListener("hashchange", handler);
}, []);
return hash;
}
export default useHash;