Skip to content

Commit d2d3954

Browse files
feat: implement setActive
1 parent 956db2f commit d2d3954

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/webview/SearchSidebar/SearchResultList/MatchList.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CodeBlock } from './CodeBlock'
22
import { MatchActions } from './Actions'
33
import type { DisplayResult } from '../../../types'
44
import { useHover } from 'react-use'
5+
import { useActiveItem } from './useListState'
56

67
import { memo } from 'react'
78
import * as stylex from '@stylexjs/stylex'
@@ -18,9 +19,10 @@ const styles = stylex.create({
1819
})
1920

2021
function OneMatch({ match }: { match: DisplayResult }) {
22+
const [_active, setActive] = useActiveItem(match)
2123
const [hoverable] = useHover(hovered => {
2224
return (
23-
<li {...stylex.props(styles.codeItem)}>
25+
<li {...stylex.props(styles.codeItem)} onClick={setActive}>
2426
<CodeBlock match={match} />
2527
{hovered && <MatchActions match={match} />}
2628
</li>

src/webview/SearchSidebar/SearchResultList/useListState.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { useCallback, useEffect, useRef } from 'react'
55
import { useBoolean } from 'react-use'
66
import { onResultChange, findIndex } from '../../hooks/useSearch'
7+
import type { DisplayResult } from '../../../types'
78
import type { VirtuosoHandle } from 'react-virtuoso'
89

910
let ref: VirtuosoHandle
@@ -17,6 +18,7 @@ const collapseMap = new Map<string, boolean>()
1718
onResultChange(() => {
1819
collapseMap.clear()
1920
lastActiveFile = ''
21+
activeItem = null
2022
})
2123

2224
export function useToggleResult(filePath: string) {
@@ -72,8 +74,30 @@ export function useStickyShadow(filePath: string) {
7274
}
7375
}
7476

75-
// let activeItem: DisplayResult | null = null
77+
// Display for one match, Array for active header
78+
type ActiveItem = DisplayResult | DisplayResult[]
7679

77-
// function setActiveItem(match: DisplayResult) {
80+
let activeItem: ActiveItem | null = null
81+
let refreshers: WeakMap<ActiveItem, (b: boolean) => void> = new WeakMap()
7882

79-
// }
83+
function setActive(item: ActiveItem) {
84+
if (activeItem) {
85+
refreshers.get(activeItem)?.(false)
86+
}
87+
refreshers.get(item)?.(true)
88+
activeItem = item
89+
}
90+
91+
export function useActiveItem(item: ActiveItem) {
92+
const [active, forceUpdate] = useBoolean(activeItem === item)
93+
useEffect(() => {
94+
refreshers.set(item, forceUpdate)
95+
return () => {
96+
refreshers.delete(item)
97+
}
98+
}, [item])
99+
const set = useCallback(() => {
100+
setActive(item)
101+
}, [item])
102+
return [active, set] as const
103+
}

0 commit comments

Comments
 (0)