| 1 | import { createSlice, PayloadAction } from "@reduxjs/toolkit"; |
| 2 | import { Tool } from "core"; |
| 3 | import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "core/tools/builtIn"; |
| 4 | import { |
| 5 | defaultOnboardingCardState, |
| 6 | OnboardingCardState, |
| 7 | } from "../../components/OnboardingCard"; |
| 8 | import { getLocalStorage, LocalStorageKey } from "../../util/localStorage"; |
| 9 | |
| 10 | export type ToolPolicy = |
| 11 | | "allowedWithPermission" |
| 12 | | "allowedWithoutPermission" |
| 13 | | "disabled"; |
| 14 | |
| 15 | export type ToolGroupPolicy = "include" | "exclude"; |
| 16 | |
| 17 | export type ToolPolicies = { [toolName: string]: ToolPolicy }; |
| 18 | export type ToolGroupPolicies = { [toolGroupName: string]: ToolGroupPolicy }; |
| 19 | |
| 20 | type UIState = { |
| 21 | showDialog: boolean; |
| 22 | dialogMessage: string | JSX.Element | undefined; |
| 23 | dialogEntryOn: boolean; |
| 24 | onboardingCard: OnboardingCardState; |
| 25 | isExploreDialogOpen: boolean; |
| 26 | hasDismissedExploreDialog: boolean; |
| 27 | shouldAddFileForEditing: boolean; |
| 28 | toolSettings: ToolPolicies; |
| 29 | toolGroupSettings: ToolGroupPolicies; |
| 30 | ttsActive: boolean; |
| 31 | }; |
| 32 | |
| 33 | export const DEFAULT_TOOL_SETTING: ToolPolicy = "allowedWithPermission"; |
| 34 | |
| 35 | export const uiSlice = createSlice({ |
| 36 | name: "ui", |
| 37 | initialState: { |
| 38 | showDialog: false, |
| 39 | dialogMessage: "", |
| 40 | dialogEntryOn: false, |
| 41 | onboardingCard: defaultOnboardingCardState, |
| 42 | isExploreDialogOpen: getLocalStorage(LocalStorageKey.IsExploreDialogOpen), |
| 43 | hasDismissedExploreDialog: getLocalStorage( |
| 44 | LocalStorageKey.HasDismissedExploreDialog, |
| 45 | ), |
| 46 | shouldAddFileForEditing: false, |
| 47 | ttsActive: false, |
| 48 | toolSettings: { |
| 49 | [BuiltInToolNames.ReadFile]: "allowedWithoutPermission", |
| 50 | [BuiltInToolNames.EditExistingFile]: "allowedWithPermission", |
| 51 | [BuiltInToolNames.CreateNewFile]: "allowedWithPermission", |
| 52 | [BuiltInToolNames.RunTerminalCommand]: "allowedWithPermission", |
| 53 | [BuiltInToolNames.GrepSearch]: "allowedWithoutPermission", |
| 54 | [BuiltInToolNames.FileGlobSearch]: "allowedWithoutPermission", |
| 55 | [BuiltInToolNames.SearchWeb]: "allowedWithoutPermission", |
| 56 | [BuiltInToolNames.ViewDiff]: "allowedWithoutPermission", |
| 57 | [BuiltInToolNames.LSTool]: "allowedWithoutPermission", |
| 58 | [BuiltInToolNames.CreateRuleBlock]: "allowedWithPermission", |
| 59 | }, |
| 60 | toolGroupSettings: { |
| 61 | [BUILT_IN_GROUP_NAME]: "include", |
| 62 | }, |
| 63 | } as UIState, |
| 64 | reducers: { |
| 65 | setOnboardingCard: ( |
| 66 | state, |
| 67 | action: PayloadAction<Partial<OnboardingCardState>>, |
| 68 | ) => { |
| 69 | state.onboardingCard = { ...state.onboardingCard, ...action.payload }; |
| 70 | }, |
| 71 | setDialogMessage: ( |
| 72 | state, |
| 73 | action: PayloadAction<UIState["dialogMessage"]>, |
| 74 | ) => { |
| 75 | state.dialogMessage = action.payload; |
| 76 | }, |
| 77 | setDialogEntryOn: ( |
| 78 | state, |
| 79 | action: PayloadAction<UIState["dialogEntryOn"]>, |
| 80 | ) => { |
| 81 | state.dialogEntryOn = action.payload; |
| 82 | }, |
| 83 | setShowDialog: (state, action: PayloadAction<UIState["showDialog"]>) => { |
| 84 | state.showDialog = action.payload; |
| 85 | }, |
| 86 | setIsExploreDialogOpen: ( |
| 87 | state, |
| 88 | action: PayloadAction<UIState[LocalStorageKey.IsExploreDialogOpen]>, |
| 89 | ) => { |
| 90 | state.isExploreDialogOpen = action.payload; |
| 91 | }, |
| 92 | setHasDismissedExploreDialog: (state, action: PayloadAction<boolean>) => { |
| 93 | state.hasDismissedExploreDialog = action.payload; |
| 94 | }, |
| 95 | // Tools |
| 96 | addTool: (state, action: PayloadAction<Tool>) => { |
| 97 | state.toolSettings[action.payload.function.name] = |
| 98 | "allowedWithPermission"; |
| 99 | }, |
| 100 | toggleToolSetting: (state, action: PayloadAction<string>) => { |
| 101 | const setting = state.toolSettings[action.payload]; |
| 102 | |
| 103 | switch (setting) { |
| 104 | case "allowedWithPermission": |
| 105 | state.toolSettings[action.payload] = "allowedWithoutPermission"; |
| 106 | break; |
| 107 | case "allowedWithoutPermission": |
| 108 | state.toolSettings[action.payload] = "disabled"; |
| 109 | break; |
| 110 | case "disabled": |
| 111 | state.toolSettings[action.payload] = "allowedWithPermission"; |
| 112 | break; |
| 113 | default: |
| 114 | state.toolSettings[action.payload] = DEFAULT_TOOL_SETTING; |
| 115 | break; |
| 116 | } |
| 117 | }, |
| 118 | toggleToolGroupSetting: (state, action: PayloadAction<string>) => { |
| 119 | const setting = state.toolGroupSettings[action.payload] ?? "include"; |
| 120 | |
| 121 | if (setting === "include") { |
| 122 | state.toolGroupSettings[action.payload] = "exclude"; |
| 123 | } else { |
| 124 | state.toolGroupSettings[action.payload] = "include"; |
| 125 | } |
| 126 | }, |
| 127 | setTTSActive: (state, { payload }: PayloadAction<boolean>) => { |
| 128 | state.ttsActive = payload; |
| 129 | }, |
| 130 | }, |
| 131 | }); |
| 132 | |
| 133 | export const { |
| 134 | setOnboardingCard, |
| 135 | setDialogMessage, |
| 136 | setDialogEntryOn, |
| 137 | setShowDialog, |
| 138 | setIsExploreDialogOpen, |
| 139 | setHasDismissedExploreDialog, |
| 140 | toggleToolSetting, |
| 141 | toggleToolGroupSetting, |
| 142 | addTool, |
| 143 | setTTSActive, |
| 144 | } = uiSlice.actions; |
| 145 | |
| 146 | export default uiSlice.reducer; |