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

289 lines
12 KiB
TypeScript

'use client'
import { useEffect, useRef, useState } from 'react'
// ─── Types ────────────────────────────────────────────────────────────────────
type EmbedType = 'post' | 'snippet' | 'user' | 'repo'
type EmbedTheme = 'dark' | 'light'
type EmbedSize = 'normal' | 'compact'
interface EmbedModalProps {
isOpen: boolean
onClose: () => void
/** id / username / owner+name depending on embedType */
id: string
/** Additional identifier — only used for repo embeds (the repo name) */
name?: string
/** Defaults to 'post' for backwards compatibility */
embedType?: EmbedType
}
const EMBED_WIDTH = 560
const BASE_URL = 'https://ritgram.rithub.dev'
const EMBED_HEIGHT_MAP: Record<EmbedType, Record<EmbedSize, number>> = {
post: { normal: 300, compact: 220 },
snippet: { normal: 380, compact: 260 },
user: { normal: 300, compact: 300 },
repo: { normal: 280, compact: 280 },
}
const TYPE_META: Record<EmbedType, { label: string; icon: React.ReactNode }> = {
post: {
label: 'Post',
icon: (
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
),
},
snippet: {
label: 'Snippet',
icon: (
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4" />
</svg>
),
},
user: {
label: 'Profile',
icon: (
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
</svg>
),
},
repo: {
label: 'Repository',
icon: (
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 7a2 2 0 012-2h14a2 2 0 012 2v10a2 2 0 01-2 2H5a2 2 0 01-2-2V7z" />
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 7l9 6 9-6" />
</svg>
),
},
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
function buildEmbedSrc(type: EmbedType, id: string, name: string | undefined, theme: EmbedTheme, size: EmbedSize): string {
const params = new URLSearchParams()
if (theme !== 'dark') params.set('theme', theme)
if (size !== 'normal') params.set('size', size)
const qs = params.toString() ? `?${params.toString()}` : ''
switch (type) {
case 'post': return `${BASE_URL}/embed/posts/${id}${qs}`
case 'snippet': return `${BASE_URL}/embed/snippets/${id}${qs}`
case 'user': return `${BASE_URL}/embed/users/${id}${qs}`
case 'repo': return `${BASE_URL}/embed/repos/${id}/${name ?? ''}${qs}`
}
}
function buildEmbedCode(src: string, width: number, height: number): string {
return `<iframe\n src="${src}"\n width="${width}"\n height="${height}"\n frameborder="0"\n scrolling="no"\n allowtransparency="true"\n style="border:none;border-radius:12px;overflow:hidden;"\n></iframe>`
}
// ─── Component ────────────────────────────────────────────────────────────────
export function EmbedModal({ isOpen, onClose, id, name, embedType = 'post' }: EmbedModalProps) {
const [theme, setTheme] = useState<EmbedTheme>('dark')
const [size, setSize] = useState<EmbedSize>('normal')
const [copied, setCopied] = useState(false)
const backdropRef = useRef<HTMLDivElement>(null)
const height = EMBED_HEIGHT_MAP[embedType][size]
const embedSrc = buildEmbedSrc(embedType, id, name, theme, size)
const embedCode = buildEmbedCode(embedSrc, EMBED_WIDTH, height)
// Reset options when modal reopens
useEffect(() => {
if (isOpen) {
setTheme('dark')
setSize('normal')
setCopied(false)
}
}, [isOpen])
// Close on Escape
useEffect(() => {
if (!isOpen) return
const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() }
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [isOpen, onClose])
// Prevent body scroll
useEffect(() => {
document.body.style.overflow = isOpen ? 'hidden' : ''
return () => { document.body.style.overflow = '' }
}, [isOpen])
const handleCopyEmbed = async () => {
try {
await navigator.clipboard.writeText(embedCode)
} catch {
const textarea = document.createElement('textarea')
textarea.value = embedCode
document.body.appendChild(textarea)
textarea.select()
document.execCommand('copy')
document.body.removeChild(textarea)
}
setCopied(true)
setTimeout(() => setCopied(false), 2500)
}
const handleBackdropClick = (e: React.MouseEvent<HTMLDivElement>) => {
if (e.target === backdropRef.current) onClose()
}
if (!isOpen) return null
const meta = TYPE_META[embedType]
const showSizeOption = embedType === 'snippet'
return (
<div
ref={backdropRef}
className="fixed inset-0 z-50 flex items-center justify-center p-4"
role="dialog"
aria-modal="true"
aria-labelledby="embed-modal-title"
onClick={handleBackdropClick}
>
{/* Backdrop */}
<div className="absolute inset-0 bg-black/70 backdrop-blur-sm" aria-hidden="true" />
{/* Panel */}
<div className="relative w-full max-w-lg bg-[var(--surface)] border border-[var(--border)] rounded-xl shadow-2xl overflow-hidden">
{/* Header */}
<div className="flex items-center justify-between px-5 py-4 border-b border-[var(--border)]">
<div className="flex items-center gap-2.5">
<span className="text-[var(--primary)]">{meta.icon}</span>
<h2 id="embed-modal-title" className="text-sm font-semibold text-[var(--foreground)]">
Embed this {meta.label.toLowerCase()}
</h2>
</div>
<button
onClick={onClose}
aria-label="Close embed modal"
className="p-1 rounded-lg text-[var(--foreground-secondary)] hover:text-[var(--foreground)] hover:bg-[var(--surface-secondary)] transition-colors"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
{/* Options row: theme + size */}
<div className="flex items-center gap-4 px-5 pt-4 pb-1">
{/* Theme toggle */}
<div className="flex items-center gap-2">
<span className="text-xs text-[var(--foreground-secondary)]">Theme</span>
<div className="flex rounded-lg border border-[var(--border)] overflow-hidden">
{(['dark', 'light'] as const).map((t) => (
<button
key={t}
onClick={() => setTheme(t)}
className={[
'px-3 py-1 text-xs font-medium transition-colors',
theme === t
? 'bg-[var(--primary)] text-white'
: 'text-[var(--foreground-secondary)] hover:text-[var(--foreground)] hover:bg-[var(--surface-secondary)]',
].join(' ')}
>
{t.charAt(0).toUpperCase() + t.slice(1)}
</button>
))}
</div>
</div>
{/* Size toggle — only for snippet */}
{showSizeOption && (
<div className="flex items-center gap-2">
<span className="text-xs text-[var(--foreground-secondary)]">Size</span>
<div className="flex rounded-lg border border-[var(--border)] overflow-hidden">
{(['normal', 'compact'] as const).map((s) => (
<button
key={s}
onClick={() => setSize(s)}
className={[
'px-3 py-1 text-xs font-medium transition-colors',
size === s
? 'bg-[var(--primary)] text-white'
: 'text-[var(--foreground-secondary)] hover:text-[var(--foreground)] hover:bg-[var(--surface-secondary)]',
].join(' ')}
>
{s.charAt(0).toUpperCase() + s.slice(1)}
</button>
))}
</div>
</div>
)}
</div>
{/* Embed code */}
<div className="px-5 pt-3 pb-3">
<label className="block text-xs font-medium text-[var(--foreground-secondary)] mb-2">
Embed code
</label>
<div className="relative">
<pre className="rounded-lg bg-[#000000] border border-[var(--border)] px-4 py-3 text-xs text-[#f5f5f7] font-mono overflow-x-auto whitespace-pre leading-relaxed">
{embedCode}
</pre>
<button
onClick={handleCopyEmbed}
className={[
'absolute top-2 right-2 flex items-center gap-1.5 px-2.5 py-1 rounded text-xs font-medium transition-all',
copied
? 'bg-[var(--accent)]/20 text-[var(--accent)] border border-[var(--accent)]/30'
: 'bg-[var(--surface-secondary)] text-[var(--foreground-secondary)] hover:text-[var(--foreground)] border border-[var(--border)]',
].join(' ')}
>
{copied ? (
<>
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2.5} d="M5 13l4 4L19 7" />
</svg>
Copied!
</>
) : (
<>
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
</svg>
Copy
</>
)}
</button>
</div>
</div>
{/* Preview */}
<div className="px-5 pb-4">
<label className="block text-xs font-medium text-[var(--foreground-secondary)] mb-2">
Preview
</label>
<div className="rounded-lg border border-[var(--border)] overflow-hidden bg-[var(--background)]">
<iframe
src={embedSrc}
width="100%"
height={height}
style={{ border: 'none', display: 'block' }}
title={`Embedded ${meta.label.toLowerCase()} preview`}
loading="lazy"
sandbox="allow-scripts allow-same-origin"
/>
</div>
<p className="mt-2 text-xs text-[var(--foreground-secondary)]">
Embed dimensions: {EMBED_WIDTH}&times;{height}px. The widget adapts to the container width.
</p>
</div>
</div>
</div>
)
}