'use client'; import React, { useState, useEffect } from 'react'; import { Trash2, Edit, Save, X, ChevronDown, ChevronRight, Download, Upload, Trash } from 'lucide-react'; import { useRouter } from "next/navigation"; // Ændret fra next/router til next/navigation // Definition af konfigurationer, der kan indlæses fra serveren const serverConfigs = [ { name: 'Sample Data', filename: 'sample.json' }, ]; const LocalStorageExplorer = () => { const [storageItems, setStorageItems] = useState([]); const [expandedItems, setExpandedItems] = useState({}); const [editingKey, setEditingKey] = useState(null); const [editValue, setEditValue] = useState(''); const [selectedConfig, setSelectedConfig] = useState(''); // State til at håndtere dropdown-valg const router = useRouter(); // Brug useRouter fra next/navigation useEffect(() => { refreshItems(); }, []); const refreshItems = () => { // Tjek om vi er i en browser-miljø (for at undgå problemer under server-side rendering) if (typeof window !== 'undefined') { const items = []; for (let i = 0; i < localStorage.length; i++) { const key = localStorage.key(i); if (key === 'ally-supports-cache') continue; // Skip this key const value = localStorage.getItem(key); let parsedValue = value; try { parsedValue = JSON.parse(value); } catch (e) { // Value is not JSON, keep as string } items.push({ key, value: parsedValue, rawValue: value }); } // Sort items alphabetically by key items.sort((a, b) => a.key.localeCompare(b.key)); setStorageItems(items); } }; const handleDelete = (key) => { if (window.confirm(`Are you sure you want to delete "${key}"?`)) { localStorage.removeItem(key); refreshItems(); } }; const handleEdit = (item) => { setEditingKey(item.key); setEditValue(item.rawValue); }; const handleSave = () => { try { localStorage.setItem(editingKey, editValue); setEditingKey(null); refreshItems(); } catch (e) { alert('Error saving value: ' + e.message); } }; const toggleExpand = (key) => { setExpandedItems(prev => ({ ...prev, [key]: !prev[key] })); }; const formatValue = (value) => { if (typeof value === 'object' && value !== null) { return JSON.stringify(value, null, 2); } return value; }; const renderValue = (item) => { const isObject = typeof item.value === 'object' && item.value !== null; if (!isObject) { return {formatValue(item.value)}; } return (
{expandedItems[item.key] && (
            {formatValue(item.value)}
          
)}
); }; const estimateStorageUsage = () => { let totalBytes = 0; storageItems.forEach(item => { totalBytes += item.key.length * 2; // key length in bytes (UTF-16) totalBytes += item.rawValue.length * 2; // value length in bytes (UTF-16) }); return (totalBytes / 1024).toFixed(2) + ' KB'; }; // Export localStorage to a JSON file const handleExport = () => { const exportData = {}; storageItems.forEach(item => { exportData[item.key] = item.value; }); const blob = new Blob([JSON.stringify(exportData, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'localStorage-export.json'; a.click(); URL.revokeObjectURL(url); }; // Import JSON file and merge into localStorage const handleImport = (event) => { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = (e) => { try { const importedData = JSON.parse(e.target.result); Object.entries(importedData).forEach(([key, value]) => { if (key !== 'ally-supports-cache') { localStorage.setItem(key, JSON.stringify(value)); } }); refreshItems(); setSelectedConfig(''); // Nulstil dropdown til standardvalg alert('Import successful!'); } catch (e) { alert('Error importing file: ' + e.message); } }; reader.readAsText(file); }; // Clear all localStorage items except 'ally-supports-cache' const handleClear = () => { if (window.confirm('Are you sure you want to clear all items ?')) { storageItems.forEach(item => { if (item.key !== 'ally-supports-cache') { localStorage.removeItem(item.key); } }); refreshItems(); } }; // Load configuration from server const handleLoadConfig = async (filename) => { try { const response = await fetch(`/configs/${filename}`); if (!response.ok) { throw new Error('Failed to load configuration'); } const configData = await response.json(); // Clear current localStorage except 'ally-supports-cache' storageItems.forEach(item => { if (item.key !== 'ally-supports-cache') { localStorage.removeItem(item.key); } }); // Load new configuration Object.entries(configData).forEach(([key, value]) => { localStorage.setItem(key, JSON.stringify(value)); }); refreshItems(); setSelectedConfig(''); // Nulstil dropdown til standardvalg alert('Configuration loaded successfully!'); } catch (error) { console.error('Error loading configuration:', error); alert('Error loading configuration'); } }; // Funktion til at håndtere klik på "Open Mock IDP"-knappen const handleOpenMockIdp = () => { // Erstat dette med den rette metode til routing i App Router router.push('?page=mock-idp.js&app=sdm'); }; return (

localStorage Explorer

{storageItems.length} items stored in your own browser localStorage ({estimateStorageUsage()})

{/* Export, Import, and Clear buttons */}
{storageItems.map((item) => (
Key Value Actions
{item.key} {editingKey === item.key ? (