Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/plugins/useLocationHash.ts
7458 views
1
import { useLocation } from 'react-router';
2
import { useMemo } from 'react';
3
4
export default () => {
5
const location = useLocation();
6
7
const getHashObject = (value: string): Record<string, string> =>
8
value
9
.substring(1)
10
.split('&')
11
.reduce((obj, str) => {
12
const [key, value = ''] = str.split('=');
13
14
return !str.trim() ? obj : { ...obj, [key]: value };
15
}, {});
16
17
const pathTo = (params: Record<string, string>): string => {
18
const current = getHashObject(location.hash);
19
20
for (const key in params) {
21
current[key] = params[key];
22
}
23
24
return Object.keys(current)
25
.map((key) => `${key}=${current[key]}`)
26
.join('&');
27
};
28
29
const hash = useMemo((): Record<string, string> => getHashObject(location.hash), [location.hash]);
30
31
return { hash, pathTo };
32
};
33
34