Skip to content

Commit 273e8ac

Browse files
committed
feat: Implement AutoApprove settings and menu
1 parent 629aee0 commit 273e8ac

File tree

7 files changed

+855
-88
lines changed

7 files changed

+855
-88
lines changed

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

Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { vscode } from "@src/utils/vscode"
66
import { useExtensionState } from "@src/context/ExtensionStateContext"
77
import { useAppTranslation } from "@src/i18n/TranslationContext"
88
import { AutoApproveToggle, AutoApproveSetting, autoApproveSettingsConfig } from "../settings/AutoApproveToggle"
9+
import { useAutoApproveState } from "@src/hooks/useAutoApproveState"
910

1011
interface AutoApproveMenuProps {
1112
style?: React.CSSProperties
@@ -39,74 +40,6 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => {
3940

4041
const { t } = useAppTranslation()
4142

42-
const onAutoApproveToggle = useCallback(
43-
(key: AutoApproveSetting, value: boolean) => {
44-
vscode.postMessage({ type: key, bool: value })
45-
46-
// Update the specific setting
47-
switch (key) {
48-
case "alwaysAllowReadOnly":
49-
setAlwaysAllowReadOnly(value)
50-
break
51-
case "alwaysAllowWrite":
52-
setAlwaysAllowWrite(value)
53-
break
54-
case "alwaysAllowExecute":
55-
setAlwaysAllowExecute(value)
56-
break
57-
case "alwaysAllowBrowser":
58-
setAlwaysAllowBrowser(value)
59-
break
60-
case "alwaysAllowMcp":
61-
setAlwaysAllowMcp(value)
62-
break
63-
case "alwaysAllowModeSwitch":
64-
setAlwaysAllowModeSwitch(value)
65-
break
66-
case "alwaysAllowSubtasks":
67-
setAlwaysAllowSubtasks(value)
68-
break
69-
case "alwaysApproveResubmit":
70-
setAlwaysApproveResubmit(value)
71-
break
72-
}
73-
74-
// After updating the specific setting, check if any action is now enabled.
75-
// If so, ensure autoApprovalEnabled is true.
76-
// This needs to be done after the state updates, so we'll use a temporary check
77-
// or re-evaluate the `toggles` object.
78-
// For simplicity, we'll assume the `toggles` state will reflect the change
79-
// in the next render cycle, and we can force autoApprovalEnabled to true
80-
// if any action is being enabled.
81-
if (value === true) {
82-
setAutoApprovalEnabled(true)
83-
vscode.postMessage({ type: "autoApprovalEnabled", bool: true })
84-
} else {
85-
// If an action is being disabled, check if all are now disabled.
86-
// If so, set autoApprovalEnabled to false.
87-
// This requires re-evaluating the state of all toggles *after* the current one is set.
88-
// A more robust solution would involve passing the updated `toggles` object
89-
// or re-calculating `hasAnyAutoApprovedAction` here.
90-
// For now, let's rely on the `hasAnyAutoApprovedAction` memoized value
91-
// which will update on the next render.
92-
// If the user unchecks the last enabled option, autoApprovalEnabled should become false.
93-
// This logic is already handled by the main checkbox's disabled state in the collapsed view.
94-
// So, we only need to ensure it turns ON when an individual is turned ON.
95-
}
96-
},
97-
[
98-
setAlwaysAllowReadOnly,
99-
setAlwaysAllowWrite,
100-
setAlwaysAllowExecute,
101-
setAlwaysAllowBrowser,
102-
setAlwaysAllowMcp,
103-
setAlwaysAllowModeSwitch,
104-
setAlwaysAllowSubtasks,
105-
setAlwaysApproveResubmit,
106-
setAutoApprovalEnabled, // Added setAutoApprovalEnabled to dependencies
107-
],
108-
)
109-
11043
const toggleExpanded = useCallback(() => setIsExpanded((prev) => !prev), [])
11144

11245
const toggles = useMemo(
@@ -132,17 +65,31 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => {
13265
],
13366
)
13467

135-
const hasAnyAutoApprovedAction = useMemo(() => Object.values(toggles).some((value) => !!value), [toggles])
68+
// Use the centralized auto-approve state hook
69+
const { hasAnyAutoApprovedAction, updateAutoApprovalState, handleMasterToggle } = useAutoApproveState({
70+
toggles,
71+
setters: {
72+
setAlwaysAllowReadOnly,
73+
setAlwaysAllowWrite,
74+
setAlwaysAllowExecute,
75+
setAlwaysAllowBrowser,
76+
setAlwaysAllowMcp,
77+
setAlwaysAllowModeSwitch,
78+
setAlwaysAllowSubtasks,
79+
setAlwaysApproveResubmit,
80+
setAutoApprovalEnabled,
81+
},
82+
})
13683

13784
const displayedAutoApproveText = useMemo(() => {
138-
if (autoApprovalEnabled && hasAnyAutoApprovedAction) {
85+
if (hasAnyAutoApprovedAction) {
13986
return Object.entries(toggles)
14087
.filter(([_key, value]) => !!value)
14188
.map(([key]) => t(autoApproveSettingsConfig[key as AutoApproveSetting].labelKey))
14289
.join(", ")
14390
}
14491
return t("chat:autoApprove.none")
145-
}, [autoApprovalEnabled, hasAnyAutoApprovedAction, toggles, t])
92+
}, [hasAnyAutoApprovedAction, toggles, t])
14693

14794
const handleOpenSettings = useCallback(
14895
() =>
@@ -172,13 +119,9 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => {
172119
onClick={toggleExpanded}>
173120
<div onClick={(e) => e.stopPropagation()}>
174121
<VSCodeCheckbox
175-
checked={autoApprovalEnabled && hasAnyAutoApprovedAction}
176-
disabled={!hasAnyAutoApprovedAction}
177-
onChange={() => {
178-
const newValue = !(autoApprovalEnabled && hasAnyAutoApprovedAction)
179-
setAutoApprovalEnabled(newValue)
180-
vscode.postMessage({ type: "autoApprovalEnabled", bool: newValue })
181-
}}
122+
checked={hasAnyAutoApprovedAction}
123+
disabled={false}
124+
onChange={() => handleMasterToggle()}
182125
/>
183126
</div>
184127
<div
@@ -234,7 +177,7 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => {
234177

235178
<AutoApproveToggle
236179
{...toggles}
237-
onToggle={onAutoApproveToggle}
180+
onToggle={updateAutoApprovalState}
238181
isOverallApprovalEnabled={autoApprovalEnabled}
239182
/>
240183

0 commit comments

Comments
 (0)