{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "feedback",
  "title": "Feedback",
  "description": "Primitives for customer feedback: a rating summary with the average and share of each rating, CSAT as the share of 4 and 5 ratings, net sentiment, stars filled to a fractional rating, and a recharts diverging bar chart of sentiment by topic with a tooltip.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/format.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/feedback.tsx",
      "content": "'use client'\n\nimport { useBlockState } from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  ChartTooltipFrame,\n  ChartTooltipItem,\n  chartAxisProps,\n  chartBarCursor,\n  chartReferenceProps,\n} from '@/registry/components/dashboardblocks/chart'\nimport { ChartPanelLegend } from '@/registry/components/dashboardblocks/chart-panel'\nimport {\n  formatNumber,\n  formatPercent,\n  formatSigned,\n  joinDetails,\n} from '@/registry/lib/dashboardblocks/format'\nimport {\n  Bar,\n  BarChart,\n  ReferenceLine,\n  ResponsiveContainer,\n  Tooltip,\n  type TooltipContentProps,\n  XAxis,\n  YAxis,\n} from 'recharts'\nimport type { NameType, ValueType } from 'recharts/types/component/DefaultTooltipContent'\n\nimport { cn } from '@/lib/utils'\n\n/** The average and share of each rating from counts, where `counts[0]` is the number of 1-star ratings. */\nfunction getRatingSummary(counts: number[]) {\n  const total = counts.reduce((sum, count) => sum + count, 0)\n  const average =\n    total > 0\n      ? counts.reduce((sum, count, index) => sum + count * (index + 1), 0) / total\n      : 0\n  return {\n    average,\n    shares: counts.map((count) => (total > 0 ? count / total : 0)),\n    total,\n  }\n}\n\n/** Customer satisfaction: the share of ratings in the top two of a 1–5 scale (4 and 5), as usually reported. */\nfunction getCsat(counts: number[]) {\n  const total = counts.reduce((sum, count) => sum + count, 0)\n  const satisfied = counts.slice(-2).reduce((sum, count) => sum + count, 0)\n  return total > 0 ? satisfied / total : 0\n}\n\ninterface SentimentCounts {\n  negative: number\n  neutral: number\n  positive: number\n}\n\n/** Positive minus negative, as a share of all mentions: −1 to 1. */\nfunction getNetSentiment({ negative, neutral, positive }: SentimentCounts) {\n  const total = negative + neutral + positive\n  return total > 0 ? (positive - negative) / total : 0\n}\n\n/** Net sentiment as points from −100 to +100. */\nfunction formatNetSentiment(net: number) {\n  return formatSigned(Math.round(net * 100), (value) => formatNumber(value))\n}\n\nconst STAR_PATH =\n  'M12 2.5l2.94 5.96 6.58.96-4.76 4.64 1.12 6.55L12 17.52l-5.88 3.09 1.12-6.55L2.48 9.42l6.58-.96L12 2.5z'\n\ninterface StarRatingProps {\n  className?: string\n  /** @default 5 */\n  max?: number\n  /** Fractions fill part of a star, e.g. */\n  value: number\n}\n\nfunction StarRating({ className, max = 5, value }: StarRatingProps) {\n  return (\n    <span\n      data-slot='star-rating'\n      className={cn('inline-flex items-center gap-0.5 text-amber-500', className)}\n    >\n      <span className='sr-only'>\n        {formatNumber(value)} out of {max} stars\n      </span>\n      {Array.from({ length: max }, (_, index) => {\n        const fill = Math.min(1, Math.max(0, value - index))\n        return (\n          <span key={index} aria-hidden className='relative inline-flex shrink-0'>\n            <svg viewBox='0 0 24 24' className='size-4'>\n              <path d={STAR_PATH} className='fill-muted' />\n            </svg>\n            {fill > 0 && (\n              <span\n                className='absolute inset-y-0 left-0 overflow-hidden'\n                style={{ width: `${fill * 100}%` }}\n              >\n                <svg viewBox='0 0 24 24' className='size-4 max-w-none'>\n                  <path d={STAR_PATH} fill='currentColor' />\n                </svg>\n              </span>\n            )}\n          </span>\n        )\n      })}\n    </span>\n  )\n}\n\nconst sentimentColors = {\n  negative: 'var(--sentiment-negative, var(--color-red-500))',\n  neutral: 'color-mix(in oklab, var(--color-muted-foreground) 35%, transparent)',\n  positive: 'var(--sentiment-positive, var(--color-emerald-600))',\n} as const\n\nconst sentimentLabels: Record<keyof SentimentCounts, string> = {\n  negative: 'Negative',\n  neutral: 'Neutral',\n  positive: 'Positive',\n}\n\ninterface SentimentTopic extends SentimentCounts {\n  label: string\n}\n\ninterface SentimentRow {\n  counts: SentimentTopic\n  label: string\n  negative: number\n  net: number\n  neutralAbove: number\n  neutralBelow: number\n  positive: number\n}\n\nconst ROW_HEIGHT = 40\nconst AXIS_HEIGHT = 28\n\nfunction toSentimentRow(topic: SentimentTopic): SentimentRow {\n  const total = topic.negative + topic.neutral + topic.positive || 1\n  return {\n    counts: topic,\n    label: topic.label,\n    negative: -topic.negative / total,\n    net: getNetSentiment(topic),\n    neutralAbove: topic.neutral / 2 / total,\n    neutralBelow: -topic.neutral / 2 / total,\n    positive: topic.positive / total,\n  }\n}\n\nfunction SentimentTooltipContent({\n  active,\n  payload,\n}: Partial<Pick<TooltipContentProps<ValueType, NameType>, 'active' | 'payload'>>) {\n  const row = payload?.[0]?.payload as SentimentRow | undefined\n  if (!active || !row) return null\n  const total = row.counts.negative + row.counts.neutral + row.counts.positive\n  return (\n    <ChartTooltipFrame>\n      <span className='font-medium'>{row.label}</span>\n      {(['positive', 'neutral', 'negative'] as const).map((sentiment) => (\n        <ChartTooltipItem\n          key={sentiment}\n          color={sentimentColors[sentiment]}\n          name={sentimentLabels[sentiment]}\n          value={joinDetails(\n            formatNumber(row.counts[sentiment]),\n            formatPercent(total > 0 ? row.counts[sentiment] / total : 0, {\n              maximumFractionDigits: 0,\n            }),\n          )}\n        />\n      ))}\n      <ChartTooltipItem name='Net' value={formatNetSentiment(row.net)} />\n    </ChartTooltipFrame>\n  )\n}\n\nconst NET_AXIS_WIDTH = 48\n\ninterface NetTickProps {\n  netByLabel: Map<string, number>\n  payload?: { value?: string }\n  x?: number | string\n  y?: number | string\n}\n\nfunction NetTick({ netByLabel, payload, x, y }: NetTickProps) {\n  const net = netByLabel.get(payload?.value ?? '') ?? 0\n  return (\n    <text\n      x={Number(x)}\n      y={Number(y)}\n      dy={4}\n      textAnchor='start'\n      fontSize={14}\n      fontWeight={500}\n      className={cn(\n        'tabular-nums',\n        net > 0.05\n          ? 'fill-emerald-700 dark:fill-emerald-400'\n          : net < -0.05\n            ? 'fill-red-700 dark:fill-red-400'\n            : 'fill-muted-foreground',\n      )}\n    >\n      {formatNetSentiment(net)}\n    </text>\n  )\n}\n\ninterface SentimentChartProps {\n  className?: string\n  /** Names the chart for keyboard and screen reader users. */\n  label: string\n  /** How many topics to sketch while loading. @default 5 */\n  placeholderRows?: number\n  /** Drawn in this order; sort them first, such as by net sentiment. */\n  topics: SentimentTopic[]\n}\n\nfunction SentimentChart({\n  className,\n  label,\n  placeholderRows = 5,\n  topics,\n}: SentimentChartProps) {\n  const state = useBlockState()\n  const wrapperClassName = cn(\n    'flex flex-col gap-3 [--sentiment-negative:var(--color-red-500)] [--sentiment-positive:var(--color-emerald-600)] dark:[--sentiment-negative:var(--color-red-400)] dark:[--sentiment-positive:var(--color-emerald-500)]',\n    className,\n  )\n  const legend = (\n    <ChartPanelLegend\n      items={(['negative', 'neutral', 'positive'] as const).map((sentiment) => ({\n        color: sentimentColors[sentiment],\n        label: sentimentLabels[sentiment],\n      }))}\n    />\n  )\n\n  if (state) {\n    return (\n      <div data-slot='sentiment-chart' className={wrapperClassName}>\n        {state === 'loading' && legend}\n        <ChartPlaceholder height={placeholderRows * ROW_HEIGHT + AXIS_HEIGHT} />\n      </div>\n    )\n  }\n\n  const rows = topics.map(toSentimentRow)\n  const netByLabel = new Map(rows.map((row) => [row.label, row.net]))\n  const widest = Math.max(\n    0.1,\n    ...rows.map((row) =>\n      Math.max(-row.negative - row.neutralBelow, row.positive + row.neutralAbove),\n    ),\n  )\n  const extent = Math.min(1, Math.ceil(widest * 5) / 5)\n  const ticks = [-extent, -extent / 2, 0, extent / 2, extent]\n\n  return (\n    <div data-slot='sentiment-chart' className={wrapperClassName}>\n      {legend}\n      <div\n        className='w-full tabular-nums'\n        style={{ height: rows.length * ROW_HEIGHT + AXIS_HEIGHT }}\n      >\n        <ResponsiveContainer width='100%' height='100%'>\n          <BarChart\n            accessibilityLayer\n            aria-label={label}\n            barCategoryGap={12}\n            data={rows}\n            layout='vertical'\n            margin={{ top: 0, right: 0, bottom: 0, left: 0 }}\n            stackOffset='sign'\n          >\n            <XAxis\n              {...chartAxisProps}\n              domain={[-extent, extent]}\n              tickFormatter={(value: number) =>\n                formatPercent(Math.abs(value), { maximumFractionDigits: 0 })\n              }\n              ticks={ticks}\n              type='number'\n            />\n            <YAxis\n              {...chartAxisProps}\n              dataKey='label'\n              type='category'\n              width={104}\n              yAxisId='topic'\n            />\n            <YAxis\n              {...chartAxisProps}\n              dataKey='label'\n              orientation='right'\n              tick={(tick: Omit<NetTickProps, 'netByLabel'>) => (\n                <NetTick {...tick} netByLabel={netByLabel} />\n              )}\n              type='category'\n              width={NET_AXIS_WIDTH}\n              yAxisId='net'\n            />\n            <Tooltip content={<SentimentTooltipContent />} cursor={chartBarCursor} />\n            <ReferenceLine {...chartReferenceProps} x={0} yAxisId='topic' />\n            <Bar\n              dataKey='neutralBelow'\n              fill={sentimentColors.neutral}\n              name='Neutral'\n              stackId='sentiment'\n              yAxisId='topic'\n            />\n            <Bar\n              dataKey='negative'\n              fill={sentimentColors.negative}\n              name='Negative'\n              stackId='sentiment'\n              yAxisId='topic'\n            />\n            <Bar\n              dataKey='neutralAbove'\n              fill={sentimentColors.neutral}\n              name='Neutral'\n              stackId='sentiment'\n              yAxisId='topic'\n            />\n            <Bar\n              dataKey='positive'\n              fill={sentimentColors.positive}\n              name='Positive'\n              stackId='sentiment'\n              yAxisId='topic'\n            />\n          </BarChart>\n        </ResponsiveContainer>\n      </div>\n    </div>\n  )\n}\n\nexport {\n  SentimentChart,\n  StarRating,\n  formatNetSentiment,\n  getCsat,\n  getNetSentiment,\n  getRatingSummary,\n  sentimentColors,\n}\n\nexport type { SentimentChartProps, SentimentCounts, SentimentTopic, StarRatingProps }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/feedback.tsx"
    }
  ],
  "type": "registry:component"
}
