@@ -49,6 +49,9 @@ export const RepoSelector = React.forwardRef<
49
49
} ,
50
50
ref
51
51
) => {
52
+ const scrollContainerRef = React . useRef < HTMLDivElement > ( null ) ;
53
+ const scrollPosition = React . useRef < number > ( 0 ) ;
54
+
52
55
const handleInputKeyDown = (
53
56
event : React . KeyboardEvent < HTMLInputElement >
54
57
) => {
@@ -62,6 +65,11 @@ export const RepoSelector = React.forwardRef<
62
65
} ;
63
66
64
67
const toggleRepo = ( repo : string ) => {
68
+ // Store current scroll position before state update
69
+ if ( scrollContainerRef . current ) {
70
+ scrollPosition . current = scrollContainerRef . current . scrollTop ;
71
+ }
72
+
65
73
const newSelectedValues = selectedRepos . includes ( repo )
66
74
? selectedRepos . filter ( ( value ) => value !== repo )
67
75
: [ ...selectedRepos , repo ] ;
@@ -76,6 +84,26 @@ export const RepoSelector = React.forwardRef<
76
84
onOpenChanged ( ! isOpen ) ;
77
85
} ;
78
86
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
+
79
107
return (
80
108
< Popover
81
109
open = { isOpen }
@@ -116,12 +144,10 @@ export const RepoSelector = React.forwardRef<
116
144
placeholder = "Search repos..."
117
145
onKeyDown = { handleInputKeyDown }
118
146
/>
119
- < CommandList >
147
+ < CommandList ref = { scrollContainerRef } >
120
148
< CommandEmpty > No results found.</ CommandEmpty >
121
149
< CommandGroup >
122
-
123
- { repos . map ( ( repo ) => {
124
- const isSelected = selectedRepos . includes ( repo ) ;
150
+ { sortedRepos . map ( ( { repo, isSelected } ) => {
125
151
return (
126
152
< CommandItem
127
153
key = { repo }
@@ -143,18 +169,18 @@ export const RepoSelector = React.forwardRef<
143
169
) ;
144
170
} ) }
145
171
</ 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
- ) }
157
172
</ 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
+ ) }
158
184
</ Command >
159
185
</ PopoverContent >
160
186
</ Popover >
0 commit comments