feat(api): add 'streaks' to allowed widgets and settings structure.

This commit is contained in:
shay
2026-04-20 13:05:34 -04:00
parent f43beb2ef6
commit 4febce9fca
4 changed files with 95 additions and 4 deletions
+2 -1
View File
@@ -18,6 +18,7 @@ export type Achievement = {
export type PerWidgetSettings = { export type PerWidgetSettings = {
grade?: { max_tags?: number } grade?: { max_tags?: number }
impact?: { line_color?: string } impact?: { line_color?: string }
streaks?: { show_dates?: boolean }
collaborators?: { max_count?: number; bar_color?: string } collaborators?: { max_count?: number; bar_color?: string }
focus?: { max_categories?: number } focus?: { max_categories?: number }
languages?: { max_languages?: number } languages?: { max_languages?: number }
@@ -36,7 +37,7 @@ export type WidgetSettings = {
const DEFAULT_SETTINGS: WidgetSettings = { const DEFAULT_SETTINGS: WidgetSettings = {
theme: 'midnight', theme: 'midnight',
widgets: ['grade', 'impact', 'collaborators', 'focus', 'languages'], widgets: ['grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages'],
impactPeriod: '6mo', impactPeriod: '6mo',
customTags: [], customTags: [],
hiddenLanguages: [], hiddenLanguages: [],
@@ -13,6 +13,11 @@ export type CollaboratorData = { username: string; shared_repos: number; shared_
export type FocusCategory = { category: string; percentage: number; commit_count: number } export type FocusCategory = { category: string; percentage: number; commit_count: number }
export type LanguageData = { language: string; percentage: number; loc: number } export type LanguageData = { language: string; percentage: number; loc: number }
export type AchievementInput = { title: string; subtitle: string; event_date: string; icon: string } export type AchievementInput = { title: string; subtitle: string; event_date: string; icon: string }
export type StreakData = {
current: number; max: number
current_start?: string; last_active_date?: string
max_start?: string; max_end?: string
}
export type WidgetData = { export type WidgetData = {
grade?: GradeData grade?: GradeData
@@ -20,11 +25,13 @@ export type WidgetData = {
collaborators?: CollaboratorData[] collaborators?: CollaboratorData[]
focus?: FocusCategory[] focus?: FocusCategory[]
languages?: LanguageData[] languages?: LanguageData[]
streaks?: StreakData
} }
export type PerWidgetSettings = { export type PerWidgetSettings = {
grade?: { max_tags?: number } grade?: { max_tags?: number }
impact?: { line_color?: string } impact?: { line_color?: string }
streaks?: { show_dates?: boolean }
collaborators?: { max_count?: number; bar_color?: string } collaborators?: { max_count?: number; bar_color?: string }
focus?: { max_categories?: number } focus?: { max_categories?: number }
languages?: { max_languages?: number } languages?: { max_languages?: number }
@@ -247,6 +254,64 @@ function renderLanguages(languages: LanguageData[], t: Theme, s?: { max_language
return cardWrapper(inner, 380, rowsH + 36, t, 'Languages') return cardWrapper(inner, 380, rowsH + 36, t, 'Languages')
} }
const STREAKS_FLAME_PATH = 'M12 2c1 4 5 5 5 10a5 5 0 1 1-10 0c0-2 1-3 2-4 0 1 1 2 2 2 -1-2 0-5 1-8z'
function streaksFlame(color: string): string {
return `<path d="${STREAKS_FLAME_PATH}" fill="${color}" opacity="0.95"/>`
}
function streaksTrophy(color: string): string {
return (
`<path d="M6 3h12v3a4 4 0 0 1-4 4h-4a4 4 0 0 1-4-4V3z" fill="none" stroke="${color}" stroke-width="1.6"/>` +
`<path d="M4 4h2v2a2 2 0 0 1-2 2H3V5a1 1 0 0 1 1-1zM20 4h-2v2a2 2 0 0 0 2 2h1V5a1 1 0 0 0-1-1z" fill="none" stroke="${color}" stroke-width="1.4"/>` +
`<rect x="9" y="11" width="6" height="3" fill="${color}"/>` +
`<rect x="7" y="14" width="10" height="2" rx="0.5" fill="${color}"/>`
)
}
const STREAK_MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
function fmtStreakDate(iso?: string): string {
if (!iso) return ''
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(iso)
if (!m) return ''
const y = Number(m[1]), mo = Number(m[2]), d = Number(m[3])
if (!y || mo < 1 || mo > 12 || d < 1 || d > 31) return ''
return `${STREAK_MONTHS[mo-1]} ${String(d).padStart(2, '0')}, ${y}`
}
function fmtStreakRange(start?: string, end?: string): string {
const s = fmtStreakDate(start), e = fmtStreakDate(end)
if (s && e) return `${s} – ${e}`
return s || e
}
function renderStreaks(data: StreakData, t: Theme, s?: { show_dates?: boolean }): string {
const showDates = s?.show_dates !== false
const width = 380, height = 170
const colW = width / 2
const currentColor = t.accent
const maxColor = t.text
const currentSub = data.current_start ? fmtStreakDate(data.current_start) : '—'
const maxSub = (data.max_start || data.max_end) ? fmtStreakRange(data.max_start, data.max_end) : ''
const currentLine = data.current > 0 ? `since ${currentSub}` : ''
const dateLines = showDates ? `
<text x="${colW / 2}" y="130" text-anchor="middle" font-family="${font}" font-size="9" fill="${t.text_secondary}">${esc(currentLine)}</text>
<text x="${colW + colW / 2}" y="130" text-anchor="middle" font-family="${font}" font-size="9" fill="${t.text_secondary}">${esc(maxSub)}</text>` : ''
const inner = `
<g transform="translate(${colW / 2 - 12}, 14)">${streaksFlame(currentColor)}</g>
<text x="${colW / 2}" y="78" text-anchor="middle" font-family="${font}" font-size="40" font-weight="800" fill="${currentColor}">${data.current}</text>
<text x="${colW / 2}" y="108" text-anchor="middle" font-family="${font}" font-size="11" fill="${t.text_secondary}" letter-spacing="0.5">Current Streak</text>
<line x1="${colW}" y1="24" x2="${colW}" y2="${height - 60}" stroke="${t.grid}" stroke-width="1"/>
<g transform="translate(${colW + colW / 2 - 12}, 14)">${streaksTrophy(maxColor)}</g>
<text x="${colW + colW / 2}" y="78" text-anchor="middle" font-family="${font}" font-size="40" font-weight="800" fill="${maxColor}">${data.max}</text>
<text x="${colW + colW / 2}" y="108" text-anchor="middle" font-family="${font}" font-size="11" fill="${t.text_secondary}" letter-spacing="0.5">Longest Streak</text>
${dateLines}`
return cardWrapper(inner, width, height, t, '')
}
function achievementIconSvg(iconType: string, color: string): string { function achievementIconSvg(iconType: string, color: string): string {
// Mirrors generator/src/widgets/achievements.py::_achievement_icon_svg so // Mirrors generator/src/widgets/achievements.py::_achievement_icon_svg so
// the Workshop client preview matches the backend-rendered composite. // the Workshop client preview matches the backend-rendered composite.
@@ -316,6 +381,8 @@ export function renderAllWidgets(opts: {
widgetSvgs.grade = renderGrade(opts.data.grade, t, opts.widgetSettings.grade) widgetSvgs.grade = renderGrade(opts.data.grade, t, opts.widgetSettings.grade)
if (enabled.has('impact') && opts.data.impact) if (enabled.has('impact') && opts.data.impact)
widgetSvgs.impact = renderImpact(opts.data.impact, t, opts.widgetSettings.impact) widgetSvgs.impact = renderImpact(opts.data.impact, t, opts.widgetSettings.impact)
if (enabled.has('streaks') && opts.data.streaks)
widgetSvgs.streaks = renderStreaks(opts.data.streaks, t, opts.widgetSettings.streaks)
if (enabled.has('collaborators') && opts.data.collaborators) if (enabled.has('collaborators') && opts.data.collaborators)
widgetSvgs.collaborators = renderCollaborators(opts.data.collaborators, t, opts.widgetSettings.collaborators) widgetSvgs.collaborators = renderCollaborators(opts.data.collaborators, t, opts.widgetSettings.collaborators)
if (enabled.has('focus') && opts.data.focus) if (enabled.has('focus') && opts.data.focus)
@@ -36,6 +36,7 @@ const THEMES = [
const ALL_WIDGETS = [ const ALL_WIDGETS = [
{ id: 'grade', label: 'Grade' }, { id: 'grade', label: 'Grade' },
{ id: 'impact', label: 'Impact Timeline' }, { id: 'impact', label: 'Impact Timeline' },
{ id: 'streaks', label: 'Streaks' },
{ id: 'collaborators', label: 'Top Collaborators' }, { id: 'collaborators', label: 'Top Collaborators' },
{ id: 'focus', label: 'Recent Focus' }, { id: 'focus', label: 'Recent Focus' },
{ id: 'languages', label: 'Languages' }, { id: 'languages', label: 'Languages' },
@@ -285,7 +286,7 @@ export function WorkshopScreen({
data: widgetData, data: widgetData,
theme: settings.theme, theme: settings.theme,
widgets: settings.widgets, widgets: settings.widgets,
widgetOrder: ['grade', 'impact', 'collaborators', 'focus', 'languages', 'achievements'], widgetOrder: ['grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages', 'achievements'],
achievements: settings.achievements, achievements: settings.achievements,
widgetSettings: settings.widgetSettings, widgetSettings: settings.widgetSettings,
hiddenLanguages: settings.hiddenLanguages, hiddenLanguages: settings.hiddenLanguages,
@@ -360,7 +361,7 @@ export function WorkshopScreen({
<div className="flex flex-col gap-0.5"> <div className="flex flex-col gap-0.5">
{ALL_WIDGETS.map(w => { {ALL_WIDGETS.map(w => {
const enabled = settings.widgets.includes(w.id) const enabled = settings.widgets.includes(w.id)
const hasAdvanced = ['grade', 'impact', 'collaborators', 'focus', 'languages', 'achievements'].includes(w.id) const hasAdvanced = ['grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages', 'achievements'].includes(w.id)
const isExpanded = expandedWidget === w.id && enabled const isExpanded = expandedWidget === w.id && enabled
return ( return (
@@ -411,6 +412,25 @@ export function WorkshopScreen({
</div> </div>
)} )}
{w.id === 'streaks' && (
<div className="flex items-center justify-between">
<span className="text-[10px] text-gray-500">Show dates</span>
<button
onClick={() => updateWidgetSetting('streaks', 'show_dates', !getWidgetSetting('streaks', 'show_dates', true))}
className={`w-8 h-4 rounded-full transition-colors relative ${
getWidgetSetting('streaks', 'show_dates', true) ? 'bg-blue-500' : 'bg-gray-300'
}`}
aria-pressed={getWidgetSetting('streaks', 'show_dates', true)}
>
<span
className={`absolute top-0.5 w-3 h-3 rounded-full bg-white transition-transform ${
getWidgetSetting('streaks', 'show_dates', true) ? 'translate-x-4' : 'translate-x-0.5'
}`}
/>
</button>
</div>
)}
{w.id === 'collaborators' && ( {w.id === 'collaborators' && (
<div className="flex flex-col gap-2.5"> <div className="flex flex-col gap-2.5">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
+4 -1
View File
@@ -184,7 +184,7 @@ app.config["MAX_CONTENT_LENGTH"] = 128 * 1024
# ["max_count"])` raise, or by ballooning the DB with unbounded fields. # ["max_count"])` raise, or by ballooning the DB with unbounded fields.
# We normalize to an explicit allow-list of fields and coerce/trim types. # We normalize to an explicit allow-list of fields and coerce/trim types.
_ALLOWED_WIDGETS = {"grade", "impact", "collaborators", "focus", "languages", "achievements"} _ALLOWED_WIDGETS = {"grade", "impact", "streaks", "collaborators", "focus", "languages", "achievements"}
_ALLOWED_ICONS = {"trophy", "medal", "star", "hackathon"} _ALLOWED_ICONS = {"trophy", "medal", "star", "hackathon"}
_MAX_ACHIEVEMENTS = 10 _MAX_ACHIEVEMENTS = 10
_TITLE_MAX = 80 _TITLE_MAX = 80
@@ -240,6 +240,7 @@ def _coerce_widget_settings(v) -> dict:
allowed = { allowed = {
"grade": {"max_tags": "int"}, "grade": {"max_tags": "int"},
"impact": {"line_color": "color"}, "impact": {"line_color": "color"},
"streaks": {"show_dates": "bool"},
"collaborators": {"max_count": "int", "bar_color": "color"}, "collaborators": {"max_count": "int", "bar_color": "color"},
"focus": {"max_categories": "int"}, "focus": {"max_categories": "int"},
"languages": {"max_languages": "int"}, "languages": {"max_languages": "int"},
@@ -259,6 +260,8 @@ def _coerce_widget_settings(v) -> dict:
clean[key] = int(val) clean[key] = int(val)
except (TypeError, ValueError): except (TypeError, ValueError):
continue continue
elif kind == "bool":
clean[key] = bool(val)
elif kind == "color": elif kind == "color":
# Deep color validation happens in the widget renderer # Deep color validation happens in the widget renderer
# (safe_color). Here we only reject obviously wrong types # (safe_color). Here we only reject obviously wrong types