Bug Fixes part1
This commit is contained in:
@@ -12,6 +12,13 @@ import {
|
||||
tabConfigurationStore,
|
||||
updateTabConfiguration as updateTabConfig,
|
||||
resetTabConfiguration as resetTabConfig,
|
||||
updateProviderSettings as updateProviderSettingsStore,
|
||||
updateLatestBranch,
|
||||
updateAutoSelectTemplate,
|
||||
updateContextOptimization,
|
||||
updateEventLogs,
|
||||
updateLocalModels,
|
||||
updatePromptId,
|
||||
} from '~/lib/stores/settings';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import Cookies from 'js-cookie';
|
||||
@@ -64,8 +71,13 @@ export interface UseSettingsReturn {
|
||||
resetTabConfiguration: () => void;
|
||||
}
|
||||
|
||||
// Add interface to match ProviderSetting type
|
||||
interface ProviderSettingWithIndex extends IProviderSetting {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export function useSettings(): UseSettingsReturn {
|
||||
const providers = useStore(providersStore) as Record<string, IProviderConfig>;
|
||||
const providers = useStore(providersStore);
|
||||
const debug = useStore(isDebugMode);
|
||||
const eventLogs = useStore(isEventLogsEnabled);
|
||||
const promptId = useStore(promptStore);
|
||||
@@ -87,16 +99,6 @@ export function useSettings(): UseSettingsReturn {
|
||||
};
|
||||
});
|
||||
|
||||
// writing values to cookies on change
|
||||
useEffect(() => {
|
||||
const providers = providersStore.get();
|
||||
const providerSetting: Record<string, IProviderSetting> = {};
|
||||
Object.keys(providers).forEach((provider) => {
|
||||
providerSetting[provider] = providers[provider].settings;
|
||||
});
|
||||
Cookies.set('providers', JSON.stringify(providerSetting));
|
||||
}, [providers]);
|
||||
|
||||
useEffect(() => {
|
||||
let active = Object.entries(providers)
|
||||
.filter(([_key, provider]) => provider.settings.enabled)
|
||||
@@ -118,8 +120,8 @@ export function useSettings(): UseSettingsReturn {
|
||||
});
|
||||
}, []);
|
||||
|
||||
const updateProviderSettings = useCallback((provider: string, config: IProviderSetting) => {
|
||||
providersStore.setKey(provider, { settings: config } as IProviderConfig);
|
||||
const updateProviderSettings = useCallback((provider: string, config: ProviderSettingWithIndex) => {
|
||||
updateProviderSettingsStore(provider, config);
|
||||
}, []);
|
||||
|
||||
const enableDebugMode = useCallback((enabled: boolean) => {
|
||||
@@ -129,38 +131,33 @@ export function useSettings(): UseSettingsReturn {
|
||||
}, []);
|
||||
|
||||
const setEventLogs = useCallback((enabled: boolean) => {
|
||||
isEventLogsEnabled.set(enabled);
|
||||
updateEventLogs(enabled);
|
||||
logStore.logSystem(`Event logs ${enabled ? 'enabled' : 'disabled'}`);
|
||||
Cookies.set('isEventLogsEnabled', String(enabled));
|
||||
}, []);
|
||||
|
||||
const enableLocalModels = useCallback((enabled: boolean) => {
|
||||
isLocalModelsEnabled.set(enabled);
|
||||
updateLocalModels(enabled);
|
||||
logStore.logSystem(`Local models ${enabled ? 'enabled' : 'disabled'}`);
|
||||
Cookies.set('isLocalModelsEnabled', String(enabled));
|
||||
}, []);
|
||||
|
||||
const setPromptId = useCallback((promptId: string) => {
|
||||
promptStore.set(promptId);
|
||||
Cookies.set('promptId', promptId);
|
||||
const setPromptId = useCallback((id: string) => {
|
||||
updatePromptId(id);
|
||||
logStore.logSystem(`Prompt template updated to ${id}`);
|
||||
}, []);
|
||||
|
||||
const enableLatestBranch = useCallback((enabled: boolean) => {
|
||||
latestBranchStore.set(enabled);
|
||||
updateLatestBranch(enabled);
|
||||
logStore.logSystem(`Main branch updates ${enabled ? 'enabled' : 'disabled'}`);
|
||||
Cookies.set('isLatestBranch', String(enabled));
|
||||
}, []);
|
||||
|
||||
const setAutoSelectTemplate = useCallback((enabled: boolean) => {
|
||||
autoSelectStarterTemplate.set(enabled);
|
||||
updateAutoSelectTemplate(enabled);
|
||||
logStore.logSystem(`Auto select template ${enabled ? 'enabled' : 'disabled'}`);
|
||||
Cookies.set('autoSelectTemplate', String(enabled));
|
||||
}, []);
|
||||
|
||||
const enableContextOptimization = useCallback((enabled: boolean) => {
|
||||
enableContextOptimizationStore.set(enabled);
|
||||
updateContextOptimization(enabled);
|
||||
logStore.logSystem(`Context optimization ${enabled ? 'enabled' : 'disabled'}`);
|
||||
Cookies.set('contextOptimizationEnabled', String(enabled));
|
||||
}, []);
|
||||
|
||||
const setTheme = useCallback(
|
||||
@@ -191,6 +188,18 @@ export function useSettings(): UseSettingsReturn {
|
||||
[saveSettings],
|
||||
);
|
||||
|
||||
// Fix the providers cookie sync
|
||||
useEffect(() => {
|
||||
const providers = providersStore.get();
|
||||
const providerSetting: Record<string, { enabled: boolean }> = {};
|
||||
Object.keys(providers).forEach((provider) => {
|
||||
providerSetting[provider] = {
|
||||
enabled: providers[provider].settings.enabled || false, // Add fallback for undefined
|
||||
};
|
||||
});
|
||||
Cookies.set('providers', JSON.stringify(providerSetting));
|
||||
}, [providers]);
|
||||
|
||||
return {
|
||||
...settings,
|
||||
providers,
|
||||
|
||||
@@ -59,40 +59,142 @@ export const shortcutsStore = map<Shortcuts>({
|
||||
},
|
||||
});
|
||||
|
||||
const initialProviderSettings: ProviderSetting = {};
|
||||
PROVIDER_LIST.forEach((provider) => {
|
||||
initialProviderSettings[provider.name] = {
|
||||
...provider,
|
||||
// Create a single key for provider settings
|
||||
const PROVIDER_SETTINGS_KEY = 'provider_settings';
|
||||
|
||||
// Initialize provider settings from both localStorage and defaults
|
||||
const getInitialProviderSettings = (): ProviderSetting => {
|
||||
const savedSettings = localStorage.getItem(PROVIDER_SETTINGS_KEY);
|
||||
const initialSettings: ProviderSetting = {};
|
||||
|
||||
// Start with default settings
|
||||
PROVIDER_LIST.forEach((provider) => {
|
||||
initialSettings[provider.name] = {
|
||||
...provider,
|
||||
settings: {
|
||||
enabled: true,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
// Override with saved settings if they exist
|
||||
if (savedSettings) {
|
||||
try {
|
||||
const parsed = JSON.parse(savedSettings);
|
||||
Object.entries(parsed).forEach(([key, value]) => {
|
||||
if (initialSettings[key]) {
|
||||
initialSettings[key].settings = (value as IProviderConfig).settings;
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error parsing saved provider settings:', error);
|
||||
}
|
||||
}
|
||||
|
||||
return initialSettings;
|
||||
};
|
||||
|
||||
export const providersStore = map<ProviderSetting>(getInitialProviderSettings());
|
||||
|
||||
// Create a function to update provider settings that handles both store and persistence
|
||||
export const updateProviderSettings = (provider: string, settings: ProviderSetting) => {
|
||||
const currentSettings = providersStore.get();
|
||||
|
||||
// Create new provider config with updated settings
|
||||
const updatedProvider = {
|
||||
...currentSettings[provider],
|
||||
settings: {
|
||||
enabled: true,
|
||||
...currentSettings[provider].settings,
|
||||
...settings,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
//TODO: need to create one single map for all these flags
|
||||
// Update the store with new settings
|
||||
providersStore.setKey(provider, updatedProvider);
|
||||
|
||||
export const providersStore = map<ProviderSetting>(initialProviderSettings);
|
||||
// Save to localStorage
|
||||
const allSettings = providersStore.get();
|
||||
localStorage.setItem(PROVIDER_SETTINGS_KEY, JSON.stringify(allSettings));
|
||||
};
|
||||
|
||||
export const isDebugMode = atom(false);
|
||||
|
||||
// Initialize event logs from cookie or default to false
|
||||
const savedEventLogs = Cookies.get('isEventLogsEnabled');
|
||||
export const isEventLogsEnabled = atom(savedEventLogs === 'true');
|
||||
// Define keys for localStorage
|
||||
const SETTINGS_KEYS = {
|
||||
LATEST_BRANCH: 'isLatestBranch',
|
||||
AUTO_SELECT_TEMPLATE: 'autoSelectTemplate',
|
||||
CONTEXT_OPTIMIZATION: 'contextOptimizationEnabled',
|
||||
EVENT_LOGS: 'isEventLogsEnabled',
|
||||
LOCAL_MODELS: 'isLocalModelsEnabled',
|
||||
PROMPT_ID: 'promptId',
|
||||
} as const;
|
||||
|
||||
// Local models settings
|
||||
export const isLocalModelsEnabled = atom(true);
|
||||
// Initialize settings from localStorage or defaults
|
||||
const getInitialSettings = () => {
|
||||
const getStoredBoolean = (key: string, defaultValue: boolean): boolean => {
|
||||
const stored = localStorage.getItem(key);
|
||||
|
||||
// Prompt settings
|
||||
export const promptStore = atom<string>('default');
|
||||
if (stored === null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
// Branch settings
|
||||
export const latestBranchStore = atom(false);
|
||||
try {
|
||||
return JSON.parse(stored);
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
};
|
||||
|
||||
// Template settings
|
||||
export const autoSelectStarterTemplate = atom(false);
|
||||
return {
|
||||
latestBranch: getStoredBoolean(SETTINGS_KEYS.LATEST_BRANCH, false),
|
||||
autoSelectTemplate: getStoredBoolean(SETTINGS_KEYS.AUTO_SELECT_TEMPLATE, false),
|
||||
contextOptimization: getStoredBoolean(SETTINGS_KEYS.CONTEXT_OPTIMIZATION, false),
|
||||
eventLogs: getStoredBoolean(SETTINGS_KEYS.EVENT_LOGS, true),
|
||||
localModels: getStoredBoolean(SETTINGS_KEYS.LOCAL_MODELS, true),
|
||||
promptId: localStorage.getItem(SETTINGS_KEYS.PROMPT_ID) || 'default',
|
||||
};
|
||||
};
|
||||
|
||||
// Context optimization settings
|
||||
export const enableContextOptimizationStore = atom(false);
|
||||
// Initialize stores with persisted values
|
||||
const initialSettings = getInitialSettings();
|
||||
|
||||
export const latestBranchStore = atom<boolean>(initialSettings.latestBranch);
|
||||
export const autoSelectStarterTemplate = atom<boolean>(initialSettings.autoSelectTemplate);
|
||||
export const enableContextOptimizationStore = atom<boolean>(initialSettings.contextOptimization);
|
||||
export const isEventLogsEnabled = atom<boolean>(initialSettings.eventLogs);
|
||||
export const isLocalModelsEnabled = atom<boolean>(initialSettings.localModels);
|
||||
export const promptStore = atom<string>(initialSettings.promptId);
|
||||
|
||||
// Helper functions to update settings with persistence
|
||||
export const updateLatestBranch = (enabled: boolean) => {
|
||||
latestBranchStore.set(enabled);
|
||||
localStorage.setItem(SETTINGS_KEYS.LATEST_BRANCH, JSON.stringify(enabled));
|
||||
};
|
||||
|
||||
export const updateAutoSelectTemplate = (enabled: boolean) => {
|
||||
autoSelectStarterTemplate.set(enabled);
|
||||
localStorage.setItem(SETTINGS_KEYS.AUTO_SELECT_TEMPLATE, JSON.stringify(enabled));
|
||||
};
|
||||
|
||||
export const updateContextOptimization = (enabled: boolean) => {
|
||||
enableContextOptimizationStore.set(enabled);
|
||||
localStorage.setItem(SETTINGS_KEYS.CONTEXT_OPTIMIZATION, JSON.stringify(enabled));
|
||||
};
|
||||
|
||||
export const updateEventLogs = (enabled: boolean) => {
|
||||
isEventLogsEnabled.set(enabled);
|
||||
localStorage.setItem(SETTINGS_KEYS.EVENT_LOGS, JSON.stringify(enabled));
|
||||
};
|
||||
|
||||
export const updateLocalModels = (enabled: boolean) => {
|
||||
isLocalModelsEnabled.set(enabled);
|
||||
localStorage.setItem(SETTINGS_KEYS.LOCAL_MODELS, JSON.stringify(enabled));
|
||||
};
|
||||
|
||||
export const updatePromptId = (id: string) => {
|
||||
promptStore.set(id);
|
||||
localStorage.setItem(SETTINGS_KEYS.PROMPT_ID, id);
|
||||
};
|
||||
|
||||
// Initialize tab configuration from cookie or default
|
||||
const savedTabConfig = Cookies.get('tabConfiguration');
|
||||
|
||||
Reference in New Issue
Block a user