Skip to content

Commit 90edab8

Browse files
committed
Refactor: re-use useHash, fix types
1 parent eecf428 commit 90edab8

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

src/app/errors/[errorCode]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function generateStaticParams(): Promise<
3636

3737
export async function generateMetadata(
3838
{params}: ErrorDecoderPageProps,
39-
parent?: ResolvingMetadata
39+
parent: Promise<ResolvingMetadata | undefined>
4040
): Promise<Metadata> {
4141
const {errorCode} = await params;
4242
const resolvedParent = await parent;

src/components/MDX/Challenges/Challenges.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {H2} from 'components/MDX/Heading';
1616
import {H4} from 'components/MDX/Heading';
1717
import {Challenge} from './Challenge';
1818
import {Navigation} from './Navigation';
19-
import {useRouter} from 'next/compat/router';
19+
import {useHash} from '../../../hooks/useHash';
2020

2121
interface ChallengesProps {
2222
children: React.ReactElement[];
@@ -97,12 +97,12 @@ export function Challenges({
9797
const queuedScrollRef = useRef<undefined | QueuedScroll>(QueuedScroll.INIT);
9898
const [activeIndex, setActiveIndex] = useState(0);
9999
const currentChallenge = challenges[activeIndex];
100-
const {asPath} = useRouter();
100+
const hash = useHash();
101101

102102
useEffect(() => {
103103
if (queuedScrollRef.current === QueuedScroll.INIT) {
104104
const initIndex = challenges.findIndex(
105-
(challenge) => challenge.id === asPath.split('#')[1]
105+
(challenge) => challenge.id === hash
106106
);
107107
if (initIndex === -1) {
108108
queuedScrollRef.current = undefined;

src/components/MDX/ExpandableExample.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,8 @@ import {IconDeepDive} from '../Icon/IconDeepDive';
1818
import {IconCodeBlock} from '../Icon/IconCodeBlock';
1919
import {Button} from '../Button';
2020
import {H4} from './Heading';
21-
import {useEffect, useRef, useState, useSyncExternalStore} from 'react';
22-
23-
function useHash() {
24-
return useSyncExternalStore(
25-
(callback) => {
26-
window.addEventListener('hashchange', callback);
27-
return () => {
28-
window.removeEventListener('hashchange', callback);
29-
};
30-
},
31-
() => window.location.hash,
32-
() => null
33-
);
34-
}
21+
import {useEffect, useRef, useState} from 'react';
22+
import {useHash} from '../../hooks/useHash';
3523

3624
interface ExpandableExampleProps {
3725
children: React.ReactNode;

src/hooks/useHash.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {useSyncExternalStore} from 'react';
2+
3+
export function useHash() {
4+
return useSyncExternalStore(
5+
(onChange) => {
6+
window.addEventListener('hashchange', onChange);
7+
return () => {
8+
window.removeEventListener('hashchange', onChange);
9+
};
10+
},
11+
() => window.location.hash,
12+
() => null
13+
);
14+
}

0 commit comments

Comments
 (0)