|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { ChevronRightIcon } from 'lucide-react'; |
| 5 | +import { Input } from '../base/input.tsx'; |
| 6 | +import { Label } from '../base/label.tsx'; |
| 7 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../base/select.tsx'; |
| 8 | +import { cn } from '../lib/utils.ts'; |
| 9 | + |
| 10 | +export type PaletteKind = 'categorical' | 'discrete' | 'continuous'; |
| 11 | + |
| 12 | +export interface PaletteValue { |
| 13 | + kind: PaletteKind; |
| 14 | + colors: string; |
| 15 | + missing_color: string; |
| 16 | +} |
| 17 | + |
| 18 | +interface PaletteSectionProps { |
| 19 | + value: PaletteValue | null; |
| 20 | + onChange: (value: PaletteValue | null) => void; |
| 21 | +} |
| 22 | + |
| 23 | +const DEFAULT_PALETTE: PaletteValue = { kind: 'categorical', colors: '', missing_color: '' }; |
| 24 | + |
| 25 | +export function paletteFromParams(params: Record<string, unknown>): PaletteValue | null { |
| 26 | + const p = params.palette as Record<string, unknown> | null | undefined; |
| 27 | + if (!p || typeof p !== 'object') return null; |
| 28 | + return { |
| 29 | + kind: (p.kind as PaletteKind) ?? 'categorical', |
| 30 | + colors: typeof p.colors === 'string' ? p.colors : '', |
| 31 | + missing_color: typeof p.missing_color === 'string' ? p.missing_color : '', |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +export function paletteToParams(value: PaletteValue | null): Record<string, unknown> | undefined { |
| 36 | + if (!value) return undefined; |
| 37 | + const result: Record<string, unknown> = { kind: value.kind }; |
| 38 | + if (value.colors) result.colors = value.colors; |
| 39 | + if (value.missing_color && value.kind === 'categorical') result.missing_color = value.missing_color; |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +export function PaletteSection({ value, onChange }: PaletteSectionProps) { |
| 44 | + const [open, setOpen] = useState(false); |
| 45 | + |
| 46 | + const palette = value ?? DEFAULT_PALETTE; |
| 47 | + |
| 48 | + const handleEnable = () => { |
| 49 | + if (!value) onChange(DEFAULT_PALETTE); |
| 50 | + setOpen(true); |
| 51 | + }; |
| 52 | + |
| 53 | + const update = (patch: Partial<PaletteValue>) => { |
| 54 | + onChange({ ...palette, ...patch }); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className='flex flex-col gap-1'> |
| 59 | + <button type='button' className='flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground w-fit' onClick={() => { if (!value) handleEnable(); else setOpen(o => !o); }}> |
| 60 | + <ChevronRightIcon className={cn('size-3 transition-transform', open && value && 'rotate-90')} /> |
| 61 | + Palette |
| 62 | + {value && !open && <span className='text-foreground'>({value.kind})</span>} |
| 63 | + </button> |
| 64 | + {open && ( |
| 65 | + <div className='flex flex-col gap-2 pl-4 pt-1'> |
| 66 | + <div className='flex gap-2 items-end'> |
| 67 | + <div className='flex flex-col gap-1'> |
| 68 | + <Label className='text-xs'>Kind</Label> |
| 69 | + <Select value={palette.kind} onValueChange={(v) => update({ kind: v as PaletteKind })}> |
| 70 | + <SelectTrigger size='sm' className='w-32'><SelectValue /></SelectTrigger> |
| 71 | + <SelectContent> |
| 72 | + <SelectItem value='categorical'>categorical</SelectItem> |
| 73 | + <SelectItem value='discrete'>discrete</SelectItem> |
| 74 | + <SelectItem value='continuous'>continuous</SelectItem> |
| 75 | + </SelectContent> |
| 76 | + </Select> |
| 77 | + </div> |
| 78 | + <div className='flex flex-col gap-1 flex-1'> |
| 79 | + <Label className='text-xs'>Colors <span className='text-muted-foreground font-normal'>(name or leave blank)</span></Label> |
| 80 | + <Input className='h-7 text-xs font-mono' placeholder='ResidueName, Viridis, …' value={palette.colors} onChange={(e) => update({ colors: e.target.value })} /> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + {palette.kind === 'categorical' && ( |
| 84 | + <div className='flex flex-col gap-1 w-36'> |
| 85 | + <Label className='text-xs'>Missing color <span className='text-muted-foreground font-normal'>(optional)</span></Label> |
| 86 | + <Input className='h-7 text-xs font-mono' placeholder='yellow' value={palette.missing_color} onChange={(e) => update({ missing_color: e.target.value })} /> |
| 87 | + </div> |
| 88 | + )} |
| 89 | + <button type='button' className='text-xs text-muted-foreground hover:text-destructive w-fit' onClick={() => { onChange(null); setOpen(false); }}> |
| 90 | + Remove palette |
| 91 | + </button> |
| 92 | + </div> |
| 93 | + )} |
| 94 | + </div> |
| 95 | + ); |
| 96 | +} |
0 commit comments