✨Feat: 分 Tab 加载展示
This commit is contained in:
+116
-9
@@ -1,15 +1,95 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { ProgressBar, Label } from "@heroui/react";
|
import { ProgressBar, Label, Tabs } from "@heroui/react";
|
||||||
import Card from "./components/Card";
|
import Card from "./components/Card";
|
||||||
import Guodegang from "./components/Guodegang";
|
import Guodegang from "./components/Guodegang";
|
||||||
import Mermaid from "./components/Mermaid";
|
import Mermaid from "./components/Mermaid";
|
||||||
import Topic from "./components/Topic";
|
import Topic from "./components/Topic";
|
||||||
|
|
||||||
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
function App() {
|
function App() {
|
||||||
const [topic, setTopic] = useState('');
|
const [topic, setTopic] = useState('');
|
||||||
const [cardLoaded, setCardLoaded] = useState(false);
|
const [cardData, setCardData]: any = useState(null);
|
||||||
const [guodegangLoaded, setGuodegangLoaded] = useState(false);
|
const [cardLoading, setCardLoading] = useState(false);
|
||||||
const [mermaidLoaded, setMermaidLoaded] = 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;
|
||||||
|
|
||||||
const progress = (() => {
|
const progress = (() => {
|
||||||
if (!topic) return 0;
|
if (!topic) return 0;
|
||||||
@@ -23,7 +103,7 @@ function App() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '20px' }}>
|
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '20px' }}>
|
||||||
<Topic onTopicSubmit={setTopic}/>
|
<Topic onTopicSubmit={handleTopicSubmit}/>
|
||||||
{topic && (
|
{topic && (
|
||||||
<div className="w-64">
|
<div className="w-64">
|
||||||
<ProgressBar value={progress} color="accent">
|
<ProgressBar value={progress} color="accent">
|
||||||
@@ -35,10 +115,37 @@ function App() {
|
|||||||
</ProgressBar>
|
</ProgressBar>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<div style={{ display: 'grid', gap: '10px' }}>
|
<div className="w-full max-w-6xl">
|
||||||
<Card topic={topic} onLoaded={setCardLoaded} />
|
<Tabs className="w-full">
|
||||||
<Guodegang topic={topic} onLoaded={setGuodegangLoaded} />
|
<Tabs.ListContainer>
|
||||||
<Mermaid topic={topic} onLoaded={setMermaidLoaded} />
|
<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>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Card } from "@heroui/react";
|
import { Card } from "@heroui/react";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
|
|
||||||
interface KeyPointItem {
|
interface KeyPointItem {
|
||||||
point: string;
|
point: string;
|
||||||
@@ -51,43 +50,12 @@ interface CardData {
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
topic: string;
|
topic: string;
|
||||||
onLoaded?: (loaded: boolean) => void;
|
data: CardData | null;
|
||||||
|
loading: boolean;
|
||||||
|
error: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Default({ topic, onLoaded }: Props) {
|
export default function Default({ topic, data, loading, error }: Props) {
|
||||||
const [data, setData] = useState<CardData | null>(null);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!topic) {
|
|
||||||
onLoaded?.(false);
|
|
||||||
setLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const fetchData = async () => {
|
|
||||||
try {
|
|
||||||
const response = await fetch(
|
|
||||||
`http://localhost:8080/card/${encodeURIComponent(topic)}`,
|
|
||||||
);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error("API Error");
|
|
||||||
}
|
|
||||||
const result = await response.json();
|
|
||||||
setData(result.message);
|
|
||||||
setError(null);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to fetch data: ", error);
|
|
||||||
setError("Failed to load data. Please try again.");
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
onLoaded?.(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
setLoading(true);
|
|
||||||
fetchData();
|
|
||||||
}, [topic, onLoaded]);
|
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return (
|
return (
|
||||||
<div className="w-full p-4 sm:p-6 md:max-w-2xl lg:max-w-4xl mx-auto">
|
<div className="w-full p-4 sm:p-6 md:max-w-2xl lg:max-w-4xl mx-auto">
|
||||||
@@ -143,7 +111,7 @@ export default function Default({ topic, onLoaded }: Props) {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Card.Description className="px-6 pb-6 space-y-6">
|
<div className="px-6 pb-6 space-y-6">
|
||||||
<div>
|
<div>
|
||||||
<h3 className="flex items-center text-lg font-semibold text-gray-700 mb-3">
|
<h3 className="flex items-center text-lg font-semibold text-gray-700 mb-3">
|
||||||
<span className="mr-2"></span>
|
<span className="mr-2"></span>
|
||||||
@@ -257,7 +225,7 @@ export default function Default({ topic, onLoaded }: Props) {
|
|||||||
{data!.mnemonic.content}
|
{data!.mnemonic.content}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</Card.Description>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Card } from "@heroui/react";
|
import { Card } from "@heroui/react";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
|
|
||||||
interface GuodegangData {
|
interface GuodegangData {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -8,43 +7,12 @@ interface GuodegangData {
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
topic: string;
|
topic: string;
|
||||||
onLoaded?: (loaded: boolean) => void;
|
data: GuodegangData | null;
|
||||||
|
loading: boolean;
|
||||||
|
error: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Guodegang({ topic, onLoaded }: Props) {
|
export default function Guodegang({ topic, data, loading, error }: Props) {
|
||||||
const [data, setData] = useState<GuodegangData | null>(null);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!topic) {
|
|
||||||
onLoaded?.(false);
|
|
||||||
setLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const fetchData = async () => {
|
|
||||||
try {
|
|
||||||
const response = await fetch(
|
|
||||||
`http://localhost:8080/guodegang/${encodeURIComponent(topic)}`,
|
|
||||||
);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error("API Error");
|
|
||||||
}
|
|
||||||
const result = await response.json();
|
|
||||||
setData(result.message);
|
|
||||||
setError(null);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to fetch data: ", error);
|
|
||||||
setError("Failed to load data. Please try again.");
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
onLoaded?.(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
setLoading(true);
|
|
||||||
fetchData();
|
|
||||||
}, [topic, onLoaded]);
|
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return (
|
return (
|
||||||
<div className="w-full p-4 sm:p-6 md:max-w-2xl lg:max-w-4xl mx-auto">
|
<div className="w-full p-4 sm:p-6 md:max-w-2xl lg:max-w-4xl mx-auto">
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Surface } from "@heroui/react";
|
import { Surface } from "@heroui/react";
|
||||||
import { useEffect, useState, useRef } from "react";
|
import { useEffect, useRef } from "react";
|
||||||
import mermaid from "mermaid";
|
import mermaid from "mermaid";
|
||||||
|
|
||||||
interface MermaidData {
|
interface MermaidData {
|
||||||
@@ -9,51 +9,22 @@ interface MermaidData {
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
topic: string;
|
topic: string;
|
||||||
onLoaded?: (loaded: boolean) => void;
|
data: MermaidData | null;
|
||||||
|
loading: boolean;
|
||||||
|
error: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Default({ topic, onLoaded }: Props) {
|
export default function Default({ topic, data, loading, error }: Props) {
|
||||||
const [data, setData] = useState<MermaidData | null>(null);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
|
||||||
const mermaidRef = useRef<HTMLDivElement>(null);
|
const mermaidRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
mermaid.initialize({ startOnLoad: true });
|
mermaid.initialize({ startOnLoad: true });
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!topic) {
|
|
||||||
onLoaded?.(false);
|
|
||||||
setLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const fetchData = async () => {
|
|
||||||
try {
|
|
||||||
const response = await fetch(
|
|
||||||
`http://localhost:8080/mermaid/${encodeURIComponent(topic)}`,
|
|
||||||
);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error("API Error");
|
|
||||||
}
|
|
||||||
const result = await response.json();
|
|
||||||
setData(result.message);
|
|
||||||
setError(null);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to fetch data: ", error);
|
|
||||||
setError("Failed to load data. Please try again.");
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
onLoaded?.(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
setLoading(true);
|
|
||||||
fetchData();
|
|
||||||
}, [topic, onLoaded]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (data && mermaidRef.current) {
|
if (data && mermaidRef.current) {
|
||||||
mermaid.render("mermaid-diagram", data.content).then((result) => {
|
const id = `mermaid-diagram-${Date.now()}`;
|
||||||
|
mermaid.render(id, data.content).then((result) => {
|
||||||
if (mermaidRef.current) {
|
if (mermaidRef.current) {
|
||||||
mermaidRef.current.innerHTML = result.svg;
|
mermaidRef.current.innerHTML = result.svg;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user