Skip to content

Commit 84da103

Browse files
committed
refactor: simplify api config reordering logic
1 parent dc58fab commit 84da103

File tree

10 files changed

+597
-498
lines changed

10 files changed

+597
-498
lines changed

packages/types/src/global-settings.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ export const globalSettingsSchema = z.object({
3737
currentApiConfigName: z.string().optional(),
3838
listApiConfigMeta: z.array(providerSettingsEntrySchema).optional(),
3939
pinnedApiConfigs: z.record(z.string(), z.boolean()).optional(),
40-
apiConfigCustomOrder: z.array(z.string()).optional(),
40+
apiConfigsCustomOrder: z
41+
.array(
42+
z.object({
43+
id: z.string(),
44+
index: z.number(),
45+
pinned: z.boolean(),
46+
}),
47+
)
48+
.optional(),
4149

4250
lastShownAnnouncementId: z.string().optional(),
4351
customInstructions: z.string().optional(),
@@ -251,7 +259,7 @@ export const EVALS_SETTINGS: RooCodeSettings = {
251259
lastShownAnnouncementId: "jul-09-2025-3-23-0",
252260

253261
pinnedApiConfigs: {},
254-
apiConfigCustomOrder: [],
262+
apiConfigsCustomOrder: [],
255263

256264
autoApprovalEnabled: true,
257265
alwaysAllowReadOnly: true,

src/core/webview/ClineProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,7 @@ export class ClineProvider
18141814
openRouterImageApiKey,
18151815
openRouterImageGenerationSelectedModel,
18161816
openRouterUseMiddleOutTransform,
1817-
apiConfigCustomOrder,
1817+
apiConfigsCustomOrder,
18181818
featureRoomoteControlEnabled,
18191819
} = await this.getState()
18201820

@@ -1901,7 +1901,7 @@ export class ClineProvider
19011901
currentApiConfigName: currentApiConfigName ?? "default",
19021902
listApiConfigMeta: listApiConfigMeta ?? [],
19031903
pinnedApiConfigs: pinnedApiConfigs ?? {},
1904-
apiConfigCustomOrder: apiConfigCustomOrder ?? [],
1904+
apiConfigsCustomOrder: apiConfigsCustomOrder ?? [],
19051905
mode: mode ?? defaultModeSlug,
19061906
customModePrompts: customModePrompts ?? {},
19071907
customSupportPrompts: customSupportPrompts ?? {},
@@ -2125,7 +2125,7 @@ export class ClineProvider
21252125
currentApiConfigName: stateValues.currentApiConfigName ?? "default",
21262126
listApiConfigMeta: stateValues.listApiConfigMeta ?? [],
21272127
pinnedApiConfigs: stateValues.pinnedApiConfigs ?? {},
2128-
apiConfigCustomOrder: stateValues.apiConfigCustomOrder ?? [],
2128+
apiConfigsCustomOrder: stateValues.apiConfigsCustomOrder ?? [],
21292129
modeApiConfigs: stateValues.modeApiConfigs ?? ({} as Record<Mode, string>),
21302130
customModePrompts: stateValues.customModePrompts ?? {},
21312131
customSupportPrompts: stateValues.customSupportPrompts ?? {},

src/core/webview/webviewMessageHandler.ts

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,45 +1636,80 @@ export const webviewMessageHandler = async (
16361636
await provider.postStateToWebview()
16371637
}
16381638
break
1639-
case "setApiConfigCustomOrder":
1639+
case "setApiConfigsCustomOrder":
16401640
if (message.values && Array.isArray(message.values.customOrder)) {
1641-
await updateGlobalState("apiConfigCustomOrder", message.values.customOrder)
1642-
await provider.postStateToWebview()
1641+
// Validate that each item has the required structure
1642+
const isValidOrder = message.values.customOrder.every(
1643+
(item: any) =>
1644+
typeof item === "object" &&
1645+
typeof item.id === "string" &&
1646+
typeof item.index === "number" &&
1647+
typeof item.pinned === "boolean",
1648+
)
1649+
1650+
if (isValidOrder) {
1651+
await updateGlobalState("apiConfigsCustomOrder", message.values.customOrder)
1652+
await provider.postStateToWebview()
1653+
}
16431654
}
16441655
break
16451656
case "moveApiConfigUp":
16461657
if (message.text) {
1647-
const currentOrder = getGlobalState("apiConfigCustomOrder") || []
1648-
const currentIndex = currentOrder.indexOf(message.text)
1649-
1650-
if (currentIndex > 0) {
1651-
// Swap with previous item
1652-
const newOrder = [...currentOrder]
1653-
;[newOrder[currentIndex - 1], newOrder[currentIndex]] = [
1654-
newOrder[currentIndex],
1655-
newOrder[currentIndex - 1],
1656-
]
1657-
1658-
await updateGlobalState("apiConfigCustomOrder", newOrder)
1659-
await provider.postStateToWebview()
1658+
const currentOrder = getGlobalState("apiConfigsCustomOrder") || []
1659+
const pinnedApiConfigs = getGlobalState("pinnedApiConfigs") || {}
1660+
const isPinned = !!pinnedApiConfigs[message.text]
1661+
1662+
// Find the item in the current order
1663+
const itemIndex = currentOrder.findIndex((item: any) => item.id === message.text)
1664+
1665+
if (itemIndex > 0) {
1666+
// Find previous item with same pinned status
1667+
const sameTypeItems = currentOrder.filter((item: any) => item.pinned === isPinned)
1668+
const currentItemInType = sameTypeItems.findIndex((item: any) => item.id === message.text)
1669+
1670+
if (currentItemInType > 0) {
1671+
const newOrder = [...currentOrder]
1672+
const prevItem = sameTypeItems[currentItemInType - 1]
1673+
const prevItemIndex = currentOrder.findIndex((item: any) => item.id === prevItem.id)
1674+
1675+
// Swap indices
1676+
const tempIndex = newOrder[itemIndex].index
1677+
newOrder[itemIndex].index = newOrder[prevItemIndex].index
1678+
newOrder[prevItemIndex].index = tempIndex
1679+
1680+
await updateGlobalState("apiConfigsCustomOrder", newOrder)
1681+
await provider.postStateToWebview()
1682+
}
16601683
}
16611684
}
16621685
break
16631686
case "moveApiConfigDown":
16641687
if (message.text) {
1665-
const currentOrder = getGlobalState("apiConfigCustomOrder") || []
1666-
const currentIndex = currentOrder.indexOf(message.text)
1667-
1668-
if (currentIndex < currentOrder.length - 1) {
1669-
// Swap with next item
1670-
const newOrder = [...currentOrder]
1671-
;[newOrder[currentIndex], newOrder[currentIndex + 1]] = [
1672-
newOrder[currentIndex + 1],
1673-
newOrder[currentIndex],
1674-
]
1675-
1676-
await updateGlobalState("apiConfigCustomOrder", newOrder)
1677-
await provider.postStateToWebview()
1688+
const currentOrder = getGlobalState("apiConfigsCustomOrder") || []
1689+
const pinnedApiConfigs = getGlobalState("pinnedApiConfigs") || {}
1690+
const isPinned = !!pinnedApiConfigs[message.text]
1691+
1692+
// Find the item in the current order
1693+
const itemIndex = currentOrder.findIndex((item: any) => item.id === message.text)
1694+
1695+
if (itemIndex !== -1 && itemIndex < currentOrder.length - 1) {
1696+
// Find next item with same pinned status
1697+
const sameTypeItems = currentOrder.filter((item: any) => item.pinned === isPinned)
1698+
const currentItemInType = sameTypeItems.findIndex((item: any) => item.id === message.text)
1699+
1700+
if (currentItemInType < sameTypeItems.length - 1) {
1701+
const newOrder = [...currentOrder]
1702+
const nextItem = sameTypeItems[currentItemInType + 1]
1703+
const nextItemIndex = currentOrder.findIndex((item: any) => item.id === nextItem.id)
1704+
1705+
// Swap indices
1706+
const tempIndex = newOrder[itemIndex].index
1707+
newOrder[itemIndex].index = newOrder[nextItemIndex].index
1708+
newOrder[nextItemIndex].index = tempIndex
1709+
1710+
await updateGlobalState("apiConfigsCustomOrder", newOrder)
1711+
await provider.postStateToWebview()
1712+
}
16781713
}
16791714
}
16801715
break

src/shared/ExtensionMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export type ExtensionState = Pick<
214214
| "currentApiConfigName"
215215
| "listApiConfigMeta"
216216
| "pinnedApiConfigs"
217-
| "apiConfigCustomOrder"
217+
| "apiConfigsCustomOrder"
218218
// | "lastShownAnnouncementId"
219219
| "customInstructions"
220220
// | "taskHistory" // Optional in GlobalSettings, required here.

src/shared/WebviewMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export interface WebviewMessage {
177177
| "maxDiagnosticMessages"
178178
| "searchFiles"
179179
| "toggleApiConfigPin"
180-
| "setApiConfigCustomOrder"
180+
| "setApiConfigsCustomOrder"
181181
| "moveApiConfigUp"
182182
| "moveApiConfigDown"
183183
| "setHistoryPreviewCollapsed"

webview-ui/src/components/chat/ApiConfigSelector.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const ApiConfigSelector = ({
3737
togglePinnedApiConfig,
3838
}: ApiConfigSelectorProps) => {
3939
const { t } = useTranslation()
40-
const { apiConfigCustomOrder: customOrder = [] } = useExtensionState()
40+
const { apiConfigsCustomOrder: customOrder = [] } = useExtensionState()
4141
const [open, setOpen] = useState(false)
4242
const [searchValue, setSearchValue] = useState("")
4343
const [sortMode, setSortMode] = useState<SortMode>("alphabetical")

0 commit comments

Comments
 (0)