All files / src/components/appearance AppearancePicker.tsx

82.6% Statements 19/23
86.2% Branches 25/29
63.63% Functions 7/11
82.6% Lines 19/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                      16x       95x                                                 16x                 16x                 376x 376x 376x   376x                                                         16x             116x 116x     1700x 1700x 1700x                                                                           16x         116x   928x                                                               16x               116x                                          
/** Icon + accent pickers, shared by every resource the user can style.
 *
 *  The icon set differs per resource type (a board's glyphs say nothing about
 *  an agent), so the pickers take an `IconRegistry` rather than owning one.
 *  The accent palette is platform-wide and needs no such parameter.
 */
 
import GeneratedMark from "@/components/badge/GeneratedMark";
import React from "react";
import { IconRegistry, iconNames, iconOf } from "./icons";
import { Tone, TONE_KEYS, toneOf, TONES } from "./tones";
 
export interface Appearance {
  icon: string;
  accent: Tone;
}
 
/** Read a resource's stored presentation into picker state, substituting the
 *  registry's default glyph and a neutral tone for anything unrecognised. */
export const appearanceOf = (
  registry: IconRegistry,
  icon: string | null | undefined,
  accent: string | null | undefined,
): Appearance => ({
  icon: icon && icon in registry.icons ? icon : registry.fallback,
  accent: toneOf(accent),
});
 
// ─── Swatch ───────────────────────────────────────────────────────────────────
 
export interface AppearanceSwatchProps {
  registry: IconRegistry;
  icon: string | null | undefined;
  accent: string | null | undefined;
  /** Tailwind size classes for the glyph and its tinted surface. */
  size?: "sm" | "md" | "lg" | "xl";
  /** Marks the swatch with a sparkle when the resource was AI-generated.
   *  Here rather than at each call site so every list, card and page header
   *  that already draws a swatch shows provenance without being edited. */
  aiGenerated?: boolean | null;
  /** Accessible name for that sparkle — "AI generated agent". */
  aiLabel?: string;
  className?: string;
}
 
const SWATCH_SIZES: Record<
  NonNullable<AppearanceSwatchProps["size"]>,
  { box: string; glyph: string }
> = {
  sm: { box: "p-1.5 rounded-lg", glyph: "size-4" },
  md: { box: "p-2 rounded-lg", glyph: "size-4" },
  lg: { box: "p-2.5 rounded-lg", glyph: "size-5" },
  xl: { box: "p-3 rounded-xl", glyph: "size-7" },
};
 
/** A resource's icon on its accent-tinted surface — the one way a styled
 *  resource is drawn, so every list, card and header stays consistent. */
export const AppearanceSwatch: React.FC<AppearanceSwatchProps> = ({
  registry,
  icon,
  accent,
  size = "md",
  aiGenerated = false,
  aiLabel,
  className = "",
}) => {
  const Icon = iconOf(registry, icon);
  const tone = TONES[toneOf(accent)];
  const dims = SWATCH_SIZES[size];
 
  return (
    <GeneratedMark
      generated={aiGenerated}
      size={size === "sm" ? "sm" : size === "xl" ? "lg" : "md"}
      label={aiLabel}
      className={className}
    >
      <span
        className={`inline-flex shrink-0 items-center justify-center border ${dims.box} ${tone.bg} ${tone.border}`}
      >
        <Icon className={`${dims.glyph} ${tone.fg}`} />
      </span>
    </GeneratedMark>
  );
};
 
// ─── Pickers ──────────────────────────────────────────────────────────────────
 
export interface IconPickerProps {
  registry: IconRegistry;
  value: string;
  accent?: Tone;
  onChange: (icon: string) => void;
  disabled?: boolean;
}
 
/** Grid of the registry's glyphs. The selected one previews in the current
 *  accent, so icon and colour are chosen against each other rather than
 *  one at a time. */
export const IconPicker: React.FC<IconPickerProps> = ({
  registry,
  value,
  accent,
  onChange,
  disabled,
}) => {
  const tone = accent ? TONES[accent] : null;
  return (
    <div className="flex flex-wrap gap-1.5">
      {iconNames(registry).map((key) => {
        const Icon = registry.icons[key];
        const selected = value === key;
        return (
          <button
            key={key}
            type="button"
            title={key}
            aria-label={key}
            aria-pressed={selected}
            disabled={disabled}
            onClick={() => onChange(key)}
            className={`rounded-lg border p-2 transition-colors disabled:opacity-50 ${
              selected
                ? tone
                  ? `${tone.border} ${tone.bg}`
                  : "border-blue-500 bg-blue-50 dark:bg-blue-900/30"
                : "border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700/50"
            }`}
          >
            <Icon
              className={`w-4 h-4 ${
                selected && tone
                  ? tone.fg
                  : "text-gray-600 dark:text-gray-300"
              }`}
            />
          </button>
        );
      })}
    </div>
  );
};
 
export interface AccentPickerProps {
  value: Tone;
  onChange: (accent: Tone) => void;
  disabled?: boolean;
}
 
/** The platform accent palette as solid swatches. */
export const AccentPicker: React.FC<AccentPickerProps> = ({
  value,
  onChange,
  disabled,
}) => (
  <div className="flex flex-wrap gap-1.5">
    {TONE_KEYS.map((key) => (
      <button
        key={key}
        type="button"
        title={key}
        aria-label={key}
        aria-pressed={value === key}
        disabled={disabled}
        onClick={() => onChange(key)}
        className={`h-6 w-6 rounded-full disabled:opacity-50 ${TONES[key].solid} ${
          value === key
            ? "ring-2 ring-offset-2 ring-gray-400 dark:ring-offset-gray-800"
            : ""
        }`}
      />
    ))}
  </div>
);
 
// ─── Labelled pair ────────────────────────────────────────────────────────────
 
export interface AppearanceFieldsProps {
  registry: IconRegistry;
  value: Appearance;
  onChange: (value: Appearance) => void;
  disabled?: boolean;
  /** Rendered above each picker; omit for a bare pair of controls. */
  labels?: boolean;
  labelClassName?: string;
}
 
/** Both pickers with their labels — the whole appearance control, so a form
 *  adds styling to a resource in one line. */
export const AppearanceFields: React.FC<AppearanceFieldsProps> = ({
  registry,
  value,
  onChange,
  disabled,
  labels = true,
  labelClassName = "mb-1 block text-xs font-medium text-gray-700 dark:text-gray-200",
}) => (
  <div className="space-y-3">
    <div>
      {labels && <span className={labelClassName}>Icon</span>}
      <IconPicker
        registry={registry}
        value={value.icon}
        accent={value.accent}
        disabled={disabled}
        onChange={(icon) => onChange({ ...value, icon })}
      />
    </div>
    <div>
      {labels && <span className={labelClassName}>Accent</span>}
      <AccentPicker
        value={value.accent}
        disabled={disabled}
        onChange={(accent) => onChange({ ...value, accent })}
      />
    </div>
  </div>
);