feat: unify pastel application into single playground and remove standalone pages
This commit is contained in:
@@ -3,17 +3,32 @@
|
||||
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 { PaletteGrid } from '@/components/pastel/PaletteGrid';
|
||||
import { ExportMenu } from '@/components/pastel/ExportMenu';
|
||||
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 { useColorInfo, useGeneratePalette, useGenerateGradient } from '@/lib/pastel/api/queries';
|
||||
import { Loader2, Share2, Palette, Plus, X, Layers } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import { toast } from 'sonner';
|
||||
|
||||
type HarmonyType =
|
||||
| 'monochromatic'
|
||||
| 'analogous'
|
||||
| 'complementary'
|
||||
| 'triadic'
|
||||
| 'tetradic';
|
||||
|
||||
function PlaygroundContent() {
|
||||
const searchParams = useSearchParams();
|
||||
const router = useRouter();
|
||||
@@ -23,16 +38,23 @@ function PlaygroundContent() {
|
||||
return urlColor ? `#${urlColor.replace('#', '')}` : '#ff0099';
|
||||
});
|
||||
|
||||
// Harmony state
|
||||
const [harmonyType, setHarmonyType] = useState<HarmonyType>('complementary');
|
||||
const [palette, setPalette] = useState<string[]>([]);
|
||||
const paletteMutation = useGeneratePalette();
|
||||
|
||||
// Gradient state
|
||||
const [stops, setStops] = useState<string[]>(['#ff0099', '#0099ff']);
|
||||
const [gradientCount, setGradientCount] = useState(10);
|
||||
const [gradientResult, setGradientResult] = useState<string[]>([]);
|
||||
const gradientMutation = useGenerateGradient();
|
||||
|
||||
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('#', '');
|
||||
@@ -41,18 +63,12 @@ function PlaygroundContent() {
|
||||
}
|
||||
}, [color, router]);
|
||||
|
||||
// Debounced history update
|
||||
// Sync first gradient stop with active color
|
||||
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]);
|
||||
const newStops = [...stops];
|
||||
newStops[0] = color;
|
||||
setStops(newStops);
|
||||
}, [color]);
|
||||
|
||||
// Share color via URL
|
||||
const handleShare = () => {
|
||||
@@ -61,16 +77,59 @@ function PlaygroundContent() {
|
||||
toast.success('Link copied to clipboard!');
|
||||
};
|
||||
|
||||
// Copy color to clipboard
|
||||
const handleCopyColor = () => {
|
||||
navigator.clipboard.writeText(color);
|
||||
toast.success('Color copied to clipboard!');
|
||||
const generateHarmony = async () => {
|
||||
try {
|
||||
const result = await paletteMutation.mutateAsync({
|
||||
base: color,
|
||||
scheme: harmonyType,
|
||||
});
|
||||
|
||||
const colors = [result.palette.primary, ...result.palette.secondary];
|
||||
setPalette(colors);
|
||||
toast.success(`Generated ${harmonyType} harmony palette`);
|
||||
} catch (error) {
|
||||
toast.error('Failed to generate harmony palette');
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Random color generation
|
||||
const handleRandomColor = () => {
|
||||
const randomHex = '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
|
||||
setColor(randomHex);
|
||||
const generateGradient = async () => {
|
||||
try {
|
||||
const result = await gradientMutation.mutateAsync({
|
||||
stops,
|
||||
count: gradientCount,
|
||||
});
|
||||
setGradientResult(result.gradient);
|
||||
toast.success(`Generated ${result.gradient.length} colors`);
|
||||
} catch (error) {
|
||||
toast.error('Failed to generate gradient');
|
||||
}
|
||||
};
|
||||
|
||||
const addStop = () => {
|
||||
setStops([...stops, '#000000']);
|
||||
};
|
||||
|
||||
const removeStop = (index: number) => {
|
||||
if (index === 0) return; // Prevent deleting the first stop (synchronized with picker)
|
||||
if (stops.length > 2) {
|
||||
setStops(stops.filter((_, i) => i !== index));
|
||||
}
|
||||
};
|
||||
|
||||
const updateStop = (index: number, colorValue: string) => {
|
||||
const newStops = [...stops];
|
||||
newStops[index] = colorValue;
|
||||
setStops(newStops);
|
||||
if (index === 0) setColor(colorValue);
|
||||
};
|
||||
|
||||
const harmonyDescriptions: Record<HarmonyType, string> = {
|
||||
monochromatic: 'Single color with variations',
|
||||
analogous: 'Colors adjacent on the color wheel (±30°)',
|
||||
complementary: 'Colors opposite on the color wheel (180°)',
|
||||
triadic: 'Three colors evenly spaced on the color wheel (120°)',
|
||||
tetradic: 'Four colors evenly spaced on the color wheel (90°)',
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -78,112 +137,50 @@ function PlaygroundContent() {
|
||||
title="Pastel"
|
||||
description="Interactive color manipulation and analysis tool"
|
||||
>
|
||||
<div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
|
||||
{/* Left Column: Color Picker and Display */}
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Color Picker</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ColorPicker color={color} onChange={setColor} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<div className="space-y-8">
|
||||
{/* Row 1: Workspace */}
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8 items-stretch">
|
||||
{/* Main Workspace: Color Picker and Information */}
|
||||
<div className="lg:col-span-2">
|
||||
<Card className="h-full">
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
||||
<CardTitle>Preview</CardTitle>
|
||||
<CardTitle>Color Picker</CardTitle>
|
||||
<Button onClick={handleShare} variant="outline" size="sm">
|
||||
<Share2 className="h-4 w-4 mr-2" />
|
||||
Share
|
||||
</Button>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex justify-center">
|
||||
<ColorDisplay color={color} size="xl" />
|
||||
<div className="flex flex-col md:flex-row gap-12">
|
||||
<div className="flex-shrink-0 mx-auto md:mx-0">
|
||||
<ColorPicker color={color} onChange={setColor} />
|
||||
</div>
|
||||
|
||||
<div className="flex-1">
|
||||
<h3 className="text-sm font-medium mb-4 text-muted-foreground uppercase tracking-wider">Color Information</h3>
|
||||
{isLoading && (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isError && (
|
||||
<div className="p-4 bg-destructive/10 text-destructive rounded-lg">
|
||||
<p className="font-medium">Error loading color information</p>
|
||||
<p className="text-sm mt-1">{error?.message || 'Unknown error'}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{colorInfo && <ColorInfo info={colorInfo} />}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{recentColors.length > 0 && (
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<History className="h-5 w-5" />
|
||||
<CardTitle>Recent Colors</CardTitle>
|
||||
</div>
|
||||
<Button
|
||||
onClick={clearHistory}
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
Clear
|
||||
</Button>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="grid grid-cols-5 gap-2">
|
||||
{recentColors.map((entry) => (
|
||||
<div
|
||||
key={entry.timestamp}
|
||||
className="group relative aspect-square rounded-lg border-2 border-border hover:border-primary transition-all hover:scale-110 cursor-pointer"
|
||||
style={{ backgroundColor: entry.color }}
|
||||
onClick={() => setColor(entry.color)}
|
||||
title={entry.color}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
setColor(entry.color);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="absolute inset-0 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity bg-black/30 rounded-lg">
|
||||
<button
|
||||
onClick={(e) => {
|
||||
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"
|
||||
>
|
||||
<X className="h-3 w-3 text-destructive-foreground" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Right Column: Color Information */}
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Color Information</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading && (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isError && (
|
||||
<div className="p-4 bg-destructive/10 text-destructive rounded-lg">
|
||||
<p className="font-medium">Error loading color information</p>
|
||||
<p className="text-sm mt-1">{error?.message || 'Unknown error'}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{colorInfo && <ColorInfo info={colorInfo} />}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
{/* Sidebar: Color Manipulation */}
|
||||
<div className="lg:col-span-1">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle>Color Manipulation</CardTitle>
|
||||
</CardHeader>
|
||||
@@ -193,6 +190,193 @@ function PlaygroundContent() {
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Row 2: Harmony Generator */}
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8 items-stretch">
|
||||
{/* Harmony Controls */}
|
||||
<div className="lg:col-span-1">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Palette className="h-5 w-5" />
|
||||
Harmony Type
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<Select
|
||||
value={harmonyType}
|
||||
onValueChange={(value) => setHarmonyType(value as HarmonyType)}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Select harmony" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="monochromatic">Monochromatic</SelectItem>
|
||||
<SelectItem value="analogous">Analogous</SelectItem>
|
||||
<SelectItem value="complementary">Complementary</SelectItem>
|
||||
<SelectItem value="triadic">Triadic</SelectItem>
|
||||
<SelectItem value="tetradic">Tetradic (Square)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{harmonyDescriptions[harmonyType]}
|
||||
</p>
|
||||
|
||||
<Button
|
||||
onClick={generateHarmony}
|
||||
disabled={paletteMutation.isPending}
|
||||
className="w-full"
|
||||
>
|
||||
{paletteMutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
Generating..
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Palette className="mr-2 h-4 w-4" />
|
||||
Generate Harmony
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Harmony Results */}
|
||||
<div className="lg:col-span-2">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
Generated Palette {palette.length > 0 && `(${palette.length} colors)`}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{palette.length > 0 ? (
|
||||
<div className="space-y-6">
|
||||
<PaletteGrid colors={palette} onColorClick={setColor} />
|
||||
<div className="pt-4 border-t">
|
||||
<ExportMenu colors={palette} />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-12 text-center text-muted-foreground">
|
||||
<Palette className="h-12 w-12 mx-auto mb-4 opacity-50" />
|
||||
<p>Select a harmony type and click Generate to create your palette based on the current color</p>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Row 3: Gradient Generator */}
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8 items-stretch">
|
||||
{/* Gradient Controls */}
|
||||
<div className="lg:col-span-1">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Layers className="h-5 w-5" />
|
||||
Gradient Controls
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium text-muted-foreground uppercase tracking-wider">Color Stops</h4>
|
||||
{stops.map((stop, index) => (
|
||||
<div key={index} className="flex items-center gap-2">
|
||||
<div className="flex-1">
|
||||
<Input
|
||||
type="color"
|
||||
value={stop}
|
||||
onChange={(e) => updateStop(index, e.target.value)}
|
||||
className="h-10 w-full cursor-pointer p-1"
|
||||
/>
|
||||
</div>
|
||||
{index !== 0 && stops.length > 2 && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => removeStop(index)}
|
||||
className="shrink-0"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
<Button onClick={addStop} variant="outline" size="sm" className="w-full">
|
||||
<Plus className="h-4 w-4 mr-2" />
|
||||
Add Stop
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium text-muted-foreground uppercase tracking-wider">Steps</h4>
|
||||
<Input
|
||||
type="number"
|
||||
min={2}
|
||||
max={100}
|
||||
value={gradientCount}
|
||||
onChange={(e) => setGradientCount(parseInt(e.target.value))}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={generateGradient}
|
||||
disabled={gradientMutation.isPending}
|
||||
className="w-full"
|
||||
>
|
||||
{gradientMutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
Generating..
|
||||
</>
|
||||
) : (
|
||||
'Generate Gradient'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Gradient Results */}
|
||||
<div className="lg:col-span-2">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
Generated Gradient {gradientResult.length > 0 && `(${gradientResult.length} colors)`}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{gradientResult.length > 0 ? (
|
||||
<div className="space-y-6">
|
||||
<div
|
||||
className="h-24 w-full rounded-lg border shadow-inner"
|
||||
style={{
|
||||
background: `linear-gradient(to right, ${gradientResult.join(', ')})`,
|
||||
}}
|
||||
/>
|
||||
<PaletteGrid colors={gradientResult} onColorClick={setColor} />
|
||||
<div className="pt-4 border-t">
|
||||
<ExportMenu colors={gradientResult} />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-12 text-center text-muted-foreground">
|
||||
<Layers className="h-12 w-12 mx-auto mb-4 opacity-50" />
|
||||
<p>Add color stops and click Generate to create your smooth gradient</p>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</AppPage>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user