aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/useHashParam.js
diff options
context:
space:
mode:
authorBen Woodward <ben@bdw.to>2026-01-24 20:46:24 -0800
committerGitHub <noreply@github.com>2026-01-24 20:46:24 -0800
commite1927f633cab988ceeb8bcd51dd03aaa5b3f2392 (patch)
treea776d7287d2c9583151f2567d9cf4f22d010aeed /src/components/useHashParam.js
parentfbde2cd27669ac29e1c0bc9393f9a83ffa93f8c6 (diff)
Updates jan 2026 (#313)
* Update dependencies to latest versions and migrate to React 19 - Update all dependencies to latest versions - Migrate to React 19 createRoot API (replaces deprecated ReactDOM.render) - Update qrcode.react import to v4 named export (QRCodeSVG) * Format CSS with prettier * Add Hebrew translation * Add Slovak translation * Add Malagasy translation * Add Bangla translation * Init field * Change the logic concerning number of cards to print * fix/clean * Fix Increment-field-ID-in-the-print-area * Increment-field-ID-in-the-print-area * Review id implementation logic * Fix git commit * Handle Query parameter * Fix package.json * Fix query parameter for language * fix query parameter for language * Fix EncryptionModeChange query parameters // Error when non in query * Fix EncryptionModeChange query parameters // Error when empty in query * clean Setting.js don't need to import i18n * clean file App.js * first iteration for Hash * Add Esperanto translation * Update translations for Occitan 2 lines added * Update translations.js Added Swiss German to the Translations JavaScript-File * Update translations.js Removed custom Translations-Strings of my own Version. * Update README.md * Format translations.js with prettier --------- Co-authored-by: Ido Bronfeld <idobronfeld@gmail.com> Co-authored-by: Matej Kubinec <matej.kubinec@outlook.com> Co-authored-by: mpilasy <88362233+mpilasy@users.noreply.github.com> Co-authored-by: Tarek Hasan <94107336+Tarek-Hasan@users.noreply.github.com> Co-authored-by: ofostier <ofostier@gmail.com> Co-authored-by: zeecho <30541894+zeecho@users.noreply.github.com> Co-authored-by: Mejans <61360811+Mejans@users.noreply.github.com> Co-authored-by: Jan Zehnder <44242812+NZehnder@users.noreply.github.com>
Diffstat (limited to 'src/components/useHashParam.js')
-rw-r--r--src/components/useHashParam.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/components/useHashParam.js b/src/components/useHashParam.js
new file mode 100644
index 0000000..e43e1e4
--- /dev/null
+++ b/src/components/useHashParam.js
@@ -0,0 +1,48 @@
1import { useState, useEffect, useCallback } from 'react';
2
3const getHashSearchParams = (location) => {
4 const hash = location.hash.slice(1);
5 const [prefix, query] = hash.split('?');
6
7 return [prefix, new URLSearchParams(query)];
8};
9
10const getHashParam = (key, location = window.location) => {
11 const [_, searchParams] = getHashSearchParams(location);
12 return searchParams.get(key);
13};
14
15const setHashParam = (key, value, location = window.location) => {
16 const [prefix, searchParams] = getHashSearchParams(location);
17
18 if (typeof value === 'undefined') {
19 searchParams.delete(key);
20 } else {
21 searchParams.set(key, value);
22 }
23
24 const search = searchParams.toString();
25 location.hash = search ? `${prefix}?${search}` : prefix;
26};
27
28const useHashParam = (key) => {
29 const [innerValue, setInnerValue] = useState();
30
31 useEffect(() => {
32 const handleHashChange = () => setInnerValue(getHashParam(key));
33 handleHashChange();
34 window.addEventListener('hashchange', handleHashChange);
35 return () => window.removeEventListener('hashchange', handleHashChange);
36 }, [key]);
37
38 const setValue = useCallback(
39 (value) => {
40 setHashParam(key, value);
41 },
42 [key]
43 );
44
45 return [innerValue, setValue];
46};
47
48export default useHashParam;