|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { usePathname } from "next/navigation"; |
| 6 | +import { fetchSharedProject, type SharedProject } from "@/src/lib/auth-client"; |
| 7 | +import { colors as archiveColors } from "@/src/data/colors"; |
| 8 | +import { hexToRgb } from "@/src/lib/color-utils"; |
| 9 | +import type { ColorRecord } from "@/src/types/color"; |
| 10 | + |
| 11 | +function colorDistance(r1: number, g1: number, b1: number, r2: number, g2: number, b2: number): number { |
| 12 | + const dr = r1 - r2, dg = g1 - g2, db = b1 - b2; |
| 13 | + return Math.sqrt(2 * dr * dr + 4 * dg * dg + 3 * db * db); |
| 14 | +} |
| 15 | + |
| 16 | +function findClosest(hex: string): ColorRecord | null { |
| 17 | + const rgb = hexToRgb(hex); |
| 18 | + if (!rgb) return null; |
| 19 | + let best: { color: ColorRecord; d: number } | null = null; |
| 20 | + for (const ac of archiveColors) { |
| 21 | + const acRgb = hexToRgb(ac.hex); |
| 22 | + if (!acRgb) continue; |
| 23 | + const d = colorDistance(rgb.r, rgb.g, rgb.b, acRgb.r, acRgb.g, acRgb.b); |
| 24 | + if (!best || d < best.d) best = { color: ac, d }; |
| 25 | + } |
| 26 | + return best?.color ?? null; |
| 27 | +} |
| 28 | + |
| 29 | +function luminance(hex: string): number { |
| 30 | + const r = parseInt(hex.slice(1, 3), 16); |
| 31 | + const g = parseInt(hex.slice(3, 5), 16); |
| 32 | + const b = parseInt(hex.slice(5, 7), 16); |
| 33 | + return (0.299 * r + 0.587 * g + 0.114 * b) / 255; |
| 34 | +} |
| 35 | + |
| 36 | +const SCORE_COLORS: Record<string, string> = { |
| 37 | + A: "bg-emerald-500", B: "bg-emerald-400", C: "bg-yellow-400", D: "bg-orange-400", F: "bg-red-500", |
| 38 | +}; |
| 39 | + |
| 40 | +export function SharedProjectPage() { |
| 41 | + const pathname = usePathname(); |
| 42 | + const shareId = pathname.split("/").pop() ?? ""; |
| 43 | + const [project, setProject] = useState<SharedProject | null>(null); |
| 44 | + const [loading, setLoading] = useState(true); |
| 45 | + const [error, setError] = useState(""); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (!shareId) return; |
| 49 | + fetchSharedProject(shareId) |
| 50 | + .then(setProject) |
| 51 | + .catch(() => setError("Project not found or link expired.")) |
| 52 | + .finally(() => setLoading(false)); |
| 53 | + }, [shareId]); |
| 54 | + |
| 55 | + if (loading) { |
| 56 | + return ( |
| 57 | + <main className="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 dark:from-neutral-950 dark:to-neutral-900 flex items-center justify-center"> |
| 58 | + <div className="w-8 h-8 border-2 border-slate-300 border-t-slate-600 rounded-full animate-spin" /> |
| 59 | + </main> |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + if (error || !project) { |
| 64 | + return ( |
| 65 | + <main className="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 dark:from-neutral-950 dark:to-neutral-900 flex items-center justify-center p-4"> |
| 66 | + <div className="text-center space-y-3"> |
| 67 | + <p className="text-slate-600 dark:text-slate-400">{error || "Project not found."}</p> |
| 68 | + <Link href="/projects" className="text-sm text-indigo-600 hover:underline">Go to Projects</Link> |
| 69 | + </div> |
| 70 | + </main> |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + return ( |
| 75 | + <main className="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 dark:from-neutral-950 dark:to-neutral-900 pb-24"> |
| 76 | + <section className="max-w-3xl mx-auto px-4 pt-10 pb-6"> |
| 77 | + <p className="text-xs font-semibold tracking-widest text-slate-400 uppercase mb-1">Shared Project</p> |
| 78 | + <h1 className="text-3xl font-bold text-slate-900 dark:text-white leading-tight mb-2"> |
| 79 | + {project.name} |
| 80 | + </h1> |
| 81 | + {project.tags.length > 0 && ( |
| 82 | + <div className="flex flex-wrap gap-1.5 mb-2"> |
| 83 | + {project.tags.map((tag) => ( |
| 84 | + <span key={tag} className="text-[10px] px-2 py-0.5 bg-slate-200 dark:bg-white/10 text-slate-600 dark:text-slate-400 rounded-full"> |
| 85 | + {tag} |
| 86 | + </span> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + )} |
| 90 | + {project.notes && ( |
| 91 | + <p className="text-sm text-slate-500 dark:text-slate-400">{project.notes}</p> |
| 92 | + )} |
| 93 | + </section> |
| 94 | + |
| 95 | + <div className="max-w-3xl mx-auto px-4 space-y-6"> |
| 96 | + {/* Palette strip */} |
| 97 | + {project.palette.length > 0 && ( |
| 98 | + <div className="bg-white dark:bg-neutral-900 rounded-2xl border border-slate-100 dark:border-white/10 shadow-sm overflow-hidden"> |
| 99 | + <div className="flex h-20"> |
| 100 | + {project.palette.map((hex, i) => ( |
| 101 | + <div key={i} className="flex-1 relative group" style={{ backgroundColor: hex }}> |
| 102 | + <div className="absolute inset-0 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity"> |
| 103 | + <span |
| 104 | + className="text-[10px] font-mono font-semibold px-1.5 py-0.5 rounded" |
| 105 | + style={{ color: luminance(hex) > 0.5 ? "#1a1a1a" : "#ffffff" }} |
| 106 | + > |
| 107 | + {hex.toUpperCase()} |
| 108 | + </span> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ))} |
| 112 | + </div> |
| 113 | + |
| 114 | + <div className="divide-y divide-slate-100 dark:divide-white/10"> |
| 115 | + {project.palette.map((hex, i) => { |
| 116 | + const match = findClosest(hex); |
| 117 | + return ( |
| 118 | + <div key={i} className="flex items-center gap-3 px-5 py-2.5"> |
| 119 | + <div className="w-7 h-7 rounded-lg border border-black/10 dark:border-white/10 shrink-0" style={{ backgroundColor: hex }} /> |
| 120 | + <span className="text-sm font-mono text-slate-700 dark:text-slate-300">{hex.toUpperCase()}</span> |
| 121 | + {match && ( |
| 122 | + <Link href={`/colors/${match.id}/`} className="text-xs text-indigo-600 dark:text-indigo-400 hover:underline ml-auto"> |
| 123 | + {match.name} |
| 124 | + </Link> |
| 125 | + )} |
| 126 | + </div> |
| 127 | + ); |
| 128 | + })} |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + )} |
| 132 | + |
| 133 | + {/* Critique */} |
| 134 | + {project.critique && ( |
| 135 | + <div className="bg-white dark:bg-neutral-900 rounded-2xl border border-slate-100 dark:border-white/10 shadow-sm overflow-hidden"> |
| 136 | + <div className="flex items-center gap-4 p-5 border-b border-slate-100 dark:border-white/10"> |
| 137 | + <div className={`w-12 h-12 rounded-xl ${SCORE_COLORS[project.critique.score] || "bg-slate-400"} flex items-center justify-center text-white text-xl font-bold`}> |
| 138 | + {project.critique.score} |
| 139 | + </div> |
| 140 | + <div> |
| 141 | + <p className="text-sm font-semibold text-slate-800 dark:text-white">Design Critique</p> |
| 142 | + <p className="text-xs text-slate-500 dark:text-slate-400">Harmony: {project.critique.harmony_type}</p> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + <div className="p-5"> |
| 146 | + <p className="text-sm text-slate-600 dark:text-slate-300 leading-relaxed"> |
| 147 | + {project.critique.overall_assessment} |
| 148 | + </p> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + )} |
| 152 | + |
| 153 | + {/* CTA */} |
| 154 | + <div className="text-center pt-4"> |
| 155 | + <Link |
| 156 | + href="/brand-generator/" |
| 157 | + className="inline-block px-6 py-2.5 bg-slate-900 text-white text-sm font-semibold rounded-xl hover:bg-slate-700 transition-colors dark:bg-white dark:text-neutral-950" |
| 158 | + > |
| 159 | + Create your own palette |
| 160 | + </Link> |
| 161 | + </div> |
| 162 | + </div> |
| 163 | + </main> |
| 164 | + ); |
| 165 | +} |
0 commit comments