%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/docs/
Upload File :
Create Path :
Current File : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/docs/CodeBlock.tsx

import { useEffect, useMemo, useRef, useState } from "react";
import type { CodeTab } from "@/data/chatCompletionDocs";

interface CodeBlockProps {
  tabs: CodeTab[];
  baseUrl?: string;
  title?: string;
  className?: string;
}

/** Blok kode dengan tab bahasa, tombol salin, dan substitusi {{BASE_URL}}. */
export default function CodeBlock({
  tabs,
  baseUrl = "",
  title,
  className = "",
}: CodeBlockProps) {
  const [active, setActive] = useState(0);
  const [copied, setCopied] = useState(false);
  const timer = useRef<number | null>(null);

  useEffect(() => {
    setActive(0);
  }, [tabs]);

  useEffect(() => {
    return () => {
      if (timer.current) window.clearTimeout(timer.current);
    };
  }, []);

  const current = tabs[active] ?? tabs[0];
  const code = useMemo(
    () => (current?.code ?? "").replaceAll("{{BASE_URL}}", baseUrl),
    [current, baseUrl],
  );

  const copy = async () => {
    try {
      await navigator.clipboard.writeText(code);
    } catch {
      // Fallback untuk browser/konteks yang tidak mendukung Clipboard API.
      const area = document.createElement("textarea");
      area.value = code;
      area.setAttribute("readonly", "");
      area.style.position = "fixed";
      area.style.opacity = "0";
      document.body.appendChild(area);
      area.select();
      try {
        document.execCommand("copy");
      } finally {
        document.body.removeChild(area);
      }
    }
    setCopied(true);
    if (timer.current) window.clearTimeout(timer.current);
    timer.current = window.setTimeout(() => setCopied(false), 2000);
  };

  if (!current) return null;

  return (
    <div
      className={`overflow-hidden rounded-xl border border-white/[0.08] bg-ink-950/90 ${className}`}
      aria-label={title}
    >
      <div className="flex items-center gap-1 overflow-x-auto border-b border-white/[0.06] bg-white/[0.02] px-2 py-1.5">
        {tabs.map((tab, index) => (
          <button
            key={tab.label}
            type="button"
            onClick={() => {
              setActive(index);
              setCopied(false);
            }}
            className={`shrink-0 rounded-md px-2.5 py-1 text-[11px] font-medium transition-colors ${
              index === active
                ? "bg-white/[0.07] text-white"
                : "text-slate-500 hover:bg-white/[0.04] hover:text-slate-300"
            }`}
          >
            {tab.label}
          </button>
        ))}

        <div className="ml-auto flex shrink-0 items-center gap-2 pl-2">
          <span className="hidden text-[10px] uppercase tracking-wider text-slate-600 sm:inline">
            {current.language}
          </span>
          <button
            type="button"
            onClick={copy}
            className={`rounded-md border px-2 py-1 text-[11px] font-medium transition-colors ${
              copied
                ? "border-emerald-400/25 bg-emerald-500/10 text-emerald-300"
                : "border-white/[0.08] bg-white/[0.03] text-slate-400 hover:bg-white/[0.06] hover:text-white"
            }`}
          >
            {copied ? "Tersalin" : "Salin"}
          </button>
        </div>
      </div>

      <pre className="max-h-[520px] overflow-auto p-4 text-[12px] leading-relaxed text-slate-300">
        <code className="font-mono">{code}</code>
      </pre>
    </div>
  );
}

Zerion Mini Shell 1.0