railyard/apps/ritgram/web/components/composer/CodeSnippetEditor.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

214 lines
7.4 KiB
TypeScript

'use client'
import { useState, useRef, useEffect } from 'react'
interface CodeSnippet {
code: string
language: string
title?: string
}
interface CodeSnippetEditorProps {
initialSnippet?: CodeSnippet | null
onSave: (snippet: CodeSnippet) => void
onClose: () => void
}
const LANGUAGES = [
{ value: 'javascript', label: 'JavaScript' },
{ value: 'typescript', label: 'TypeScript' },
{ value: 'python', label: 'Python' },
{ value: 'go', label: 'Go' },
{ value: 'rust', label: 'Rust' },
{ value: 'java', label: 'Java' },
{ value: 'c', label: 'C' },
{ value: 'cpp', label: 'C++' },
{ value: 'csharp', label: 'C#' },
{ value: 'ruby', label: 'Ruby' },
{ value: 'php', label: 'PHP' },
{ value: 'swift', label: 'Swift' },
{ value: 'kotlin', label: 'Kotlin' },
{ value: 'bash', label: 'Bash / Shell' },
{ value: 'sql', label: 'SQL' },
{ value: 'html', label: 'HTML' },
{ value: 'css', label: 'CSS' },
{ value: 'scss', label: 'SCSS' },
{ value: 'json', label: 'JSON' },
{ value: 'yaml', label: 'YAML' },
{ value: 'markdown', label: 'Markdown' },
{ value: 'jsx', label: 'JSX' },
{ value: 'tsx', label: 'TSX' },
{ value: 'text', label: 'Plain Text' },
]
export default function CodeSnippetEditor({
initialSnippet,
onSave,
onClose,
}: CodeSnippetEditorProps) {
const [code, setCode] = useState(initialSnippet?.code || '')
const [language, setLanguage] = useState(initialSnippet?.language || 'javascript')
const [title, setTitle] = useState(initialSnippet?.title || '')
const [error, setError] = useState('')
const textareaRef = useRef<HTMLTextAreaElement>(null)
useEffect(() => {
// Focus textarea on mount
if (textareaRef.current) {
textareaRef.current.focus()
}
}, [])
// Handle tab key in textarea for indentation
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Tab') {
e.preventDefault()
const textarea = e.currentTarget
const start = textarea.selectionStart
const end = textarea.selectionEnd
const newValue = code.substring(0, start) + ' ' + code.substring(end)
setCode(newValue)
// Set cursor position after the tab
setTimeout(() => {
textarea.selectionStart = textarea.selectionEnd = start + 2
}, 0)
}
// Cmd/Ctrl+Enter to save
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
e.preventDefault()
handleSave()
}
// Escape to close
if (e.key === 'Escape') {
onClose()
}
}
const handleSave = () => {
if (!code.trim()) {
setError('Code cannot be empty')
return
}
onSave({
code: code.trim(),
language,
title: title.trim() || undefined,
})
}
const lineCount = code.split('\n').length
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/50 backdrop-blur-sm">
<div className="w-full max-w-3xl bg-surface border border-border rounded-2xl shadow-xl overflow-hidden max-h-[90vh] flex flex-col">
{/* Header */}
<div className="flex items-center justify-between px-5 py-4 border-b border-border">
<div className="flex items-center gap-3">
<svg className="w-5 h-5 text-primary" 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>
<h3 className="text-lg font-semibold text-foreground">Code Snippet</h3>
</div>
<button
onClick={onClose}
className="p-1.5 text-foreground-secondary hover:text-foreground hover:bg-surface-secondary rounded-lg transition-colors"
>
<svg className="w-5 h-5" 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>
{/* Controls */}
<div className="px-5 py-3 border-b border-border flex gap-3 flex-wrap">
<div className="flex-1 min-w-[200px]">
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Snippet title (optional)"
className="w-full px-3 py-2 text-sm bg-background border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-primary text-foreground placeholder:text-foreground-secondary"
/>
</div>
<div className="w-48">
<select
value={language}
onChange={(e) => setLanguage(e.target.value)}
className="w-full px-3 py-2 text-sm bg-background border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-primary text-foreground appearance-none cursor-pointer"
>
{LANGUAGES.map((lang) => (
<option key={lang.value} value={lang.value}>
{lang.label}
</option>
))}
</select>
</div>
</div>
{/* Editor */}
<div className="flex-1 overflow-hidden relative bg-[#000000]">
<div className="flex h-full overflow-auto" style={{ maxHeight: '400px' }}>
{/* Line numbers */}
<div className="flex-shrink-0 py-4 pl-4 pr-2 text-right select-none border-r border-gray-800">
{Array.from({ length: lineCount }, (_, i) => (
<div key={i} className="text-gray-500 text-sm font-mono leading-6">
{i + 1}
</div>
))}
</div>
{/* Code textarea */}
<textarea
ref={textareaRef}
value={code}
onChange={(e) => {
setCode(e.target.value)
setError('')
}}
onKeyDown={handleKeyDown}
placeholder="Paste or write your code here..."
className="flex-1 py-4 px-3 bg-transparent text-gray-200 font-mono text-sm leading-6 resize-none focus:outline-none min-h-[200px] placeholder:text-gray-600"
spellCheck={false}
/>
</div>
</div>
{/* Footer */}
<div className="px-5 py-3 border-t border-border flex items-center justify-between">
<div className="flex items-center gap-4">
<span className="text-xs text-foreground-secondary">
{lineCount} lines | {language}
</span>
{error && (
<span className="text-xs text-error">{error}</span>
)}
</div>
<div className="flex items-center gap-2">
<span className="text-xs text-foreground-secondary mr-2">
Cmd+Enter to save
</span>
<button
onClick={onClose}
className="px-4 py-2 text-sm text-foreground-secondary hover:text-foreground border border-border rounded-lg hover:bg-surface-secondary transition-colors"
>
Cancel
</button>
<button
onClick={handleSave}
disabled={!code.trim()}
className="px-4 py-2 text-sm bg-primary text-white rounded-lg hover:bg-primary-hover disabled:opacity-50 disabled:cursor-not-allowed transition-colors font-medium"
>
{initialSnippet ? 'Update Snippet' : 'Add Snippet'}
</button>
</div>
</div>
</div>
</div>
)
}