{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "prompt-library",
  "title": "Prompt library",
  "description": "Folder sidebar with counts and a starred filter, searchable prompt cards with highlighted {{variables}}, tags, versions and usage, a use dialog that fills variables into a live preview with copy and send, plus new prompt and delete with undo.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/badge.json",
    "https://goodui.dev/r/button.json",
    "https://goodui.dev/r/dialog.json",
    "https://goodui.dev/r/input.json"
  ],
  "files": [
    {
      "path": "registry/blocks/ai/prompt-library.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { Braces, Check, Copy, FolderOpen, Plus, Search, Send, Star, Tag, Trash2, Undo2 } from \"lucide-react\"\nimport { Badge } from \"@/components/goodui/badge\"\nimport { Button } from \"@/components/goodui/button\"\nimport { Dialog } from \"@/components/goodui/dialog\"\nimport { Input } from \"@/components/goodui/input\"\nimport { cn } from \"@/lib/utils\"\n\n/* ---------- Demo data ---------- */\n\ntype Prompt = { id: string; title: string; folder: string; tags: string[]; body: string; uses: number; starred: boolean; version: number; updated: string }\nconst FOLDERS = [\"Client messages\", \"Marketing\", \"Internal\", \"Support\"]\nconst INITIAL: Prompt[] = [\n  { id: \"p1\", title: \"Rebooking nudge\", folder: \"Client messages\", tags: [\"sms\", \"retention\"], body: \"Write a warm, 2-sentence SMS to {{client_name}} who last visited {{weeks_since}} weeks ago for a {{service}}. Mention that {{stylist}} has openings this week. No emojis, no discount.\", uses: 412, starred: true, version: 3, updated: \"2 days ago\" },\n  { id: \"p2\", title: \"Review reply (negative)\", folder: \"Support\", tags: [\"reviews\", \"tone\"], body: \"Reply to this {{rating}}-star review from {{client_name}} in a calm, accountable tone. Acknowledge the specific issue, avoid excuses, invite them to contact {{owner_name}} directly. Max 80 words.\\n\\nReview: {{review_text}}\", uses: 188, starred: true, version: 5, updated: \"Yesterday\" },\n  { id: \"p3\", title: \"Instagram caption\", folder: \"Marketing\", tags: [\"social\"], body: \"Write 3 Instagram caption options for a photo of a {{service}} by {{stylist}}. Playful but not cringe, one line each, include 5 niche hashtags for a salon in {{city}}.\", uses: 96, starred: false, version: 2, updated: \"1 week ago\" },\n  { id: \"p4\", title: \"Aftercare email\", folder: \"Client messages\", tags: [\"email\"], body: \"Draft a short aftercare email for {{client_name}} after a {{service}}. Include 3 bullet tips, the recommended products {{products}}, and when to book the next visit.\", uses: 240, starred: false, version: 1, updated: \"3 weeks ago\" },\n  { id: \"p5\", title: \"Shift swap announcement\", folder: \"Internal\", tags: [\"team\"], body: \"Write a friendly team message announcing that {{stylist}} is off on {{date}} and asking who can cover the {{shift}} shift. Keep it under 50 words.\", uses: 31, starred: false, version: 1, updated: \"1 month ago\" },\n  { id: \"p6\", title: \"Google Business post\", folder: \"Marketing\", tags: [\"social\", \"seo\"], body: \"Write a 60-word Google Business Profile post promoting {{offer}} valid until {{date}}. End with a clear call to book at {{url}}.\", uses: 58, starred: false, version: 2, updated: \"5 days ago\" },\n]\nconst VARS = (body: string) => Array.from(new Set(Array.from(body.matchAll(/{{(\\w+)}}/g), (m) => m[1]!)))\n\n/* ---------- Block ---------- */\n\nexport default function PromptLibrary() {\n  const [prompts, setPrompts] = React.useState<Prompt[]>(INITIAL)\n  const [folder, setFolder] = React.useState<string>(\"All\")\n  const [query, setQuery] = React.useState(\"\")\n  const [onlyStarred, setOnlyStarred] = React.useState(false)\n  const [openId, setOpenId] = React.useState<string | null>(null)\n  const [values, setValues] = React.useState<Record<string, string>>({})\n  const [copied, setCopied] = React.useState(false)\n  const [sent, setSent] = React.useState(false)\n  const [creating, setCreating] = React.useState(false)\n  const [draft, setDraft] = React.useState({ title: \"\", body: \"\", folder: FOLDERS[0]! })\n  const [removed, setRemoved] = React.useState<{ p: Prompt; index: number } | null>(null)\n\n  const visible = prompts.filter((p) => (folder === \"All\" || p.folder === folder) && (!onlyStarred || p.starred) && (p.title + p.body + p.tags.join(\" \")).toLowerCase().includes(query.toLowerCase()))\n  const current = prompts.find((p) => p.id === openId) ?? null\n  const vars = current ? VARS(current.body) : []\n  const filled = current ? current.body.replace(/{{(\\w+)}}/g, (_, k: string) => values[k] || `{{${k}}}`) : \"\"\n  const missing = vars.filter((v) => !values[v])\n\n  const open = (p: Prompt) => {\n    setOpenId(p.id)\n    setValues({})\n    setCopied(false)\n    setSent(false)\n  }\n  const copy = () => {\n    void navigator.clipboard?.writeText(filled)\n    setCopied(true)\n    window.setTimeout(() => setCopied(false), 1500)\n  }\n  const use = () => {\n    if (!current) return\n    setPrompts((all) => all.map((p) => (p.id === current.id ? { ...p, uses: p.uses + 1 } : p)))\n    setSent(true)\n  }\n  const toggleStar = (id: string) => setPrompts((all) => all.map((p) => (p.id === id ? { ...p, starred: !p.starred } : p)))\n  const remove = (p: Prompt) => {\n    const index = prompts.findIndex((x) => x.id === p.id)\n    setRemoved({ p, index })\n    setPrompts(prompts.filter((x) => x.id !== p.id))\n    setOpenId(null)\n    window.setTimeout(() => setRemoved((r) => (r?.p.id === p.id ? null : r)), 6000)\n  }\n  const create = () => {\n    if (!draft.title.trim() || !draft.body.trim()) return\n    setPrompts([{ id: `n${prompts.length + 1}-${draft.title.length}`, title: draft.title.trim(), folder: draft.folder, tags: [], body: draft.body, uses: 0, starred: false, version: 1, updated: \"Just now\" }, ...prompts])\n    setDraft({ title: \"\", body: \"\", folder: FOLDERS[0]! })\n    setCreating(false)\n  }\n\n  return (\n    <div className=\"p-4 md:p-6\">\n      <div className=\"mx-auto grid max-w-6xl gap-5 md:grid-cols-[220px_1fr]\">\n        {/* Folders */}\n        <aside className=\"space-y-1\">\n          <div className=\"mb-3 flex items-center justify-between px-1\">\n            <h2 className=\"font-heading text-lg font-semibold tracking-tight\">Prompts</h2>\n            <Button size=\"xs\" iconOnly aria-label=\"New prompt\" onClick={() => setCreating(true)}>\n              <Plus />\n            </Button>\n          </div>\n          {[\"All\", ...FOLDERS].map((f) => {\n            const n = f === \"All\" ? prompts.length : prompts.filter((p) => p.folder === f).length\n            return (\n              <button key={f} type=\"button\" aria-pressed={folder === f} onClick={() => setFolder(f)} className={cn(\"flex w-full items-center gap-2 rounded-lg px-2.5 py-1.5 text-sm transition-colors\", folder === f ? \"bg-muted font-medium\" : \"text-muted-foreground hover:bg-muted/60 hover:text-foreground\")}>\n                <FolderOpen className=\"size-4\" />\n                <span className=\"flex-1 text-left\">{f}</span>\n                <span className=\"text-xs tabular-nums opacity-60\">{n}</span>\n              </button>\n            )\n          })}\n          <button type=\"button\" aria-pressed={onlyStarred} onClick={() => setOnlyStarred((s) => !s)} className={cn(\"mt-2 flex w-full items-center gap-2 rounded-lg px-2.5 py-1.5 text-sm transition-colors\", onlyStarred ? \"bg-amber-500/15 font-medium text-amber-800 dark:text-amber-300\" : \"text-muted-foreground hover:bg-muted/60 hover:text-foreground\")}>\n            <Star className={cn(\"size-4\", onlyStarred && \"fill-current\")} />\n            <span className=\"flex-1 text-left\">Starred</span>\n            <span className=\"text-xs tabular-nums opacity-60\">{prompts.filter((p) => p.starred).length}</span>\n          </button>\n          <div aria-live=\"polite\" className=\"pt-2\">\n            {removed && (\n              <div className=\"rounded-lg border border-dashed p-2 text-xs\">\n                Deleted “{removed.p.title}”.\n                <Button\n                  size=\"xs\"\n                  variant=\"link\"\n                  iconLeft={<Undo2 />}\n                  className=\"ml-1\"\n                  onClick={() => {\n                    const next = [...prompts]\n                    next.splice(removed.index, 0, removed.p)\n                    setPrompts(next)\n                    setRemoved(null)\n                  }}\n                >\n                  Undo\n                </Button>\n              </div>\n            )}\n          </div>\n        </aside>\n\n        {/* Grid */}\n        <div className=\"min-w-0\">\n          <Input label=\"Search prompts\" labelMode=\"none\" leading={<Search />} placeholder=\"Search titles, text or tags…\" value={query} onValueChange={setQuery} clearable fullWidth />\n          <div className=\"mt-4 grid gap-3 sm:grid-cols-2 xl:grid-cols-3\">\n            {visible.map((p) => (\n              <article key={p.id} className=\"group relative flex flex-col rounded-2xl border bg-card p-4 shadow-xs transition-all hover:-translate-y-0.5 hover:shadow-md motion-reduce:transition-none motion-reduce:hover:translate-y-0\">\n                <div className=\"flex items-start justify-between gap-2\">\n                  <button type=\"button\" onClick={() => open(p)} className=\"text-left font-medium hover:underline\">\n                    {p.title}\n                  </button>\n                  <button type=\"button\" aria-label={p.starred ? \"Unstar\" : \"Star\"} aria-pressed={p.starred} onClick={() => toggleStar(p.id)} className={cn(\"rounded p-0.5 transition-colors\", p.starred ? \"text-amber-500\" : \"text-muted-foreground/50 hover:text-amber-500\")}>\n                    <Star className={cn(\"size-4\", p.starred && \"fill-current\")} />\n                  </button>\n                </div>\n                <p className=\"mt-2 line-clamp-3 font-mono text-xs leading-relaxed text-muted-foreground\">\n                  {p.body.split(/({{\\w+}})/g).map((part, i) => (part.startsWith(\"{{\") ? <span key={i} className=\"rounded bg-primary/10 px-1 text-primary\">{part.slice(2, -2)}</span> : part))}\n                </p>\n                <div className=\"mt-3 flex flex-wrap items-center gap-1.5\">\n                  {p.tags.map((t) => (\n                    <Badge key={t} variant=\"soft\" tone=\"neutral\" size=\"xs\" icon={<Tag />}>\n                      {t}\n                    </Badge>\n                  ))}\n                  <span className=\"ml-auto inline-flex items-center gap-1 text-[11px] text-muted-foreground\">\n                    <Braces className=\"size-3\" /> {VARS(p.body).length}\n                  </span>\n                </div>\n                <div className=\"mt-3 flex items-center justify-between border-t pt-3 text-[11px] text-muted-foreground\">\n                  <span>\n                    v{p.version} · {p.updated}\n                  </span>\n                  <span className=\"tabular-nums\">{p.uses} uses</span>\n                </div>\n                <button type=\"button\" onClick={() => open(p)} className=\"absolute inset-0 rounded-2xl outline-none focus-visible:ring-2 focus-visible:ring-ring\" aria-label={`Open ${p.title}`} tabIndex={-1} />\n              </article>\n            ))}\n            {visible.length === 0 && <div className=\"col-span-full rounded-2xl border border-dashed p-10 text-center text-sm text-muted-foreground\">No prompts here yet.</div>}\n          </div>\n        </div>\n      </div>\n\n      {/* Use dialog */}\n      <Dialog\n        open={!!current}\n        onOpenChange={(o) => !o && setOpenId(null)}\n        title={current?.title}\n        description={current ? `${current.folder} · v${current.version} · ${current.uses} uses` : undefined}\n        icon={<Braces />}\n        size=\"lg\"\n        mobile=\"sheet\"\n        footer={({ close }) => (\n          <div className=\"flex w-full flex-wrap items-center justify-between gap-2\">\n            <Button size=\"sm\" variant=\"ghost\" tone=\"danger\" iconLeft={<Trash2 />} onClick={() => current && remove(current)}>\n              Delete\n            </Button>\n            <div className=\"flex gap-2\">\n              <Button size=\"sm\" variant=\"outline\" tone=\"neutral\" iconLeft={copied ? <Check /> : <Copy />} onClick={copy}>\n                {copied ? \"Copied\" : \"Copy\"}\n              </Button>\n              <Button size=\"sm\" iconLeft={sent ? <Check /> : <Send />} tone={sent ? \"success\" : \"primary\"} onClick={sent ? close : use} disabled={missing.length > 0}>\n                {sent ? \"Sent to chat\" : \"Use prompt\"}\n              </Button>\n            </div>\n          </div>\n        )}\n      >\n        {current && (\n          <div className=\"grid gap-4 sm:grid-cols-[200px_1fr]\">\n            <div className=\"space-y-3\">\n              <div className=\"text-xs font-medium text-muted-foreground\">Fill in {vars.length} variables</div>\n              {vars.map((v) => (\n                <Input key={v} size=\"sm\" label={v.replace(/_/g, \" \")} value={values[v] ?? \"\"} onValueChange={(val) => setValues({ ...values, [v]: val })} placeholder={`{{${v}}}`} fullWidth />\n              ))}\n              {vars.length === 0 && <p className=\"text-xs text-muted-foreground\">No variables in this prompt.</p>}\n            </div>\n            <div>\n              <div className=\"text-xs font-medium text-muted-foreground\">Preview</div>\n              <pre className=\"mt-1 max-h-72 overflow-auto whitespace-pre-wrap rounded-xl border bg-muted/40 p-3 font-mono text-xs leading-relaxed\">\n                {filled.split(/({{\\w+}})/g).map((part, i) => (part.startsWith(\"{{\") ? <mark key={i} className=\"rounded bg-amber-500/20 px-1 text-amber-900 dark:text-amber-200\">{part}</mark> : part))}\n              </pre>\n              {missing.length > 0 && <p className=\"mt-2 text-xs text-muted-foreground\">{missing.length} to fill before sending.</p>}\n            </div>\n          </div>\n        )}\n      </Dialog>\n\n      {/* New prompt */}\n      <Dialog open={creating} onOpenChange={setCreating} title=\"New prompt\" description=\"Use {{variable}} for anything that changes per use.\" icon={<Plus />} mobile=\"sheet\" onConfirm={create} closeOnConfirm={false} confirmText=\"Save prompt\">\n        <div className=\"grid gap-3\">\n          <Input label=\"Title\" value={draft.title} onValueChange={(v) => setDraft({ ...draft, title: v })} placeholder=\"e.g. Waitlist opening SMS\" fullWidth autoFocus />\n          <div className=\"flex flex-wrap gap-1.5\">\n            {FOLDERS.map((f) => (\n              <button key={f} type=\"button\" aria-pressed={draft.folder === f} onClick={() => setDraft({ ...draft, folder: f })} className={cn(\"rounded-full border px-2.5 py-1 text-xs font-medium\", draft.folder === f ? \"border-foreground bg-foreground text-background\" : \"text-foreground/70\")}>\n                {f}\n              </button>\n            ))}\n          </div>\n          <Input label=\"Prompt\" multiline rows={5} value={draft.body} onValueChange={(v) => setDraft({ ...draft, body: v })} placeholder=\"Write a short SMS to {{client_name}}…\" fullWidth className=\"font-mono text-xs\" />\n          <p className=\"text-xs text-muted-foreground\">\n            Variables detected: {VARS(draft.body).length ? VARS(draft.body).join(\", \") : \"none\"}\n          </p>\n        </div>\n      </Dialog>\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/prompt-library.tsx"
    }
  ],
  "categories": [
    "ai"
  ],
  "type": "registry:block"
}
