feat: implement settings management component with import/export functionality
This commit is contained in:
@@ -1 +1 @@
|
|||||||
NEXT_PUBLIC_COMMIT_SHA=3734f67
|
NEXT_PUBLIC_COMMIT_SHA=da58c49
|
||||||
|
|||||||
105
app/components/SettingsManagement.tsx
Normal file
105
app/components/SettingsManagement.tsx
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import React, { useState } from 'react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import {
|
||||||
|
Download,
|
||||||
|
Upload,
|
||||||
|
RotateCcw,
|
||||||
|
Settings
|
||||||
|
} from 'lucide-react';
|
||||||
|
import { useSidebarLayout } from '@/hooks/use-sidebar-layout';
|
||||||
|
|
||||||
|
export function SettingsManagement() {
|
||||||
|
const { exportSettings, importSettings, resetToDefaults } = useSidebarLayout();
|
||||||
|
const [importFile, setImportFile] = useState<File | null>(null);
|
||||||
|
const [importing, setImporting] = useState(false);
|
||||||
|
const [importError, setImportError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const handleImportFile = async () => {
|
||||||
|
if (!importFile) return;
|
||||||
|
|
||||||
|
setImporting(true);
|
||||||
|
setImportError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await importSettings(importFile);
|
||||||
|
setImportFile(null);
|
||||||
|
// Reset file input
|
||||||
|
const fileInput = document.getElementById('settings-import') as HTMLInputElement;
|
||||||
|
if (fileInput) fileInput.value = '';
|
||||||
|
} catch (error) {
|
||||||
|
setImportError(error instanceof Error ? error.message : 'Failed to import settings');
|
||||||
|
} finally {
|
||||||
|
setImporting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="flex items-center gap-2">
|
||||||
|
<Settings className="h-5 w-5" />
|
||||||
|
Settings Management
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Export, import, or reset your application settings
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
<div className="flex flex-wrap gap-2">
|
||||||
|
<Button onClick={exportSettings} variant="outline">
|
||||||
|
<Download className="h-4 w-4 mr-2" />
|
||||||
|
Export Settings
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Input
|
||||||
|
id="settings-import"
|
||||||
|
type="file"
|
||||||
|
accept=".json"
|
||||||
|
onChange={(e) => setImportFile(e.target.files?.[0] || null)}
|
||||||
|
className="hidden"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => document.getElementById('settings-import')?.click()}
|
||||||
|
>
|
||||||
|
<Upload className="h-4 w-4 mr-2" />
|
||||||
|
Select File
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
{importFile && (
|
||||||
|
<Button
|
||||||
|
onClick={handleImportFile}
|
||||||
|
disabled={importing}
|
||||||
|
variant="default"
|
||||||
|
>
|
||||||
|
{importing ? 'Importing...' : 'Import'}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Button onClick={resetToDefaults} variant="outline">
|
||||||
|
<RotateCcw className="h-4 w-4 mr-2" />
|
||||||
|
Reset to Default
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{importFile && (
|
||||||
|
<div className="text-sm text-muted-foreground">
|
||||||
|
Selected: {importFile.name}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{importError && (
|
||||||
|
<div className="text-sm text-destructive">
|
||||||
|
Error: {importError}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import React, { useState } from 'react';
|
import React from 'react';
|
||||||
import {
|
import {
|
||||||
DndContext,
|
DndContext,
|
||||||
closestCenter,
|
closestCenter,
|
||||||
@@ -25,14 +25,10 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
|
|||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
|
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
|
||||||
import { Switch } from '@/components/ui/switch';
|
import { Switch } from '@/components/ui/switch';
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import {
|
import {
|
||||||
GripVertical,
|
GripVertical,
|
||||||
Eye,
|
Eye,
|
||||||
EyeOff,
|
EyeOff,
|
||||||
Download,
|
|
||||||
Upload,
|
|
||||||
RotateCcw,
|
|
||||||
Search,
|
Search,
|
||||||
Home,
|
Home,
|
||||||
List,
|
List,
|
||||||
@@ -130,19 +126,15 @@ function SortableItem({ item, onToggleVisibility, showIcons }: SortableItemProps
|
|||||||
export function SidebarCustomization() {
|
export function SidebarCustomization() {
|
||||||
const {
|
const {
|
||||||
settings,
|
settings,
|
||||||
|
hasUnsavedChanges,
|
||||||
reorderItems,
|
reorderItems,
|
||||||
toggleItemVisibility,
|
toggleItemVisibility,
|
||||||
updateShortcuts,
|
updateShortcuts,
|
||||||
updateShowIcons,
|
updateShowIcons,
|
||||||
exportSettings,
|
applyChanges,
|
||||||
importSettings,
|
discardChanges,
|
||||||
resetToDefaults,
|
|
||||||
} = useSidebarLayout();
|
} = useSidebarLayout();
|
||||||
|
|
||||||
const [importFile, setImportFile] = useState<File | null>(null);
|
|
||||||
const [importing, setImporting] = useState(false);
|
|
||||||
const [importError, setImportError] = useState<string | null>(null);
|
|
||||||
|
|
||||||
const sensors = useSensors(
|
const sensors = useSensors(
|
||||||
useSensor(PointerSensor),
|
useSensor(PointerSensor),
|
||||||
useSensor(KeyboardSensor, {
|
useSensor(KeyboardSensor, {
|
||||||
@@ -158,24 +150,7 @@ export function SidebarCustomization() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleImportFile = async () => {
|
|
||||||
if (!importFile) return;
|
|
||||||
|
|
||||||
setImporting(true);
|
|
||||||
setImportError(null);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await importSettings(importFile);
|
|
||||||
setImportFile(null);
|
|
||||||
// Reset file input
|
|
||||||
const fileInput = document.getElementById('settings-import') as HTMLInputElement;
|
|
||||||
if (fileInput) fileInput.value = '';
|
|
||||||
} catch (error) {
|
|
||||||
setImportError(error instanceof Error ? error.message : 'Failed to import settings');
|
|
||||||
} finally {
|
|
||||||
setImporting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
@@ -249,61 +224,20 @@ export function SidebarCustomization() {
|
|||||||
</DndContext>
|
</DndContext>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Settings Import/Export */}
|
{/* Apply/Discard Changes */}
|
||||||
<div className="space-y-3 pt-4 border-t">
|
{hasUnsavedChanges() && (
|
||||||
<Label>Settings Management</Label>
|
<div className="space-y-3 pt-4 border-t">
|
||||||
|
<Label>Unsaved Changes</Label>
|
||||||
<div className="flex flex-wrap gap-2">
|
<div className="flex gap-2">
|
||||||
<Button onClick={exportSettings} variant="outline">
|
<Button onClick={applyChanges} variant="default">
|
||||||
<Download className="h-4 w-4 mr-2" />
|
Apply Changes
|
||||||
Export Settings
|
</Button>
|
||||||
</Button>
|
<Button onClick={discardChanges} variant="outline">
|
||||||
|
Discard Changes
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<Input
|
|
||||||
id="settings-import"
|
|
||||||
type="file"
|
|
||||||
accept=".json"
|
|
||||||
onChange={(e) => setImportFile(e.target.files?.[0] || null)}
|
|
||||||
className="hidden"
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
onClick={() => document.getElementById('settings-import')?.click()}
|
|
||||||
>
|
|
||||||
<Upload className="h-4 w-4 mr-2" />
|
|
||||||
Select File
|
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
{importFile && (
|
|
||||||
<Button
|
|
||||||
onClick={handleImportFile}
|
|
||||||
disabled={importing}
|
|
||||||
variant="default"
|
|
||||||
>
|
|
||||||
{importing ? 'Importing...' : 'Import'}
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button onClick={resetToDefaults} variant="outline">
|
|
||||||
<RotateCcw className="h-4 w-4 mr-2" />
|
|
||||||
Reset to Default
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
{importFile && (
|
|
||||||
<div className="text-sm text-muted-foreground">
|
|
||||||
Selected: {importFile.name}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{importError && (
|
|
||||||
<div className="text-sm text-destructive">
|
|
||||||
Error: {importError}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { useToast } from '@/hooks/use-toast';
|
|||||||
import { useStandaloneLastFm } from '@/hooks/use-standalone-lastfm';
|
import { useStandaloneLastFm } from '@/hooks/use-standalone-lastfm';
|
||||||
import { useSidebarShortcuts, SidebarShortcutType } from '@/hooks/use-sidebar-shortcuts';
|
import { useSidebarShortcuts, SidebarShortcutType } from '@/hooks/use-sidebar-shortcuts';
|
||||||
import { SidebarCustomization } from '@/app/components/SidebarCustomization';
|
import { SidebarCustomization } from '@/app/components/SidebarCustomization';
|
||||||
|
import { SettingsManagement } from '@/app/components/SettingsManagement';
|
||||||
import { FaServer, FaUser, FaLock, FaCheck, FaTimes, FaLastfm, FaCog } from 'react-icons/fa';
|
import { FaServer, FaUser, FaLock, FaCheck, FaTimes, FaLastfm, FaCog } from 'react-icons/fa';
|
||||||
import { Settings, ExternalLink } from 'lucide-react';
|
import { Settings, ExternalLink } from 'lucide-react';
|
||||||
|
|
||||||
@@ -347,10 +348,11 @@ const SettingsPage = () => {
|
|||||||
<p className="text-muted-foreground">Customize your music experience</p>
|
<p className="text-muted-foreground">Customize your music experience</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-6">
|
<div className="columns-1 lg:columns-2 xl:columns-3 gap-6 space-y-6"
|
||||||
|
style={{ columnFill: 'balance' }}>
|
||||||
|
|
||||||
{!hasEnvConfig && (
|
{!hasEnvConfig && (
|
||||||
<Card className="mb-6">
|
<Card className="mb-6 break-inside-avoid">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle className="flex items-center gap-2">
|
<CardTitle className="flex items-center gap-2">
|
||||||
<FaServer className="w-5 h-5" />
|
<FaServer className="w-5 h-5" />
|
||||||
@@ -439,7 +441,7 @@ const SettingsPage = () => {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{hasEnvConfig && (
|
{hasEnvConfig && (
|
||||||
<Card className="mb-6">
|
<Card className="mb-6 break-inside-avoid">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle className="flex items-center gap-2">
|
<CardTitle className="flex items-center gap-2">
|
||||||
<FaServer className="w-5 h-5" />
|
<FaServer className="w-5 h-5" />
|
||||||
@@ -466,7 +468,7 @@ const SettingsPage = () => {
|
|||||||
</Card>
|
</Card>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Card className="mb-6">
|
<Card className="mb-6 break-inside-avoid">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle className="flex items-center gap-2">
|
<CardTitle className="flex items-center gap-2">
|
||||||
<FaLastfm className="w-5 h-5" />
|
<FaLastfm className="w-5 h-5" />
|
||||||
@@ -544,7 +546,7 @@ const SettingsPage = () => {
|
|||||||
</CardContent>
|
</CardContent>
|
||||||
</Card> */}
|
</Card> */}
|
||||||
|
|
||||||
<Card className="mb-6">
|
<Card className="mb-6 break-inside-avoid">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle className="flex items-center gap-2">
|
<CardTitle className="flex items-center gap-2">
|
||||||
<Settings className="w-5 h-5" />
|
<Settings className="w-5 h-5" />
|
||||||
@@ -695,11 +697,16 @@ const SettingsPage = () => {
|
|||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* Sidebar Customization */}
|
{/* Sidebar Customization */}
|
||||||
<div className="lg:col-span-2 xl:col-span-3">
|
<div className="break-inside-avoid mb-6">
|
||||||
<SidebarCustomization />
|
<SidebarCustomization />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Card className="mb-6">
|
{/* Settings Management */}
|
||||||
|
<div className="break-inside-avoid mb-6">
|
||||||
|
<SettingsManagement />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Card className="mb-6 break-inside-avoid">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>Appearance</CardTitle>
|
<CardTitle>Appearance</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
@@ -748,7 +755,7 @@ const SettingsPage = () => {
|
|||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* Theme Preview */}
|
{/* Theme Preview */}
|
||||||
<Card className="mb-6">
|
<Card className="mb-6 break-inside-avoid">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>Preview</CardTitle>
|
<CardTitle>Preview</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ const defaultSettings: SidebarLayoutSettings = {
|
|||||||
|
|
||||||
export function useSidebarLayout() {
|
export function useSidebarLayout() {
|
||||||
const [settings, setSettings] = useState<SidebarLayoutSettings>(defaultSettings);
|
const [settings, setSettings] = useState<SidebarLayoutSettings>(defaultSettings);
|
||||||
|
const [pendingSettings, setPendingSettings] = useState<SidebarLayoutSettings | null>(null);
|
||||||
|
|
||||||
// Load settings from localStorage on mount
|
// Load settings from localStorage on mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -66,53 +67,86 @@ export function useSidebarLayout() {
|
|||||||
return savedItem ? { ...defaultItem, ...savedItem } : defaultItem;
|
return savedItem ? { ...defaultItem, ...savedItem } : defaultItem;
|
||||||
});
|
});
|
||||||
|
|
||||||
setSettings({
|
const loadedSettings = {
|
||||||
items: mergedItems,
|
items: mergedItems,
|
||||||
shortcuts: parsed.shortcuts || defaultSettings.shortcuts,
|
shortcuts: parsed.shortcuts || defaultSettings.shortcuts,
|
||||||
showIcons: parsed.showIcons !== undefined ? parsed.showIcons : defaultSettings.showIcons,
|
showIcons: parsed.showIcons !== undefined ? parsed.showIcons : defaultSettings.showIcons,
|
||||||
});
|
};
|
||||||
|
setSettings(loadedSettings);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to parse sidebar layout settings:', error);
|
console.error('Failed to parse sidebar layout settings:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Save settings to localStorage whenever they change
|
const saveSettings = (newSettings: SidebarLayoutSettings) => {
|
||||||
useEffect(() => {
|
setSettings(newSettings);
|
||||||
localStorage.setItem('sidebar-layout-settings', JSON.stringify(settings));
|
setPendingSettings(null);
|
||||||
}, [settings]);
|
localStorage.setItem('sidebar-layout-settings', JSON.stringify(newSettings));
|
||||||
|
|
||||||
const updateItemOrder = (newItems: SidebarItem[]) => {
|
|
||||||
setSettings(prev => ({ ...prev, items: newItems }));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const updatePendingSettings = (newSettings: SidebarLayoutSettings) => {
|
||||||
|
setPendingSettings(newSettings);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCurrentSettings = () => pendingSettings || settings;
|
||||||
|
|
||||||
|
const hasUnsavedChanges = () => pendingSettings !== null;
|
||||||
|
|
||||||
const reorderItems = (activeId: string, overId: string) => {
|
const reorderItems = (activeId: string, overId: string) => {
|
||||||
const activeIndex = settings.items.findIndex(item => item.id === activeId);
|
const currentSettings = getCurrentSettings();
|
||||||
const overIndex = settings.items.findIndex(item => item.id === overId);
|
const activeIndex = currentSettings.items.findIndex(item => item.id === activeId);
|
||||||
|
const overIndex = currentSettings.items.findIndex(item => item.id === overId);
|
||||||
|
|
||||||
if (activeIndex !== -1 && overIndex !== -1) {
|
if (activeIndex !== -1 && overIndex !== -1) {
|
||||||
const newItems = [...settings.items];
|
const newItems = [...currentSettings.items];
|
||||||
const [removed] = newItems.splice(activeIndex, 1);
|
const [removed] = newItems.splice(activeIndex, 1);
|
||||||
newItems.splice(overIndex, 0, removed);
|
newItems.splice(overIndex, 0, removed);
|
||||||
setSettings(prev => ({ ...prev, items: newItems }));
|
|
||||||
|
const newSettings = { ...currentSettings, items: newItems };
|
||||||
|
updatePendingSettings(newSettings);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const updateItemOrder = (newItems: SidebarItem[]) => {
|
||||||
|
const currentSettings = getCurrentSettings();
|
||||||
|
const newSettings = { ...currentSettings, items: newItems };
|
||||||
|
updatePendingSettings(newSettings);
|
||||||
|
};
|
||||||
|
|
||||||
const toggleItemVisibility = (itemId: SidebarItemType) => {
|
const toggleItemVisibility = (itemId: SidebarItemType) => {
|
||||||
setSettings(prev => ({
|
const currentSettings = getCurrentSettings();
|
||||||
...prev,
|
const newItems = currentSettings.items.map(item =>
|
||||||
items: prev.items.map(item =>
|
item.id === itemId ? { ...item, visible: !item.visible } : item
|
||||||
item.id === itemId ? { ...item, visible: !item.visible } : item
|
);
|
||||||
),
|
const newSettings = { ...currentSettings, items: newItems };
|
||||||
}));
|
updatePendingSettings(newSettings);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateShortcuts = (shortcuts: 'albums' | 'playlists' | 'both') => {
|
const updateShortcuts = (shortcuts: 'albums' | 'playlists' | 'both') => {
|
||||||
setSettings(prev => ({ ...prev, shortcuts }));
|
const currentSettings = getCurrentSettings();
|
||||||
|
const newSettings = { ...currentSettings, shortcuts };
|
||||||
|
updatePendingSettings(newSettings);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateShowIcons = (showIcons: boolean) => {
|
const updateShowIcons = (showIcons: boolean) => {
|
||||||
setSettings(prev => ({ ...prev, showIcons }));
|
const currentSettings = getCurrentSettings();
|
||||||
|
const newSettings = { ...currentSettings, showIcons };
|
||||||
|
updatePendingSettings(newSettings);
|
||||||
|
};
|
||||||
|
|
||||||
|
const applyChanges = () => {
|
||||||
|
if (pendingSettings) {
|
||||||
|
saveSettings(pendingSettings);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const discardChanges = () => {
|
||||||
|
setPendingSettings(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetToDefaults = () => {
|
||||||
|
saveSettings(defaultSettings);
|
||||||
};
|
};
|
||||||
|
|
||||||
const exportSettings = () => {
|
const exportSettings = () => {
|
||||||
@@ -179,17 +213,16 @@ export function useSidebarLayout() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const resetToDefaults = () => {
|
|
||||||
setSettings(defaultSettings);
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
settings,
|
settings: getCurrentSettings(),
|
||||||
|
hasUnsavedChanges,
|
||||||
updateItemOrder,
|
updateItemOrder,
|
||||||
reorderItems,
|
reorderItems,
|
||||||
toggleItemVisibility,
|
toggleItemVisibility,
|
||||||
updateShortcuts,
|
updateShortcuts,
|
||||||
updateShowIcons,
|
updateShowIcons,
|
||||||
|
applyChanges,
|
||||||
|
discardChanges,
|
||||||
exportSettings,
|
exportSettings,
|
||||||
importSettings,
|
importSettings,
|
||||||
resetToDefaults,
|
resetToDefaults,
|
||||||
|
|||||||
Reference in New Issue
Block a user