railyard/apps/ritgram/web/components/RepositorySocialCard.tsx
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

219 lines
8.0 KiB
TypeScript

'use client';
import Link from 'next/link';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import api from '@/services/api';
import { buildRepoPath } from '@/utils/repositoryRouting';
import { forgeBadgeText } from '@/lib/forge';
const LANGUAGE_COLORS: Record<string, string> = {
JavaScript: '#f1e05a',
TypeScript: '#3178c6',
Python: '#3572A5',
Go: '#00ADD8',
Rust: '#dea584',
};
const MAX_VISIBLE_TOPICS = 3;
interface RepositorySocialCardProps {
id: string;
owner: string;
name: string;
fullName: string;
description?: string;
stars: number;
forks: number;
language?: string;
topics?: string[];
isFollowed?: boolean;
followersCount?: number;
/** Which forge this repo lives on — 'github' (default) or 'gitea' (Rithub). */
forge?: string;
}
interface FollowState {
isFollowed: boolean;
followersCount: number;
}
function formatCount(n: number): string {
if (n >= 1000) {
return `${(n / 1000).toFixed(1).replace(/\.0$/, '')}k`;
}
return String(n);
}
export default function RepositorySocialCard({
id,
owner,
name,
fullName,
description,
stars,
forks,
language,
topics = [],
isFollowed = false,
followersCount = 0,
forge,
}: RepositorySocialCardProps) {
const queryClient = useQueryClient();
const queryKey = ['repo-follow', id];
const badgeText = forgeBadgeText(forge);
const currentState: FollowState = queryClient.getQueryData<FollowState>(queryKey) ?? {
isFollowed,
followersCount,
};
const { mutate, isPending } = useMutation({
mutationFn: async () => {
if (currentState.isFollowed) {
await api.delete(`/v1/repositories/${id}/follow`);
} else {
await api.post(`/v1/repositories/${id}/follow`);
}
},
onMutate: async () => {
await queryClient.cancelQueries({ queryKey });
const prev = queryClient.getQueryData<FollowState>(queryKey) ?? { isFollowed, followersCount };
const next: FollowState = {
isFollowed: !prev.isFollowed,
followersCount: prev.isFollowed ? prev.followersCount - 1 : prev.followersCount + 1,
};
queryClient.setQueryData<FollowState>(queryKey, next);
return { prev };
},
onError: (_err, _vars, ctx) => {
if (ctx?.prev) {
queryClient.setQueryData<FollowState>(queryKey, ctx.prev);
}
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey });
},
});
const state = queryClient.getQueryData<FollowState>(queryKey) ?? { isFollowed, followersCount };
const languageColor = language ? (LANGUAGE_COLORS[language] ?? '#6b7280') : null;
const visibleTopics = topics.slice(0, MAX_VISIBLE_TOPICS);
const extraTopics = topics.length - MAX_VISIBLE_TOPICS;
return (
<article className="bg-white dark:bg-gray-800 rounded-xl p-4 border border-gray-200 dark:border-gray-700 hover:border-primary dark:hover:border-primary transition-colors duration-200">
<div className="flex items-start justify-between gap-3">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<Link
href={buildRepoPath(owner, name)}
className="font-semibold text-gray-900 dark:text-gray-100 hover:text-primary dark:hover:text-primary hover:underline truncate block"
title={fullName}
>
{fullName}
</Link>
{badgeText && (
<span
className="shrink-0 px-2 py-0.5 rounded-full text-[11px] font-medium bg-emerald-50 dark:bg-emerald-900/30 text-emerald-700 dark:text-emerald-300 border border-emerald-200 dark:border-emerald-800"
title="Hosted on Rithub (rithub.dev)"
>
{badgeText}
</span>
)}
</div>
{description && (
<p className="mt-1 text-sm text-gray-500 dark:text-gray-400 line-clamp-2">
{description}
</p>
)}
</div>
<button
onClick={() => mutate()}
disabled={isPending}
aria-pressed={state.isFollowed}
aria-label={state.isFollowed ? `Unfollow ${fullName}` : `Follow ${fullName}`}
className={`shrink-0 px-4 py-1.5 rounded-full text-sm font-medium border transition-colors duration-150 disabled:opacity-50 disabled:cursor-not-allowed ${
state.isFollowed
? 'border-gray-300 dark:border-gray-600 bg-transparent text-gray-700 dark:text-gray-300 hover:border-red-400 hover:text-red-500 dark:hover:border-red-400 dark:hover:text-red-400'
: 'border-transparent bg-primary text-white hover:bg-primary dark:bg-primary dark:hover:bg-primary'
}`}
>
{state.isFollowed ? 'Following' : 'Follow'}
</button>
</div>
<div className="mt-3 flex flex-wrap items-center gap-x-4 gap-y-1.5 text-sm text-gray-500 dark:text-gray-400">
{languageColor && language && (
<span className="flex items-center gap-1.5">
<span
className="inline-block w-3 h-3 rounded-full shrink-0"
style={{ backgroundColor: languageColor }}
aria-hidden="true"
/>
<span>{language}</span>
</span>
)}
<span className="flex items-center gap-1" title={`${stars} stars`}>
<svg
viewBox="0 0 16 16"
className="w-4 h-4"
fill="currentColor"
aria-hidden="true"
>
<path d="M8 .25a.75.75 0 0 1 .673.418l1.882 3.815 4.21.612a.75.75 0 0 1 .416 1.279l-3.046 2.97.719 4.192a.751.751 0 0 1-1.088.791L8 12.347l-3.766 1.98a.75.75 0 0 1-1.088-.79l.72-4.194L.818 6.374a.75.75 0 0 1 .416-1.28l4.21-.611L7.327.668A.75.75 0 0 1 8 .25Z" />
</svg>
<span>{formatCount(stars)}</span>
</span>
<span className="flex items-center gap-1" title={`${forks} forks`}>
<svg
viewBox="0 0 16 16"
className="w-4 h-4"
fill="currentColor"
aria-hidden="true"
>
<path d="M5 5.372v.878c0 .414.336.75.75.75h4.5a.75.75 0 0 0 .75-.75v-.878a2.25 2.25 0 1 1 1.5 0v.878a2.25 2.25 0 0 1-2.25 2.25h-1.5v2.128a2.251 2.251 0 1 1-1.5 0V8.5h-1.5A2.25 2.25 0 0 1 3.5 6.25v-.878a2.25 2.25 0 1 1 1.5 0ZM5 3.25a.75.75 0 1 0-1.5 0 .75.75 0 0 0 1.5 0Zm6.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5Zm-3 8.75a.75.75 0 1 0-1.5 0 .75.75 0 0 0 1.5 0Z" />
</svg>
<span>{formatCount(forks)}</span>
</span>
{state.followersCount > 0 && (
<span className="flex items-center gap-1" title={`${state.followersCount} followers`}>
<svg
viewBox="0 0 16 16"
className="w-4 h-4"
fill="currentColor"
aria-hidden="true"
>
<path d="M2 5.5a3.5 3.5 0 1 1 5.898 2.549 5.508 5.508 0 0 1 3.034 4.084.75.75 0 1 1-1.482.235 4 4 0 0 0-7.9 0 .75.75 0 0 1-1.482-.236A5.507 5.507 0 0 1 3.102 8.05 3.493 3.493 0 0 1 2 5.5ZM11 4a3.001 3.001 0 0 1 2.22 5.018 5.01 5.01 0 0 1 2.56 3.012.749.749 0 0 1-.885.954.752.752 0 0 1-.549-.514 3.507 3.507 0 0 0-2.522-2.372.75.75 0 0 1-.574-.73v-.352a.75.75 0 0 1 .416-.672A1.5 1.5 0 0 0 11 5.5.75.75 0 0 1 11 4Zm-5.5-.5a2 2 0 1 0 0 4 2 2 0 0 0 0-4Z" />
</svg>
<span>{formatCount(state.followersCount)}</span>
</span>
)}
</div>
{visibleTopics.length > 0 && (
<div className="mt-2.5 flex flex-wrap gap-1.5">
{visibleTopics.map((topic) => (
<span
key={topic}
className="px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-50 dark:bg-blue-900/30 text-blue-700 dark:text-blue-300 border border-blue-200 dark:border-blue-800"
>
{topic}
</span>
))}
{extraTopics > 0 && (
<span className="px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 dark:bg-gray-700 text-gray-500 dark:text-gray-400">
+{extraTopics} more
</span>
)}
</div>
)}
</article>
);
}