feat(growth): themes and alternative pages, result-screen hints

This commit is contained in:
shay
2026-11-28 10:22:36 -08:00
parent 3653ef66a5
commit c0eba0b5a4
6 changed files with 628 additions and 2 deletions
@@ -1,5 +1,16 @@
import DOMPurify from 'dompurify'
import { useMemo, useState } from 'react'
import { useEffect, useMemo, useState } from 'react'
// Stats cards people already have in their profile README. A visitor who has
// one needs "replace that line", not "paste this somewhere".
const OTHER_CARDS: [RegExp, string][] = [
[/github-readme-stats/i, 'github-readme-stats'],
[/streak-stats|readme-streak/i, 'GitHub Readme Streak Stats'],
[/github-stats-extended/i, 'github-stats-extended'],
[/github-profile-summary-cards/i, 'github-profile-summary-cards'],
[/github-profile-trophy/i, 'github-profile-trophy'],
[/github-readme-activity-graph/i, 'github-readme-activity-graph'],
]
function sanitizeSvg(raw: string): string {
return DOMPurify.sanitize(raw, {
@@ -40,6 +51,36 @@ export function ResultScreen({ username, generating, generatedSvg, generateError
const profileRepo = encodeURIComponent(username)
const editReadmeUrl = `https://github.com/${profileRepo}/${profileRepo}/edit/HEAD/README.md`
const createRepoUrl = `https://github.com/new?name=${profileRepo}&description=${encodeURIComponent('My GitHub profile')}&visibility=public`
// raw.githubusercontent.com sends Access-Control-Allow-Origin: *, so the
// browser can read the profile README directly. Any failure leaves the
// generic hint in place.
const [found, setFound] = useState<{ repo: string; card: string } | null>(null)
const otherCard = found?.repo === profileRepo ? found.card : null
// No profile repo at all: GitHub's new-file page takes ?filename=&value=, so
// after creating the (empty) repo one click opens a README with the card in it.
const [noRepo, setNoRepo] = useState<string | null>(null)
const addReadmeUrl = `https://github.com/${profileRepo}/${profileRepo}/new/main?filename=README.md&value=${encodeURIComponent(embedSnippet + '\n')}`
useEffect(() => {
let live = true
fetch(`https://raw.githubusercontent.com/${profileRepo}/${profileRepo}/HEAD/README.md`)
.then(r => {
// A 404 here can also mean readme.md in another case, so only the
// API's 404 for the repo itself counts as "no repo".
if (r.status === 404) {
fetch(`https://api.github.com/repos/${profileRepo}/${profileRepo}`)
.then(a => { if (live && a.status === 404) setNoRepo(profileRepo) })
.catch(() => {})
}
return r.ok ? r.text() : ''
})
.then(text => {
if (!live || /gh-stats\.com/i.test(text)) return
const hit = OTHER_CARDS.find(([re]) => re.test(text))
if (hit) setFound({ repo: profileRepo, card: hit[1] })
})
.catch(() => {})
return () => { live = false }
}, [profileRepo])
const copy = async () => {
try {
@@ -160,6 +201,20 @@ export function ResultScreen({ username, generating, generatedSvg, generateError
{' \u00b7 '}
<a href={createRepoUrl} target="_blank" rel="noopener noreferrer" className="text-gray-700 underline hover:text-gray-900">Create the repo</a>
</p>
{noRepo === profileRepo && (
<p className="mt-2 text-xs text-gray-600">
{username} has no profile README yet. 1.{' '}
<a href={createRepoUrl} target="_blank" rel="noopener noreferrer" className="text-gray-800 underline hover:text-gray-900">Create the repo</a>
{' '}(leave "Add a README" unticked). 2.{' '}
<a href={addReadmeUrl} target="_blank" rel="noopener noreferrer" className="text-gray-800 underline hover:text-gray-900">Add a README with this card in it</a>
{' '}and commit.
</p>
)}
{otherCard && (
<p className="mt-2 text-xs text-gray-600">
This README already shows a {otherCard} card. Replace that line with the one above to swap it for this card.
</p>
)}
</div>
</>
)}