mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(composite): add show_name parameter to control header visibility.
This commit is contained in:
@@ -7,6 +7,7 @@ import { AuthButton } from './components/AuthButton'
|
||||
import type { WidgetData } from './lib/renderWidgets'
|
||||
import { DEMO_WIDGET_DATA } from './lib/demoData'
|
||||
import { fetchMe, type Me } from './lib/auth'
|
||||
import { buildEmbedUrl } from './lib/settingsQuery'
|
||||
|
||||
export type Achievement = {
|
||||
title: string
|
||||
@@ -38,8 +39,8 @@ export type WidgetSettings = {
|
||||
|
||||
const DEFAULT_SETTINGS: WidgetSettings = {
|
||||
theme: 'midnight',
|
||||
widgets: ['grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages'],
|
||||
widgetOrder: ['grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages', 'achievements'],
|
||||
widgets: ['name', 'grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages'],
|
||||
widgetOrder: ['name', 'grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages', 'achievements'],
|
||||
impactPeriod: '6mo',
|
||||
customTags: [],
|
||||
hiddenLanguages: [],
|
||||
@@ -65,6 +66,11 @@ export default function App() {
|
||||
const [generatedSvg, setGeneratedSvg] = useState<string | null>(null)
|
||||
const [generateError, setGenerateError] = useState<string | null>(null)
|
||||
const [generating, setGenerating] = useState(false)
|
||||
// The URL we actually fetched for the preview — also the one we show as
|
||||
// the embed snippet. For owners this is /api/<u> (backed by stored
|
||||
// settings). For visitors it's /api/<u>?<query>, which round-trips their
|
||||
// Workshop edits without touching the owner's saved widget.
|
||||
const [embedUrl, setEmbedUrl] = useState<string>('')
|
||||
const pollTimer = useRef<number | null>(null)
|
||||
const pollStart = useRef<number>(0)
|
||||
const aborted = useRef(false)
|
||||
@@ -185,53 +191,76 @@ export default function App() {
|
||||
setGenerating(true)
|
||||
setGeneratedSvg(null)
|
||||
setGenerateError(null)
|
||||
const isOwner = !!me.login && me.login.toLowerCase() === username.toLowerCase()
|
||||
try {
|
||||
// Push the user's Workshop choices to the backend BEFORE rendering.
|
||||
// The backend's /generate endpoint re-reads settings from SQLite and
|
||||
// renders with them, so without this PATCH the SVG would reflect
|
||||
// whatever was last persisted (usually the enroll-time defaults)
|
||||
// rather than the user's current selections.
|
||||
const backendSettings = {
|
||||
theme: settings.theme,
|
||||
enabled: settings.widgets,
|
||||
widget_order: settings.widgetOrder,
|
||||
custom_tags: settings.customTags,
|
||||
hidden_languages: settings.hiddenLanguages,
|
||||
widget_settings: settings.widgetSettings,
|
||||
achievements: settings.achievements.filter(a => a.title.trim()),
|
||||
}
|
||||
const patchRes = await fetch(`/api/${encodeURIComponent(username)}/settings`, {
|
||||
method: 'PATCH',
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(backendSettings),
|
||||
})
|
||||
if (!patchRes.ok && patchRes.status !== 404) {
|
||||
setGenerateError(`Settings sync failed (HTTP ${patchRes.status})`)
|
||||
return
|
||||
}
|
||||
if (isOwner) {
|
||||
// Owner path: persist settings, then force a server-side render
|
||||
// against the stored payload. The resulting /api/<u> is the embed.
|
||||
const backendSettings = {
|
||||
theme: settings.theme,
|
||||
enabled: settings.widgets,
|
||||
widget_order: settings.widgetOrder,
|
||||
custom_tags: settings.customTags,
|
||||
hidden_languages: settings.hiddenLanguages,
|
||||
widget_settings: settings.widgetSettings,
|
||||
achievements: settings.achievements.filter(a => a.title.trim()),
|
||||
}
|
||||
const patchRes = await fetch(`/api/${encodeURIComponent(username)}/settings`, {
|
||||
method: 'PATCH',
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(backendSettings),
|
||||
})
|
||||
if (!patchRes.ok && patchRes.status !== 404) {
|
||||
setGenerateError(`Settings sync failed (HTTP ${patchRes.status})`)
|
||||
return
|
||||
}
|
||||
|
||||
const r = await fetch(`/api/${encodeURIComponent(username)}/generate`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
})
|
||||
if (r.status === 404) {
|
||||
const body = await r.json().catch(() => ({} as { status?: string }))
|
||||
setGenerateError(body.status === 'not_found' ? 'User not found' : 'Not enrolled')
|
||||
return
|
||||
const r = await fetch(`/api/${encodeURIComponent(username)}/generate`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
})
|
||||
if (r.status === 404) {
|
||||
const body = await r.json().catch(() => ({} as { status?: string }))
|
||||
setGenerateError(body.status === 'not_found' ? 'User not found' : 'Not enrolled')
|
||||
return
|
||||
}
|
||||
if (!r.ok) {
|
||||
setGenerateError(`Generate failed (HTTP ${r.status})`)
|
||||
return
|
||||
}
|
||||
const cleanUrl = `${window.location.origin}/api/${encodeURIComponent(username)}`
|
||||
// Cache-bust so regenerate after settings change shows the new
|
||||
// version, not a stale browser copy. The cache-busting `t` param
|
||||
// is not read server-side (not in the allow-list) so the render
|
||||
// stays cacheable at the edge.
|
||||
const svgRes = await fetch(`${cleanUrl}?t=${Date.now()}`)
|
||||
if (!svgRes.ok) {
|
||||
setGenerateError(`SVG fetch failed (HTTP ${svgRes.status})`)
|
||||
return
|
||||
}
|
||||
setGeneratedSvg(await svgRes.text())
|
||||
setEmbedUrl(cleanUrl)
|
||||
} else {
|
||||
// Visitor path: encode Workshop edits into a query string and hit
|
||||
// the same URL we'll hand the user for their README. No owner
|
||||
// settings are written. The backend renders ad-hoc against the
|
||||
// fetcher's cached payload for this user.
|
||||
const url = buildEmbedUrl(window.location.origin, username, settings)
|
||||
// Same cache-bust trick — unknown query params are ignored by the
|
||||
// sanitizer, so this doesn't alter the rendered output.
|
||||
const svgRes = await fetch(`${url}${url.includes('?') ? '&' : '?'}t=${Date.now()}`)
|
||||
if (svgRes.status === 404) {
|
||||
setGenerateError('User not enrolled — ask them to sign in and generate first')
|
||||
return
|
||||
}
|
||||
if (!svgRes.ok) {
|
||||
setGenerateError(`SVG fetch failed (HTTP ${svgRes.status})`)
|
||||
return
|
||||
}
|
||||
setGeneratedSvg(await svgRes.text())
|
||||
setEmbedUrl(url)
|
||||
}
|
||||
if (!r.ok) {
|
||||
setGenerateError(`Generate failed (HTTP ${r.status})`)
|
||||
return
|
||||
}
|
||||
// Fetch the backend-rendered composite SVG. Cache-bust so regenerate
|
||||
// after settings change shows the new version, not a stale browser copy.
|
||||
const svgRes = await fetch(`/api/${encodeURIComponent(username)}?t=${Date.now()}`)
|
||||
if (!svgRes.ok) {
|
||||
setGenerateError(`SVG fetch failed (HTTP ${svgRes.status})`)
|
||||
return
|
||||
}
|
||||
setGeneratedSvg(await svgRes.text())
|
||||
} catch (e) {
|
||||
console.warn('generate call failed:', e)
|
||||
setGenerateError('Network error')
|
||||
@@ -277,6 +306,7 @@ export default function App() {
|
||||
generating={generating}
|
||||
generatedSvg={generatedSvg}
|
||||
generateError={generateError}
|
||||
embedUrl={embedUrl}
|
||||
onBack={handleBack}
|
||||
onRegenerate={handleGenerate}
|
||||
/>
|
||||
|
||||
@@ -426,8 +426,10 @@ export function renderAllWidgets(opts: {
|
||||
widgetSvgs.achievements = renderAchievements(opts.achievements, t, opts.widgetSettings.achievements)
|
||||
|
||||
// Compose
|
||||
const totalW = 420, headerH = 60, padding = 20, gap = 16
|
||||
const ordered = opts.widgetOrder.filter(k => enabled.has(k) && widgetSvgs[k])
|
||||
const showName = enabled.has('name')
|
||||
const totalW = 420, headerH = showName ? 60 : 0, padding = 20, gap = 16
|
||||
// "name" is a pseudo-widget — it controls the header, not a rendered svg.
|
||||
const ordered = opts.widgetOrder.filter(k => k !== 'name' && enabled.has(k) && widgetSvgs[k])
|
||||
|
||||
const parts = ordered.map(key => {
|
||||
const { inner, h } = extractInner(widgetSvgs[key], key)
|
||||
@@ -437,8 +439,21 @@ export function renderAllWidgets(opts: {
|
||||
const contentH = parts.reduce((a, p) => a + p.h, 0) + gap * Math.max(parts.length - 1, 0)
|
||||
const totalH = headerH + contentH + padding * 2 + 10
|
||||
|
||||
const avatar = `<circle cx="30" cy="30" r="16" fill="${t.accent}" opacity="0.3"/>`
|
||||
const header = `${avatar}<text x="54" y="38" font-family="${font}" font-size="16" font-weight="700" fill="${t.text}">${esc(opts.username || 'Developer')}</text><line x1="${padding}" y1="${headerH}" x2="${totalW-padding}" y2="${headerH}" stroke="${t.card_border}" stroke-width="0.5"/>`
|
||||
// Header (avatar + username). Only drawn when "name" is enabled. The
|
||||
// avatar pulls from github.com/<user>.png which redirects to
|
||||
// avatars.githubusercontent.com; the accent circle underneath is a
|
||||
// fallback for missing users / slow loads.
|
||||
let header = ''
|
||||
if (showName) {
|
||||
const avatarUrl = opts.username
|
||||
? `https://github.com/${encodeURIComponent(opts.username)}.png?size=64`
|
||||
: ''
|
||||
const avatarImg = avatarUrl
|
||||
? `<image x="14" y="14" width="32" height="32" href="${esc(avatarUrl)}" clip-path="url(#mainAvatarClip)" preserveAspectRatio="xMidYMid slice"/>`
|
||||
: ''
|
||||
const avatar = `<defs><clipPath id="mainAvatarClip"><circle cx="30" cy="30" r="16"/></clipPath></defs><circle cx="30" cy="30" r="16" fill="${t.accent}" opacity="0.3"/>${avatarImg}`
|
||||
header = `${avatar}<text x="54" y="38" font-family="${font}" font-size="16" font-weight="700" fill="${t.text}">${esc(opts.username || 'Developer')}</text><line x1="${padding}" y1="${headerH}" x2="${totalW-padding}" y2="${headerH}" stroke="${t.card_border}" stroke-width="0.5"/>`
|
||||
}
|
||||
|
||||
let yOffset = headerH + padding
|
||||
let embedded = ''
|
||||
|
||||
@@ -25,15 +25,21 @@ const SVG_SANITIZE_CONFIG = {
|
||||
],
|
||||
}
|
||||
|
||||
// Mini-preview swatches (bg + card + accent). Flat-color swatches all read
|
||||
// the same dark/light — showing the accent color is what actually separates
|
||||
// Midnight (blue) from Onyx (purple) from Nord (cyan), etc.
|
||||
const THEMES = [
|
||||
{ id: 'midnight', label: 'Midnight', color: '#121820' },
|
||||
{ id: 'onyx', label: 'Onyx', color: '#09090b' },
|
||||
{ id: 'nord', label: 'Nord', color: '#1e2433' },
|
||||
{ id: 'clean', label: 'Clean', color: '#ffffff', border: true },
|
||||
{ id: 'paper', label: 'Paper', color: '#faf8f5', border: true },
|
||||
{ id: 'midnight', label: 'Midnight', bg: '#121820', card: '#1a2230', border: '#2a3444', accent: '#58a6ff', light: false },
|
||||
{ id: 'onyx', label: 'Onyx', bg: '#09090b', card: '#18181b', border: '#27272a', accent: '#a78bfa', light: false },
|
||||
{ id: 'nord', label: 'Nord', bg: '#1e2433', card: '#2a3142', border: '#3d4659', accent: '#88c0d0', light: false },
|
||||
{ id: 'clean', label: 'Clean', bg: '#ffffff', card: '#f6f8fa', border: '#d8dee4', accent: '#0969da', light: true },
|
||||
{ id: 'paper', label: 'Paper', bg: '#faf8f5', card: '#f5f0eb', border: '#e0d8cf', accent: '#b45309', light: true },
|
||||
]
|
||||
|
||||
const ALL_WIDGETS = [
|
||||
// "name" is the avatar + username header. Pinned at the top — can be
|
||||
// toggled off but never reordered. Keep this entry first.
|
||||
{ id: 'name', label: 'Name' },
|
||||
{ id: 'grade', label: 'Grade' },
|
||||
{ id: 'impact', label: 'Impact Timeline' },
|
||||
{ id: 'streaks', label: 'Streaks' },
|
||||
@@ -75,8 +81,12 @@ const ICONS: { id: string; svg: ReactNode }[] = [
|
||||
id: 'medal',
|
||||
svg: (
|
||||
<svg {...ICON_SVG_PROPS}>
|
||||
<circle cx="12" cy="8" r="6" />
|
||||
<path d="m15.477 12.89 1.515 8.526a.5.5 0 0 1-.81.47l-3.58-2.687a1 1 0 0 0-1.197 0l-3.586 2.686a.5.5 0 0 1-.81-.469l1.514-8.526" />
|
||||
<path d="M7.21 15 2.66 7.14a2 2 0 0 1 .13-2.2L4.4 2.8A2 2 0 0 1 6 2h12a2 2 0 0 1 1.6.8l1.6 2.14a2 2 0 0 1 .14 2.2L16.79 15" />
|
||||
<path d="M11 12 5.12 2.2" />
|
||||
<path d="m13 12 5.88-9.8" />
|
||||
<path d="M8 7h8" />
|
||||
<circle cx="12" cy="17" r="5" />
|
||||
<path d="M12 18v-2h-.5" />
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
@@ -199,7 +209,7 @@ function ColorPicker({ value, onChange }: { value: string; onChange: (v: string)
|
||||
key={c}
|
||||
onClick={() => onChange(c)}
|
||||
className={`w-5 h-5 rounded-full transition-all ${
|
||||
value === c ? 'ring-2 ring-offset-1 ring-blue-500' : 'hover:ring-1 hover:ring-gray-300'
|
||||
value === c ? 'ring-2 ring-offset-1 ring-gray-900' : 'hover:ring-1 hover:ring-gray-300'
|
||||
}`}
|
||||
style={{ backgroundColor: c }}
|
||||
/>
|
||||
@@ -243,9 +253,13 @@ export function WorkshopScreen({
|
||||
// the stored order forgot. Keeps the UI functional if ALL_WIDGETS
|
||||
// gains or loses an entry between releases.
|
||||
const knownIds = useMemo(() => new Set(ALL_WIDGETS.map(w => w.id)), [])
|
||||
// "name" is pinned at index 0 regardless of persisted order. We still keep
|
||||
// it in orderedIds so the composite-render path can find it, but the drag
|
||||
// UI below treats it as non-reorderable.
|
||||
const orderedIds = useMemo(() => {
|
||||
const seen = new Set<string>()
|
||||
const out: string[] = []
|
||||
const out: string[] = ['name']
|
||||
seen.add('name')
|
||||
for (const id of settings.widgetOrder) {
|
||||
if (knownIds.has(id) && !seen.has(id)) { seen.add(id); out.push(id) }
|
||||
}
|
||||
@@ -257,6 +271,8 @@ export function WorkshopScreen({
|
||||
|
||||
const moveWidget = (from: number, to: number) => {
|
||||
if (from === to || from < 0 || to < 0 || from >= orderedIds.length || to >= orderedIds.length) return
|
||||
// Never move "name" and never drop another widget above it (index 0).
|
||||
if (orderedIds[from] === 'name' || to === 0) return
|
||||
const next = orderedIds.slice()
|
||||
const [item] = next.splice(from, 1)
|
||||
next.splice(to, 0, item)
|
||||
@@ -367,15 +383,23 @@ export function WorkshopScreen({
|
||||
key={t.id}
|
||||
onClick={() => updateSetting('theme', t.id)}
|
||||
title={t.label}
|
||||
className={`w-9 h-9 rounded-lg transition-all ${
|
||||
aria-label={t.label}
|
||||
className={`w-9 h-9 rounded-lg overflow-hidden transition-all ${
|
||||
settings.theme === t.id
|
||||
? 'ring-2 ring-blue-500 ring-offset-2'
|
||||
: t.border
|
||||
? 'border border-gray-200 hover:border-gray-300'
|
||||
: 'hover:ring-1 hover:ring-gray-300 hover:ring-offset-1'
|
||||
? 'ring-2 ring-gray-900 ring-offset-2'
|
||||
: t.light
|
||||
? 'ring-1 ring-gray-200 hover:ring-gray-300'
|
||||
: 'hover:ring-1 hover:ring-gray-400 hover:ring-offset-1'
|
||||
}`}
|
||||
style={{ backgroundColor: t.color }}
|
||||
/>
|
||||
>
|
||||
<svg viewBox="0 0 36 36" width="36" height="36">
|
||||
<rect width="36" height="36" rx="8" fill={t.bg} />
|
||||
<rect x="6" y="8" width="24" height="20" rx="4" fill={t.card} stroke={t.border} strokeWidth="1" />
|
||||
<circle cx="11" cy="18" r="3" fill={t.accent} />
|
||||
<rect x="16" y="16.25" width="10" height="1.75" rx="0.875" fill={t.accent} opacity="0.75" />
|
||||
<rect x="16" y="19.75" width="7" height="1.5" rx="0.75" fill={t.accent} opacity="0.35" />
|
||||
</svg>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div className="text-[10px] text-gray-400 mt-1.5">
|
||||
@@ -393,24 +417,28 @@ export function WorkshopScreen({
|
||||
const enabled = settings.widgets.includes(w.id)
|
||||
const hasAdvanced = ['grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages', 'achievements'].includes(w.id)
|
||||
const isExpanded = expandedWidget === w.id && enabled
|
||||
const isDragging = dragIndex === idx
|
||||
const showDropLineAbove = dropIndex === idx && dragIndex !== null && dragIndex !== idx
|
||||
// "name" is pinned — no drag affordance, no drop lines.
|
||||
// The dropIndex comparisons below still use the real idx
|
||||
// so downstream rows behave normally when name is skipped.
|
||||
const isPinned = w.id === 'name'
|
||||
const isDragging = !isPinned && dragIndex === idx
|
||||
const showDropLineAbove = !isPinned && idx > 0 && dropIndex === idx && dragIndex !== null && dragIndex !== idx
|
||||
// Drop-line below the last row when dragging past the end.
|
||||
const showDropLineBelow = idx === orderedIds.length - 1 && dropIndex === orderedIds.length && dragIndex !== null
|
||||
const showDropLineBelow = !isPinned && idx === orderedIds.length - 1 && dropIndex === orderedIds.length && dragIndex !== null
|
||||
|
||||
return (
|
||||
<div key={w.id}>
|
||||
{showDropLineAbove && <div className="h-0.5 bg-blue-400 rounded-full mx-2 mb-0.5" />}
|
||||
{showDropLineAbove && <div className="h-0.5 bg-gray-900 rounded-full mx-2 mb-0.5" />}
|
||||
<div
|
||||
className={`flex items-center transition-opacity ${isDragging ? 'opacity-40' : ''}`}
|
||||
draggable
|
||||
onDragStart={e => {
|
||||
draggable={!isPinned}
|
||||
onDragStart={isPinned ? undefined : e => {
|
||||
setDragIndex(idx)
|
||||
e.dataTransfer.effectAllowed = 'move'
|
||||
// Firefox needs some payload to start a drag.
|
||||
e.dataTransfer.setData('text/plain', String(idx))
|
||||
}}
|
||||
onDragOver={e => {
|
||||
onDragOver={isPinned ? undefined : e => {
|
||||
if (dragIndex === null) return
|
||||
e.preventDefault()
|
||||
e.dataTransfer.dropEffect = 'move'
|
||||
@@ -427,7 +455,7 @@ export function WorkshopScreen({
|
||||
setDropIndex(target)
|
||||
}
|
||||
}}
|
||||
onDrop={e => {
|
||||
onDrop={isPinned ? undefined : e => {
|
||||
e.preventDefault()
|
||||
if (dragIndex === null || dropIndex === null) {
|
||||
setDragIndex(null); setDropIndex(null); return
|
||||
@@ -438,15 +466,28 @@ export function WorkshopScreen({
|
||||
moveWidget(dragIndex, to)
|
||||
setDragIndex(null); setDropIndex(null)
|
||||
}}
|
||||
onDragEnd={() => { setDragIndex(null); setDropIndex(null) }}
|
||||
onDragEnd={isPinned ? undefined : () => { setDragIndex(null); setDropIndex(null) }}
|
||||
>
|
||||
<span
|
||||
className="px-1 text-gray-300 hover:text-gray-500 cursor-grab active:cursor-grabbing select-none"
|
||||
title="Drag to reorder"
|
||||
aria-hidden
|
||||
>
|
||||
⋮⋮
|
||||
</span>
|
||||
{isPinned ? (
|
||||
<span
|
||||
className="px-1 text-gray-300 select-none"
|
||||
title="Pinned — can't be reordered"
|
||||
aria-hidden
|
||||
>
|
||||
<svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||||
<rect x="3" y="11" width="18" height="10" rx="2"/>
|
||||
<path d="M7 11V7a5 5 0 0 1 10 0v4"/>
|
||||
</svg>
|
||||
</span>
|
||||
) : (
|
||||
<span
|
||||
className="px-1 text-gray-300 hover:text-gray-500 cursor-grab active:cursor-grabbing select-none"
|
||||
title="Drag to reorder"
|
||||
aria-hidden
|
||||
>
|
||||
⋮⋮
|
||||
</span>
|
||||
)}
|
||||
<button
|
||||
onClick={() => toggleWidget(w.id)}
|
||||
className={`flex-1 flex items-center gap-2.5 px-2.5 py-1.5 rounded-lg text-xs transition-colors text-left ${
|
||||
@@ -455,7 +496,7 @@ export function WorkshopScreen({
|
||||
: 'text-gray-400 hover:text-gray-500 hover:bg-gray-50/50'
|
||||
}`}
|
||||
>
|
||||
<div className={`w-3.5 h-3.5 rounded flex-shrink-0 transition-colors ${enabled ? 'bg-blue-500' : 'bg-gray-200'}`} />
|
||||
<div className={`w-3.5 h-3.5 rounded flex-shrink-0 transition-colors ${enabled ? 'bg-gray-900' : 'bg-gray-200'}`} />
|
||||
{w.label}
|
||||
</button>
|
||||
{hasAdvanced && enabled && (
|
||||
@@ -468,7 +509,7 @@ export function WorkshopScreen({
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{showDropLineBelow && <div className="h-0.5 bg-blue-400 rounded-full mx-2 mt-0.5" />}
|
||||
{showDropLineBelow && <div className="h-0.5 bg-gray-900 rounded-full mx-2 mt-0.5" />}
|
||||
|
||||
{isExpanded && (
|
||||
<div className="ml-6 mt-1 mb-2 p-2.5 rounded-lg bg-gray-50/80 border border-gray-100 flex flex-col gap-2.5">
|
||||
|
||||
Reference in New Issue
Block a user