Skip to content

Commit 74e37d1

Browse files
fix: put selected repos at the top of the repo selector list. Aslo retain scroll position
1 parent 1cc2381 commit 74e37d1

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

packages/web/src/features/chat/components/chatBox/repoSelector.tsx

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export const RepoSelector = React.forwardRef<
4949
},
5050
ref
5151
) => {
52+
const scrollContainerRef = React.useRef<HTMLDivElement>(null);
53+
const scrollPosition = React.useRef<number>(0);
54+
5255
const handleInputKeyDown = (
5356
event: React.KeyboardEvent<HTMLInputElement>
5457
) => {
@@ -62,6 +65,11 @@ export const RepoSelector = React.forwardRef<
6265
};
6366

6467
const toggleRepo = (repo: string) => {
68+
// Store current scroll position before state update
69+
if (scrollContainerRef.current) {
70+
scrollPosition.current = scrollContainerRef.current.scrollTop;
71+
}
72+
6573
const newSelectedValues = selectedRepos.includes(repo)
6674
? selectedRepos.filter((value) => value !== repo)
6775
: [...selectedRepos, repo];
@@ -76,6 +84,26 @@ export const RepoSelector = React.forwardRef<
7684
onOpenChanged(!isOpen);
7785
};
7886

87+
const sortedRepos = React.useMemo(() => {
88+
return repos
89+
.map((repo) => ({
90+
repo,
91+
isSelected: selectedRepos.includes(repo)
92+
}))
93+
.sort((a, b) => {
94+
if (a.isSelected && !b.isSelected) return -1;
95+
if (!a.isSelected && b.isSelected) return 1;
96+
return 0;
97+
})
98+
}, [repos, selectedRepos]);
99+
100+
// Restore scroll position after re-render
101+
React.useEffect(() => {
102+
if (scrollContainerRef.current && scrollPosition.current > 0) {
103+
scrollContainerRef.current.scrollTop = scrollPosition.current;
104+
}
105+
}, [sortedRepos]);
106+
79107
return (
80108
<Popover
81109
open={isOpen}
@@ -116,12 +144,10 @@ export const RepoSelector = React.forwardRef<
116144
placeholder="Search repos..."
117145
onKeyDown={handleInputKeyDown}
118146
/>
119-
<CommandList>
147+
<CommandList ref={scrollContainerRef}>
120148
<CommandEmpty>No results found.</CommandEmpty>
121149
<CommandGroup>
122-
123-
{repos.map((repo) => {
124-
const isSelected = selectedRepos.includes(repo);
150+
{sortedRepos.map(({ repo, isSelected }) => {
125151
return (
126152
<CommandItem
127153
key={repo}
@@ -143,18 +169,18 @@ export const RepoSelector = React.forwardRef<
143169
);
144170
})}
145171
</CommandGroup>
146-
{selectedRepos.length > 0 && (
147-
<>
148-
<CommandSeparator />
149-
<CommandItem
150-
onSelect={handleClear}
151-
className="flex-1 justify-center cursor-pointer"
152-
>
153-
Clear
154-
</CommandItem>
155-
</>
156-
)}
157172
</CommandList>
173+
{selectedRepos.length > 0 && (
174+
<>
175+
<CommandSeparator />
176+
<CommandItem
177+
onSelect={handleClear}
178+
className="flex-1 justify-center cursor-pointer"
179+
>
180+
Clear
181+
</CommandItem>
182+
</>
183+
)}
158184
</Command>
159185
</PopoverContent>
160186
</Popover>

0 commit comments

Comments
 (0)