ai-toolkit / ui /src /utils /storage /settingsStorage.ts
multimodalart's picture
Upload 121 files
f555806 verified
import { apiClient } from '@/utils/api';
import { usingBrowserDb } from '@/utils/env';
import { SettingsData } from '@/types';
const LOCAL_SETTINGS_STORAGE_KEY = 'aitk.browser.settings';
export const DEFAULT_SETTINGS: SettingsData = {
HF_TOKEN: '',
TRAINING_FOLDER: '',
DATASETS_FOLDER: '',
HF_JOBS_NAMESPACE: '',
HF_JOBS_DEFAULT_HARDWARE: 'a100-large',
};
const mergeWithDefaults = (data: Partial<SettingsData> | null | undefined): SettingsData => ({
...DEFAULT_SETTINGS,
...data,
});
export const loadSettings = async (): Promise<SettingsData> => {
if (usingBrowserDb) {
if (typeof window === 'undefined') {
return DEFAULT_SETTINGS;
}
try {
const raw = window.localStorage.getItem(LOCAL_SETTINGS_STORAGE_KEY);
if (!raw) {
return DEFAULT_SETTINGS;
}
const parsed = JSON.parse(raw);
return mergeWithDefaults(parsed);
} catch (error) {
console.error('Failed to read settings from localStorage:', error);
return DEFAULT_SETTINGS;
}
}
const response = await apiClient.get('/api/settings');
return mergeWithDefaults(response.data);
};
export const persistSettings = async (settings: SettingsData): Promise<void> => {
if (usingBrowserDb) {
if (typeof window === 'undefined') {
return;
}
try {
window.localStorage.setItem(LOCAL_SETTINGS_STORAGE_KEY, JSON.stringify(settings));
} catch (error) {
console.error('Failed to write settings to localStorage:', error);
}
return;
}
await apiClient.post('/api/settings', settings);
};