'use client'; import { useState, useMemo } from 'react'; import { ColorSwatch } from '@/components/pastel/ColorSwatch'; import { Input } from '@/components/ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Card, CardContent } from '@/components/ui/card'; import { AppPage } from '@/components/layout/AppPage'; import { useNamedColors } from '@/lib/pastel/api/queries'; import { Loader2 } from 'lucide-react'; import { parse_color } from '@valknarthing/pastel-wasm'; export default function NamedColorsPage() { const [search, setSearch] = useState(''); const [sortBy, setSortBy] = useState<'name' | 'hue'>('name'); const { data, isLoading, isError } = useNamedColors(); const filteredColors = useMemo(() => { if (!data) return []; let colors = data.colors.filter( (color) => color.name.toLowerCase().includes(search.toLowerCase()) || color.hex.toLowerCase().includes(search.toLowerCase()) ); if (sortBy === 'name') { colors.sort((a, b) => a.name.localeCompare(b.name)); } else if (sortBy === 'hue') { colors.sort((a, b) => { const infoA = parse_color(a.hex); const infoB = parse_color(b.hex); return infoA.hsl[0] - infoB.hsl[0]; }); } return colors; }, [data, search, sortBy]); return (
{/* Search and Filters */}
setSearch(e.target.value)} />
{/* Colors Grid */} {isLoading && (
)} {isError && (
Failed to load named colors
)} {filteredColors.length > 0 && ( <>
Showing {filteredColors.length} colors
{filteredColors.map((color) => (
{color.name}
{color.hex}
))}
)} {filteredColors.length === 0 && !isLoading && !isError && (
No colors match your search
)}
); }