|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { createSupabaseBrowserClient } from "@/lib/db"; |
| 6 | +import toolsData from "@/data/tools.json"; |
| 7 | +import { Tool, getCategoryColor } from "@/lib/types"; |
| 8 | +import { SITE_URL } from "@/lib/constants"; |
| 9 | + |
| 10 | +const allTools = toolsData as Tool[]; |
| 11 | + |
| 12 | +interface ToolUsageRow { |
| 13 | + tool_id: string; |
| 14 | + avatar_url: string | null; |
| 15 | + used_at: string; |
| 16 | +} |
| 17 | + |
| 18 | +interface Props { |
| 19 | + username: string; |
| 20 | +} |
| 21 | + |
| 22 | +export default function ProfileClient({ username }: Props) { |
| 23 | + const [rows, setRows] = useState<ToolUsageRow[]>([]); |
| 24 | + const [loading, setLoading] = useState(createSupabaseBrowserClient() !== null); |
| 25 | + const [copiedAll, setCopiedAll] = useState(false); |
| 26 | + const [copiedId, setCopiedId] = useState<string | null>(null); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const supabase = createSupabaseBrowserClient(); |
| 30 | + if (!supabase) return; |
| 31 | + supabase |
| 32 | + .from("tool_usage") |
| 33 | + .select("tool_id, avatar_url, used_at") |
| 34 | + .eq("github_username", username) |
| 35 | + .order("used_at", { ascending: false }) |
| 36 | + .then(({ data }) => { |
| 37 | + setRows((data as ToolUsageRow[]) ?? []); |
| 38 | + setLoading(false); |
| 39 | + }); |
| 40 | + }, [username]); |
| 41 | + |
| 42 | + const tools = rows.map((r) => allTools.find((t) => t.id === r.tool_id)).filter(Boolean) as Tool[]; |
| 43 | + |
| 44 | + const avatarUrl = rows.find((r) => r.avatar_url)?.avatar_url ?? null; |
| 45 | + |
| 46 | + function badgeUrl(toolId: string) { |
| 47 | + return `${SITE_URL}/badge/tool/${toolId}`; |
| 48 | + } |
| 49 | + |
| 50 | + function badgeMarkdown(tool: Tool) { |
| 51 | + return `[})](${SITE_URL}/explore)`; |
| 52 | + } |
| 53 | + |
| 54 | + function copyBadge(tool: Tool) { |
| 55 | + navigator.clipboard.writeText(badgeMarkdown(tool)).then(() => { |
| 56 | + setCopiedId(tool.id); |
| 57 | + setTimeout(() => setCopiedId(null), 2000); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + function copyAll() { |
| 62 | + const markdown = tools.map(badgeMarkdown).join("\n"); |
| 63 | + navigator.clipboard.writeText(markdown).then(() => { |
| 64 | + setCopiedAll(true); |
| 65 | + setTimeout(() => setCopiedAll(false), 2000); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className="max-w-2xl mx-auto px-6 py-10"> |
| 71 | + {/* Breadcrumb */} |
| 72 | + <nav |
| 73 | + className="flex items-center gap-1.5 text-[11px] mb-8" |
| 74 | + style={{ color: "var(--text-muted)" }} |
| 75 | + > |
| 76 | + <Link href="/" className="hover:underline"> |
| 77 | + AIchitect |
| 78 | + </Link> |
| 79 | + <span>/</span> |
| 80 | + <span style={{ color: "var(--text-secondary)" }}>{username}</span> |
| 81 | + </nav> |
| 82 | + |
| 83 | + {/* Header */} |
| 84 | + <div className="flex items-center gap-4 mb-8"> |
| 85 | + {avatarUrl ? ( |
| 86 | + // eslint-disable-next-line @next/next/no-img-element |
| 87 | + <img |
| 88 | + src={avatarUrl} |
| 89 | + alt={username} |
| 90 | + className="w-12 h-12 rounded-full" |
| 91 | + style={{ border: "2px solid var(--border)" }} |
| 92 | + /> |
| 93 | + ) : ( |
| 94 | + <div |
| 95 | + className="w-12 h-12 rounded-full flex items-center justify-center text-lg font-bold" |
| 96 | + style={{ |
| 97 | + background: "#7c6bff22", |
| 98 | + color: "var(--accent)", |
| 99 | + border: "2px solid #7c6bff44", |
| 100 | + }} |
| 101 | + > |
| 102 | + {username[0]?.toUpperCase()} |
| 103 | + </div> |
| 104 | + )} |
| 105 | + <div> |
| 106 | + <h1 className="text-xl font-bold" style={{ color: "var(--text-primary)" }}> |
| 107 | + @{username} |
| 108 | + </h1> |
| 109 | + <p className="text-xs mt-0.5" style={{ color: "var(--text-muted)" }}> |
| 110 | + AI tools I use |
| 111 | + </p> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + |
| 115 | + {loading && ( |
| 116 | + <p className="text-sm" style={{ color: "var(--text-muted)" }}> |
| 117 | + Loading… |
| 118 | + </p> |
| 119 | + )} |
| 120 | + |
| 121 | + {!loading && tools.length === 0 && ( |
| 122 | + <div |
| 123 | + className="rounded-xl p-8 text-center" |
| 124 | + style={{ background: "var(--surface)", border: "1px solid var(--border)" }} |
| 125 | + > |
| 126 | + <p className="text-sm" style={{ color: "var(--text-muted)" }}> |
| 127 | + No tools marked yet. |
| 128 | + </p> |
| 129 | + <Link |
| 130 | + href="/explore" |
| 131 | + className="inline-block mt-3 text-xs font-medium px-3 py-1.5 rounded-md" |
| 132 | + style={{ |
| 133 | + background: "#7c6bff18", |
| 134 | + color: "var(--accent)", |
| 135 | + border: "1px solid #7c6bff44", |
| 136 | + }} |
| 137 | + > |
| 138 | + Explore tools → |
| 139 | + </Link> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + |
| 143 | + {!loading && tools.length > 0 && ( |
| 144 | + <> |
| 145 | + {/* Badge wall */} |
| 146 | + <div className="flex flex-wrap gap-2 mb-6"> |
| 147 | + {tools.map((tool) => { |
| 148 | + const color = getCategoryColor(tool.category); |
| 149 | + const isCopied = copiedId === tool.id; |
| 150 | + return ( |
| 151 | + <button |
| 152 | + key={tool.id} |
| 153 | + onClick={() => copyBadge(tool)} |
| 154 | + title={isCopied ? "Copied!" : `Copy badge for ${tool.name}`} |
| 155 | + className="group relative" |
| 156 | + style={{ background: "none", border: "none", padding: 0, cursor: "pointer" }} |
| 157 | + > |
| 158 | + {/* eslint-disable-next-line @next/next/no-img-element */} |
| 159 | + <img |
| 160 | + src={badgeUrl(tool.id)} |
| 161 | + alt={`${tool.name} badge`} |
| 162 | + className="h-5 transition-opacity" |
| 163 | + style={{ opacity: isCopied ? 0.6 : 1 }} |
| 164 | + /> |
| 165 | + <span |
| 166 | + className="absolute inset-0 flex items-center justify-center text-[9px] font-semibold rounded opacity-0 group-hover:opacity-100 transition-opacity" |
| 167 | + style={{ |
| 168 | + background: color + "dd", |
| 169 | + color: "#fff", |
| 170 | + fontSize: 9, |
| 171 | + }} |
| 172 | + > |
| 173 | + {isCopied ? "Copied!" : "Copy"} |
| 174 | + </span> |
| 175 | + </button> |
| 176 | + ); |
| 177 | + })} |
| 178 | + </div> |
| 179 | + |
| 180 | + {/* Copy all section */} |
| 181 | + <div |
| 182 | + className="rounded-xl p-4 space-y-3" |
| 183 | + style={{ background: "var(--surface)", border: "1px solid var(--border)" }} |
| 184 | + > |
| 185 | + <div> |
| 186 | + <p className="text-[11px] font-semibold" style={{ color: "var(--text-primary)" }}> |
| 187 | + Add to your GitHub README |
| 188 | + </p> |
| 189 | + <p className="text-[10px] mt-0.5" style={{ color: "var(--text-muted)" }}> |
| 190 | + Paste all {tools.length} badge{tools.length !== 1 ? "s" : ""} as Markdown — each |
| 191 | + links back to AIchitect. |
| 192 | + </p> |
| 193 | + </div> |
| 194 | + <div |
| 195 | + className="rounded px-2 py-2 font-mono text-[9px] break-all leading-relaxed max-h-24 overflow-y-auto" |
| 196 | + style={{ background: "var(--surface-2)", color: "var(--text-muted)" }} |
| 197 | + > |
| 198 | + {tools.map(badgeMarkdown).join("\n")} |
| 199 | + </div> |
| 200 | + <button |
| 201 | + onClick={copyAll} |
| 202 | + className="w-full py-2 rounded text-[10px] font-semibold transition-all" |
| 203 | + style={{ |
| 204 | + background: copiedAll ? "#26de8122" : "var(--accent)", |
| 205 | + color: copiedAll ? "var(--success)" : "#fff", |
| 206 | + border: copiedAll ? "1px solid #26de8144" : "none", |
| 207 | + }} |
| 208 | + > |
| 209 | + {copiedAll |
| 210 | + ? "Copied!" |
| 211 | + : `Copy all ${tools.length} badge${tools.length !== 1 ? "s" : ""}`} |
| 212 | + </button> |
| 213 | + </div> |
| 214 | + </> |
| 215 | + )} |
| 216 | + </div> |
| 217 | + ); |
| 218 | +} |
0 commit comments