Skip to content

Commit 8c24302

Browse files
fix: keep toggle state for virtual scroll
1 parent 2970f28 commit 8c24302

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/webview/SearchSidebar/SearchResultList/TreeItem.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import TreeHeader from './TreeHeader'
33
import type { DisplayResult } from '../../../types'
44
import { MatchList } from './MatchList'
55
import { memo, useEffect, useRef } from 'react'
6+
import { useToggleResult } from './useListState'
67
import * as stylex from '@stylexjs/stylex'
78

89
const styles = stylex.create({
@@ -52,7 +53,7 @@ interface TreeItemProps {
5253
}
5354

5455
const TreeItem = ({ className, matches }: TreeItemProps) => {
55-
const [isExpanded, toggleIsExpanded] = useBoolean(true)
56+
const [isExpanded, toggleIsExpanded] = useToggleResult(matches[0].file)
5657
const { isScrolled, ref } = useStickyShadow()
5758
const props = stylex.props(styles.treeItem)
5859

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// maintains data list's UI state
2+
// e.g. toggle expand selected item
3+
4+
import { useCallback } from 'react'
5+
import { useBoolean } from 'react-use'
6+
7+
const collapseMap = new Map<string, boolean>()
8+
9+
export function useToggleResult(filePath: string) {
10+
const collapsedBefore = collapseMap.get(filePath)
11+
const [isExpanded, toggle] = useBoolean(!collapsedBefore)
12+
const toggleIsExpanded = useCallback(() => {
13+
toggleResult(filePath)
14+
toggle()
15+
}, [filePath, toggle])
16+
return [isExpanded, toggleIsExpanded] as const
17+
}
18+
19+
function toggleResult(filePath: string) {
20+
if (collapseMap.has(filePath)) {
21+
collapseMap.delete(filePath)
22+
} else {
23+
collapseMap.set(filePath, true)
24+
}
25+
}
26+
27+
// let activeItem: DisplayResult | null = null
28+
29+
// function setActiveItem(match: DisplayResult) {
30+
31+
// }

0 commit comments

Comments
 (0)