Features: - Created ConversionHistory component showing recent conversions - Displays input/output formats, file sizes, and timestamps - Relative time display (e.g., "5 minutes ago", "2 hours ago") - Clear all history functionality with confirmation - Individual history item removal - Real-time updates when new conversions complete - Custom event system for same-page updates - Storage event listener for cross-tab synchronization - Empty state with helpful messaging - Clean, organized card-based layout - Responsive design with proper spacing Technical improvements: - Enhanced history storage to dispatch custom events - History component auto-refreshes on new conversions - Maintains up to 10 most recent conversions - Integrated seamlessly into main page layout 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
89 lines
2.2 KiB
TypeScript
89 lines
2.2 KiB
TypeScript
import type { ConversionHistoryItem } from '@/types/conversion';
|
|
|
|
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);
|
|
}
|