{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "review-02",
  "title": "Permission request",
  "description": "An agent asks to run an exact command with its risk, and the choice of how long it lasts (once, this session or always) unfolds a warning before the card resizes into what was allowed.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/agent-run.json",
    "https://ui.ndekocode.dev/r/animated-size.json",
    "https://ui.ndekocode.dev/r/badge.json",
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/chat-motion.json",
    "https://ui.ndekocode.dev/r/chip.json",
    "https://ui.ndekocode.dev/r/copy-button.json",
    "https://ui.ndekocode.dev/r/demonstration.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/kbd.json",
    "https://ui.ndekocode.dev/r/live-duration.json",
    "https://ui.ndekocode.dev/r/live-region.json",
    "https://ui.ndekocode.dev/r/motion-presets.json",
    "https://ui.ndekocode.dev/r/segments.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-autoplay.json",
    "https://ui.ndekocode.dev/r/use-kept-height.json",
    "https://ui.ndekocode.dev/r/use-timeouts.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/review/review-02.tsx",
      "content": "'use client';\n\nimport { AnimatePresence, motion } from 'motion/react';\nimport { useId, useImperativeHandle, useRef, useState } from 'react';\n\nimport {\n  useAutoplay,\n  type AutoplayStep,\n} from '@/registry/ndeko/hooks/use-autoplay';\nimport { useKeptHeight } from '@/registry/ndeko/hooks/use-kept-height';\nimport {\n  seriesColorOf,\n  type ApprovalRequest,\n  type ApprovalScope,\n  type RiskLevel,\n  type RunAgent,\n} from '@/registry/ndeko/lib/agent-run';\nimport {\n  hold,\n  PRESS_LEAD_MS,\n  pressedQuietOf,\n  pressedSolidOf,\n  spokenUnless,\n  usePress,\n} from '@/registry/ndeko/lib/demonstration';\nimport {\n  CheckIcon,\n  CircleAlertIcon,\n  CircleXIcon,\n  RotateCcwIcon,\n  ShieldCheckIcon,\n  TriangleAlertIcon,\n} from '@/registry/ndeko/lib/icons';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { AnimatedSize } from '@/registry/ndeko/ui/animated-size';\nimport { Badge } from '@/registry/ndeko/ui/badge';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport { Fold } from '@/registry/ndeko/ui/chat-motion';\nimport { Chip } from '@/registry/ndeko/ui/chip';\nimport { Kbd } from '@/registry/ndeko/ui/kbd';\n\nimport { CopyButton } from '@/registry/ndeko/ui/copy-button';\nimport { ClockGate } from '@/registry/ndeko/ui/live-duration';\nimport { LiveRegion } from '@/registry/ndeko/ui/live-region';\nimport { fadePresence } from '@/registry/ndeko/lib/motion-presets';\nimport { Segments, type Segment } from '@/registry/ndeko/ui/segments';\nimport { useTimeouts } from '@/registry/ndeko/hooks/use-timeouts';\n\nexport type PermissionEntry = {\n  request: ApprovalRequest;\n  agent: Pick<RunAgent, 'name' | 'series'>;\n  tool: string;\n  /** One line on what could go wrong, shown under the risk. */\n  why: string;\n};\n\nexport type PermissionDecision = 'allowed' | 'denied';\n\n/** The decision a demonstration holds down before it takes it. */\nexport type PermissionPress = 'allow' | 'deny';\n\nexport type PermissionRequestHandle = {\n  /** Picks how long the permission lasts, as the segmented switch does. */\n  pickScope: (scope: ApprovalScope) => void;\n  allow: () => void;\n  deny: () => void;\n};\n\nconst SCOPES: Segment<ApprovalScope>[] = [\n  { value: 'once', label: 'Once' },\n  { value: 'session', label: 'This session' },\n  { value: 'always', label: 'Always' },\n];\n\nconst SCOPE_WORDS: Record<ApprovalScope, string> = {\n  once: 'once',\n  session: 'for this session',\n  always: 'always, in every session',\n};\n\ntype RiskLook = {\n  label: string;\n  variant: 'success' | 'warning' | 'destructive';\n  icon: React.ReactNode;\n};\n\nconst RISK_LOOK: Record<RiskLevel, RiskLook> = {\n  low: {\n    label: 'Low risk',\n    variant: 'success',\n    icon: <ShieldCheckIcon />,\n  },\n  medium: {\n    label: 'Medium risk',\n    variant: 'warning',\n    icon: <TriangleAlertIcon />,\n  },\n  high: {\n    label: 'High risk',\n    variant: 'destructive',\n    icon: <CircleAlertIcon />,\n  },\n};\n\nfunction resultLineOf(\n  decision: PermissionDecision,\n  scope: ApprovalScope,\n  agentName: string\n) {\n  if (decision === 'denied') {\n    return `denied, ${agentName} will not run it`;\n  }\n  return `allowed ${SCOPE_WORDS[scope]}`;\n}\n\nfunction ResultLine({\n  entry,\n  decision,\n  scope,\n}: {\n  entry: PermissionEntry;\n  decision: PermissionDecision;\n  scope: ApprovalScope;\n}) {\n  const isAllowed = decision === 'allowed';\n  const line = resultLineOf(decision, scope, entry.agent.name);\n\n  return (\n    <div className='flex items-start gap-2.5 p-3.5 text-[13px]'>\n      {isAllowed && (\n        <CheckIcon className='text-success-foreground mt-0.5 size-4 shrink-0' />\n      )}\n      {!isAllowed && (\n        <CircleXIcon className='text-muted-foreground mt-0.5 size-4 shrink-0' />\n      )}\n      <p className='min-w-0 leading-5'>\n        <code className='bg-foreground/[0.05] rounded-sm px-1 py-0.5 font-mono text-xs break-all'>\n          {entry.request.command}\n        </code>{' '}\n        <span className='text-muted-foreground'>{line}</span>\n      </p>\n    </div>\n  );\n}\n\ntype RequestBodyProps = {\n  entry: PermissionEntry;\n  scope: ApprovalScope;\n  pressed: PermissionPress | null;\n  onScopeChange: (scope: ApprovalScope) => void;\n  onAllow: () => void;\n  onDeny: () => void;\n};\n\nfunction RequestBody({\n  entry,\n  scope,\n  pressed,\n  onScopeChange,\n  onAllow,\n  onDeny,\n}: RequestBodyProps) {\n  const titleId = useId();\n  const keysId = useId();\n  const warningId = useId();\n  const { request, agent, tool } = entry;\n  const risk = RISK_LOOK[request.risk];\n  const isAlways = scope === 'always';\n  const command = request.command ?? '';\n  const chipColor = seriesColorOf(agent.series);\n\n  function decideFromKeys(event: React.KeyboardEvent<HTMLDivElement>) {\n    if (event.key === 'Escape') {\n      event.preventDefault();\n      onDeny();\n      return;\n    }\n    const isOnCard = event.target === event.currentTarget;\n    const isEnter = event.key === 'Enter';\n    if (isEnter && isOnCard) {\n      event.preventDefault();\n      onAllow();\n    }\n  }\n\n  return (\n    <div\n      role='group'\n      tabIndex={0}\n      aria-labelledby={titleId}\n      aria-describedby={keysId}\n      onKeyDown={decideFromKeys}\n      className='focus-visible:ring-ring/25 @container flex flex-col gap-3 rounded-xl p-4 outline-none focus-visible:ring-[3px]'\n    >\n      <div className='flex flex-wrap items-center gap-2'>\n        <Chip color={chipColor}>{agent.name}</Chip>\n        <span className='text-muted-foreground text-xs'>\n          wants to use <span className='text-foreground font-mono'>{tool}</span>\n        </span>\n      </div>\n      <p id={titleId} className='text-sm font-medium text-balance'>\n        {request.title}\n      </p>\n      <div className='bg-foreground/[0.04] flex items-start gap-2 rounded-lg py-2 ps-3 pe-1.5'>\n        <code className='min-w-0 flex-1 py-0.5 font-mono text-xs leading-5 break-all'>\n          <span\n            aria-hidden='true'\n            className='text-muted-foreground select-none'\n          >\n            ${' '}\n          </span>\n          {command}\n        </code>\n        <CopyButton text={command} label='Copy the command' />\n      </div>\n      <div className='flex flex-col gap-1.5'>\n        <Badge variant={risk.variant}>\n          {risk.icon}\n          {risk.label}\n        </Badge>\n        <p className='text-muted-foreground text-xs leading-5'>{entry.why}</p>\n      </div>\n      <div className='flex flex-col gap-1.5'>\n        <span className='text-xs font-medium'>Allow it</span>\n        <Segments\n          value={scope}\n          segments={SCOPES}\n          onValueChange={onScopeChange}\n          label='How long the permission lasts'\n          className='w-full @sm:w-fit'\n        />\n        <Fold open={isAlways} id={warningId}>\n          <p className='text-warning-foreground flex items-start gap-1.5 pt-1 text-xs leading-5'>\n            <TriangleAlertIcon className='mt-0.5 size-3.5 shrink-0' />\n            {agent.name} will run this command without asking, in this session\n            and every next one, until you take it back in settings.\n          </p>\n        </Fold>\n      </div>\n      <div className='flex flex-wrap items-center gap-2'>\n        <Button\n          type='button'\n          size='sm'\n          onClick={onAllow}\n          className={pressedSolidOf(pressed === 'allow')}\n        >\n          Allow\n          <Kbd className='ms-0.5'>Enter</Kbd>\n        </Button>\n        <Button\n          type='button'\n          size='sm'\n          variant='outline'\n          onClick={onDeny}\n          className={pressedQuietOf(pressed === 'deny')}\n        >\n          Deny\n          <Kbd className='ms-0.5'>Esc</Kbd>\n        </Button>\n        <span id={keysId} className='sr-only'>\n          Enter allows, Escape denies\n        </span>\n      </div>\n    </div>\n  );\n}\n\ntype PickedScope = { entryId: string; scope: ApprovalScope };\n\n/** A new request starts from its own suggested scope, never from the last one picked. */\nfunction pickedOf(entry: PermissionEntry): PickedScope {\n  const scope = entry.request.scope ?? 'once';\n  return { entryId: entry.request.id, scope };\n}\n\nexport type PermissionRequestCardProps = {\n  entry: PermissionEntry;\n  /** Set once a decision is taken: the card resizes into one result line. */\n  result?: { decision: PermissionDecision; scope: ApprovalScope } | null;\n  onDecide?: (decision: PermissionDecision, scope: ApprovalScope) => void;\n  /** Picks a scope or decides from outside, for a demonstration or a shortcut of the page. */\n  handleRef?: React.Ref<PermissionRequestHandle>;\n  /** Shows Allow or Deny held down, for a demonstration that presses it. */\n  pressed?: PermissionPress | null;\n  className?: string;\n};\n\nexport function PermissionRequestCard({\n  entry,\n  result = null,\n  onDecide,\n  handleRef,\n  pressed = null,\n  className,\n}: PermissionRequestCardProps) {\n  const entryId = entry.request.id;\n  const [picked, setPicked] = useState<PickedScope>(() => pickedOf(entry));\n  if (picked.entryId !== entryId) {\n    setPicked(pickedOf(entry));\n  }\n  const scope = picked.scope;\n  const fade = fadePresence();\n\n  function changeScope(next: ApprovalScope) {\n    setPicked({ entryId, scope: next });\n  }\n  const swapKey = swapKeyOf(entry, result);\n\n  function allow() {\n    onDecide?.('allowed', scope);\n  }\n\n  function deny() {\n    onDecide?.('denied', scope);\n  }\n\n  useImperativeHandle(handleRef, () => ({\n    pickScope: changeScope,\n    allow,\n    deny,\n  }));\n\n  return (\n    <div className={cn('surface-raised overflow-hidden rounded-xl', className)}>\n      <AnimatedSize>\n        <AnimatePresence initial={false} mode='popLayout'>\n          <motion.div key={swapKey} {...fade}>\n            {result && (\n              <ResultLine\n                entry={entry}\n                decision={result.decision}\n                scope={result.scope}\n              />\n            )}\n            {!result && (\n              <RequestBody\n                entry={entry}\n                scope={scope}\n                pressed={pressed}\n                onScopeChange={changeScope}\n                onAllow={allow}\n                onDeny={deny}\n              />\n            )}\n          </motion.div>\n        </AnimatePresence>\n      </AnimatedSize>\n    </div>\n  );\n}\n\nfunction swapKeyOf(\n  entry: PermissionEntry,\n  result: PermissionRequestCardProps['result']\n) {\n  if (result) {\n    return `${entry.request.id}-result`;\n  }\n  return `${entry.request.id}-ask`;\n}\n\nconst CODER: Pick<RunAgent, 'name' | 'series'> = { name: 'Coder', series: 5 };\n\nconst REQUESTS: PermissionEntry[] = [\n  {\n    request: {\n      id: 'diff',\n      title: 'See which files changed since the last commit',\n      risk: 'low',\n      status: 'pending',\n      command: 'git diff --stat',\n    },\n    agent: CODER,\n    tool: 'shell',\n    why: 'It only reads the repository. Nothing is written or sent.',\n  },\n  {\n    request: {\n      id: 'install',\n      title: 'Add a CSV parser to the project',\n      risk: 'medium',\n      status: 'pending',\n      command: 'npm install csv-parse@5.6.0',\n    },\n    agent: CODER,\n    tool: 'shell',\n    why: 'It downloads code from the npm registry and changes package.json and the lockfile.',\n  },\n  {\n    request: {\n      id: 'cache',\n      title: 'Clear the cache of old price pages',\n      risk: 'high',\n      status: 'pending',\n      command: 'rm -rf ./tmp/cache',\n    },\n    agent: CODER,\n    tool: 'shell',\n    why: 'It deletes 214 files that are not in git. They cannot be brought back.',\n  },\n];\n\nconst NEXT_REQUEST_MS = 1800;\n\ntype Result = NonNullable<PermissionRequestCardProps['result']>;\n\nfunction announcementOf(entry: PermissionEntry, result: Result | null) {\n  const command = entry.request.command ?? '';\n  if (!result) {\n    return `${entry.agent.name} asks to run ${command}`;\n  }\n  const line = resultLineOf(result.decision, result.scope, entry.agent.name);\n  return `${command}: ${line}`;\n}\n\n/** A request that waits on a person is answered by the demonstration after this long. */\nconst DECIDE_AFTER_MS = 2200;\n\nexport default function Review02() {\n  const [index, setIndex] = useState(0);\n  const [result, setResult] = useState<Result | null>(null);\n  const { schedule, clearAll } = useTimeouts();\n  const { pressed, press } = usePress<PermissionPress>();\n  const card = useRef<PermissionRequestHandle>(null);\n  const entry = REQUESTS[index];\n  const isLast = index === REQUESTS.length - 1;\n  const isFinished = isLast && result !== null;\n  const announcement = announcementOf(entry, result);\n  const stepLabel = `Request ${index + 1} of ${REQUESTS.length}`;\n\n  function decide(decision: PermissionDecision, scope: ApprovalScope) {\n    if (result) {\n      return;\n    }\n    setResult({ decision, scope });\n    if (isLast) {\n      return;\n    }\n    schedule(() => {\n      setResult(null);\n      setIndex((current) => current + 1);\n    }, NEXT_REQUEST_MS);\n  }\n\n  function startOver() {\n    clearAll();\n    setResult(null);\n    setIndex(0);\n  }\n\n  const nextAfter = NEXT_REQUEST_MS + DECIDE_AFTER_MS;\n  const steps: AutoplayStep[] = [\n    { after: 1400, run: () => card.current?.pickScope('session') },\n    { after: 900, run: () => card.current?.pickScope('always') },\n    { after: 1800, run: () => card.current?.pickScope('session') },\n    { after: 800, run: () => press('allow') },\n    { after: PRESS_LEAD_MS, run: () => card.current?.allow() },\n    { after: nextAfter, run: () => press('allow') },\n    { after: PRESS_LEAD_MS, run: () => card.current?.allow() },\n    { after: nextAfter, run: () => press('deny') },\n    { after: PRESS_LEAD_MS, run: () => card.current?.deny() },\n    { after: 1600, run: hold },\n  ];\n  const { ref, isInView, isAutoplaying } = useAutoplay(steps, {\n    loopAfter: 2600,\n    onReset: startOver,\n  });\n  const canStartOver = isFinished && !isAutoplaying;\n\n  useKeptHeight(ref);\n\n  return (\n    <div\n      ref={ref}\n      className='flex min-h-[27rem] w-full max-w-[26rem] flex-col gap-2'\n    >\n      <ClockGate isActive={isInView}>\n        <PermissionRequestCard\n          entry={entry}\n          result={result}\n          onDecide={decide}\n          handleRef={card}\n          pressed={pressed}\n        />\n      </ClockGate>\n      <div className='flex h-7 items-center justify-between gap-2 px-1'>\n        <span className='text-muted-foreground text-xs tabular-nums'>\n          {stepLabel}\n        </span>\n        {canStartOver && (\n          <Button\n            type='button'\n            variant='ghost'\n            size='sm'\n            onClick={startOver}\n            className='text-muted-foreground'\n          >\n            <RotateCcwIcon />\n            Start over\n          </Button>\n        )}\n      </div>\n      <LiveRegion message={spokenUnless(isAutoplaying, announcement)} />\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "review"
  ],
  "type": "registry:component"
}