@@ -8,6 +8,7 @@ import { useRooPortal } from "@/components/ui/hooks/useRooPortal"
88import { Popover , PopoverContent , PopoverTrigger , StandardTooltip } from "@/components/ui"
99import { vscode } from "@/utils/vscode"
1010import { Button } from "@/components/ui"
11+ import { useExtensionState } from "@/context/ExtensionStateContext"
1112
1213import { IconButton } from "./IconButton"
1314
@@ -29,10 +30,6 @@ interface ApiConfigSelectorProps {
2930 listApiConfigMeta : Array < { id : string ; name : string ; modelId ?: string } >
3031 pinnedApiConfigs ?: Record < string , boolean >
3132 togglePinnedApiConfig : ( id : string ) => void
32- sortMode ?: SortMode
33- customOrder ?: string [ ]
34- onSortModeChange ?: ( mode : SortMode ) => void
35- onCustomOrderChange ?: ( order : string [ ] ) => void
3633}
3734
3835export const ApiConfigSelector = ( {
@@ -45,10 +42,9 @@ export const ApiConfigSelector = ({
4542 listApiConfigMeta,
4643 pinnedApiConfigs,
4744 togglePinnedApiConfig,
48- sortMode : externalSortMode ,
49- customOrder : externalCustomOrder = [ ] ,
5045} : ApiConfigSelectorProps ) => {
5146 const { t } = useTranslation ( )
47+ const { apiConfigCustomOrder : persistedCustomOrder } = useExtensionState ( )
5248 const [ open , setOpen ] = useState ( false )
5349 const [ searchValue , setSearchValue ] = useState ( "" )
5450 const [ isReorderMode , setIsReorderMode ] = useState ( false )
@@ -59,12 +55,12 @@ export const ApiConfigSelector = ({
5955 } )
6056
6157 // Internal state for sort mode and custom order when parent doesn't provide them
62- const [ internalSortMode , setInternalSortMode ] = useState < SortMode > ( "alphabetical" )
58+ const [ sortMode , setSortMode ] = useState < SortMode > ( "alphabetical" )
6359 const [ internalCustomOrder , setInternalCustomOrder ] = useState < string [ ] > ( [ ] )
6460
65- // Use external props if provided, otherwise use internal state
66- const sortMode = externalSortMode ?? internalSortMode
67- const customOrder = externalCustomOrder . length > 0 ? externalCustomOrder : internalCustomOrder
61+ // Use persisted state then internal state as fallback
62+ const customOrder =
63+ persistedCustomOrder && persistedCustomOrder . length > 0 ? persistedCustomOrder : internalCustomOrder
6864
6965 const portalContainer = useRooPortal ( "roo-portal" )
7066 const scrollContainerRef = useRef < HTMLDivElement | null > ( null )
@@ -78,13 +74,13 @@ export const ApiConfigSelector = ({
7874 sorted . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
7975 break
8076 case "custom" :
81- if ( customOrder . length > 0 ) {
77+ if ( customOrder && customOrder . length > 0 ) {
8278 // Sort by custom order, with unordered items at the end
83- const orderMap = new Map ( customOrder . map ( ( id , index ) => [ id , index ] ) )
79+ const orderMap = new Map ( customOrder . map ( ( id : string , index : number ) => [ id , index ] ) )
8480 sorted . sort ( ( a , b ) => {
8581 const aIndex = orderMap . get ( a . id ) ?? Number . MAX_SAFE_INTEGER
8682 const bIndex = orderMap . get ( b . id ) ?? Number . MAX_SAFE_INTEGER
87- return aIndex - bIndex
83+ return ( aIndex as number ) - ( bIndex as number )
8884 } )
8985 }
9086 break
@@ -132,7 +128,7 @@ export const ApiConfigSelector = ({
132128
133129 const handleSortModeChange = useCallback ( ( mode : SortMode ) => {
134130 // Always update internal state as fallback
135- setInternalSortMode ( mode )
131+ setSortMode ( mode )
136132
137133 if ( mode !== "custom" ) {
138134 setIsReorderMode ( false )
@@ -176,6 +172,12 @@ export const ApiConfigSelector = ({
176172 const newCustomOrder = newOrder . map ( ( config ) => config . id )
177173 // Always update internal state as fallback
178174 setInternalCustomOrder ( newCustomOrder )
175+
176+ // Persist the custom order to storage
177+ vscode . postMessage ( {
178+ type : "setApiConfigCustomOrder" ,
179+ values : { customOrder : newCustomOrder } ,
180+ } )
179181 } ,
180182 [ filteredConfigs ] ,
181183 )
0 commit comments