'use client' import {FormEvent, useState} from 'react' import {ArrowUpRight, Check, Copy, FileCode2, Loader2, Send, Terminal, X} from 'lucide-react' import {languageLabel, rawSnippetUrl, SnippetEditor, snippetUrl} from '@/components/snippet-editor' const API_BASE = 'http://127.0.0.1:6868' async function copyToClipboard(text: string) { if (navigator.clipboard && window.isSecureContext) { try { await navigator.clipboard.writeText(text); return true; } catch (err) { } } try { const textArea = document.createElement('textarea'); textArea.value = text; textArea.style.position = 'fixed'; textArea.style.left = '-999999px'; textArea.style.top = '-999999px'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); const successful = document.execCommand('copy'); document.body.removeChild(textArea); if (!successful) throw new Error('execCommand copy failed'); return true; } catch (err) { return false; } } export default function Page() { const [content, setContent] = useState('') const [filename, setFilename] = useState('untitled.txt') const [snippetId, setSnippetId] = useState('') const [status, setStatus] = useState<'idle' | 'loading' | 'error'>('idle') const [error, setError] = useState('') const [copied, setCopied] = useState(false) async function submit(event: FormEvent) { event.preventDefault() if (!content.trim()) { setError('Add some content before publishing.'); setStatus('error'); return } setStatus('loading'); setError(''); setSnippetId('') try { const response = await fetch(`${API_BASE}/api/v1/snippets`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({content, filename}) }) if (!response.ok) throw new Error(`Request failed with ${response.status}`) const data = await response.json() if (!data.snippet_id) throw new Error('The server did not return a snippet_id.') setSnippetId(data.snippet_id); setStatus('idle') } catch (caught) { setStatus('error'); setError(caught instanceof Error ? caught.message : 'Could not reach the snippet server.') } } async function copyLink() { if (!snippetId) return await copyToClipboard(snippetUrl(snippetId)); setCopied(true); window.setTimeout(() => setCopied(false), 1600) } function reset() { setContent(''); setFilename('untitled.txt'); setSnippetId(''); setError(''); setStatus('idle') } return (
KSnippets
New snippet
{languageLabel(filename)}
{status === 'error' &&
{error}
} {snippetId &&

published successfully

/{snippetId}
Raw
}
) }