2026-03-28 18:03:46 +08:00
|
|
|
import { useState } from "react";
|
2026-03-28 18:47:05 +08:00
|
|
|
import { ProgressBar, Label, Tabs } from "@heroui/react";
|
2026-03-25 09:14:24 +08:00
|
|
|
import Card from "./components/Card";
|
2026-03-27 20:30:49 +08:00
|
|
|
import Guodegang from "./components/Guodegang";
|
2026-03-28 18:03:46 +08:00
|
|
|
import Mermaid from "./components/Mermaid";
|
2026-03-28 15:08:05 +08:00
|
|
|
import Topic from "./components/Topic";
|
2026-03-24 11:59:20 +08:00
|
|
|
|
2026-03-28 18:47:05 +08:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2026-03-23 22:33:30 +08:00
|
|
|
function App() {
|
2026-03-28 15:08:05 +08:00
|
|
|
const [topic, setTopic] = useState('');
|
2026-03-28 18:47:05 +08:00
|
|
|
const [cardData, setCardData]: any = useState(null);
|
|
|
|
|
const [cardLoading, setCardLoading] = useState(false);
|
|
|
|
|
const [cardError, setCardError] = useState<string | null>(null);
|
|
|
|
|
const [guodegangData, setGuodegangData]: any = useState(null);
|
|
|
|
|
const [guodegangLoading, setGuodegangLoading] = useState(false);
|
|
|
|
|
const [guodegangError, setGuodegangError] = useState<string | null>(null);
|
|
|
|
|
const [mermaidData, setMermaidData]: any = useState(null);
|
|
|
|
|
const [mermaidLoading, setMermaidLoading] = useState(false);
|
|
|
|
|
const [mermaidError, setMermaidError] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
const fetchData = async (topicToFetch: string) => {
|
|
|
|
|
setCardLoading(true);
|
|
|
|
|
setGuodegangLoading(true);
|
|
|
|
|
setMermaidLoading(true);
|
|
|
|
|
setCardError(null);
|
|
|
|
|
setGuodegangError(null);
|
|
|
|
|
setMermaidError(null);
|
|
|
|
|
|
|
|
|
|
const cardUrl = `http://localhost:8080/card/${encodeURIComponent(topicToFetch)}`;
|
|
|
|
|
const guodegangUrl = `http://localhost:8080/guodegang/${encodeURIComponent(topicToFetch)}`;
|
|
|
|
|
const mermaidUrl = `http://localhost:8080/mermaid/${encodeURIComponent(topicToFetch)}`;
|
|
|
|
|
|
|
|
|
|
const abortController = new AbortController();
|
|
|
|
|
const timeoutId = setTimeout(() => abortController.abort(), 300000);
|
|
|
|
|
|
|
|
|
|
const fetchCard = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(cardUrl, { signal: abortController.signal });
|
|
|
|
|
if (!response.ok) throw new Error(`Card API failed: ${response.status}`);
|
|
|
|
|
const result = await response.json();
|
|
|
|
|
setCardData(result.message);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching card data:", error);
|
|
|
|
|
setCardError("Failed to load card data");
|
|
|
|
|
} finally {
|
|
|
|
|
setCardLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fetchGuodegang = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(guodegangUrl, { signal: abortController.signal });
|
|
|
|
|
if (!response.ok) throw new Error(`Guodegang API failed: ${response.status}`);
|
|
|
|
|
const result = await response.json();
|
|
|
|
|
setGuodegangData(result.message);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching guodegang data:", error);
|
|
|
|
|
setGuodegangError("Failed to load guodegang data");
|
|
|
|
|
} finally {
|
|
|
|
|
setGuodegangLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fetchMermaid = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(mermaidUrl, { signal: abortController.signal });
|
|
|
|
|
if (!response.ok) throw new Error(`Mermaid API failed: ${response.status}`);
|
|
|
|
|
const result = await response.json();
|
|
|
|
|
setMermaidData(result.message);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching mermaid data:", error);
|
|
|
|
|
setMermaidError("Failed to load mermaid data");
|
|
|
|
|
} finally {
|
|
|
|
|
setMermaidLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Promise.allSettled([fetchCard(), fetchGuodegang(), fetchMermaid()]).finally(() => {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleTopicSubmit = (newTopic: string) => {
|
|
|
|
|
setTopic(newTopic);
|
|
|
|
|
if (newTopic) {
|
|
|
|
|
fetchData(newTopic);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const cardLoaded = !cardLoading && !!cardData;
|
|
|
|
|
const guodegangLoaded = !guodegangLoading && !!guodegangData;
|
|
|
|
|
const mermaidLoaded = !mermaidLoading && !!mermaidData;
|
2026-03-28 15:08:05 +08:00
|
|
|
|
|
|
|
|
const progress = (() => {
|
|
|
|
|
if (!topic) return 0;
|
2026-03-28 18:03:46 +08:00
|
|
|
if (cardLoaded && guodegangLoaded && mermaidLoaded) return 100;
|
|
|
|
|
if (cardLoaded && guodegangLoaded) return 67;
|
|
|
|
|
if (cardLoaded && mermaidLoaded) return 67;
|
|
|
|
|
if (guodegangLoaded && mermaidLoaded) return 67;
|
|
|
|
|
if (cardLoaded || guodegangLoaded || mermaidLoaded) return 33;
|
2026-03-28 15:08:05 +08:00
|
|
|
return 10;
|
|
|
|
|
})();
|
|
|
|
|
|
2026-03-23 22:33:30 +08:00
|
|
|
return (
|
2026-03-28 15:08:05 +08:00
|
|
|
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '20px' }}>
|
2026-03-28 18:47:05 +08:00
|
|
|
<Topic onTopicSubmit={handleTopicSubmit}/>
|
2026-03-28 15:08:05 +08:00
|
|
|
{topic && (
|
|
|
|
|
<div className="w-64">
|
|
|
|
|
<ProgressBar value={progress} color="accent">
|
|
|
|
|
<Label>Data Loading</Label>
|
|
|
|
|
<ProgressBar.Output />
|
|
|
|
|
<ProgressBar.Track>
|
|
|
|
|
<ProgressBar.Fill />
|
|
|
|
|
</ProgressBar.Track>
|
|
|
|
|
</ProgressBar>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-03-28 18:47:05 +08:00
|
|
|
<div className="w-full max-w-6xl">
|
|
|
|
|
<Tabs className="w-full">
|
|
|
|
|
<Tabs.ListContainer>
|
|
|
|
|
<Tabs.List aria-label="View options">
|
|
|
|
|
<Tabs.Tab id="card">
|
|
|
|
|
<Tabs.Separator />
|
|
|
|
|
<Tabs.Indicator />
|
|
|
|
|
📚 学习卡片
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
<Tabs.Tab id="guodegang">
|
|
|
|
|
<Tabs.Separator />
|
|
|
|
|
<Tabs.Indicator />
|
|
|
|
|
🎭 德云社讲解
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
<Tabs.Tab id="mermaid">
|
|
|
|
|
<Tabs.Separator />
|
|
|
|
|
<Tabs.Indicator />
|
|
|
|
|
📊 Mermaid 图表
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
</Tabs.List>
|
|
|
|
|
</Tabs.ListContainer>
|
|
|
|
|
<Tabs.Panel id="card">
|
|
|
|
|
<Card topic={topic} data={cardData} loading={cardLoading} error={cardError} />
|
|
|
|
|
</Tabs.Panel>
|
|
|
|
|
<Tabs.Panel id="guodegang">
|
|
|
|
|
<Guodegang topic={topic} data={guodegangData} loading={guodegangLoading} error={guodegangError} />
|
|
|
|
|
</Tabs.Panel>
|
|
|
|
|
<Tabs.Panel id="mermaid">
|
|
|
|
|
<Mermaid topic={topic} data={mermaidData} loading={mermaidLoading} error={mermaidError} />
|
|
|
|
|
</Tabs.Panel>
|
|
|
|
|
</Tabs>
|
2026-03-28 15:08:05 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-03-25 09:14:24 +08:00
|
|
|
);
|
2026-03-23 22:33:30 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-25 09:14:24 +08:00
|
|
|
export default App;
|