feat: 添加关键词词云客户端数据获取 Hook
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
interface KeywordData {
|
||||
id: number;
|
||||
word: string;
|
||||
trendScore: number;
|
||||
description: string;
|
||||
detailPoints: string[];
|
||||
visualConfig: {
|
||||
color: string;
|
||||
size: string;
|
||||
border: string;
|
||||
rotation?: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface QuarterData {
|
||||
quarter: string;
|
||||
title: string;
|
||||
titleEn?: string;
|
||||
subtitle?: string;
|
||||
subtitleEn?: string;
|
||||
keywords: KeywordData[];
|
||||
}
|
||||
|
||||
export function useKeywordCloud(quarter: string) {
|
||||
const [data, setData] = useState<QuarterData | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchData() {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/keyword-cloud/keywords/${quarter}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
if (!json.success) {
|
||||
throw new Error(json.error || 'Unknown error');
|
||||
}
|
||||
|
||||
setData(json);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err : new Error('Unknown error'));
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (quarter) {
|
||||
fetchData();
|
||||
}
|
||||
}, [quarter]);
|
||||
|
||||
return { data, isLoading, error };
|
||||
}
|
||||
Reference in New Issue
Block a user