feat: add media converter app and fix compilation errors
This commit is contained in:
88
lib/media/storage/history.ts
Normal file
88
lib/media/storage/history.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import type { ConversionHistoryItem } from '@/types/media';
|
||||
|
||||
const HISTORY_KEY = 'convert-ui-history';
|
||||
const MAX_HISTORY_ITEMS = 10;
|
||||
|
||||
/**
|
||||
* Get conversion history from localStorage
|
||||
*/
|
||||
export function getHistory(): ConversionHistoryItem[] {
|
||||
if (typeof window === 'undefined') return [];
|
||||
|
||||
try {
|
||||
const stored = localStorage.getItem(HISTORY_KEY);
|
||||
if (!stored) return [];
|
||||
|
||||
const history = JSON.parse(stored);
|
||||
return Array.isArray(history) ? history : [];
|
||||
} catch (error) {
|
||||
console.error('Failed to load history:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add item to conversion history
|
||||
*/
|
||||
export function addToHistory(item: Omit<ConversionHistoryItem, 'id' | 'timestamp'>): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
const history = getHistory();
|
||||
|
||||
const newItem: ConversionHistoryItem = {
|
||||
...item,
|
||||
id: Math.random().toString(36).substring(7),
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
// Add to beginning of array
|
||||
history.unshift(newItem);
|
||||
|
||||
// Keep only the latest MAX_HISTORY_ITEMS
|
||||
const trimmed = history.slice(0, MAX_HISTORY_ITEMS);
|
||||
|
||||
localStorage.setItem(HISTORY_KEY, JSON.stringify(trimmed));
|
||||
|
||||
// Dispatch custom event for same-page updates
|
||||
window.dispatchEvent(new CustomEvent('conversionHistoryUpdated'));
|
||||
} catch (error) {
|
||||
console.error('Failed to save history:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all conversion history
|
||||
*/
|
||||
export function clearHistory(): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
localStorage.removeItem(HISTORY_KEY);
|
||||
} catch (error) {
|
||||
console.error('Failed to clear history:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove single item from history
|
||||
*/
|
||||
export function removeHistoryItem(id: string): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
const history = getHistory();
|
||||
const filtered = history.filter((item) => item.id !== id);
|
||||
localStorage.setItem(HISTORY_KEY, JSON.stringify(filtered));
|
||||
} catch (error) {
|
||||
console.error('Failed to remove history item:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get history item by ID
|
||||
*/
|
||||
export function getHistoryItem(id: string): ConversionHistoryItem | undefined {
|
||||
const history = getHistory();
|
||||
return history.find((item) => item.id === id);
|
||||
}
|
||||
73
lib/media/storage/settings.ts
Normal file
73
lib/media/storage/settings.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
export interface UserSettings {
|
||||
// Quality preferences
|
||||
defaultQualityPreset: 'high-quality' | 'balanced' | 'small-file' | 'web-optimized';
|
||||
|
||||
// Behavior preferences
|
||||
autoStartConversion: boolean;
|
||||
showConversionHistory: boolean;
|
||||
clearHistoryOnReset: boolean;
|
||||
|
||||
// Default formats (optional)
|
||||
defaultVideoFormat?: string;
|
||||
defaultAudioFormat?: string;
|
||||
defaultImageFormat?: string;
|
||||
}
|
||||
|
||||
const SETTINGS_KEY = 'convert-ui-settings';
|
||||
|
||||
const DEFAULT_SETTINGS: UserSettings = {
|
||||
defaultQualityPreset: 'balanced',
|
||||
autoStartConversion: false,
|
||||
showConversionHistory: true,
|
||||
clearHistoryOnReset: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Get user settings from localStorage
|
||||
*/
|
||||
export function getSettings(): UserSettings {
|
||||
if (typeof window === 'undefined') return DEFAULT_SETTINGS;
|
||||
|
||||
try {
|
||||
const stored = localStorage.getItem(SETTINGS_KEY);
|
||||
if (!stored) return DEFAULT_SETTINGS;
|
||||
|
||||
const settings = JSON.parse(stored);
|
||||
return { ...DEFAULT_SETTINGS, ...settings };
|
||||
} catch (error) {
|
||||
console.error('Failed to load settings:', error);
|
||||
return DEFAULT_SETTINGS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save user settings to localStorage
|
||||
*/
|
||||
export function saveSettings(settings: Partial<UserSettings>): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
const current = getSettings();
|
||||
const updated = { ...current, ...settings };
|
||||
localStorage.setItem(SETTINGS_KEY, JSON.stringify(updated));
|
||||
|
||||
// Dispatch custom event for settings updates
|
||||
window.dispatchEvent(new CustomEvent('settingsUpdated', { detail: updated }));
|
||||
} catch (error) {
|
||||
console.error('Failed to save settings:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset settings to defaults
|
||||
*/
|
||||
export function resetSettings(): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
localStorage.setItem(SETTINGS_KEY, JSON.stringify(DEFAULT_SETTINGS));
|
||||
window.dispatchEvent(new CustomEvent('settingsUpdated', { detail: DEFAULT_SETTINGS }));
|
||||
} catch (error) {
|
||||
console.error('Failed to reset settings:', error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user