|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect, useRef, useMemo } from "react"; |
| 4 | +import { useTranslations } from "next-intl"; |
| 5 | +import { motion, AnimatePresence } from "framer-motion"; |
| 6 | +import CopyButton from "./CopyButton"; |
| 7 | +import Prism from "prismjs"; |
| 8 | +import "prismjs/components/prism-bash"; |
| 9 | +import "prismjs/components/prism-javascript"; |
| 10 | +import "prismjs/components/prism-python"; |
| 11 | + |
| 12 | +const API_URL = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:4000"; |
| 13 | + |
| 14 | +interface IntegrationCodeSnippetsProps { |
| 15 | + apiKey: string; |
| 16 | + amount: string; |
| 17 | + asset: "XLM" | "USDC"; |
| 18 | + recipient: string; |
| 19 | + description: string; |
| 20 | + usdcIssuer: string; |
| 21 | +} |
| 22 | + |
| 23 | +type Language = "curl" | "node" | "python"; |
| 24 | + |
| 25 | +const LANGUAGES: { id: Language; label: string; grammar: string }[] = [ |
| 26 | + { id: "curl", label: "cURL", grammar: "bash" }, |
| 27 | + { id: "node", label: "Node.js", grammar: "javascript" }, |
| 28 | + { id: "python", label: "Python", grammar: "python" }, |
| 29 | +]; |
| 30 | + |
| 31 | +function generateSnippet( |
| 32 | + lang: Language, |
| 33 | + apiKey: string, |
| 34 | + amount: string, |
| 35 | + asset: "XLM" | "USDC", |
| 36 | + recipient: string, |
| 37 | + description: string, |
| 38 | + usdcIssuer: string, |
| 39 | +): string { |
| 40 | + const numAmount = parseFloat(amount) || 0; |
| 41 | + const safeRecipient = |
| 42 | + recipient || "GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; |
| 43 | + const safeDescription = description || "Payment for services"; |
| 44 | + |
| 45 | + const body: Record<string, unknown> = { |
| 46 | + amount: numAmount, |
| 47 | + asset, |
| 48 | + recipient: safeRecipient, |
| 49 | + }; |
| 50 | + if (asset === "USDC") body.asset_issuer = usdcIssuer; |
| 51 | + if (description) body.description = safeDescription; |
| 52 | + |
| 53 | + const jsonBody = JSON.stringify(body, null, 2); |
| 54 | + |
| 55 | + switch (lang) { |
| 56 | + case "curl": |
| 57 | + return `curl -X POST "${API_URL}/api/create-payment" \\ |
| 58 | + -H "Content-Type: application/json" \\ |
| 59 | + -H "x-api-key: ${apiKey}" \\ |
| 60 | + -d '${jsonBody}'`; |
| 61 | + |
| 62 | + case "node": |
| 63 | + return `const response = await fetch("${API_URL}/api/create-payment", { |
| 64 | + method: "POST", |
| 65 | + headers: { |
| 66 | + "Content-Type": "application/json", |
| 67 | + "x-api-key": "${apiKey}", |
| 68 | + }, |
| 69 | + body: JSON.stringify(${jsonBody}), |
| 70 | +}); |
| 71 | +
|
| 72 | +const data = await response.json(); |
| 73 | +console.log(data.payment_link);`; |
| 74 | + |
| 75 | + case "python": |
| 76 | + return `import requests |
| 77 | +
|
| 78 | +response = requests.post( |
| 79 | + "${API_URL}/api/create-payment", |
| 80 | + headers={ |
| 81 | + "Content-Type": "application/json", |
| 82 | + "x-api-key": "${apiKey}", |
| 83 | + }, |
| 84 | + json=${jsonBody}, |
| 85 | +) |
| 86 | +
|
| 87 | +data = response.json() |
| 88 | +print(data["payment_link"])`; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +export default function IntegrationCodeSnippets({ |
| 93 | + apiKey, |
| 94 | + amount, |
| 95 | + asset, |
| 96 | + recipient, |
| 97 | + description, |
| 98 | + usdcIssuer, |
| 99 | +}: IntegrationCodeSnippetsProps) { |
| 100 | + const t = useTranslations("createPaymentForm"); |
| 101 | + const [activeTab, setActiveTab] = useState<Language>("curl"); |
| 102 | + const codeRef = useRef<HTMLElement>(null); |
| 103 | + |
| 104 | + const snippet = useMemo( |
| 105 | + () => generateSnippet(activeTab, apiKey, amount, asset, recipient, description, usdcIssuer), |
| 106 | + [activeTab, apiKey, amount, asset, recipient, description, usdcIssuer], |
| 107 | + ); |
| 108 | + |
| 109 | + useEffect(() => { |
| 110 | + if (codeRef.current) { |
| 111 | + Prism.highlightElement(codeRef.current); |
| 112 | + } |
| 113 | + }, [snippet, activeTab]); |
| 114 | + |
| 115 | + const grammarMap: Record<Language, string> = { |
| 116 | + curl: "bash", |
| 117 | + node: "javascript", |
| 118 | + python: "python", |
| 119 | + }; |
| 120 | + |
| 121 | + return ( |
| 122 | + <div className="flex flex-col gap-4"> |
| 123 | + {/* Tab bar */} |
| 124 | + <div className="flex gap-1 rounded-xl border border-white/10 bg-white/5 p-1"> |
| 125 | + {LANGUAGES.map((lang) => ( |
| 126 | + <button |
| 127 | + key={lang.id} |
| 128 | + type="button" |
| 129 | + onClick={() => setActiveTab(lang.id)} |
| 130 | + className={`relative flex-1 rounded-lg px-3 py-2 text-sm font-medium transition-all ${ |
| 131 | + activeTab === lang.id |
| 132 | + ? "text-black" |
| 133 | + : "text-slate-400 hover:text-white" |
| 134 | + }`} |
| 135 | + > |
| 136 | + {activeTab === lang.id && ( |
| 137 | + <motion.div |
| 138 | + layoutId="snippet-tab-bg" |
| 139 | + className="absolute inset-0 rounded-lg bg-mint" |
| 140 | + transition={{ type: "spring", stiffness: 380, damping: 30 }} |
| 141 | + /> |
| 142 | + )} |
| 143 | + <span className="relative z-10">{lang.label}</span> |
| 144 | + </button> |
| 145 | + ))} |
| 146 | + </div> |
| 147 | + |
| 148 | + {/* Code block */} |
| 149 | + <div className="group relative overflow-hidden rounded-xl border border-white/10 bg-[rgba(2,6,23,0.82)]"> |
| 150 | + {/* Copy button */} |
| 151 | + <div className="absolute right-3 top-3 z-10 opacity-0 transition-opacity group-hover:opacity-100"> |
| 152 | + <CopyButton text={snippet} /> |
| 153 | + </div> |
| 154 | + |
| 155 | + <AnimatePresence mode="wait"> |
| 156 | + <motion.div |
| 157 | + key={activeTab} |
| 158 | + initial={{ opacity: 0, y: 6 }} |
| 159 | + animate={{ opacity: 1, y: 0 }} |
| 160 | + exit={{ opacity: 0, y: -6 }} |
| 161 | + transition={{ duration: 0.2 }} |
| 162 | + className="overflow-x-auto p-4" |
| 163 | + > |
| 164 | + <pre className="!m-0 !bg-transparent !p-0"> |
| 165 | + <code |
| 166 | + ref={codeRef} |
| 167 | + className={`language-${grammarMap[activeTab]} !bg-transparent`} |
| 168 | + > |
| 169 | + {snippet} |
| 170 | + </code> |
| 171 | + </pre> |
| 172 | + </motion.div> |
| 173 | + </AnimatePresence> |
| 174 | + </div> |
| 175 | + |
| 176 | + {/* Helper text */} |
| 177 | + <p className="text-xs text-slate-500"> |
| 178 | + {t("snippetsHelper")} |
| 179 | + </p> |
| 180 | + </div> |
| 181 | + ); |
| 182 | +} |
0 commit comments