import cx from 'clsx'; import { isEqual } from 'lodash-es'; import { ChangeEvent, useMemo, useState } from 'react'; import { useColorFunction } from '../ColorFunctionContext'; import { HSegmentedControl } from '../components/HSegmentedControl'; import { ScrollArea } from '../components/ScrollArea'; import { ColorDescription } from '../models'; import { ColorCard } from '../page-components/cards-detail/ColorCard'; import { mapToObject } from '../utls'; import styles from './CardsDetail.module.css'; type ColorModes = 'hex' | 'rgb' | 'hsl' | 'lab' | 'oklch'; type CardsDetailProps = { mainTag: string; }; export function CardsDetail({ mainTag }: CardsDetailProps) { const { colorFn } = useColorFunction(); const categories = useMemo(() => { if (!colorFn) { return []; } try { const embededCategories = colorFn.color_categories().map(mapToObject) as { label: string; value: string; }[]; console.debug('[Fetch color categories]', embededCategories); return embededCategories.filter((cate) => !isEqual(cate.value, 'unknown')); } catch (e) { console.error('[Fetch color categories]', e); } return []; }, [colorFn]); const [colorCategory, setCategory] = useState('null'); const handleSelectCategory = (e: ChangeEvent) => { const selectedValue = e.target.value; setCategory(selectedValue); }; const [mode, setMode] = useState('hex'); const colors = useMemo(() => { if (!colorFn) { return []; } try { const colorCate = isEqual(colorCategory, 'null') ? undefined : colorCategory; let tag = ''; switch (mainTag) { case 'japanese': tag = 'japanese_traditional'; break; case 'chinese': tag = 'chinese_traditional'; break; default: tag = ''; break; } const embedColors = colorFn.search_color_cards(tag, colorCate) as ColorDescription[]; console.debug('[Fetch cards]', embedColors); return embedColors; } catch (e) { console.error('[Fetch colors]', e); } return []; }, [colorFn, mainTag, colorCategory]); return (
Show colors.
Copy color value in
setMode(v as ColorModes)} />
{colors.map((c, index) => (
))}
); }