Skip to content

Commit 5188ed1

Browse files
author
Chris Hasson
committed
The MaxRequestsInput component was extracted from AutoApproveMenu.tsx into its own file (MaxRequestsInput.tsx) to improve reusability and maintainability. This component is now used in both AutoApproveMenu.tsx and AutoApproveSettings.tsx to manage the auto-approve API request limit.
Previously this option was only available in the Chat Approval popup menu
1 parent 0b0bccd commit 5188ed1

File tree

4 files changed

+24
-91
lines changed

4 files changed

+24
-91
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => {
214214
<MaxRequestsInput
215215
allowedMaxRequests={allowedMaxRequests ?? undefined}
216216
onValueChange={(value) => setAllowedMaxRequests(value)}
217-
variant="menu"
218217
/>
219218
{/* kilocode_change end */}
220219
</div>

webview-ui/src/components/settings/AutoApproveSettings.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ export const AutoApproveSettings = ({
131131
<MaxRequestsInput
132132
allowedMaxRequests={allowedMaxRequests}
133133
onValueChange={(value) => setCachedStateField("allowedMaxRequests", value)}
134-
variant="settings"
135-
testId="max-requests-input"
136134
/>
137135
{/* kilocode_change end */}
138136

webview-ui/src/components/settings/MaxRequestsInput.tsx

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,34 @@
11
// kilocode_change - new file
22
import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
3-
import { Trans, useTranslation } from "react-i18next"
3+
import { useTranslation } from "react-i18next"
44
import { vscode } from "@/utils/vscode"
5+
import { useCallback } from "react"
56

67
interface MaxRequestsInputProps {
78
allowedMaxRequests?: number | undefined
89
onValueChange: (value: number | undefined) => void
9-
variant?: "menu" | "settings"
1010
className?: string
11-
style?: React.CSSProperties
12-
testId?: string
1311
}
1412

15-
export function MaxRequestsInput({
16-
allowedMaxRequests,
17-
onValueChange,
18-
variant = "settings",
19-
className,
20-
style,
21-
testId,
22-
}: MaxRequestsInputProps) {
13+
export function MaxRequestsInput({ allowedMaxRequests, onValueChange, className }: MaxRequestsInputProps) {
2314
const { t } = useTranslation()
2415

25-
const handleInput = (e: any) => {
26-
const input = e.target as HTMLInputElement
27-
input.value = input.value.replace(/[^0-9]/g, "")
28-
const value = parseInt(input.value)
29-
const parsedValue = !isNaN(value) && value > 0 ? value : undefined
30-
onValueChange(parsedValue)
31-
vscode.postMessage({ type: "allowedMaxRequests", value: parsedValue })
32-
}
16+
const handleInput = useCallback(
17+
(e: any) => {
18+
const input = e.target as HTMLInputElement
19+
input.value = input.value.replace(/[^0-9]/g, "")
20+
const value = parseInt(input.value)
21+
const parsedValue = !isNaN(value) && value > 0 ? value : undefined
22+
onValueChange(parsedValue)
23+
vscode.postMessage({ type: "allowedMaxRequests", value: parsedValue })
24+
},
25+
[onValueChange],
26+
)
3327

3428
const inputValue = (allowedMaxRequests ?? Infinity) === Infinity ? "" : allowedMaxRequests?.toString()
3529

36-
if (variant === "menu") {
37-
return (
38-
<>
39-
<div
40-
style={{
41-
display: "flex",
42-
alignItems: "center",
43-
gap: "8px",
44-
marginTop: "10px",
45-
marginBottom: "8px",
46-
color: "var(--vscode-descriptionForeground)",
47-
...style,
48-
}}
49-
className={className}>
50-
<span style={{ flexShrink: 1, minWidth: 0 }}>
51-
<Trans i18nKey="settings:autoApprove.apiRequestLimit.title" />:
52-
</span>
53-
<VSCodeTextField
54-
placeholder={t("settings:autoApprove.apiRequestLimit.unlimited")}
55-
value={inputValue}
56-
onInput={handleInput}
57-
style={{ flex: 1 }}
58-
data-testid={testId}
59-
/>
60-
</div>
61-
<div
62-
style={{
63-
color: "var(--vscode-descriptionForeground)",
64-
fontSize: "12px",
65-
marginBottom: "10px",
66-
}}>
67-
<Trans i18nKey="settings:autoApprove.apiRequestLimit.description" />
68-
</div>
69-
</>
70-
)
71-
}
72-
7330
return (
74-
<div
75-
className={`flex flex-col gap-3 pl-3 border-l-2 border-vscode-button-background ${className || ""}`}
76-
style={style}>
31+
<div className={`flex flex-col gap-3 pl-3 border-l-2 border-vscode-button-background ${className || ""}`}>
7732
<div className="flex items-center gap-4 font-bold">
7833
<span className="codicon codicon-pulse" />
7934
<div>{t("settings:autoApprove.apiRequestLimit.title")}</div>
@@ -84,7 +39,7 @@ export function MaxRequestsInput({
8439
value={inputValue}
8540
onInput={handleInput}
8641
style={{ flex: 1, maxWidth: "200px" }}
87-
data-testid={testId || "max-requests-input"}
42+
data-testid="max-requests-input"
8843
/>
8944
</div>
9045
<div className="text-vscode-descriptionForeground text-sm">

webview-ui/src/components/settings/__tests__/MaxRequestsInput.spec.tsx

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,20 @@ vi.mock("@/utils/vscode", () => ({
77
vscode: { postMessage: vi.fn() },
88
}))
99

10-
const translations: Record<string, string> = {
11-
"settings:autoApprove.apiRequestLimit.title": "Max API Requests",
12-
"settings:autoApprove.apiRequestLimit.unlimited": "Unlimited",
13-
"settings:autoApprove.apiRequestLimit.description": "Limit the number of API requests",
14-
}
1510
vi.mock("react-i18next", () => ({
16-
useTranslation: () => ({
17-
t: (key: string) => translations[key] || key,
18-
}),
19-
Trans: ({ i18nKey }: { i18nKey: string; children?: React.ReactNode }) => (
20-
<span>{translations[i18nKey] || i18nKey}</span>
21-
),
11+
useTranslation: () => {
12+
const translations: Record<string, string> = {
13+
"settings:autoApprove.apiRequestLimit.title": "Max API Requests",
14+
"settings:autoApprove.apiRequestLimit.unlimited": "Unlimited",
15+
"settings:autoApprove.apiRequestLimit.description": "Limit the number of API requests",
16+
}
17+
return { t: (key: string) => translations[key] || key }
18+
},
2219
}))
2320

2421
describe("MaxRequestsInput", () => {
2522
const mockOnValueChange = vi.fn()
2623

27-
it("renders with settings variant by default", () => {
28-
render(<MaxRequestsInput allowedMaxRequests={10} onValueChange={mockOnValueChange} />)
29-
30-
expect(screen.getByText("Max API Requests")).toBeInTheDocument()
31-
expect(screen.getByText("Limit the number of API requests")).toBeInTheDocument()
32-
expect(screen.getByDisplayValue("10")).toBeInTheDocument()
33-
})
34-
35-
it("renders with menu variant styling", () => {
36-
render(<MaxRequestsInput allowedMaxRequests={5} onValueChange={mockOnValueChange} variant="menu" />)
37-
38-
expect(screen.getByText("Max API Requests")).toBeInTheDocument()
39-
expect(screen.getByText("Limit the number of API requests")).toBeInTheDocument()
40-
expect(screen.getByDisplayValue("5")).toBeInTheDocument()
41-
})
42-
4324
it("shows empty input when allowedMaxRequests is undefined", () => {
4425
render(<MaxRequestsInput allowedMaxRequests={undefined} onValueChange={mockOnValueChange} />)
4526

0 commit comments

Comments
 (0)