File size: 5,575 Bytes
6e38ce1 | 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | import React, { useCallback } from "react";
import { appContext } from "../../hooks/provider";
import SignInModal from "../signin";
import { useSettingsStore } from "../store";
import { settingsAPI } from "../views/api";
import GeneralSettings from "./tabs/GeneralSettings/GeneralSettings";
import AgentSettingsTab from "./tabs/agentSettings/AgentSettingsTab";
import AdvancedConfigEditor from "./tabs/advancedSetings/AdvancedSettings";
import {
Button,
Divider,
Flex,
message,
Modal,
Spin,
Tabs,
Typography,
} from "antd";
import { validateAll } from "./validation";
interface SettingsMenuProps {
isOpen: boolean;
onClose: () => void;
}
const SettingsModal: React.FC<SettingsMenuProps> = ({ isOpen, onClose }) => {
const { darkMode, setDarkMode, user } = React.useContext(appContext);
const [isEmailModalOpen, setIsEmailModalOpen] = React.useState(false);
const [hasChanges, setHasChanges] = React.useState(false);
const [isLoading, setIsLoading] = React.useState(false);
const [originalConfig, setOriginalConfig] = React.useState<any>(null);
const { config, updateConfig, resetToDefaults } = useSettingsStore();
React.useEffect(() => {
if (isOpen) {
setHasChanges(false);
setIsLoading(true);
// Load settings when modal opens
const loadSettings = async () => {
if (user?.email) {
try {
const settings = await settingsAPI.getSettings(user.email);
// Merge backend settings with frontend defaults
const { config: defaultConfig } = useSettingsStore.getState();
const mergedConfig = { ...defaultConfig, ...settings };
// Always use the merged config since we provide proper defaults
updateConfig(mergedConfig);
setOriginalConfig(mergedConfig);
} catch (error) {
message.error(
"Failed to load settings. Using defaults. Error: " + error
);
resetToDefaults();
setOriginalConfig(null);
}
}
setIsLoading(false);
};
loadSettings();
}
}, [isOpen, user?.email]);
const handleUpdateConfig = async (changes: any) => {
updateConfig(changes);
setHasChanges(true);
};
const handleResetDefaults = async () => {
resetToDefaults();
setHasChanges(true);
};
const handleClose = useCallback(async () => {
// Check all validation states before saving
const validationErrors = validateAll(config);
if (validationErrors.length > 0) {
const errors = validationErrors.join("\n");
message.error(errors);
return;
}
// Only save if there are actual changes
const hasActualChanges =
originalConfig &&
JSON.stringify(config) !== JSON.stringify(originalConfig);
if (hasActualChanges && user?.email) {
try {
await settingsAPI.updateSettings(user.email, config);
message.success("Updated settings!");
} catch (error) {
message.error("Failed to save settings");
console.error("Failed to save settings:", error);
return;
}
}
onClose();
}, [config, originalConfig, user?.email, onClose]);
const tabItems = {
general: {
label: "General",
children: (
<>
<Typography.Text strong>General Settings</Typography.Text>
<Divider />
<GeneralSettings
darkMode={darkMode}
setDarkMode={setDarkMode}
config={config}
handleUpdateConfig={handleUpdateConfig}
/>
</>
),
},
agents: {
label: "Agent Settings",
children: (
<>
<Typography.Text strong>Agent Settings</Typography.Text>
<Divider />
<AgentSettingsTab
config={config}
handleUpdateConfig={handleUpdateConfig}
/>
</>
),
},
advanced_config: {
label: "Advanced Settings",
children: (
<>
<Typography.Text strong>Advanced Settings</Typography.Text>
<Divider />
<AdvancedConfigEditor
config={config}
darkMode={darkMode}
handleUpdateConfig={handleUpdateConfig}
/>
</>
),
},
};
return (
<>
<Modal
open={isOpen}
style={{ maxHeight: 800, overflow: "auto" }}
onCancel={handleClose}
closable={true}
width={800}
height={800}
footer={[
<Flex gap="large" justify="start" align="center">
<Button
key="reset"
onClick={handleResetDefaults}
disabled={isLoading}
>
Reset to Defaults
</Button>
{hasChanges && (
<Typography.Text italic type="warning">
Warning: Settings changes will only apply when you create a new
session
</Typography.Text>
)}
</Flex>,
]}
>
{isLoading ? (
<Flex justify="center" align="center" style={{ height: "400px" }}>
<Spin size="large" />
</Flex>
) : (
<Tabs
tabPosition="left"
items={Object.entries(tabItems).map(
([key, { label, children }]) => ({ key, label, children })
)}
/>
)}
</Modal>
<SignInModal
isVisible={isEmailModalOpen}
onClose={() => setIsEmailModalOpen(false)}
/>
</>
);
};
export default SettingsModal;
|