From 976bc7ad462bf8d53aec63702de40dc6af6c60cf Mon Sep 17 00:00:00 2001 From: mtvpls Date: Fri, 13 Mar 2026 10:46:32 +0800 Subject: [PATCH] =?UTF-8?q?pansou=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PansouSearch.tsx | 88 ++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/src/components/PansouSearch.tsx b/src/components/PansouSearch.tsx index 47c7d5a..0d78a75 100644 --- a/src/components/PansouSearch.tsx +++ b/src/components/PansouSearch.tsx @@ -1,8 +1,8 @@ /* eslint-disable @typescript-eslint/no-explicit-any,no-console */ 'use client'; -import { AlertCircle, Copy, ExternalLink, Loader2 } from 'lucide-react'; -import { useEffect, useState } from 'react'; +import { AlertCircle, Copy, ExternalLink, Loader2, RefreshCw } from 'lucide-react'; +import { useEffect, useState, useCallback } from 'react'; import { PansouLink, PansouSearchResult } from '@/lib/pansou.client'; @@ -57,51 +57,52 @@ export default function PansouSearch({ const [copiedUrl, setCopiedUrl] = useState(null); const [selectedType, setSelectedType] = useState('all'); // 'all' 表示显示全部 + // 提取搜索函数,以便在重试时调用 + const searchPansou = useCallback(async () => { + const currentKeyword = keyword.trim(); + if (!currentKeyword) { + return; + } + + setLoading(true); + setError(null); + setResults(null); + + try { + const response = await fetch('/api/pansou/search', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + keyword: currentKeyword, + }), + }); + + if (!response.ok) { + const errorData = await response.json(); + throw new Error(errorData.error || '搜索失败'); + } + + const data: PansouSearchResult = await response.json(); + setResults(data); + } catch (err: any) { + const errorMsg = err.message || '搜索失败,请检查配置'; + setError(errorMsg); + onError?.(errorMsg); + } finally { + setLoading(false); + } + }, [keyword, onError]); + useEffect(() => { // triggerSearch 变化时触发搜索(无论是 true 还是 false) if (triggerSearch === undefined) { return; } - const currentKeyword = keyword.trim(); - if (!currentKeyword) { - return; - } - - const searchPansou = async () => { - setLoading(true); - setError(null); - setResults(null); - - try { - const response = await fetch('/api/pansou/search', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - keyword: currentKeyword, - }), - }); - - if (!response.ok) { - const errorData = await response.json(); - throw new Error(errorData.error || '搜索失败'); - } - - const data: PansouSearchResult = await response.json(); - setResults(data); - } catch (err: any) { - const errorMsg = err.message || '搜索失败,请检查配置'; - setError(errorMsg); - onError?.(errorMsg); - } finally { - setLoading(false); - } - }; - searchPansou(); - }, [triggerSearch, onError]); // 移除 keyword 依赖,只依赖 triggerSearch + }, [triggerSearch, searchPansou]); // 依赖 triggerSearch 和 searchPansou const handleCopy = async (text: string, url: string) => { try { @@ -136,6 +137,13 @@ export default function PansouSearch({

{error}

+
);