import { useState, useMemo, type ReactNode } from 'react'
import DOMPurify from 'dompurify'
import type { WidgetSettings, Achievement, PerWidgetSettings } from '../App'
import { renderAllWidgets, type WidgetData } from '../lib/renderWidgets'
import { signInUrl } from '../lib/auth'
import type { Me } from '../lib/auth'
// Defense-in-depth: the client-rendered preview assembles SVG from
// GitHub-sourced strings (collaborator logins, repo names) and user-typed
// achievement text. renderWidgets escapes known fields, but one missed
// interpolation anywhere upstream would turn into HTML in the preview.
// DOMPurify neutralises that without affecting normal SVG shapes/styles.
const SVG_SANITIZE_CONFIG = {
USE_PROFILES: { svg: true, svgFilters: true },
ADD_TAGS: [
'image', 'use', 'clipPath', 'defs', 'style', 'animate', 'animateTransform',
'feDropShadow', 'filter', 'linearGradient', 'stop',
],
ADD_ATTR: [
'xlink:href', 'href', 'clip-path', 'viewBox', 'preserveAspectRatio',
'xmlns', 'xmlns:xlink', 'rx', 'ry', 'attributeName', 'from', 'to', 'dur',
'fill', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap',
'stroke-linejoin', 'dominant-baseline', 'letter-spacing', 'opacity',
'filter', 'flood-color', 'flood-opacity', 'stdDeviation',
],
}
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 },
]
const ALL_WIDGETS = [
{ id: 'grade', label: 'Grade' },
{ id: 'impact', label: 'Impact Timeline' },
{ id: 'streaks', label: 'Streaks' },
{ id: 'collaborators', label: 'Top Collaborators' },
{ id: 'focus', label: 'Recent Focus' },
{ id: 'languages', label: 'Languages' },
{ id: 'achievements', label: 'Achievements' },
]
// Lucide-style hollow glyphs: consistent stroke-weight + viewBox so the 4
// buttons line up and read as one icon family. Kept as JSX so they inherit
// currentColor from the surrounding button's text color.
const ICON_SVG_PROPS = {
width: 16,
height: 16,
viewBox: '0 0 24 24',
fill: 'none',
stroke: 'currentColor',
strokeWidth: 1.75,
strokeLinecap: 'round' as const,
strokeLinejoin: 'round' as const,
}
const ICONS: { id: string; svg: ReactNode }[] = [
{
id: 'trophy',
svg: (
),
},
{
id: 'medal',
svg: (
),
},
{
id: 'star',
svg: (
),
},
{
id: 'hackathon',
svg: (
),
},
]
const COLOR_PRESETS = [
'#58a6ff', '#a78bfa', '#4ade80', '#fb923c', '#f472b6',
'#ff6b6b', '#38bdf8', '#facc15', '#34d399', '#c084fc',
]
function ChevronIcon({ open }: { open: boolean }) {
return (
)
}
function NumberStepper({ value, min, max, onChange }: {
value: number; min: number; max: number; onChange: (v: number) => void
}) {
return (
{value}
)
}
// Common "noise" languages users typically want to hide — shown even when
// the account-level detection hasn't surfaced them yet (e.g., first load).
const COMMON_HIDE_LANGS = ['HTML', 'CSS', 'SCSS', 'Jupyter Notebook', 'TeX', 'Shell', 'Dockerfile', 'Makefile', 'Batchfile']
function HiddenLanguagesPicker({
detected, hidden, onChange,
}: {
detected: { language: string }[]
hidden: string[]
onChange: (next: string[]) => void
}) {
const seen = new Set()
const options: string[] = []
for (const d of detected) if (d.language && !seen.has(d.language)) { seen.add(d.language); options.push(d.language) }
for (const c of COMMON_HIDE_LANGS) if (!seen.has(c)) { seen.add(c); options.push(c) }
// Preserve any user-set hidden that we haven't otherwise seen.
for (const h of hidden) if (!seen.has(h)) { seen.add(h); options.push(h) }
const toggle = (lang: string) => {
onChange(hidden.includes(lang) ? hidden.filter(h => h !== lang) : [...hidden, lang])
}
return (