mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
chore(ResultScreen): add copy functionality for embed snippet button
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import DOMPurify from 'dompurify'
|
||||
import { useMemo } from 'react'
|
||||
import { useMemo, useState } from 'react'
|
||||
|
||||
function sanitizeSvg(raw: string): string {
|
||||
return DOMPurify.sanitize(raw, {
|
||||
@@ -19,12 +19,23 @@ type Props = {
|
||||
|
||||
export function ResultScreen({ username, generating, generatedSvg, generateError, onBack }: Props) {
|
||||
const safeSvg = useMemo(() => (generatedSvg ? sanitizeSvg(generatedSvg) : ''), [generatedSvg])
|
||||
const [copied, setCopied] = useState(false)
|
||||
const embedUrl = `${window.location.origin}/api/${encodeURIComponent(username)}`
|
||||
const embedSnippet = ``
|
||||
|
||||
const copy = async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(embedSnippet)
|
||||
setCopied(true)
|
||||
setTimeout(() => setCopied(false), 1500)
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex flex-col items-center animate-fade-in-up">
|
||||
<div className="w-full max-w-2xl px-6 pt-6">
|
||||
<div className="w-full max-w-3xl px-6 pt-6">
|
||||
<button
|
||||
onClick={onBack}
|
||||
className="flex items-center gap-1.5 text-xs text-gray-400 hover:text-gray-600 transition-colors"
|
||||
@@ -36,31 +47,56 @@ export function ResultScreen({ username, generating, generatedSvg, generateError
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 flex flex-col items-center justify-center px-6 pb-12 w-full max-w-2xl">
|
||||
<h2 className="font-serif text-2xl text-gray-800 tracking-tight mb-1">
|
||||
<div className="flex-1 flex flex-col items-center justify-center px-6 pb-12 w-full max-w-3xl">
|
||||
<h2 className="font-serif text-3xl text-gray-800 tracking-tight mb-2">
|
||||
Your Widget
|
||||
</h2>
|
||||
|
||||
{generating && (
|
||||
<div className="mt-8 flex flex-col items-center gap-3">
|
||||
<div className="w-5 h-5 border-2 border-gray-300 border-t-gray-600 rounded-full animate-spin-slow" />
|
||||
<div className="mt-10 flex flex-col items-center gap-3">
|
||||
<div className="w-6 h-6 border-2 border-gray-300 border-t-gray-600 rounded-full animate-spin-slow" />
|
||||
<p className="text-xs text-gray-400">Generating with real data…</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!generating && generateError && (
|
||||
<div className="mt-8 flex flex-col items-center gap-3">
|
||||
<p className="text-xs text-red-500">{generateError}</p>
|
||||
<div className="mt-10 flex flex-col items-center gap-3">
|
||||
<p className="text-sm text-red-500">{generateError}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!generating && !generateError && safeSvg && (
|
||||
<>
|
||||
<p className="text-xs text-gray-400 mb-6">Rendered from live data</p>
|
||||
<div className="w-full flex justify-center" dangerouslySetInnerHTML={{ __html: safeSvg }} />
|
||||
<p className="text-xs text-gray-400 mb-6">Rendered from live GitHub data</p>
|
||||
<div className="w-full rounded-2xl bg-white shadow-lg ring-1 ring-gray-200 p-6 flex justify-center">
|
||||
{/*
|
||||
Force the inline SVG to scale up to the card width. The
|
||||
composite is authored at 380px wide — without this the
|
||||
preview looked tiny on wide screens.
|
||||
*/}
|
||||
<div
|
||||
className="w-full max-w-xl [&_svg]:w-full [&_svg]:h-auto"
|
||||
dangerouslySetInnerHTML={{ __html: safeSvg }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="w-full mt-8">
|
||||
<p className="text-xs text-gray-500 mb-2">Embed in your README:</p>
|
||||
<pre className="bg-gray-100 text-xs p-3 rounded overflow-x-auto">{embedSnippet}</pre>
|
||||
<div className="flex items-baseline justify-between mb-2">
|
||||
<p className="text-xs uppercase tracking-wider text-gray-500">Embed in your README</p>
|
||||
<button
|
||||
onClick={copy}
|
||||
className="text-xs text-gray-500 hover:text-gray-800 transition-colors"
|
||||
>
|
||||
{copied ? 'Copied!' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre className="bg-gray-900 text-gray-100 text-xs p-4 rounded-lg overflow-x-auto">
|
||||
{embedSnippet}
|
||||
</pre>
|
||||
<p className="text-[11px] text-gray-400 mt-2">
|
||||
GitHub requires HTTPS for README images — serve this behind a TLS
|
||||
reverse proxy or Cloudflare Tunnel if the page is on http://.
|
||||
</p>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -86,7 +86,6 @@ type Props = {
|
||||
onBack: () => void
|
||||
fetchDone: boolean
|
||||
fetchError: string | null
|
||||
isDemo: boolean
|
||||
widgetData: WidgetData | null
|
||||
}
|
||||
|
||||
@@ -98,7 +97,6 @@ export function WorkshopScreen({
|
||||
onBack,
|
||||
fetchDone,
|
||||
fetchError,
|
||||
isDemo,
|
||||
widgetData,
|
||||
}: Props) {
|
||||
const [expandedWidget, setExpandedWidget] = useState<string | null>(null)
|
||||
@@ -177,30 +175,13 @@ export function WorkshopScreen({
|
||||
Back
|
||||
</button>
|
||||
|
||||
<div className="text-sm font-semibold text-gray-800 mb-1">{username}</div>
|
||||
<div className="flex items-center gap-1.5 text-[10px] text-gray-400 mb-6">
|
||||
{fetchError ? (
|
||||
<>
|
||||
<div className="w-1.5 h-1.5 rounded-full bg-red-400" />
|
||||
{fetchError}
|
||||
</>
|
||||
) : isDemo ? (
|
||||
<>
|
||||
<div className="w-1.5 h-1.5 rounded-full bg-amber-400 animate-pulse" />
|
||||
Building your widget — showing demo
|
||||
</>
|
||||
) : fetchDone ? (
|
||||
<>
|
||||
<div className="w-1.5 h-1.5 rounded-full bg-emerald-400" />
|
||||
Data ready
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="w-1.5 h-1.5 rounded-full bg-amber-400 animate-pulse" />
|
||||
Fetching data...
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-sm font-semibold text-gray-800 mb-6">{username}</div>
|
||||
{fetchError && (
|
||||
<div className="flex items-center gap-1.5 text-[10px] text-red-500 mb-6">
|
||||
<div className="w-1.5 h-1.5 rounded-full bg-red-400" />
|
||||
{fetchError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Theme */}
|
||||
<div className="mb-5">
|
||||
|
||||
Reference in New Issue
Block a user