All files / src/hooks useAutoApproveActions.ts

0% Statements 0/140
100% Branches 1/1
100% Functions 1/1
0% Lines 0/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173                                                                                                                                                                                                                                                                                                                                                         
import { AutoApprovalSettings } from "@shared/AutoApprovalSettings"
import { useCallback } from "react"
import { updateAutoApproveSettings } from "@/components/chat/auto-approve-menu/AutoApproveSettingsAPI"
import { ActionMetadata } from "@/components/chat/auto-approve-menu/types"
import { useExtensionState } from "@/context/ExtensionStateContext"
 
export function useAutoApproveActions() {
	const { autoApprovalSettings } = useExtensionState()
 
	// Check if action is enabled
	const isChecked = useCallback(
		(action: ActionMetadata): boolean => {
			switch (action.id) {
				case "enableAll":
					return Object.values(autoApprovalSettings.actions).every(Boolean)
				case "enableNotifications":
					return autoApprovalSettings.enableNotifications
				case "enableAutoApprove":
					return autoApprovalSettings.enabled
				default:
					return autoApprovalSettings.actions[action.id] ?? false
			}
		},
		[autoApprovalSettings],
	)
 
	// Check if action is favorited
	const isFavorited = useCallback(
		(action: ActionMetadata): boolean => {
			const favorites = autoApprovalSettings.favorites || []
			return favorites.includes(action.id)
		},
		[autoApprovalSettings.favorites],
	)
 
	// Toggle favorite status
	const toggleFavorite = useCallback(
		async (actionId: string) => {
			const currentFavorites = autoApprovalSettings.favorites || []
			let newFavorites: string[]
 
			if (currentFavorites.includes(actionId)) {
				newFavorites = currentFavorites.filter((id) => id !== actionId)
			} else {
				newFavorites = [...currentFavorites, actionId]
			}
 
			await updateAutoApproveSettings({
				...autoApprovalSettings,
				version: (autoApprovalSettings.version ?? 1) + 1,
				favorites: newFavorites,
			})
		},
		[autoApprovalSettings],
	)
 
	// Update action state
	const updateAction = useCallback(
		async (action: ActionMetadata, value: boolean) => {
			const actionId = action.id
			const subActionId = action.subAction?.id
 
			if (actionId === "enableAutoApprove") {
				await updateAutoApproveEnabled(value)
				return
			}
 
			if (actionId === "enableAll" || subActionId === "enableAll") {
				await toggleAll(action, value)
				return
			}
 
			if (actionId === "enableNotifications" || subActionId === "enableNotifications") {
				await updateNotifications(action, value)
				return
			}
 
			const newActions = {
				...autoApprovalSettings.actions,
				[actionId]: value,
			}
 
			if (value === false && subActionId) {
				// @ts-expect-error: TODO: See how we can fix this
				newActions[subActionId] = false
			}
 
			if (value === true && action.parentActionId) {
				newActions[action.parentActionId as keyof AutoApprovalSettings["actions"]] = true
			}
 
			// Check if this will result in any enabled actions
			const willHaveEnabledActions = Object.values(newActions).some(Boolean)
 
			await updateAutoApproveSettings({
				...autoApprovalSettings,
				version: (autoApprovalSettings.version ?? 1) + 1,
				actions: newActions,
				enabled: willHaveEnabledActions,
			})
		},
		[autoApprovalSettings],
	)
 
	// Update max requests
	const updateMaxRequests = useCallback(
		async (maxRequests: number) => {
			await updateAutoApproveSettings({
				...autoApprovalSettings,
				version: (autoApprovalSettings.version ?? 1) + 1,
				maxRequests,
			})
		},
		[autoApprovalSettings],
	)
 
	// Update auto-approve enabled state
	const updateAutoApproveEnabled = useCallback(
		async (checked: boolean) => {
			await updateAutoApproveSettings({
				...autoApprovalSettings,
				version: (autoApprovalSettings.version ?? 1) + 1,
				enabled: checked,
			})
		},
		[autoApprovalSettings],
	)
 
	// Toggle all actions
	const toggleAll = useCallback(
		async (_action: ActionMetadata, checked: boolean) => {
			const actions = { ...autoApprovalSettings.actions }
 
			for (const action of Object.keys(actions)) {
				actions[action as keyof AutoApprovalSettings["actions"]] = checked
			}
 
			await updateAutoApproveSettings({
				...autoApprovalSettings,
				version: (autoApprovalSettings.version ?? 1) + 1,
				actions,
				enabled: checked,
			})
		},
		[autoApprovalSettings],
	)
 
	// Update notifications setting
	const updateNotifications = useCallback(
		async (action: ActionMetadata, checked: boolean) => {
			if (action.id === "enableNotifications") {
				await updateAutoApproveSettings({
					...autoApprovalSettings,
					version: (autoApprovalSettings.version ?? 1) + 1,
					enableNotifications: checked,
				})
			}
		},
		[autoApprovalSettings],
	)
 
	return {
		isChecked,
		isFavorited,
		toggleFavorite,
		updateAction,
		updateMaxRequests,
		updateAutoApproveEnabled,
		toggleAll,
		updateNotifications,
	}
}