Files
WifiShare/services/storage.ts
2026-02-06 15:16:49 +09:00

49 lines
1.4 KiB
TypeScript

import { WiFiHotspot } from '../types';
const STORAGE_KEY = 'wifi_hotspots_v2_db';
export const storageService = {
getHotspots: (): WiFiHotspot[] => {
try {
const data = localStorage.getItem(STORAGE_KEY);
if (!data) return [];
const parsed = JSON.parse(data);
return Array.isArray(parsed) ? parsed : [];
} catch (e) {
console.error("Failed to load hotspots from storage", e);
return [];
}
},
saveHotspot: (hotspot: WiFiHotspot): void => {
try {
const hotspots = storageService.getHotspots();
const index = hotspots.findIndex(h => h.id === hotspot.id);
if (index > -1) {
hotspots[index] = hotspot;
} else {
hotspots.push(hotspot);
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(hotspots));
// Trigger a storage event for cross-tab sync if needed
window.dispatchEvent(new Event('storage_updated'));
} catch (e) {
console.error("Failed to save hotspot", e);
}
},
deleteHotspot: (id: string): void => {
try {
const hotspots = storageService.getHotspots();
const filtered = hotspots.filter(h => h.id !== id);
localStorage.setItem(STORAGE_KEY, JSON.stringify(filtered));
window.dispatchEvent(new Event('storage_updated'));
} catch (e) {
console.error("Failed to delete hotspot", e);
}
}
};