Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit a214c91

Browse files
committed
Debug
1 parent cdfb1f5 commit a214c91

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/components/blocks/Accordion.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import React, { useEffect, useMemo, useState } from 'react'
1+
import React, { useMemo } from 'react'
22

33
import styled from 'styled-components'
44
import { AccordionSingle } from '@ecmwf-projects/cads-ui-library'
55

66
import { GenerateBlocks } from '../GenerateBlocks'
77

88
import type { AccordionBlock } from '../../models'
9+
import { useHash } from 'src/hooks/useHash'
910

1011
export const Accordion = ({ block }: { block: AccordionBlock }) => {
1112

1213

1314
// Get the current hash
14-
const [hash, setHash] = useState<string>()
15-
useEffect(() => {
16-
setHash(window.location.hash)
17-
}, [])
15+
const hash = useHash();
16+
17+
console.log('hash', hash)
18+
console.log('Block id', block.id)
1819

1920
// Check if the hash is the same as the block id
2021
const isHash = hash === `#${block.id}`
@@ -26,6 +27,7 @@ export const Accordion = ({ block }: { block: AccordionBlock }) => {
2627
}
2728
return block.collapsed ? '' : block.title
2829
}, [isHash, block.title, block.collapsed])
30+
console.log('Block id', block.id, defaultValue)
2931

3032
return (<AccordionSingle
3133
rootProps={{

src/hooks/useHash.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useCallback, useEffect, useState } from 'react'
2+
3+
export const useHash = () => {
4+
const [hash, setHash] = useState(() => window.location.hash)
5+
6+
const hashChangeHandler = useCallback(() => {
7+
setHash(window.location.hash)
8+
}, [])
9+
10+
useEffect(() => {
11+
window.addEventListener('hashchange', hashChangeHandler)
12+
return () => {
13+
window.removeEventListener('hashchange', hashChangeHandler)
14+
}
15+
}, [])
16+
17+
return hash
18+
}

0 commit comments

Comments
 (0)