caelum0x aa848c66d0 fix(ritgram): Apple-design unslop pass + Rithub OAuth2 sign-in + critical login-redirect-loop fix
Apple design (plans/gtm/APPLE-DESIGN-RULES-2026-11-17.md): rewrote theme
tokens in globals.css/tailwind.config.js to neutral black/white + one
accent (#0071E3), replacing the GitHub-dark blue+green dual-accent theme;
mechanically swept every raw inline hex/rgba literal across app/components
that bypassed the CSS vars (506 hex + 326 rgb(a) replacements) since most
of the app hardcoded colors rather than using tokens; removed all emoji,
decorative gradients (Tailwind bg-gradient-to-* and raw linear/radial-
gradient() in inline styles), confetti, and glow/blur decorative orbs;
converted Navbar/Navigation to icon-only with aria-label+title. Finished
two stopped agents' in-progress work: deleted the fake AnimatedStats/
LiveActivity/ProjectShowcase/Testimonials auth-panel components and
cleaned up the emoji-laden feature list in (auth)/layout.tsx. Removed
remaining fabricated stats (topics/topics/[slug] posts-per-week/
contributors/followers) per the no-fake-numbers rule.

Rithub sign-in: added "Continue with Rithub" as an icon-only OAuth2
button (RithubMark component) on login/register, hidden "Continue with
GitHub" when GITHUB_CLIENT_ID is unset (confirmed unset in prod). Backend:
authenticateWithGitea in oauthService.ts exchanges the Gitea OAuth2 code,
resolves identity via the existing ForgeConnection table (no schema
migration needed), issues the same JWT/session as GitHub/Google login.
OAuth2 app created live on rithub.dev via its REST API as admin user
caelum0x; client id/secret stored in railyard-box's compose file (env
files don't ship to the runtime image, so the actual wiring lives in
docker-compose.yml, not backend/.env) and in ~/products/.env.

Critical bug found and fixed while verifying: services/api.ts's global
401 interceptor redirected every anonymous visitor to /login a couple
seconds after ANY page load -- including /login and /register themselves
-- because the AuthBootstrap's background GET /auth/me (expected to 401
for signed-out visitors) was treated the same as a real session-expiry
401, triggering a refresh attempt, another 401, and a hard
window.location.href redirect, which re-ran AuthBootstrap and repeated.
Fixed by excluding /auth/me and /auth/refresh from that redirect path.
Verified live with a fresh, cookie-less browser: previously looped
/register -> /login forever; now stable after 6s.

Also fixed: production CORS/API origin mismatch -- the frontend was
built with NEXT_PUBLIC_API_URL pointing at ritgram.ereboslab.com while
served from ritgram.rithub.dev, so every API call (login, register, etc.)
was blocked by CORS; rebuilt with rithub.dev as canonical and CORS_ORIGIN
allowing both domains.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-17 17:33:41 +03:00

203 lines
7.0 KiB
TypeScript

'use client';
import { useState } from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { UserHoverCard } from './UserHoverCard';
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface UserCardUser {
id: string;
username: string;
displayName?: string;
avatar?: string | null;
bio?: string | null;
followersCount: number;
isFollowing?: boolean;
}
interface UserCardProps {
user: UserCardUser;
variant?: 'compact' | 'full';
onFollowChange?: (userId: string, isFollowing: boolean) => void;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function formatFollowers(count: number): string {
if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M`;
if (count >= 1_000) return `${(count / 1_000).toFixed(1)}k`;
return String(count);
}
function getInitials(username: string, displayName?: string | null): string {
const name = displayName ?? username;
const parts = name.trim().split(/\s+/);
if (parts.length >= 2) {
return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase();
}
return name.slice(0, 2).toUpperCase();
}
// ---------------------------------------------------------------------------
// Avatar sub-component
// ---------------------------------------------------------------------------
function Avatar({ user, size }: { user: UserCardUser; size: 'sm' | 'lg' }) {
const [imgError, setImgError] = useState(false);
const dim = size === 'lg' ? 48 : 32;
const sizeClass = size === 'lg' ? 'w-12 h-12' : 'w-8 h-8';
const textClass = size === 'lg' ? 'text-base' : 'text-xs';
if (user.avatar && !imgError) {
return (
<Image
src={user.avatar}
alt={user.username}
width={dim}
height={dim}
className={`${sizeClass} rounded-full object-cover shrink-0`}
onError={() => setImgError(true)}
unoptimized
/>
);
}
return (
<div
className={`${sizeClass} rounded-full bg-primary/10 flex items-center justify-center shrink-0`}
aria-hidden="true"
>
<span className={`${textClass} font-semibold text-white`}>
{getInitials(user.username, user.displayName)}
</span>
</div>
);
}
// ---------------------------------------------------------------------------
// Main component
// ---------------------------------------------------------------------------
export function UserCard({ user, variant = 'full', onFollowChange }: UserCardProps) {
const [isFollowing, setIsFollowing] = useState(user.isFollowing ?? false);
const [isPending, setIsPending] = useState(false);
async function handleFollowToggle(e: React.MouseEvent) {
e.preventDefault();
e.stopPropagation();
if (isPending) return;
const next = !isFollowing;
// Optimistic update
setIsFollowing(next);
setIsPending(true);
try {
onFollowChange?.(user.id, next);
} finally {
setIsPending(false);
}
}
if (variant === 'compact') {
return (
<Link
href={`/profile/${user.username}`}
className="flex items-center gap-3 p-2 rounded-lg hover:bg-[var(--surface-secondary)] transition-colors group"
>
<Avatar user={user} size="sm" />
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-[var(--foreground)] truncate group-hover:text-[var(--primary)] transition-colors">
{user.displayName ?? user.username}
</p>
<p className="text-xs text-[var(--foreground-secondary)] truncate">
@{user.username}
</p>
</div>
<button
type="button"
onClick={handleFollowToggle}
disabled={isPending}
aria-label={isFollowing ? `Unfollow ${user.username}` : `Follow ${user.username}`}
className={[
'shrink-0 px-3 py-1 rounded-full text-xs font-semibold transition-colors disabled:opacity-50',
isFollowing
? 'bg-[var(--surface-secondary)] text-[var(--foreground-secondary)] hover:bg-[var(--error)]/15 hover:text-[var(--error)] border border-[var(--border)]'
: 'bg-[var(--primary)] text-white hover:bg-[var(--primary)]/80',
].join(' ')}
>
{isFollowing ? 'Following' : 'Follow'}
</button>
</Link>
);
}
// Full variant
return (
<div className="rounded-xl border border-[var(--border)] bg-[var(--surface)] p-4 flex flex-col gap-3">
{/* Header row */}
<div className="flex items-start justify-between gap-3">
<UserHoverCard username={user.username}>
<Link
href={`/profile/${user.username}`}
className="flex items-center gap-3 group min-w-0"
>
<Avatar user={user} size="lg" />
<div className="min-w-0">
<p className="text-sm font-semibold text-[var(--foreground)] truncate group-hover:text-[var(--primary)] transition-colors">
{user.displayName ?? user.username}
</p>
<p className="text-xs text-[var(--foreground-secondary)] truncate">
@{user.username}
</p>
</div>
</Link>
</UserHoverCard>
<button
type="button"
onClick={handleFollowToggle}
disabled={isPending}
aria-label={isFollowing ? `Unfollow ${user.username}` : `Follow ${user.username}`}
className={[
'shrink-0 px-4 py-1.5 rounded-full text-sm font-semibold transition-colors disabled:opacity-50',
isFollowing
? 'bg-[var(--surface-secondary)] text-[var(--foreground-secondary)] hover:bg-[var(--error)]/15 hover:text-[var(--error)] border border-[var(--border)]'
: 'bg-[var(--primary)] text-white hover:bg-[var(--primary)]/80',
].join(' ')}
>
{isPending ? '...' : isFollowing ? 'Following' : 'Follow'}
</button>
</div>
{/* Bio */}
{user.bio && (
<p className="text-sm text-[var(--foreground-secondary)] line-clamp-2 leading-relaxed">
{user.bio}
</p>
)}
{/* Followers count */}
<div className="flex items-center gap-1.5 text-xs text-[var(--foreground-secondary)]">
<svg className="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
<span>
<span className="font-semibold text-[var(--foreground)]">
{formatFollowers(user.followersCount)}
</span>{' '}
followers
</span>
</div>
</div>
);
}
export default UserCard;