import React, { useEffect, useState, useCallback } from 'react'; import CategoryTabs from '../components/schemes/CategoryTabs'; import SchemeList from '../components/schemes/SchemeList'; import type { SchemeData } from '../components/schemes/SchemeCard'; import { getSchemes, getCategory, useScheme } from '../services/schemes.api'; import { addFavorite, removeFavorite, checkFavorite } from '../services/favorites.api'; import { useAuthStore } from '../stores/authStore'; import { useNavigate } from 'react-router-dom'; import { useToast } from '../components/ui/Toast'; const DEFAULT_CATEGORIES = [ { code: 'AR', name: '突击步枪' }, { code: 'SMG', name: '冲锋枪' }, { code: 'SR', name: '狙击枪' }, { code: 'LMG', name: '轻机枪' }, { code: 'SG', name: '霰弹枪' }, { code: 'Pistol', name: '手枪' }, { code: 'Launcher', name: '发射器' }, ]; export default function WeaponSchemes() { const navigate = useNavigate(); const toast = useToast(); const isLoggedIn = useAuthStore(s => s.isLoggedIn); const [categories, setCategories] = useState>(DEFAULT_CATEGORIES); const [activeCode, setActiveCode] = useState('AR'); const [schemes, setSchemes] = useState([]); const [loading, setLoading] = useState(false); const [page, setPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [favoritedIds, setFavoritedIds] = useState>(new Set()); const [sort, setSort] = useState<'hot' | 'new'>('hot'); useEffect(() => { getCategory('AR').then(r => { if (r.success && Array.isArray(r.data) && r.data.length > 0) { setCategories(r.data); } }).catch(() => {}); }, []); const loadSchemes = useCallback(async (code: string, p: number, s: string) => { setLoading(true); try { const res = await getSchemes({ sort: s, page: p, limit: 12, source: code, weaponCategory: code }); if (res.success && Array.isArray(res.data)) { setSchemes(res.data || []); setTotalPages(Math.ceil((res as any).total || res.data.length / 12)); } else { setSchemes([]); } } catch { setSchemes([]); } finally { setLoading(false); } }, []); useEffect(() => { loadSchemes(activeCode, page, sort); }, [activeCode, page, sort, loadSchemes]); const handleCategoryChange = (code: string) => { setActiveCode(code); setPage(1); }; const handleFavorite = async (id: string) => { if (!isLoggedIn) { toast('warning', '请先登录'); navigate('/login'); return; } try { if (favoritedIds.has(id)) { await removeFavorite(id); setFavoritedIds(prev => { const n = new Set(prev); n.delete(id); return n; }); toast('success', '已取消收藏'); } else { await addFavorite({ targetId: id, targetType: 'Scheme' }); setFavoritedIds(prev => new Set(prev).add(id)); toast('success', '已收藏'); } } catch { toast('error', '操作失败'); } }; const handleUse = async (id: string) => { if (!isLoggedIn) { toast('warning', '请先登录'); navigate('/login'); return; } try { await useScheme(id, activeCode); toast('success', '方案使用记录已保存'); } catch { toast('error', '操作失败'); } }; return (
{/* 标题 */}

改枪方案

{(['hot', 'new'] as const).map(s => ( ))}
{/* 分类标签 */} {/* 方案列表 */} navigate(`/scheme-detail/${id}`)} />
); }