caelum0x 86ab8811a7 feat: RIT-RET Ritgram activation -- fix onboarding 404/bounce, real repo-activity feed, one-click Rithub entry
Fixed real breakages found walking the live "Sign in with Rithub" ->
onboarding -> feed flow with a fresh mail.tm account:
- onboarding/interests called a nonexistent PATCH /users/preferences (404
  every time, treated as fatal) instead of the real POST /users/interests
  endpoint -- every real signup got stuck on step 2 unless they clicked
  "Skip for now".
- the cached auth-store user object stayed stale right after saving
  interests, so the feed page's empty-feed onboarding-redirect bounced a
  user who just finished onboarding straight back to /onboarding/welcome.
  Now updates the store immediately after the real POST succeeds.
- onboarding/follow's handleFinish called the same nonexistent preferences
  endpoint (silently swallowed, but still real dead code) -- removed.

Feed empty-state is no longer blank for a newcomer with 0 follows: new
GET /forge-activity/public-site-feed (no auth) polls Rithub's own public
API for recently updated/created repos + real events, no fake users or
posts. Wired into the feed page as a labeled "Repo activity on Rithub"
section.

New one-click "Open on Ritgram" entry point at /go/rithub (Ritgram-owned
Next.js route handler, not a Rithub template) -- server-redirects straight
into the existing Rithub OAuth flow. Added to the already-live
rithub-lifecycle welcome/48h-nudge emails (apps/rithub-lifecycle, a
separate RIT-RET pass) so every real new Rithub signup gets the link.

Also fixed stale ritgram.ereboslab.com domain references and emoji in the
Ritgram welcome email template (dead code, never wired to send -- fixed
for correctness regardless) and the DEFAULT_FROM fallback address.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-30 15:31:49 +03:00

445 lines
15 KiB
TypeScript

'use client'
import { useState, useEffect, useCallback } from 'react'
import { useRouter } from 'next/navigation'
import Image from 'next/image'
import api from '@/services/api'
import { userService } from '@/services/userService'
interface SuggestedUser {
id: string
username: string
bio?: string
avatar?: string
followers_count?: number
}
interface SuggestedUsersResponse {
users: SuggestedUser[]
}
const TOTAL_STEPS = 4
const CURRENT_STEP = 3
const SUGGESTED_LIMIT = 12
const MIN_FOLLOWS = 1
function getErrorMessage(error: unknown): string {
if (error instanceof Error) return error.message
return 'Something went wrong. Please try again.'
}
function UserAvatar({ user }: { user: SuggestedUser }) {
if (user.avatar) {
return (
<Image
src={user.avatar}
alt={user.username}
width={52}
height={52}
className="rounded-full object-cover"
style={{ width: 52, height: 52 }}
/>
)
}
return (
<div
className="flex items-center justify-center rounded-full text-white font-bold text-lg select-none"
style={{
width: 52,
height: 52,
backgroundColor: 'var(--primary)',
flexShrink: 0,
}}
aria-hidden="true"
>
{user.username[0]?.toUpperCase() ?? '?'}
</div>
)
}
export default function FollowPage() {
const router = useRouter()
const [users, setUsers] = useState<SuggestedUser[]>([])
const [following, setFollowing] = useState<Set<string>>(new Set())
const [pendingFollows, setPendingFollows] = useState<Set<string>>(new Set())
const [loading, setLoading] = useState(true)
const [loadError, setLoadError] = useState<string | null>(null)
const [finishing, setFinishing] = useState(false)
const [finishError, setFinishError] = useState<string | null>(null)
const progressPercent = Math.round((CURRENT_STEP / TOTAL_STEPS) * 100)
const canFinish = following.size >= MIN_FOLLOWS && !finishing
const loadSuggestedUsers = useCallback(async () => {
setLoading(true)
setLoadError(null)
try {
const { data } = await api.get<SuggestedUsersResponse>('/users/suggested', {
params: { limit: SUGGESTED_LIMIT },
})
setUsers(data?.users ?? [])
} catch (err: unknown) {
setLoadError(getErrorMessage(err))
} finally {
setLoading(false)
}
}, [])
useEffect(() => {
loadSuggestedUsers()
}, [loadSuggestedUsers])
const toggleFollow = async (userId: string) => {
// Optimistic update
setFollowing(prev => {
const next = new Set(prev)
if (next.has(userId)) {
next.delete(userId)
} else {
next.add(userId)
}
return next
})
// Track as pending if not yet confirmed
setPendingFollows(prev => new Set(prev).add(userId))
try {
if (following.has(userId)) {
await userService.unfollow(userId)
} else {
await userService.follow(userId)
}
} catch (err: unknown) {
// Roll back on failure
setFollowing(prev => {
const next = new Set(prev)
if (next.has(userId)) {
next.delete(userId)
} else {
next.add(userId)
}
return next
})
} finally {
setPendingFollows(prev => {
const next = new Set(prev)
next.delete(userId)
return next
})
}
}
const handleFollowAll = async () => {
const unfollowed = users.filter(u => !following.has(u.id))
// Optimistic: mark all as followed immediately
setFollowing(prev => {
const next = new Set(prev)
unfollowed.forEach(u => next.add(u.id))
return next
})
// Fire follow requests in parallel — failures are silent (best-effort)
await Promise.allSettled(unfollowed.map(u => userService.follow(u.id)))
}
const handleFinish = () => {
if (!canFinish) return
// Any in-flight follows are already fired in toggleFollow. There is no
// server-side "onboarding complete" flag to persist (no such column or
// endpoint exists on the backend) — this used to call the same
// nonexistent `PATCH /users/preferences` route fixed on the interests
// step (found 2026-11-30), silently 404ing on every real signup. Removed
// rather than inventing a new DB column for a one-off flag nothing reads.
router.push('/onboarding/first-repo')
}
const handleBack = () => {
router.push('/onboarding/interests')
}
const allFollowed = users.length > 0 && users.every(u => following.has(u.id))
return (
<div
className="min-h-screen flex flex-col items-center justify-start py-10 px-4"
style={{ backgroundColor: 'var(--background)' }}
>
<div className="w-full max-w-2xl">
{/* Progress */}
<div className="mb-8">
<div className="flex items-center justify-between mb-2">
<span
className="text-sm font-medium"
style={{ color: 'var(--foreground-secondary)' }}
>
Step {CURRENT_STEP} of {TOTAL_STEPS}
</span>
<span
className="text-sm tabular-nums"
style={{ color: 'var(--foreground-secondary)' }}
>
{following.size} following
</span>
</div>
<div
className="w-full h-1.5 rounded-full overflow-hidden"
style={{ backgroundColor: 'var(--surface)' }}
>
<div
className="h-full rounded-full"
style={{
width: `${progressPercent}%`,
backgroundColor: 'var(--primary)',
transition: 'width 0.4s ease',
}}
/>
</div>
</div>
{/* Heading + Follow All */}
<div className="flex items-start justify-between gap-4 mb-8">
<div>
<h1
className="text-3xl font-bold mb-2"
style={{ color: 'var(--foreground)' }}
>
Follow some developers to get started
</h1>
<p style={{ color: 'var(--foreground-secondary)' }} className="text-base">
Your feed will come alive once you follow a few people.
</p>
</div>
{!loading && users.length > 0 && !allFollowed && (
<button
type="button"
onClick={handleFollowAll}
className="shrink-0 px-4 py-2 rounded-lg text-sm font-semibold focus:outline-none focus-visible:ring-2"
style={{
backgroundColor: 'rgba(0, 113, 227, 0.12)',
border: '1px solid var(--primary)',
color: 'var(--primary)',
}}
>
Follow all
</button>
)}
</div>
{/* Counter badge */}
<div className="mb-5">
<span
className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-sm font-medium"
style={{
backgroundColor: following.size >= MIN_FOLLOWS
? 'rgba(0, 113, 227, 0.12)'
: 'var(--surface)',
color: following.size >= MIN_FOLLOWS
? 'var(--primary)'
: 'var(--foreground-secondary)',
border: '1px solid',
borderColor: following.size >= MIN_FOLLOWS ? 'var(--primary)' : 'var(--border)',
transition: 'all 0.2s ease',
}}
>
{following.size >= MIN_FOLLOWS ? (
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
<circle cx="7" cy="7" r="7" fill="var(--primary)" fillOpacity="0.2" />
<path
d="M4 7l2 2 4-4"
stroke="var(--primary)"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
) : null}
{following.size} following · follow at least {MIN_FOLLOWS} to continue
</span>
</div>
{/* Load error */}
{loadError !== null && (
<div
className="flex items-center gap-3 rounded-xl px-4 py-3 mb-5 text-sm"
style={{
backgroundColor: 'rgba(255, 69, 58, 0.1)',
border: '1px solid rgba(255, 69, 58, 0.3)',
color: 'var(--error)',
}}
>
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" className="shrink-0" aria-hidden="true">
<circle cx="8" cy="8" r="7" stroke="currentColor" strokeWidth="1.5" />
<path d="M8 4.5v4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
<circle cx="8" cy="11" r="0.75" fill="currentColor" />
</svg>
<span className="flex-1">{loadError}</span>
<button
type="button"
onClick={loadSuggestedUsers}
className="font-medium underline focus:outline-none"
>
Retry
</button>
</div>
)}
{/* User grid */}
{loading ? (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3 mb-8">
{Array.from({ length: 6 }).map((_, i) => (
<div
key={i}
className="flex items-center gap-4 p-4 rounded-2xl animate-pulse"
style={{ backgroundColor: 'var(--surface)', border: '1px solid var(--border)' }}
aria-hidden="true"
>
<div className="rounded-full shrink-0" style={{ width: 52, height: 52, backgroundColor: 'var(--surface-secondary)' }} />
<div className="flex-1 space-y-2">
<div className="h-3 rounded" style={{ backgroundColor: 'var(--surface-secondary)', width: '60%' }} />
<div className="h-2.5 rounded" style={{ backgroundColor: 'var(--surface-secondary)', width: '80%' }} />
</div>
<div className="rounded-lg h-8 w-20 shrink-0" style={{ backgroundColor: 'var(--surface-secondary)' }} />
</div>
))}
</div>
) : users.length === 0 && loadError === null ? (
<div
className="text-center py-12 rounded-2xl mb-8"
style={{ backgroundColor: 'var(--surface)', border: '1px solid var(--border)' }}
>
<p style={{ color: 'var(--foreground-secondary)' }} className="text-sm">
No suggested users found right now. You can follow people from the feed later.
</p>
</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3 mb-8">
{users.map(user => {
const isFollowing = following.has(user.id)
const isPending = pendingFollows.has(user.id)
return (
<div
key={user.id}
className="flex items-center gap-4 p-4 rounded-2xl"
style={{
backgroundColor: 'var(--surface)',
border: '1px solid',
borderColor: isFollowing ? 'var(--border)' : 'var(--border)',
transition: 'border-color 0.2s ease',
}}
>
<div className="shrink-0">
<UserAvatar user={user} />
</div>
<div className="flex-1 min-w-0">
<p
className="font-semibold text-sm truncate"
style={{ color: 'var(--foreground)' }}
>
@{user.username}
</p>
{user.bio && (
<p
className="text-xs leading-relaxed mt-0.5 line-clamp-2"
style={{ color: 'var(--foreground-secondary)' }}
>
{user.bio}
</p>
)}
{typeof user.followers_count === 'number' && (
<p
className="text-xs mt-1"
style={{ color: 'var(--foreground-secondary)' }}
>
{user.followers_count.toLocaleString()} followers
</p>
)}
</div>
<button
type="button"
onClick={() => toggleFollow(user.id)}
disabled={isPending}
className="shrink-0 px-3 py-1.5 rounded-lg text-xs font-semibold focus:outline-none focus-visible:ring-2"
style={{
backgroundColor: isFollowing
? 'var(--surface-secondary)'
: 'var(--primary)',
color: isFollowing ? 'var(--foreground-secondary)' : '#fff',
border: isFollowing ? '1px solid var(--border)' : 'none',
opacity: isPending ? 0.6 : 1,
cursor: isPending ? 'wait' : 'pointer',
transition: 'background-color 0.15s ease, color 0.15s ease',
minWidth: 72,
}}
>
{isPending ? '…' : isFollowing ? 'Following' : 'Follow'}
</button>
</div>
)
})}
</div>
)}
{/* Finish error */}
{finishError !== null && (
<div
className="flex items-center gap-3 rounded-xl px-4 py-3 mb-5 text-sm"
style={{
backgroundColor: 'rgba(255, 69, 58, 0.1)',
border: '1px solid rgba(255, 69, 58, 0.3)',
color: 'var(--error)',
}}
>
{finishError}
</div>
)}
{/* Actions */}
<div className="flex gap-3">
<button
type="button"
onClick={handleBack}
className="flex-none px-5 py-3.5 rounded-xl font-semibold text-base focus:outline-none focus-visible:ring-2"
style={{
backgroundColor: 'var(--surface)',
border: '1px solid var(--border)',
color: 'var(--foreground-secondary)',
}}
>
Back
</button>
<button
type="button"
onClick={handleFinish}
disabled={!canFinish}
className="flex-1 py-3.5 rounded-xl font-semibold text-base focus:outline-none focus-visible:ring-2"
style={{
backgroundColor: canFinish ? 'var(--accent)' : 'var(--surface)',
color: canFinish ? '#fff' : 'var(--foreground-secondary)',
border: canFinish ? 'none' : '1px solid var(--border)',
cursor: canFinish ? 'pointer' : 'not-allowed',
opacity: canFinish ? 1 : 0.6,
transition: 'background-color 0.15s ease, opacity 0.15s ease',
}}
>
{finishing ? 'Setting up your feed…' : 'Finish — go to my feed'}
</button>
</div>
{/* Skip */}
<div className="text-center mt-4">
<button
type="button"
onClick={() => router.push('/onboarding/first-repo')}
className="text-sm hover:underline focus:outline-none"
style={{ color: 'var(--foreground-secondary)' }}
>
Skip for now
</button>
</div>
</div>
</div>
)
}