'use client' import { useState, useCallback, useRef, useEffect } from 'react' import { AnimatePresence, motion } from 'framer-motion' import { cn } from '@/lib/utils' import { useToast } from '@/components/ui/toast' import api from '@/services/api' interface RepoToTextWidgetProps { repoFullName?: string compact?: boolean } interface ConvertResponse { success: boolean data?: { text: string } error?: string } function parseRepoFullName(input: string): string { const trimmed = input.trim() // Accept full GitHub URL: https://github.com/owner/repo const urlMatch = trimmed.match(/github\.com\/([^/]+\/[^/?\s]+)/) if (urlMatch) return urlMatch[1] // Accept owner/repo directly if (/^[^/]+\/[^/]+$/.test(trimmed)) return trimmed return trimmed } function getErrorMessage(error: unknown): string { if (error instanceof Error) return error.message return 'Unexpected error' } // --------------------------------------------------------------------------- // Core widget body — shared between full and modal modes // --------------------------------------------------------------------------- interface WidgetBodyProps { initialRepo: string onClose?: () => void } function WidgetBody({ initialRepo, onClose }: WidgetBodyProps) { const { showToast } = useToast() const [repoInput, setRepoInput] = useState(initialRepo) const [result, setResult] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [copied, setCopied] = useState(false) const textareaRef = useRef(null) const handleConvert = useCallback(async () => { const repoFullName = parseRepoFullName(repoInput) if (!repoFullName) { setError('Enter a GitHub repo URL or owner/repo.') return } setLoading(true) setError('') setResult('') try { const response = await api.post('/tools/repo-to-text', { repoFullName, }) if (!response.data.success || !response.data.data?.text) { throw new Error(response.data.error ?? 'No text returned from server.') } setResult(response.data.data.text) } catch (err: unknown) { const msg = getErrorMessage(err) setError(msg) } finally { setLoading(false) } }, [repoInput]) const handleCopy = useCallback(async () => { if (!result) return try { await navigator.clipboard.writeText(result) setCopied(true) showToast('Copied to clipboard', 'success') setTimeout(() => setCopied(false), 2000) } catch { showToast('Failed to copy', 'error') } }, [result, showToast]) const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === 'Enter') handleConvert() }, [handleConvert], ) return (
{/* Header row */}

Repo to Text

{onClose && ( )}
{/* URL input + button */}
setRepoInput(e.target.value)} onKeyDown={handleKeyDown} placeholder="github.com/owner/repo" aria-label="GitHub repository URL or owner/repo" className={cn( 'min-w-0 flex-1 rounded-xl border border-border bg-background px-4 py-2.5 text-sm text-foreground', 'placeholder:text-foreground-secondary', 'focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/20', 'transition-all duration-200', error && !result && 'border-error focus:border-error focus:ring-error/20', )} />
{/* Error state */} {error && ( {error} )} {/* Result area */} {result && (
{result.length.toLocaleString()} characters