初始化提交
This commit is contained in:
53
mengyamonitor-frontend/src/utils/storage.ts
Normal file
53
mengyamonitor-frontend/src/utils/storage.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { ServerConfig } from '../types';
|
||||
|
||||
const STORAGE_KEY = 'mengya_monitor_servers';
|
||||
|
||||
export const loadServers = (): ServerConfig[] => {
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored) {
|
||||
const parsed = JSON.parse(stored);
|
||||
// Ensure it's an array
|
||||
if (Array.isArray(parsed)) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load servers:', error);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
export const saveServers = (servers: ServerConfig[]): void => {
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(servers));
|
||||
} catch (error) {
|
||||
console.error('Failed to save servers:', error);
|
||||
}
|
||||
};
|
||||
|
||||
export const addServer = (server: Omit<ServerConfig, 'id'>): ServerConfig => {
|
||||
const servers = loadServers();
|
||||
const newServer: ServerConfig = {
|
||||
...server,
|
||||
id: Date.now().toString(),
|
||||
};
|
||||
servers.push(newServer);
|
||||
saveServers(servers);
|
||||
return newServer;
|
||||
};
|
||||
|
||||
export const updateServer = (id: string, updates: Partial<ServerConfig>): void => {
|
||||
const servers = loadServers();
|
||||
const index = servers.findIndex(s => s.id === id);
|
||||
if (index !== -1) {
|
||||
servers[index] = { ...servers[index], ...updates };
|
||||
saveServers(servers);
|
||||
}
|
||||
};
|
||||
|
||||
export const removeServer = (id: string): void => {
|
||||
const servers = loadServers();
|
||||
const filtered = servers.filter(s => s.id !== id);
|
||||
saveServers(filtered);
|
||||
};
|
||||
Reference in New Issue
Block a user