From 4febce9fcafca4ac858bf256ea1bc7f5f8b5820f Mon Sep 17 00:00:00 2001 From: shay Date: Mon, 20 Apr 2026 13:05:34 -0400 Subject: [PATCH] feat(api): add 'streaks' to allowed widgets and settings structure. --- generator/frontend/src/App.tsx | 3 +- generator/frontend/src/lib/renderWidgets.ts | 67 +++++++++++++++++++ .../frontend/src/screens/WorkshopScreen.tsx | 24 ++++++- generator/src/api.py | 5 +- 4 files changed, 95 insertions(+), 4 deletions(-) diff --git a/generator/frontend/src/App.tsx b/generator/frontend/src/App.tsx index 0abaaff..0075280 100644 --- a/generator/frontend/src/App.tsx +++ b/generator/frontend/src/App.tsx @@ -18,6 +18,7 @@ export type Achievement = { export type PerWidgetSettings = { grade?: { max_tags?: number } impact?: { line_color?: string } + streaks?: { show_dates?: boolean } collaborators?: { max_count?: number; bar_color?: string } focus?: { max_categories?: number } languages?: { max_languages?: number } @@ -36,7 +37,7 @@ export type WidgetSettings = { const DEFAULT_SETTINGS: WidgetSettings = { theme: 'midnight', - widgets: ['grade', 'impact', 'collaborators', 'focus', 'languages'], + widgets: ['grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages'], impactPeriod: '6mo', customTags: [], hiddenLanguages: [], diff --git a/generator/frontend/src/lib/renderWidgets.ts b/generator/frontend/src/lib/renderWidgets.ts index 941661a..0e354a9 100644 --- a/generator/frontend/src/lib/renderWidgets.ts +++ b/generator/frontend/src/lib/renderWidgets.ts @@ -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 LanguageData = { language: string; percentage: number; loc: number } 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 = { grade?: GradeData @@ -20,11 +25,13 @@ export type WidgetData = { collaborators?: CollaboratorData[] focus?: FocusCategory[] languages?: LanguageData[] + streaks?: StreakData } export type PerWidgetSettings = { grade?: { max_tags?: number } impact?: { line_color?: string } + streaks?: { show_dates?: boolean } collaborators?: { max_count?: number; bar_color?: string } focus?: { max_categories?: 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') } +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 `` +} + +function streaksTrophy(color: string): string { + return ( + `` + + `` + + `` + + `` + ) +} + +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 ? ` + ${esc(currentLine)} + ${esc(maxSub)}` : '' + + const inner = ` + ${streaksFlame(currentColor)} + ${data.current} + Current Streak + + ${streaksTrophy(maxColor)} + ${data.max} + Longest Streak + ${dateLines}` + + return cardWrapper(inner, width, height, t, '') +} + function achievementIconSvg(iconType: string, color: string): string { // Mirrors generator/src/widgets/achievements.py::_achievement_icon_svg so // 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) if (enabled.has('impact') && opts.data.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) widgetSvgs.collaborators = renderCollaborators(opts.data.collaborators, t, opts.widgetSettings.collaborators) if (enabled.has('focus') && opts.data.focus) diff --git a/generator/frontend/src/screens/WorkshopScreen.tsx b/generator/frontend/src/screens/WorkshopScreen.tsx index b754a4b..43b1019 100644 --- a/generator/frontend/src/screens/WorkshopScreen.tsx +++ b/generator/frontend/src/screens/WorkshopScreen.tsx @@ -36,6 +36,7 @@ const THEMES = [ 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' }, @@ -285,7 +286,7 @@ export function WorkshopScreen({ data: widgetData, theme: settings.theme, widgets: settings.widgets, - widgetOrder: ['grade', 'impact', 'collaborators', 'focus', 'languages', 'achievements'], + widgetOrder: ['grade', 'impact', 'streaks', 'collaborators', 'focus', 'languages', 'achievements'], achievements: settings.achievements, widgetSettings: settings.widgetSettings, hiddenLanguages: settings.hiddenLanguages, @@ -360,7 +361,7 @@ export function WorkshopScreen({
{ALL_WIDGETS.map(w => { 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 return ( @@ -411,6 +412,25 @@ export function WorkshopScreen({
)} + {w.id === 'streaks' && ( +
+ Show dates + +
+ )} + {w.id === 'collaborators' && (
diff --git a/generator/src/api.py b/generator/src/api.py index 3837c85..fc0e3fb 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -184,7 +184,7 @@ app.config["MAX_CONTENT_LENGTH"] = 128 * 1024 # ["max_count"])` raise, or by ballooning the DB with unbounded fields. # 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"} _MAX_ACHIEVEMENTS = 10 _TITLE_MAX = 80 @@ -240,6 +240,7 @@ def _coerce_widget_settings(v) -> dict: allowed = { "grade": {"max_tags": "int"}, "impact": {"line_color": "color"}, + "streaks": {"show_dates": "bool"}, "collaborators": {"max_count": "int", "bar_color": "color"}, "focus": {"max_categories": "int"}, "languages": {"max_languages": "int"}, @@ -259,6 +260,8 @@ def _coerce_widget_settings(v) -> dict: clean[key] = int(val) except (TypeError, ValueError): continue + elif kind == "bool": + clean[key] = bool(val) elif kind == "color": # Deep color validation happens in the widget renderer # (safe_color). Here we only reject obviously wrong types