{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "feedback-04",
  "description": "Weekly customer satisfaction against a target, with the change on the week before and how many weeks met it.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "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",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/feedback/feedback-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  ChartTooltipFrame,\n  ChartTooltipItem,\n  chartActiveDot,\n  chartAxisProps,\n  chartGridProps,\n  chartLastDot,\n  chartLineCursor,\n  chartReferenceLabel,\n  chartReferenceProps,\n  chartValueAxisWidth,\n  useChartId,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport { KPIChange, KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport { formatNumber, formatPercent } from '@/registry/lib/dashboardblocks/format'\nimport {\n  Area,\n  AreaChart,\n  CartesianGrid,\n  ReferenceLine,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface CsatPeriod {\n  label: string\n  responses: number\n  satisfied: number\n}\n\ninterface Feedback4Props extends BlockStateProps {\n  description: string\n  /** Oldest first. Leave out, or pass none, to show the empty state. */\n  periods?: CsatPeriod[]\n  /** The CSAT to stay above, 0–1. */\n  target?: number\n  title: string\n}\n\nconst CHART_HEIGHT = 224\n\nconst WEEKS = [\n  'Jul 5',\n  'Jul 12',\n  'Jul 19',\n  'Jul 26',\n  'Aug 2',\n  'Aug 9',\n  'Aug 16',\n  'Aug 23',\n  'Aug 30',\n  'Sep 6',\n  'Sep 13',\n  'Sep 20',\n]\nconst RESPONSES = [412, 398, 441, 430, 455, 468, 472, 459, 481, 503, 497, 516]\nconst SATISFIED = [343, 334, 366, 349, 360, 365, 382, 381, 404, 427, 427, 449]\n\nconst exampleProps: Feedback4Props = {\n  description: 'The share of support survey responses rating us 4 or 5, by week.',\n  periods: WEEKS.map((label, index) => ({\n    label,\n    responses: RESPONSES[index],\n    satisfied: SATISFIED[index],\n  })),\n  target: 0.85,\n  title: 'Customer satisfaction',\n}\n\nconst CSAT_COLOR = 'var(--color-chart-2)'\n\nconst percent = (value: number) => formatPercent(value, { maximumFractionDigits: 0 })\nconst exactPercent = (value: number) =>\n  formatPercent(value, { maximumFractionDigits: 1, minimumFractionDigits: 1 })\n\nconst Feedback4 = (props: Feedback4Props) => {\n  const { description, periods = [], errorMessage, status, target, title } = props\n  const gradient = useChartId()\n  const state = getBlockState(status, periods)\n  const rows = periods.map((period) => ({\n    ...period,\n    csat: period.responses > 0 ? period.satisfied / period.responses : 0,\n  }))\n  const latest = rows[rows.length - 1]\n  const previous = rows[rows.length - 2]\n  const responses = periods.reduce((sum, period) => sum + period.responses, 0)\n  const weeksMet =\n    target === undefined ? 0 : rows.filter((row) => row.csat >= target).length\n  const low = Math.min(...rows.map((row) => row.csat), target ?? 1)\n  const floor = Math.max(0, Math.floor((low - 0.05) * 20) / 20)\n  const step = 1 - floor <= 0.3 ? 0.05 : 0.1\n  const ticks = Array.from(\n    { length: Math.round((1 - floor) / step) + 1 },\n    (_, index) => Math.round((floor + index * step) * 100) / 100,\n  )\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider errorMessage={errorMessage} state={state}>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <p className='text-muted-foreground text-sm'>{description}</p>\n            </header>\n            <dl className='grid grid-cols-2 gap-x-6 gap-y-4 border-t pt-4 @lg:grid-cols-3'>\n              <div className='col-span-2 flex flex-col gap-1 @lg:col-span-1'>\n                <dt className='text-muted-foreground text-sm'>\n                  {state ? 'Latest week' : `Week of ${latest?.label}`}\n                </dt>\n                {state ? (\n                  <dd>\n                    <KPIValue />\n                  </dd>\n                ) : (\n                  <dd className='text-4xl font-light tracking-tight tabular-nums'>\n                    {exactPercent(latest?.csat ?? 0)}\n                  </dd>\n                )}\n                {latest && previous && (\n                  <dd>\n                    <KPIChange\n                      changeType='points'\n                      comparison='the week before'\n                      previous={previous.csat * 100}\n                      value={latest.csat * 100}\n                    />\n                  </dd>\n                )}\n              </div>\n              {state !== 'error' && state !== 'empty' && target !== undefined && (\n                <div className='flex flex-col gap-1'>\n                  <dt className='text-muted-foreground text-sm'>Target</dt>\n                  <dd>\n                    <KPIValue className='text-2xl' format={percent} value={target} />\n                  </dd>\n                  <dd className='text-muted-foreground text-xs tabular-nums'>\n                    <BlockReady className='text-xs'>\n                      Met in {weeksMet} of {rows.length} weeks\n                    </BlockReady>\n                  </dd>\n                </div>\n              )}\n              {state !== 'error' && state !== 'empty' && (\n                <div className='flex flex-col gap-1'>\n                  <dt className='text-muted-foreground text-sm'>Responses</dt>\n                  <dd>\n                    <KPIValue className='text-2xl' value={responses} />\n                  </dd>\n                  <dd className='text-muted-foreground text-xs tabular-nums'>\n                    <BlockReady className='text-xs'>\n                      {formatNumber(Math.round(responses / Math.max(1, rows.length)))} a\n                      week\n                    </BlockReady>\n                  </dd>\n                </div>\n              )}\n            </dl>\n            {state && <ChartPlaceholder height={CHART_HEIGHT} />}\n            {!state && (\n              <ChartPanelFigure className='h-56'>\n                <ResponsiveContainer width='100%' height='100%'>\n                  <AreaChart\n                    aria-label={`${title} by week${target !== undefined ? `, against a target of ${percent(target)}` : ''}`}\n                    data={rows}\n                    margin={{ top: 16, right: 12, bottom: 0, left: 0 }}\n                  >\n                    <defs>\n                      <linearGradient id={gradient} x1='0' x2='0' y1='0' y2='1'>\n                        <stop offset='0%' stopColor={CSAT_COLOR} stopOpacity={0.3} />\n                        <stop offset='100%' stopColor={CSAT_COLOR} stopOpacity={0} />\n                      </linearGradient>\n                    </defs>\n                    <CartesianGrid {...chartGridProps} />\n                    <XAxis\n                      {...chartAxisProps}\n                      dataKey='label'\n                      interval='preserveStartEnd'\n                      minTickGap={20}\n                    />\n                    <YAxis\n                      {...chartAxisProps}\n                      domain={[floor, 1]}\n                      tickFormatter={percent}\n                      ticks={ticks}\n                      width={chartValueAxisWidth}\n                    />\n                    <Tooltip\n                      content={({ active, payload }) => {\n                        const row = payload?.[0]?.payload as\n                          | (typeof rows)[number]\n                          | undefined\n                        if (!active || !row) return null\n                        return (\n                          <ChartTooltipFrame>\n                            <span className='font-medium'>Week of {row.label}</span>\n                            <ChartTooltipItem\n                              color={CSAT_COLOR}\n                              name='CSAT'\n                              value={exactPercent(row.csat)}\n                            />\n                            <ChartTooltipItem\n                              name='Responses'\n                              value={formatNumber(row.responses)}\n                            />\n                          </ChartTooltipFrame>\n                        )\n                      }}\n                      cursor={chartLineCursor}\n                    />\n                    {target !== undefined && (\n                      <ReferenceLine\n                        {...chartReferenceProps}\n                        y={target}\n                        label={{\n                          ...chartReferenceLabel,\n                          position: 'insideTopLeft',\n                          value: `Target ${percent(target)}`,\n                        }}\n                      />\n                    )}\n                    <Area\n                      activeDot={chartActiveDot}\n                      baseValue={floor}\n                      dataKey='csat'\n                      dot={chartLastDot(rows.length, CSAT_COLOR)}\n                      fill={`url(#${gradient})`}\n                      name='CSAT'\n                      stroke={CSAT_COLOR}\n                      strokeWidth={2}\n                      type='monotone'\n                    />\n                  </AreaChart>\n                </ResponsiveContainer>\n              </ChartPanelFigure>\n            )}\n            {!state && (\n              <ChartPanelTable\n                caption={`${title}: ${description}${target !== undefined ? ` Target ${percent(target)}.` : ''}`}\n                columns={[\n                  { key: 'label', label: 'Week of' },\n                  { key: 'csat', label: 'CSAT' },\n                  { key: 'responses', label: 'Responses' },\n                ]}\n                rows={rows.map((row) => ({\n                  csat: exactPercent(row.csat),\n                  label: row.label,\n                  responses: formatNumber(row.responses),\n                }))}\n              />\n            )}\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport {\n  Feedback4,\n  exampleProps as feedback4ExampleProps,\n  type CsatPeriod,\n  type Feedback4Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/feedback/feedback-04.tsx"
    }
  ],
  "meta": {
    "shows": "Weekly customer satisfaction against a target, with the change on the week before and how many weeks met it.",
    "size": "half",
    "usedWith": [
      "feedback-01",
      "feedback-03"
    ],
    "component": "Feedback4",
    "props": "{ description: string; periods?: { label: string; responses: number; satisfied: number }[]; target?: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
