mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(frontend): show the product, keep work across sign-in, add social preview
Of 27 people who typed a username in the last 14 days, 15 left before a widget rendered. The landing page showed zero pixels of a product whose entire value is visual, so it now leads with a live example card and says concretely what you get. Sign-in used to discard everything: with no router the OAuth `next` is always "/", so users came back to an empty search box with their theme, widget order and achievements gone. Persist work-in-progress to sessionStorage and restore it on mount. The Workshop preview seeds from demo data (grade A, 842 commits, collaborators named alex/jordan/sam) with nothing saying so, so users saw numbers they knew were wrong. Label it until real data arrives. Add title, description, canonical, OG and Twitter tags, robots.txt and a sitemap. The page was a 445-byte shell titled "GitHub Widgets", so every link shared anywhere rendered with no image and no description. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BwMoSbmK77Ph7XaV5YnAdS
This commit is contained in:
@@ -51,6 +51,27 @@ const DEFAULT_SETTINGS: WidgetSettings = {
|
||||
|
||||
type Step = 'search' | 'workshop' | 'result'
|
||||
|
||||
// Signing in navigates the whole tab through GitHub's OAuth flow, and the
|
||||
// SPA keeps username + settings only in React state, so without this the
|
||||
// user comes back to an empty search box with their edits gone.
|
||||
// sessionStorage survives the round trip within the tab.
|
||||
const RESUME_KEY = 'gh-stats:in-progress'
|
||||
|
||||
type Resume = { username: string; settings: WidgetSettings }
|
||||
|
||||
function loadResume(): Resume | null {
|
||||
try {
|
||||
const raw = sessionStorage.getItem(RESUME_KEY)
|
||||
if (!raw) return null
|
||||
const parsed = JSON.parse(raw) as Partial<Resume> | null
|
||||
if (!parsed || typeof parsed.username !== 'string' || !parsed.username) return null
|
||||
if (!parsed.settings || typeof parsed.settings !== 'object') return null
|
||||
return { username: parsed.username, settings: { ...DEFAULT_SETTINGS, ...parsed.settings } }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const POLL_INTERVAL_MS = 2000
|
||||
const POLL_TIMEOUT_MS = 120_000
|
||||
// ResultScreen poll: a bit slower than the workshop poll because each tick
|
||||
@@ -74,9 +95,11 @@ export default function App() {
|
||||
return <DevScreen />
|
||||
}
|
||||
const [me, setMe] = useState<Me>({ login: null })
|
||||
const [step, setStep] = useState<Step>('search')
|
||||
const [username, setUsername] = useState('')
|
||||
const [settings, setSettings] = useState<WidgetSettings>({ ...DEFAULT_SETTINGS })
|
||||
// Work in progress left behind by a sign-in redirect, if any.
|
||||
const [resumed] = useState(loadResume)
|
||||
const [step, setStep] = useState<Step>(resumed ? 'workshop' : 'search')
|
||||
const [username, setUsername] = useState(resumed?.username ?? '')
|
||||
const [settings, setSettings] = useState<WidgetSettings>(resumed?.settings ?? { ...DEFAULT_SETTINGS })
|
||||
const [widgetData, setWidgetData] = useState<WidgetData | null>(null)
|
||||
const [fetchDone, setFetchDone] = useState(false)
|
||||
const [fetchError, setFetchError] = useState<string | null>(null)
|
||||
@@ -155,7 +178,10 @@ export default function App() {
|
||||
return stopResultPolling
|
||||
}, [step, widgetStatus, embedUrl, refetchEmbed, stopResultPolling])
|
||||
|
||||
const startFetch = useCallback((user: string) => {
|
||||
// `keepSettings` is set when resuming after a sign-in redirect: the
|
||||
// settings already on screen are the user's own unsaved edits, so neither
|
||||
// the reset nor the server hydration below may overwrite them.
|
||||
const startFetch = useCallback((user: string, keepSettings = false) => {
|
||||
stopPolling()
|
||||
aborted.current = false
|
||||
pollStart.current = Date.now()
|
||||
@@ -165,12 +191,12 @@ export default function App() {
|
||||
setWidgetData(DEMO_WIDGET_DATA)
|
||||
// Reset to defaults first so the form doesn't briefly show the previous
|
||||
// user's settings while we fetch this user's saved blob.
|
||||
setSettings({ ...DEFAULT_SETTINGS })
|
||||
if (!keepSettings) setSettings({ ...DEFAULT_SETTINGS })
|
||||
|
||||
// Hydrate the Workshop form from the user's persisted settings.
|
||||
// Backend stores snake_case keys; we map them onto the camelCase
|
||||
// state shape here. 404 (not enrolled yet) is expected — keep defaults.
|
||||
void (async () => {
|
||||
if (!keepSettings) void (async () => {
|
||||
try {
|
||||
const r = await fetch(`/api/${encodeURIComponent(user)}/settings`, {
|
||||
credentials: 'include',
|
||||
@@ -258,6 +284,24 @@ export default function App() {
|
||||
poll()
|
||||
}, [stopPolling])
|
||||
|
||||
// Keep the in-progress username and settings recoverable across the
|
||||
// sign-in redirect (and a plain reload). Cleared once the user is back
|
||||
// on the search screen, so a fresh visit starts fresh.
|
||||
useEffect(() => {
|
||||
try {
|
||||
if (step === 'search' || !username) sessionStorage.removeItem(RESUME_KEY)
|
||||
else sessionStorage.setItem(RESUME_KEY, JSON.stringify({ username, settings }))
|
||||
} catch {
|
||||
/* private mode or quota: resuming is a nicety, not a requirement */
|
||||
}
|
||||
}, [step, username, settings])
|
||||
|
||||
// Restore: the widget data itself is not persisted, so re-fetch it for
|
||||
// the username we came back to.
|
||||
useEffect(() => {
|
||||
if (resumed) startFetch(resumed.username, true)
|
||||
}, [resumed, startFetch])
|
||||
|
||||
const handleSearch = (user: string) => {
|
||||
setUsername(user)
|
||||
startFetch(user)
|
||||
@@ -377,6 +421,7 @@ export default function App() {
|
||||
fetchDone={fetchDone}
|
||||
fetchError={fetchError}
|
||||
widgetData={widgetData}
|
||||
isDemoData={widgetData === DEMO_WIDGET_DATA}
|
||||
me={me}
|
||||
onAuthChange={setMe}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user