import React, { useRef, useState } from 'react'; import Dropdown from '../Dropdown'; import { Config, Wallpaper } from '../../types'; interface ThemeTabProps { config: Config; onChange: (updates: Partial) => void; userWallpapers: Wallpaper[]; allWallpapers: Wallpaper[]; chromeStorageAvailable: boolean; onAddWallpaper: (name: string, url: string) => Promise; onAddWallpaperFile: (file: File) => Promise; onDeleteWallpaper: (wallpaper: Wallpaper) => Promise; onNextWallpaper: () => void; } type RangeStyle = React.CSSProperties & { '--range-progress': string }; const getRangeStyle = (value: number, min: number, max: number): RangeStyle => { const progress = Math.min(100, Math.max(0, ((value - min) / (max - min)) * 100)); return { '--range-progress': `${progress}%` }; }; const MIN_WALLPAPER_FREQUENCY_HOURS = 1; const MAX_WALLPAPER_FREQUENCY_HOURS = 48; const DEFAULT_WALLPAPER_FREQUENCY_HOURS = 24; const clampWallpaperFrequencyHours = (hours: number): number => Math.min(MAX_WALLPAPER_FREQUENCY_HOURS, Math.max(MIN_WALLPAPER_FREQUENCY_HOURS, Math.round(hours))); const getWallpaperFrequencyHours = (frequency: string): number => { const match = frequency.match(/^(\d+)(h|d)$/); if (!match) return DEFAULT_WALLPAPER_FREQUENCY_HOURS; const value = Number(match[1]); const hours = match[2] === 'd' ? value * 24 : value; return clampWallpaperFrequencyHours(hours); }; const formatWallpaperFrequency = (hours: number): string => `${hours} ${hours === 1 ? 'hour' : 'hours'}`; const ThemeTab: React.FC = ({ config, onChange, userWallpapers, allWallpapers, chromeStorageAvailable, onAddWallpaper, onAddWallpaperFile, onDeleteWallpaper, onNextWallpaper, }) => { const [newWallpaperName, setNewWallpaperName] = useState(''); const [newWallpaperUrl, setNewWallpaperUrl] = useState(''); const fileInputRef = useRef(null); const wallpaperFrequencyHours = getWallpaperFrequencyHours(config.wallpaperFrequency); const handleAddWallpaper = async () => { if (newWallpaperUrl.trim() === '') return; try { await onAddWallpaper(newWallpaperName, newWallpaperUrl); setNewWallpaperName(''); setNewWallpaperUrl(''); } catch (error) { alert('Error adding wallpaper. Please check the URL and try again.'); console.error(error); } }; const handleFileUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; try { await onAddWallpaperFile(file); } catch (error: any) { alert(error?.message || 'Error adding wallpaper. Please try again.'); console.error(error); } e.target.value = ''; }; return (
onChange({ currentWallpapers: e.target.value as string[] })} multiple options={allWallpapers.map((w) => ({ value: w.name, label: w.name }))} />
{Array.isArray(config.currentWallpapers) && config.currentWallpapers.length > 1 && (
onChange({ wallpaperFrequency: `${Number(e.target.value)}h` })} className="liquid-range" style={getRangeStyle( wallpaperFrequencyHours, MIN_WALLPAPER_FREQUENCY_HOURS, MAX_WALLPAPER_FREQUENCY_HOURS, )} /> {formatWallpaperFrequency(wallpaperFrequencyHours)}
)}
onChange({ wallpaperBlur: Number(e.target.value) })} className="liquid-range" style={getRangeStyle(config.wallpaperBlur, 0, 50)} /> {config.wallpaperBlur}px
onChange({ wallpaperBrightness: Number(e.target.value) })} className="liquid-range" style={getRangeStyle(config.wallpaperBrightness, 0, 200)} /> {config.wallpaperBrightness}%
onChange({ wallpaperOpacity: Number(e.target.value) })} className="liquid-range" style={getRangeStyle(config.wallpaperOpacity, 1, 100)} /> {config.wallpaperOpacity}%
{chromeStorageAvailable && ( <>

User Wallpapers

{userWallpapers.map((wallpaper) => (
{wallpaper.name}
))}

Add New Wallpaper

setNewWallpaperName(e.target.value)} className="liquid-input p-2.5" />
setNewWallpaperUrl(e.target.value)} className="liquid-input p-2.5" />
)}
); }; export default ThemeTab;