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

522 lines
16 KiB
TypeScript

'use client'
import { useEffect, useRef, useState } from 'react'
import { useRouter } from 'next/navigation'
import { useAuthStore } from '@/store/authStore'
import { postService } from '@/services/postService'
import PostCard, { type Post } from '@/components/PostCard'
import StoriesRing from '@/components/StoriesRing'
import { useToast } from '@/components/ui/toast'
import PullToRefresh from '@/components/PullToRefresh'
import { getErrorMessage } from '@/utils/error'
import api from '@/services/api'
// ---------------------------------------------------------------------------
// Compose widget — "What's happening" quick-post box
// ---------------------------------------------------------------------------
interface ComposeWidgetProps {
userAvatarUrl?: string | null
onNavigateToCreate: (mode?: 'photo' | 'code' | 'repo') => void
}
function ComposeWidget({ userAvatarUrl, onNavigateToCreate }: ComposeWidgetProps) {
const [focused, setFocused] = useState(false)
const avatarInitial = userAvatarUrl ? null : '?'
return (
<div
style={{
margin: '12px 16px 0',
background: 'var(--surface, #1c1c1e)',
border: '1px solid var(--border, #38383a)',
borderRadius: 12,
padding: '12px 14px',
transition: 'border-color 0.15s',
...(focused ? { borderColor: 'var(--primary)' } : {}),
}}
>
<div style={{ display: 'flex', gap: 10, alignItems: 'center' }}>
{/* Avatar */}
<div
style={{
width: 36,
height: 36,
borderRadius: '50%',
background: userAvatarUrl ? 'transparent' : 'var(--primary)',
flexShrink: 0,
overflow: 'hidden',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
fontSize: 15,
fontWeight: 700,
color: '#fff',
}}
>
{userAvatarUrl ? (
// eslint-disable-next-line @next/next/no-img-element
<img src={userAvatarUrl} alt="avatar" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
) : (
avatarInitial
)}
</div>
{/* Fake text input — clicking opens the full create page */}
<button
onClick={() => onNavigateToCreate()}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
style={{
flex: 1,
background: 'var(--surface-secondary, #2c2c2e)',
border: 'none',
borderRadius: 20,
padding: '9px 16px',
textAlign: 'left',
color: 'var(--foreground-secondary, #98989d)',
fontSize: 14,
cursor: 'pointer',
fontFamily: 'inherit',
}}
>
What are you building?
</button>
</div>
{/* Quick action row */}
<div
style={{
display: 'flex',
gap: 4,
marginTop: 10,
paddingTop: 10,
borderTop: '1px solid var(--border, #38383a)',
}}
>
<QuickActionButton
label="Photo"
icon={
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<rect x="3" y="3" width="18" height="18" rx="2" />
<circle cx="8.5" cy="8.5" r="1.5" />
<polyline points="21 15 16 10 5 21" />
</svg>
}
onClick={() => onNavigateToCreate('photo')}
/>
<QuickActionButton
label="Code"
icon={
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<polyline points="16 18 22 12 16 6" />
<polyline points="8 6 2 12 8 18" />
</svg>
}
onClick={() => onNavigateToCreate('code')}
/>
<QuickActionButton
label="Repository"
icon={
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22" />
</svg>
}
onClick={() => onNavigateToCreate('repo')}
/>
</div>
</div>
)
}
interface QuickActionButtonProps {
label: string
icon: React.ReactNode
onClick: () => void
}
function QuickActionButton({ label, icon, onClick }: QuickActionButtonProps) {
return (
<button
onClick={onClick}
style={{
display: 'flex',
alignItems: 'center',
gap: 6,
padding: '6px 12px',
background: 'transparent',
border: '1px solid var(--border, #38383a)',
borderRadius: 20,
color: 'var(--foreground-secondary, #98989d)',
fontSize: 13,
fontWeight: 500,
cursor: 'pointer',
fontFamily: 'inherit',
transition: 'background 0.12s, color 0.12s, border-color 0.12s',
}}
onMouseEnter={e => {
const el = e.currentTarget as HTMLButtonElement
el.style.background = 'var(--surface-secondary, #2c2c2e)'
el.style.color = 'var(--foreground, #f5f5f7)'
el.style.borderColor = 'var(--primary)'
}}
onMouseLeave={e => {
const el = e.currentTarget as HTMLButtonElement
el.style.background = 'transparent'
el.style.color = 'var(--foreground-secondary, #98989d)'
el.style.borderColor = 'var(--border, #38383a)'
}}
>
{icon}
{label}
</button>
)
}
// ---------------------------------------------------------------------------
// Feed post normalization — maps the API payload into the shared `Post` shape
// consumed by PostCard, tolerating field-name variations from the backend.
// ---------------------------------------------------------------------------
function toNumber(value: unknown): number {
const n = Number(value)
return Number.isFinite(n) ? n : 0
}
function normalizePost(raw: unknown): Post {
const r = (raw ?? {}) as Record<string, unknown>
const authorRaw = (r.author ?? r.user ?? {}) as Record<string, unknown>
const count = (r._count ?? {}) as Record<string, unknown>
return {
id: String(r.id ?? ''),
content: String(r.content ?? ''),
author: {
username: String(authorRaw.username ?? ''),
displayName: String(authorRaw.displayName ?? authorRaw.name ?? authorRaw.username ?? ''),
avatarUrl: (authorRaw.avatarUrl as string) ?? (authorRaw.avatar as string) ?? undefined,
},
likes: toNumber(r.likes ?? r.likesCount ?? count.likes),
comments: toNumber(r.comments ?? r.commentsCount ?? count.comments),
reposts: toNumber(r.reposts ?? r.sharesCount ?? count.shares),
createdAt: String(r.createdAt ?? new Date().toISOString()),
tags: Array.isArray(r.tags) ? (r.tags as unknown[]).map(String) : undefined,
repoOwner: (r.repoOwner as string) ?? undefined,
repoName: (r.repoName as string) ?? undefined,
isLiked: Boolean(r.isLiked),
}
}
interface FeedApiResponse {
posts?: unknown[]
nextCursor?: string | null
}
function extractPosts(data: FeedApiResponse | unknown[]): unknown[] {
if (Array.isArray(data)) return data
return data.posts ?? []
}
// ---------------------------------------------------------------------------
// Site activity — real repo/activity items from public Rithub repos, used to
// fill the empty-feed state for a brand-new signup who follows no one yet.
// Built 2026-11-30 (RIT-RET). Backed by GET /forge-activity/public-site-feed
// (no auth, no fake users/posts — every item is a real repo or a real event
// polled live from Rithub's own public API).
// ---------------------------------------------------------------------------
interface SiteActivityItem {
id: string
kind: string
label: string
repository: { fullName: string; url: string; description: string | null; stars: number; language: string | null }
actorLogin: string | null
createdAt: string
}
function relativeTime(iso: string): string {
const diffMs = Date.now() - new Date(iso).getTime()
const mins = Math.floor(diffMs / 60000)
if (mins < 1) return 'just now'
if (mins < 60) return `${mins}m ago`
const hours = Math.floor(mins / 60)
if (hours < 24) return `${hours}h ago`
return `${Math.floor(hours / 24)}d ago`
}
// ---------------------------------------------------------------------------
// FeedPage
// ---------------------------------------------------------------------------
export default function FeedPage() {
const router = useRouter()
const { isAuthenticated, isInitialized, user } = useAuthStore()
const { showToast } = useToast()
const [posts, setPosts] = useState<Post[]>([])
const [loading, setLoading] = useState(true)
const [loadingMore, setLoadingMore] = useState(false)
const [cursor, setCursor] = useState<string | null>(null)
const [hasMore, setHasMore] = useState(true)
const [siteActivity, setSiteActivity] = useState<SiteActivityItem[]>([])
const sentinelRef = useRef<HTMLDivElement>(null)
const avatarUrl: string | null = user?.avatar ?? null
const handleNavigateToCreate = (mode?: 'photo' | 'code' | 'repo') => {
const params = mode ? `?mode=${mode}` : ''
router.push(`/create${params}`)
}
useEffect(() => {
if (!isInitialized) return
if (!isAuthenticated) {
router.push('/login')
return
}
void loadFeed()
}, [isAuthenticated, isInitialized])
useEffect(() => {
if (loading) return
if (!isAuthenticated || !user || posts.length > 0) return
const interests = user.interests
if (!interests?.length) {
const redirected = sessionStorage.getItem('gitgram_onboarding_redirected')
if (!redirected) {
sessionStorage.setItem('gitgram_onboarding_redirected', '1')
router.push('/onboarding/welcome')
}
}
}, [loading, isAuthenticated, user, posts.length])
// Fill the empty-feed state with real public Rithub repo activity so a
// brand-new signup never sees a blank page — no fake users/posts, just the
// real sitewide feed described above.
useEffect(() => {
if (loading || posts.length > 0) return
let cancelled = false
;(async () => {
try {
const { data } = await api.get<{ success: boolean; data: SiteActivityItem[] }>(
'/forge-activity/public-site-feed',
{ params: { limit: 12 } }
)
if (!cancelled) setSiteActivity(data?.data ?? [])
} catch {
// Non-fatal — the empty-state CTAs still render without it.
}
})()
return () => {
cancelled = true
}
}, [loading, posts.length])
// Infinite scroll sentinel
useEffect(() => {
if (!sentinelRef.current || !hasMore) return
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting && !loadingMore && hasMore) {
void loadMore()
}
},
{ threshold: 0.1 }
)
observer.observe(sentinelRef.current)
return () => observer.disconnect()
}, [hasMore, loadingMore, cursor])
const loadFeed = async () => {
setLoading(true)
try {
// Use the cursor-based endpoint for the first page too, so infinite
// scroll continues consistently past page one regardless of whether the
// page-based response happens to include a cursor.
const data = (await postService.getFeedCursor()) as FeedApiResponse | unknown[]
const nextCursor = Array.isArray(data) ? null : data.nextCursor ?? null
const loaded = extractPosts(data).map(normalizePost)
setPosts(loaded)
setCursor(nextCursor)
setHasMore(!!nextCursor)
} catch (error: unknown) {
showToast(getErrorMessage(error), 'error')
} finally {
setLoading(false)
}
}
const loadMore = async () => {
if (!cursor || loadingMore) return
setLoadingMore(true)
try {
const data = (await postService.getFeedCursor(cursor)) as FeedApiResponse | unknown[]
const nextCursor = Array.isArray(data) ? null : data.nextCursor ?? null
const newPosts = extractPosts(data).map(normalizePost)
setPosts(prev => [...prev, ...newPosts])
setCursor(nextCursor)
setHasMore(!!nextCursor)
} catch (error: unknown) {
showToast(getErrorMessage(error), 'error')
} finally {
setLoadingMore(false)
}
}
const handleLike = async (postId: string) => {
try {
await postService.likePost(postId)
setPosts(prev =>
prev.map(p => (p.id === postId ? { ...p, isLiked: true, likes: p.likes + 1 } : p)),
)
} catch {
showToast('Failed to like post', 'error')
}
}
if (!isInitialized) {
return (
<div style={{ minHeight: '100vh', background: '#0F0F1A', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<div style={{ color: '#94A3B8' }}>Loading...</div>
</div>
)
}
if (!isAuthenticated) return null
return (
<div style={{ minHeight: '100vh', background: '#0F0F1A' }}>
<div style={{ maxWidth: 640, margin: '0 auto', paddingBottom: 80 }}>
{/* Stories ring */}
<div style={{ padding: '16px 0 8px', borderBottom: '1px solid #2D2D50' }}>
<StoriesRing onUserClick={(userId) => router.push(`/stories/${userId}`)} />
</div>
{/* Feed header */}
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '16px 16px 8px' }}>
<h1 style={{ fontSize: 22, fontWeight: 700, color: '#E2E8F0', margin: 0 }}>For You</h1>
<button
onClick={() => router.push('/create')}
aria-label="New post"
title="New post"
style={{
width: 36,
height: 36,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
background: 'var(--primary)',
color: '#fff',
border: 'none',
borderRadius: '50%',
cursor: 'pointer',
}}
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
</svg>
</button>
</div>
{/* Compose widget — "What's happening" quick-post box */}
<ComposeWidget
userAvatarUrl={avatarUrl}
onNavigateToCreate={handleNavigateToCreate}
/>
{/* Divider */}
<div style={{ height: 1, background: 'var(--border, #38383a)', margin: '12px 0 0' }} />
{loading ? (
<div style={{ padding: '48px 16px', textAlign: 'center', color: '#94A3B8' }}>
Loading your feed...
</div>
) : posts.length === 0 ? (
<div style={{ padding: '48px 16px', textAlign: 'center', color: '#94A3B8' }}>
<p style={{ marginBottom: 16 }}>Your feed is empty. Bring in a repo or follow some developers!</p>
<div style={{ display: 'flex', gap: 12, justifyContent: 'center', flexWrap: 'wrap' }}>
<button
onClick={() => router.push('/import-from-github')}
aria-label="Import from GitHub"
title="Import from GitHub"
style={{ width: 44, height: 44, display: 'flex', alignItems: 'center', justifyContent: 'center', background: '#0071e3', color: '#fff', border: 'none', borderRadius: '50%', cursor: 'pointer' }}
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16v2a2 2 0 002 2h12a2 2 0 002-2v-2M7 10l5 5 5-5M12 15V3" />
</svg>
</button>
<button
onClick={() => router.push('/discover')}
aria-label="Discover people"
title="Discover people"
style={{ width: 44, height: 44, display: 'flex', alignItems: 'center', justifyContent: 'center', background: 'var(--primary)', color: '#fff', border: 'none', borderRadius: '50%', cursor: 'pointer' }}
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
<circle cx="9" cy="7" r="3" strokeWidth={2} />
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M2 20c0-3.3 3.1-5 7-5s7 1.7 7 5M16 11a3 3 0 100-6M22 20c0-2.5-2-4-4.5-4.5" />
</svg>
</button>
<button
onClick={() => router.push('/trending')}
aria-label="Trending"
title="Trending"
style={{ width: 44, height: 44, display: 'flex', alignItems: 'center', justifyContent: 'center', background: 'transparent', color: 'var(--primary)', border: '1px solid var(--primary)', borderRadius: '50%', cursor: 'pointer' }}
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 7h8m0 0v8m0-8l-9 9-5-5-5 5" />
</svg>
</button>
</div>
{siteActivity.length > 0 && (
<div style={{ marginTop: 40, textAlign: 'left' }}>
<p style={{ fontSize: 13, fontWeight: 600, color: '#64748B', textTransform: 'uppercase', letterSpacing: 0.4, marginBottom: 12 }}>
Repo activity on Rithub
</p>
<div style={{ display: 'flex', flexDirection: 'column', gap: 1, border: '1px solid var(--border, #2D2D50)', borderRadius: 12, overflow: 'hidden' }}>
{siteActivity.map(item => (
<a
key={item.id}
href={item.repository.url}
target="_blank"
rel="noreferrer"
style={{
display: 'flex',
flexDirection: 'column',
gap: 2,
padding: '12px 14px',
background: 'var(--surface, #161b22)',
textDecoration: 'none',
}}
>
<span style={{ fontSize: 14, color: '#E2E8F0' }}>{item.label}</span>
<span style={{ fontSize: 12, color: '#64748B' }}>{relativeTime(item.createdAt)}</span>
</a>
))}
</div>
</div>
)}
</div>
) : (
<PullToRefresh onRefresh={loadFeed}>
<div style={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
{posts.map(post => (
<PostCard
key={post.id}
post={post}
onLike={handleLike}
/>
))}
{/* Infinite scroll sentinel */}
<div ref={sentinelRef} style={{ height: 40, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
{loadingMore && <span style={{ color: '#94A3B8', fontSize: 13 }}>Loading more...</span>}
{!hasMore && posts.length > 0 && <span style={{ color: '#64748B', fontSize: 13 }}>You're all caught up </span>}
</div>
</div>
</PullToRefresh>
)}
</div>
</div>
)
}