import { useState } from 'react'; interface MicroComposerProps { onClose?: () => void; onSuccess?: () => void; apiBaseUrl?: string; } const API_BASE = typeof window !== 'undefined' ? (import.meta.env.VITE_API_BASE || 'http://localhost:8080/api') : 'http://localhost:8080/api'; export default function MicroComposer({ onClose, onSuccess, apiBaseUrl }: MicroComposerProps) { const baseUrl = apiBaseUrl || API_BASE; const [content, setContent] = useState(''); const [tags, setTags] = useState(''); const [isPublic, setIsPublic] = useState(true); const [submitting, setSubmitting] = useState(false); const handleSubmit = async () => { if (!content.trim()) { alert('请输入内容'); return; } const token = localStorage.getItem('token'); if (!token) { alert('请登录后再发布'); return; } setSubmitting(true); try { const tagList = tags .split(/[,,\s]+/) .map(t => t.trim()) .filter(t => t.length > 0); const response = await fetch(`${baseUrl}/micros`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, body: JSON.stringify({ content: content.trim(), tags: tagList, is_public: isPublic, images: [], }), }); if (response.ok) { setContent(''); setTags(''); setIsPublic(true); onSuccess?.(); onClose?.(); } else { const error = await response.json(); alert(error.error || '发布失败'); } } catch (error) { console.error('Failed to post:', error); alert('发布失败,请重试'); } finally { setSubmitting(false); } }; const remainingChars = 2000 - content.length; return (