Skip to content

Commit 97b39de

Browse files
committed
feat(site,ui): add HashContext and HashProvider for global URL state
1 parent 0f7aec8 commit 97b39de

4 files changed

Lines changed: 78 additions & 3 deletions

File tree

‎apps/site/app/[locale]/layout.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getLocale } from 'next-intl/server';
66

77
import BaseLayout from '#site/layouts/Base';
88
import { IBM_PLEX_MONO, OPEN_SANS } from '#site/next.fonts';
9+
import { HashProvider } from '#site/providers/hashProvider';
910
import { ThemeProvider } from '#site/providers/themeProvider';
1011

1112
import type { FC, PropsWithChildren } from 'react';
@@ -29,9 +30,11 @@ const RootLayout: FC<PropsWithChildren> = async ({ children }) => {
2930
>
3031
<body suppressHydrationWarning>
3132
<NextIntlClientProvider>
32-
<ThemeProvider>
33-
<BaseLayout>{children}</BaseLayout>
34-
</ThemeProvider>
33+
<HashProvider>
34+
<ThemeProvider>
35+
<BaseLayout>{children}</BaseLayout>
36+
</ThemeProvider>
37+
</HashProvider>
3538
</NextIntlClientProvider>
3639

3740
<a

‎apps/site/hooks/useHash.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use client';
2+
3+
import { useSyncExternalStore } from 'react';
4+
5+
const listeners = new Set<() => void>();
6+
let isListening = false;
7+
8+
const onHashChange = () => {
9+
listeners.forEach(rerender => rerender());
10+
};
11+
12+
const subscribe = (callback: () => void) => {
13+
listeners.add(callback);
14+
if (!isListening && typeof window !== 'undefined') {
15+
window.addEventListener('hashchange', onHashChange);
16+
isListening = true;
17+
}
18+
return () => {
19+
listeners.delete(callback);
20+
if (listeners.size === 0 && typeof window !== 'undefined') {
21+
window.removeEventListener('hashchange', onHashChange);
22+
isListening = false;
23+
}
24+
};
25+
};
26+
27+
const getSnapshot = () =>
28+
typeof window !== 'undefined' ? window.location.hash.slice(1) : '';
29+
const getServerSnapshot = () => '';
30+
31+
const useHash = () => {
32+
const hash = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
33+
34+
const setHash = (newHash: string) => {
35+
if (typeof window !== 'undefined') {
36+
window.history.replaceState(null, '', `#${newHash}`);
37+
onHashChange();
38+
}
39+
};
40+
41+
return [hash, setHash] as const;
42+
};
43+
44+
export default useHash;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use client';
2+
3+
import { HashContext } from '@node-core/ui-components/contexts/HashContext';
4+
5+
import useHash from '#site/hooks/useHash';
6+
7+
import type { FC, PropsWithChildren } from 'react';
8+
9+
export const HashProvider: FC<PropsWithChildren> = ({ children }) => {
10+
const [hash, setHash] = useHash();
11+
12+
return <HashContext value={{ hash, setHash }}>{children}</HashContext>;
13+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use client';
2+
3+
import { createContext, use } from 'react';
4+
5+
type HashContextType = {
6+
hash: string;
7+
setHash: (newHash: string) => void;
8+
};
9+
10+
export const HashContext = createContext<HashContextType>({
11+
hash: '',
12+
setHash: () => {},
13+
});
14+
15+
export const useHashContext = () => use(HashContext);

0 commit comments

Comments
 (0)