import { useState, useCallback } from 'react'; import { Card } from '@/components/ui/Card'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { Textarea } from '@/components/ui/Textarea'; import { JsonViewer } from '@/components/ui/JsonViewer'; import { useToast } from '@/context/ToastContext'; import { toggleLike, toggleFavorite, postComment, replyComment } from '@/api/endpoints'; interface LogEntry { id: number; action: string; time: string; result: unknown; } let logId = 0; export function InteractionsPage() { const { toast } = useToast(); const [feedId, setFeedId] = useState(''); const [xsecToken, setXsecToken] = useState(''); const [loading, setLoading] = useState(null); const [log, setLog] = useState([]); const addLog = (action: string, result: unknown) => { setLog((prev) => [{ id: logId++, action, time: new Date().toLocaleTimeString(), result }, ...prev].slice(0, 50)); }; // Comment state const [commentText, setCommentText] = useState(''); // Reply state const [replyText, setReplyText] = useState(''); const [replyCommentId, setReplyCommentId] = useState(''); const [replyUserId, setReplyUserId] = useState(''); const checkIds = () => { if (!feedId.trim() || !xsecToken.trim()) { toast('warning', 'Feed ID 和 xsec_token 为必填项'); return false; } return true; }; const handleLike = useCallback(async (unlike: boolean) => { if (!checkIds()) return; setLoading(unlike ? 'unlike' : 'like'); try { const res = await toggleLike(feedId, xsecToken, unlike); addLog(unlike ? '取消点赞' : '点赞', res); toast('success', unlike ? '已取消点赞' : '已点赞'); } catch (err) { const msg = err instanceof Error ? err.message : '操作失败'; addLog(unlike ? '取消点赞' : '点赞', { error: msg }); toast('error', msg); } finally { setLoading(null); } }, [feedId, xsecToken, toast]); const handleFavorite = useCallback(async (unfavorite: boolean) => { if (!checkIds()) return; setLoading(unfavorite ? 'unfavorite' : 'favorite'); try { const res = await toggleFavorite(feedId, xsecToken, unfavorite); addLog(unfavorite ? '取消收藏' : '收藏', res); toast('success', unfavorite ? '已取消收藏' : '已收藏'); } catch (err) { const msg = err instanceof Error ? err.message : '操作失败'; addLog(unfavorite ? '取消收藏' : '收藏', { error: msg }); toast('error', msg); } finally { setLoading(null); } }, [feedId, xsecToken, toast]); const handleComment = useCallback(async () => { if (!checkIds() || !commentText.trim()) { toast('warning', '评论内容为必填项'); return; } setLoading('comment'); try { const res = await postComment(feedId, xsecToken, commentText); addLog('评论', res); toast('success', '评论已发布'); setCommentText(''); } catch (err) { const msg = err instanceof Error ? err.message : '操作失败'; addLog('评论', { error: msg }); toast('error', msg); } finally { setLoading(null); } }, [feedId, xsecToken, commentText, toast]); const handleReply = useCallback(async () => { if (!checkIds() || !replyText.trim()) { toast('warning', '回复内容为必填项'); return; } setLoading('reply'); try { const res = await replyComment({ feed_id: feedId, xsec_token: xsecToken, content: replyText, comment_id: replyCommentId || undefined, user_id: replyUserId || undefined, }); addLog('回复', res); toast('success', '回复已发布'); setReplyText(''); } catch (err) { const msg = err instanceof Error ? err.message : '操作失败'; addLog('回复', { error: msg }); toast('error', msg); } finally { setLoading(null); } }, [feedId, xsecToken, replyText, replyCommentId, replyUserId, toast]); return (

互动

{/* Target */}

目标笔记

setFeedId(e.target.value)} placeholder="Feed ID" /> setXsecToken(e.target.value)} placeholder="xsec_token" />
{/* Like / Favorite */}

快捷操作

{/* Comment */}

发表评论