'use client'; import { useState, useEffect, Suspense } from 'react'; import { useSearchParams, useRouter } from 'next/navigation'; import { ColorPicker } from '@/components/pastel/ColorPicker'; import { ColorDisplay } from '@/components/pastel/ColorDisplay'; import { ColorInfo } from '@/components/pastel/ColorInfo'; import { ManipulationPanel } from '@/components/pastel/ManipulationPanel'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { AppPage } from '@/components/layout/AppPage'; import { useColorInfo } from '@/lib/pastel/api/queries'; import { useColorHistory } from '@/lib/pastel/stores/historyStore'; import { Loader2, Share2, History, X } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { toast } from 'sonner'; function PlaygroundContent() { const searchParams = useSearchParams(); const router = useRouter(); const [color, setColor] = useState(() => { // Initialize from URL if available const urlColor = searchParams.get('color'); return urlColor ? `#${urlColor.replace('#', '')}` : '#ff0099'; }); const { data, isLoading, isError, error } = useColorInfo({ colors: [color], }); const colorInfo = data?.colors[0]; // Color history const { history, addColor, removeColor, clearHistory, getRecent } = useColorHistory(); const recentColors = getRecent(10); // Update URL when color changes useEffect(() => { const hex = color.replace('#', ''); if (hex.length === 6 || hex.length === 3) { router.push(`/pastel?color=${hex}`, { scroll: false }); } }, [color, router]); // Debounced history update useEffect(() => { const timer = setTimeout(() => { const hex = color.replace('#', ''); // Only add valid hex colors to history if (hex.length === 6 || hex.length === 3) { addColor(color); } }, 1000); // Wait 1 second before adding to history return () => clearTimeout(timer); }, [color, addColor]); // Share color via URL const handleShare = () => { const url = `${window.location.origin}/pastel?color=${color.replace('#', '')}`; navigator.clipboard.writeText(url); toast.success('Link copied to clipboard!'); }; // Copy color to clipboard const handleCopyColor = () => { navigator.clipboard.writeText(color); toast.success('Color copied to clipboard!'); }; // Random color generation const handleRandomColor = () => { const randomHex = '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0'); setColor(randomHex); }; return ( {/* Left Column: Color Picker and Display */} Color Picker Preview Share {recentColors.length > 0 && ( Recent Colors Clear {recentColors.map((entry) => ( setColor(entry.color)} title={entry.color} role="button" tabIndex={0} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { setColor(entry.color); } }} > { e.stopPropagation(); removeColor(entry.color); toast.success('Color removed from history'); }} className="p-1 bg-destructive rounded-full hover:bg-destructive/80" aria-label="Remove color" > ))} )} {/* Right Column: Color Information */} Color Information {isLoading && ( )} {isError && ( Error loading color information {error?.message || 'Unknown error'} )} {colorInfo && } Color Manipulation ); } export default function PlaygroundPage() { return ( }> ); }
Error loading color information
{error?.message || 'Unknown error'}