forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseLocationHash.ts
More file actions
32 lines (24 loc) · 919 Bytes
/
useLocationHash.ts
File metadata and controls
32 lines (24 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { useLocation } from 'react-router';
import { useMemo } from 'react';
export default () => {
const location = useLocation();
const getHashObject = (value: string): Record<string, string> =>
value
.substring(1)
.split('&')
.reduce((obj, str) => {
const [key, value = ''] = str.split('=');
return !str.trim() ? obj : { ...obj, [key]: value };
}, {});
const pathTo = (params: Record<string, string>): string => {
const current = getHashObject(location.hash);
for (const key in params) {
current[key] = params[key];
}
return Object.keys(current)
.map((key) => `${key}=${current[key]}`)
.join('&');
};
const hash = useMemo((): Record<string, string> => getHashObject(location.hash), [location.hash]);
return { hash, pathTo };
};