All files / src/components/badge GeneratedMark.tsx

100% Statements 4/4
100% Branches 6/6
100% Functions 1/1
100% Lines 4/4

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                                                                                          106x           106x             1024x 1024x                                            
import { Sparkles } from "lucide-react";
import React from "react";
 
/**
 * The AI-generated indicator: a sparkle on the corner of a resource's icon.
 *
 * It replaced a pill reading "AI Generated" that sat beside the resource
 * name. On a list page that pill competed with the name, the tags and the
 * status badge for the same row of space — three things already saying what
 * the resource *is*, and a fourth saying where it came from, all weighted the
 * same. Provenance is a property of the resource, not another tag, so it
 * belongs on the thing that identifies it rather than in the queue of labels
 * beside it.
 *
 * Wrap the icon, don't replace it — the glyph underneath still says what kind
 * of resource this is:
 *
 * ```tsx
 * <GeneratedMark generated={skill.is_ai_generated}>
 *   <div className="p-2.5 rounded-lg bg-violet-100 dark:bg-violet-900/30">
 *     <Network className="size-5 text-violet-600 dark:text-violet-400" />
 *   </div>
 * </GeneratedMark>
 * ```
 *
 * The wrapper renders either way and only the sparkle is conditional, so
 * `className` — positioning from the call site, "mt-0.5" — always lands on
 * the outermost element rather than on whichever one happens to exist.
 */
 
export interface GeneratedMarkProps {
  /** Whether to draw the sparkle. False renders the icon unmarked. */
  generated?: boolean | null;
  /** Scales the sparkle to the icon it sits on. */
  size?: "sm" | "md" | "lg";
  /** Tooltip and accessible name. Defaults to a generic phrase; pass the
   *  resource noun ("AI generated agent") where one is known. */
  label?: string;
  children: React.ReactNode;
  className?: string;
}
 
const MARK_SIZES: Record<
  NonNullable<GeneratedMarkProps["size"]>,
  { dot: string; glyph: string; offset: string }
> = {
  sm: { dot: "size-3", glyph: "size-2", offset: "-top-1 -right-1" },
  md: { dot: "size-3.5", glyph: "size-2.5", offset: "-top-1 -right-1" },
  lg: { dot: "size-4", glyph: "size-3", offset: "-top-1.5 -right-1.5" },
};
 
export const GeneratedMark: React.FC<GeneratedMarkProps> = ({
  generated = false,
  size = "md",
  label = "AI generated",
  children,
  className = "",
}) => {
  const dims = MARK_SIZES[size];
  return (
    <span className={`relative inline-flex shrink-0 ${className}`}>
      {children}
      {generated && (
        <span
          role="img"
          aria-label={label}
          title={label}
          className={`absolute ${dims.offset} inline-flex items-center justify-center rounded-full ${dims.dot} bg-violet-500 dark:bg-violet-400 ring-2 ring-white dark:ring-gray-800 shadow-sm`}
        >
          <Sparkles
            className={`${dims.glyph} text-white dark:text-gray-900`}
            strokeWidth={2.5}
            aria-hidden="true"
          />
        </span>
      )}
    </span>
  );
};
 
export default GeneratedMark;