{
  "name": "ai-post",
  "type": "registry:component",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "files": [
    {
      "type": "registry:component",
      "content": "\"use client\";\n\n/**\n * @author: @kokonutui\n * @description: AI Post\n * @version: 1.0.0\n * @date: 2026-11-28\n * @license: MIT\n * @website: https://kokonutui.com\n * @github: https://github.com/kokonut-labs/kokonutui\n */\n\nimport { Check, LoaderCircle } from \"lucide-react\";\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport { type ReactNode, useEffect, useRef, useState } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\n/** Where a target is: waiting for its time, going out, live or refused. */\nexport type PostTargetStatus = \"scheduled\" | \"posting\" | \"posted\" | \"failed\";\n\nexport interface PostTarget {\n  /** \"x\", \"bluesky\" and \"linkedin\" get their marks and limits; any other name gets its initial. */\n  platform: string;\n  /** The account it posts as, e.g. \"@northwind\". */\n  account: string;\n  status: PostTargetStatus;\n  /** The time, as shown: when it goes out if scheduled, when it went out if posted. */\n  when?: string;\n  /** The live post, for a View link once posted. */\n  url?: string;\n  /** Why it failed, in the platform's own words if you have them. */\n  error?: string;\n  /** Characters allowed. Defaults to the platform's limit when known. */\n  limit?: number;\n}\n\nexport interface AIPostProps {\n  /** The user's message, drawn as a bubble. Only the demo draws one by default. */\n  prompt?: string;\n  /** What the assistant says above the card. */\n  message?: string;\n  /** Said under the card once every target is live. */\n  done?: string;\n  /** A short name for the post, above the card. */\n  label?: string;\n  /** The text that goes out. */\n  caption?: string;\n  /**\n   * One row per account. Pass them to drive the card from your scheduler;\n   * leave them out for the demo.\n   */\n  targets?: PostTarget[];\n  /** Shows Post now while any target is still scheduled. */\n  onPostNow?: () => void;\n  /** Shows Edit while any target is still scheduled. */\n  onEdit?: () => void;\n  className?: string;\n}\n\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\n\nconst LIMITS: Record<string, number> = {\n  x: 280,\n  bluesky: 300,\n  linkedin: 3000,\n};\n\nconst NAMES: Record<string, string> = {\n  x: \"X\",\n  bluesky: \"Bluesky\",\n  linkedin: \"LinkedIn\",\n};\n\nconst DEMO = {\n  prompt: \"Post the launch to X, Bluesky and LinkedIn. LinkedIn at 9 tomorrow.\",\n  message: \"Sending X and Bluesky now. LinkedIn goes out at 9:00 tomorrow.\",\n  done: \"All three are live.\",\n  label: \"Launch post\",\n  caption:\n    \"Northwind 2.0 is out. Offline sync, a faster editor, and dark mode that finally matches your OS. Free for everyone today.\",\n};\n\nconst DEMO_TARGETS: PostTarget[] = [\n  { platform: \"x\", account: \"@northwind\", status: \"posting\" },\n  { platform: \"bluesky\", account: \"northwind.dev\", status: \"posting\" },\n  {\n    platform: \"linkedin\",\n    account: \"Northwind\",\n    status: \"scheduled\",\n    when: \"Tomorrow 9:00\",\n  },\n];\n\n/** When each demo row lands, in ms from the step that started it. */\nconst DEMO_STEP_MS = [900, 1500];\nconst DEMO_POSTED_AT = \"8:02\";\n\n/**\n * The demo's rows: the first two go live one after the other, and Post now\n * sends the scheduled one.\n */\nfunction useDemoTargets(enabled: boolean) {\n  const [targets, setTargets] = useState(DEMO_TARGETS);\n  const sending = useRef<ReturnType<typeof setTimeout>>(undefined);\n  useEffect(() => () => clearTimeout(sending.current), []);\n\n  useEffect(() => {\n    if (!enabled) {\n      return;\n    }\n    const timers = DEMO_STEP_MS.map((ms, i) =>\n      setTimeout(\n        () =>\n          setTargets((rows) =>\n            rows.map((row, j) =>\n              j === i ? { ...row, status: \"posted\", when: DEMO_POSTED_AT } : row\n            )\n          ),\n        ms\n      )\n    );\n    return () => {\n      for (const t of timers) {\n        clearTimeout(t);\n      }\n    };\n  }, [enabled]);\n\n  const postNow = () => {\n    setTargets((rows) =>\n      rows.map((row) =>\n        row.status === \"scheduled\"\n          ? { ...row, status: \"posting\", when: undefined }\n          : row\n      )\n    );\n    clearTimeout(sending.current);\n    sending.current = setTimeout(\n      () =>\n        setTargets((rows) =>\n          rows.map((row) =>\n            row.status === \"posting\"\n              ? { ...row, status: \"posted\", when: \"Just now\" }\n              : row\n          )\n        ),\n      DEMO_STEP_MS[0]\n    );\n  };\n\n  return { targets, postNow };\n}\n\n/** The one line of state above the card: how many are live. */\nfunction summarize(targets: PostTarget[]) {\n  const live = targets.filter((t) => t.status === \"posted\").length;\n  const failed = targets.some((t) => t.status === \"failed\");\n  if (failed) {\n    return { tone: \"failed\" as const, text: \"Needs a look\" };\n  }\n  if (live === targets.length) {\n    return { tone: \"done\" as const, text: \"All live\" };\n  }\n  if (live === 0 && targets.every((t) => t.status === \"scheduled\")) {\n    return { tone: \"waiting\" as const, text: \"Scheduled\" };\n  }\n  return {\n    tone: \"waiting\" as const,\n    text: `${live} of ${targets.length} live`,\n  };\n}\n\n/** The chat text around the card: the demo's only when it's the demo. */\nfunction shownText(\n  text: Pick<AIPostProps, \"prompt\" | \"message\" | \"done\" | \"label\" | \"caption\">,\n  isDemo: boolean\n) {\n  const pick = (value: string | undefined, demo: string) =>\n    value ?? (isDemo ? demo : undefined);\n  return {\n    prompt: pick(text.prompt, DEMO.prompt),\n    message: pick(text.message, DEMO.message),\n    done: pick(text.done, DEMO.done),\n    label: pick(text.label, DEMO.label) ?? \"Post\",\n    caption: pick(text.caption, DEMO.caption) ?? \"\",\n  };\n}\n\n/** The post's name and how many of its rows are live. */\nfunction Header({ label, targets }: { label: string; targets: PostTarget[] }) {\n  const summary = summarize(targets);\n  return (\n    <div className=\"flex h-8 items-center justify-between gap-3 px-3 text-[12px] text-zinc-500 dark:text-zinc-400\">\n      <span className=\"truncate font-medium text-zinc-700 dark:text-zinc-300\">\n        {label}\n      </span>\n      <span className=\"flex shrink-0 items-center gap-1.5 tabular-nums\">\n        <span\n          className={cn(\n            \"size-1.5 rounded-full transition-colors duration-300\",\n            summary.tone === \"done\" && \"bg-emerald-500\",\n            summary.tone === \"waiting\" && \"bg-blue-500\",\n            summary.tone === \"failed\" && \"bg-red-500\"\n          )}\n        />\n        {summary.text}\n      </span>\n    </div>\n  );\n}\n\n/** Edit and Post now, while a row is still waiting for its time. */\nfunction Actions({\n  onEdit,\n  onPostNow,\n}: {\n  onEdit?: () => void;\n  onPostNow?: () => void;\n}) {\n  return (\n    <motion.div\n      animate={{ height: \"auto\", opacity: 1 }}\n      className=\"overflow-hidden\"\n      exit={{ height: 0, opacity: 0 }}\n      initial={{ height: 0, opacity: 0 }}\n      transition={{ duration: 0.25, ease: EASE_OUT }}\n    >\n      <div className=\"flex items-center justify-between gap-3 border-black/[0.06] border-t px-4 py-2.5 dark:border-white/[0.08]\">\n        <span className=\"min-w-0 truncate text-[12px] text-zinc-500 dark:text-zinc-400\">\n          Change or cancel it until it posts\n        </span>\n        <span className=\"flex shrink-0 gap-1.5\">\n          {onEdit ? (\n            <button\n              className=\"h-8 rounded-full px-3 font-medium text-[12.5px] text-zinc-700 outline-hidden ring-1 ring-black/[0.08] transition-colors duration-150 hover:bg-zinc-50 focus-visible:ring-2 focus-visible:ring-blue-500/50 active:bg-zinc-100 dark:text-zinc-200 dark:ring-white/10 dark:active:bg-white/[0.08] dark:hover:bg-white/[0.05]\"\n              onClick={onEdit}\n              type=\"button\"\n            >\n              Edit\n            </button>\n          ) : null}\n          {onPostNow ? (\n            <button\n              className=\"h-8 rounded-full bg-blue-600 px-3.5 font-medium text-[12.5px] text-white shadow-[inset_0_1px_0_rgb(255_255_255/0.2),0_1px_2px_rgb(29_78_216/0.35)] outline-hidden transition-colors duration-150 hover:bg-blue-700 focus-visible:ring-2 focus-visible:ring-blue-500/50 focus-visible:ring-offset-2 active:bg-blue-800 dark:bg-blue-500 dark:active:bg-blue-700 dark:focus-visible:ring-offset-zinc-900 dark:hover:bg-blue-600\"\n              onClick={onPostNow}\n              type=\"button\"\n            >\n              Post now\n            </button>\n          ) : null}\n        </span>\n      </div>\n    </motion.div>\n  );\n}\n\nexport default function AIPost({\n  prompt,\n  message,\n  done,\n  label,\n  caption,\n  targets,\n  onPostNow,\n  onEdit,\n  className,\n}: AIPostProps) {\n  const isDemo = targets === undefined;\n  const demo = useDemoTargets(isDemo);\n  const rows = targets ?? demo.targets;\n  const text = shownText({ prompt, message, done, label, caption }, isDemo);\n  const postNow = onPostNow ?? (isDemo ? demo.postNow : undefined);\n  const edit = onEdit ?? (isDemo ? () => undefined : undefined);\n  const waiting = rows.some((t) => t.status === \"scheduled\");\n  const allLive = rows.length > 0 && rows.every((t) => t.status === \"posted\");\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div\n        className={cn(\"flex w-full max-w-md flex-col gap-4\", className)}\n        data-state={allLive ? \"live\" : \"pending\"}\n      >\n        {text.prompt ? (\n          <p className=\"max-w-[85%] self-end text-pretty rounded-2xl rounded-br-md bg-zinc-100 px-3.5 py-2 text-[14px] text-zinc-800 leading-snug dark:bg-zinc-800 dark:text-zinc-100\">\n            {text.prompt}\n          </p>\n        ) : null}\n        {text.message ? (\n          <p className=\"text-pretty text-[15px] text-zinc-800 leading-7 dark:text-zinc-200\">\n            {text.message}\n          </p>\n        ) : null}\n\n        <div className=\"rounded-[22px] bg-black/[0.04] p-1 dark:bg-white/[0.06]\">\n          <Header label={text.label} targets={rows} />\n\n          <div className=\"overflow-hidden rounded-[18px] bg-white shadow-[0_1px_2px_rgb(0_0_0/0.04)] ring-1 ring-black/[0.06] dark:bg-zinc-900 dark:ring-white/[0.08]\">\n            {text.caption ? (\n              <p className=\"line-clamp-4 whitespace-pre-line text-pretty border-black/[0.06] border-b px-4 py-3.5 text-[14px] text-zinc-800 leading-relaxed dark:border-white/[0.08] dark:text-zinc-200\">\n                {text.caption}\n              </p>\n            ) : null}\n\n            <ul className=\"divide-y divide-black/[0.06] dark:divide-white/[0.08]\">\n              {rows.map((target) => (\n                <TargetRow\n                  captionLength={text.caption.length}\n                  key={`${target.platform}:${target.account}`}\n                  target={target}\n                />\n              ))}\n            </ul>\n\n            <AnimatePresence initial={false}>\n              {waiting && (postNow || edit) ? (\n                <Actions onEdit={edit} onPostNow={postNow} />\n              ) : null}\n            </AnimatePresence>\n          </div>\n        </div>\n\n        <AnimatePresence>\n          {allLive && text.done ? (\n            <motion.p\n              animate={{ opacity: 1, filter: \"blur(0px)\", y: 0 }}\n              className=\"text-pretty text-[15px] text-zinc-800 leading-7 dark:text-zinc-200\"\n              initial={{ opacity: 0, filter: \"blur(4px)\", y: 4 }}\n              transition={{ duration: 0.4, ease: EASE_OUT, delay: 0.3 }}\n            >\n              {text.done}\n            </motion.p>\n          ) : null}\n        </AnimatePresence>\n        <p aria-live=\"polite\" className=\"sr-only\">\n          {summarize(rows).text}\n        </p>\n      </div>\n    </MotionConfig>\n  );\n}\n\n/** One account: its mark, the character count, and where the post is. */\nfunction TargetRow({\n  target,\n  captionLength,\n}: {\n  target: PostTarget;\n  captionLength: number;\n}) {\n  const limit = target.limit ?? LIMITS[target.platform];\n  const over = limit !== undefined && captionLength > limit;\n  const name = NAMES[target.platform] ?? target.platform;\n\n  return (\n    <li className=\"px-4 py-2.5\">\n      <div className=\"flex items-center gap-3\">\n        <span className=\"flex size-7 shrink-0 items-center justify-center rounded-lg bg-white text-zinc-900 ring-1 ring-black/[0.08] dark:bg-zinc-800 dark:text-zinc-100 dark:ring-white/10\">\n          <PlatformMark platform={target.platform} />\n        </span>\n        <span className=\"min-w-0 flex-1\">\n          <span className=\"block truncate font-medium text-[13.5px] text-zinc-900 capitalize leading-5 dark:text-zinc-100\">\n            {name}\n          </span>\n          <span className=\"block truncate text-[12px] text-zinc-500 leading-4 dark:text-zinc-400\">\n            {target.account}\n            {limit === undefined ? null : (\n              <>\n                {\" · \"}\n                <span\n                  className={cn(\n                    \"tabular-nums\",\n                    over && \"font-medium text-red-600 dark:text-red-400\"\n                  )}\n                >\n                  {captionLength.toLocaleString(\"en-US\")} /{\" \"}\n                  {limit.toLocaleString(\"en-US\")}\n                </span>\n              </>\n            )}\n          </span>\n        </span>\n        <span className=\"relative flex h-5 shrink-0 items-center overflow-hidden text-[12.5px]\">\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            <motion.span\n              animate={{ y: 0, opacity: 1, filter: \"blur(0px)\" }}\n              className=\"flex items-center gap-1.5 whitespace-nowrap\"\n              exit={{ y: -10, opacity: 0, filter: \"blur(2px)\" }}\n              initial={{ y: 10, opacity: 0, filter: \"blur(2px)\" }}\n              key={target.status}\n              transition={{ duration: 0.3, ease: EASE_OUT }}\n            >\n              <Status target={target} />\n            </motion.span>\n          </AnimatePresence>\n        </span>\n        {target.status === \"posted\" && target.url ? (\n          <a\n            className=\"shrink-0 rounded-sm font-medium text-[12.5px] text-zinc-900 underline-offset-4 outline-hidden hover:underline focus-visible:ring-2 focus-visible:ring-blue-500/50 dark:text-zinc-100\"\n            href={target.url}\n            rel=\"noopener\"\n            target=\"_blank\"\n          >\n            View\n          </a>\n        ) : null}\n      </div>\n      {target.status === \"failed\" && target.error ? (\n        <p className=\"mt-1.5 pl-10 text-[12px] text-red-600 leading-snug dark:text-red-400\">\n          {target.error}\n        </p>\n      ) : null}\n    </li>\n  );\n}\n\nfunction Status({ target }: { target: PostTarget }): ReactNode {\n  switch (target.status) {\n    case \"scheduled\":\n      return (\n        <span className=\"font-medium text-blue-600 tabular-nums dark:text-blue-400\">\n          {target.when ?? \"Scheduled\"}\n        </span>\n      );\n    case \"posting\":\n      return (\n        <>\n          <LoaderCircle\n            aria-hidden=\"true\"\n            className=\"size-3.5 animate-spin text-zinc-400 motion-reduce:animate-none\"\n          />\n          <span className=\"text-zinc-500 dark:text-zinc-400\">Posting</span>\n        </>\n      );\n    case \"posted\":\n      return (\n        <>\n          <Check\n            aria-hidden=\"true\"\n            className=\"size-3.5 text-emerald-600 dark:text-emerald-400\"\n            strokeWidth={2.5}\n          />\n          <span className=\"text-zinc-500 tabular-nums dark:text-zinc-400\">\n            {target.when ? `Posted ${target.when}` : \"Posted\"}\n          </span>\n        </>\n      );\n    default:\n      return (\n        <span className=\"font-medium text-red-600 dark:text-red-400\">\n          Failed\n        </span>\n      );\n  }\n}\n\n/** Single-color marks, so they take the row's ink in both themes. */\nfunction PlatformMark({ platform }: { platform: string }) {\n  switch (platform) {\n    case \"x\":\n      return (\n        <svg aria-hidden=\"true\" className=\"size-3.5\" viewBox=\"0 0 24 24\">\n          <path\n            d=\"M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z\"\n            fill=\"currentColor\"\n          />\n        </svg>\n      );\n    case \"bluesky\":\n      return (\n        <svg aria-hidden=\"true\" className=\"size-4\" viewBox=\"0 0 24 24\">\n          <path\n            d=\"M12 10.8c-1.087-2.114-4.046-6.053-6.798-7.995C2.566.944 1.561 1.266.902 1.565.139 1.908 0 3.08 0 3.768c0 .69.378 5.65.624 6.479.815 2.736 3.713 3.66 6.383 3.364.136-.02.275-.039.415-.056-.138.022-.276.04-.415.056-3.912.58-7.387 2.005-2.83 7.078 5.013 5.19 6.87-1.113 7.823-4.308.953 3.195 2.05 9.271 7.733 4.308 4.267-4.308 1.172-6.498-2.74-7.078a8.741 8.741 0 0 1-.415-.056c.14.017.279.036.415.056 2.67.297 5.568-.628 6.383-3.364.246-.828.624-5.79.624-6.478 0-.69-.139-1.861-.902-2.206-.659-.298-1.664-.62-4.3 1.24C16.046 4.748 13.087 8.687 12 10.8Z\"\n            fill=\"currentColor\"\n          />\n        </svg>\n      );\n    case \"linkedin\":\n      return (\n        <svg aria-hidden=\"true\" className=\"size-3.5\" viewBox=\"0 0 24 24\">\n          <path\n            d=\"M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.064 2.064 0 1 1 0-4.128 2.064 2.064 0 0 1 0 4.128zM7.119 20.452H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z\"\n            fill=\"currentColor\"\n          />\n        </svg>\n      );\n    default:\n      return (\n        <span className=\"font-semibold text-[12px] uppercase\">\n          {platform.slice(0, 1)}\n        </span>\n      );\n  }\n}\n",
      "path": "/components/kokonutui/ai-post.tsx",
      "target": "components/kokonutui/ai-post.tsx"
    }
  ]
}