refactor: Migrate App.tsx to use React Router
This commit is contained in:
1 parent
d7847bb621
commit
f64680578d
1 file changed
+48
-42
+48
-42
@@ -2,13 +2,13 @@ import { Header } from './components/Header';
|
||||
import { HomePage } from './components/HomePage';
|
||||
import { DocumentationPage } from './components/DocumentationPage';
|
||||
import { useState } from 'react';
|
||||
import { Routes, Route, useParams, useNavigate } from 'react-router-dom';
|
||||
|
||||
export default function App() {
|
||||
const [currentPage, setCurrentPage] = useState<'home' | 'documentation'>('home');
|
||||
function DocumentationPageWrapper() {
|
||||
const { taxonomyId, articleSlug } = useParams<{ taxonomyId?: string; articleSlug?: string }>();
|
||||
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
|
||||
const [theme, setTheme] = useState<'light' | 'dark'>('light');
|
||||
const [currentTaxonomyId, setCurrentTaxonomyId] = useState<string | null>(null);
|
||||
const [currentArticleSlug, setCurrentArticleSlug] = useState<string | null>(null);
|
||||
const navigate = useNavigate();
|
||||
|
||||
const toggleSidebar = () => {
|
||||
setIsSidebarOpen(!isSidebarOpen);
|
||||
@@ -18,27 +18,12 @@ export default function App() {
|
||||
setTheme(theme === 'light' ? 'dark' : 'light');
|
||||
};
|
||||
|
||||
const navigateToHome = () => {
|
||||
setCurrentPage('home');
|
||||
setIsSidebarOpen(false);
|
||||
setCurrentTaxonomyId(null);
|
||||
setCurrentArticleSlug(null);
|
||||
};
|
||||
|
||||
const navigateToDocumentation = () => {
|
||||
setCurrentPage('documentation');
|
||||
setIsSidebarOpen(false);
|
||||
};
|
||||
|
||||
const handleTaxonomyClick = (taxonomyId: string) => {
|
||||
setCurrentPage('documentation');
|
||||
setCurrentTaxonomyId(taxonomyId);
|
||||
setCurrentArticleSlug(null);
|
||||
setIsSidebarOpen(false);
|
||||
};
|
||||
|
||||
const handleArticleClick = (slug: string) => {
|
||||
setCurrentArticleSlug(slug);
|
||||
if (taxonomyId) {
|
||||
navigate(`/contents/${taxonomyId}/${slug}`);
|
||||
} else {
|
||||
navigate(`/contents/article/${slug}`);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -47,26 +32,17 @@ export default function App() {
|
||||
toggleSidebar={toggleSidebar}
|
||||
theme={theme}
|
||||
toggleTheme={toggleTheme}
|
||||
navigateToHome={navigateToHome}
|
||||
navigateToDocumentation={navigateToDocumentation}
|
||||
onTaxonomyClick={handleTaxonomyClick}
|
||||
/>
|
||||
|
||||
{currentPage === 'home' ? (
|
||||
<HomePage theme={theme} navigateToDocumentation={navigateToDocumentation} />
|
||||
) : (
|
||||
<DocumentationPage
|
||||
isSidebarOpen={isSidebarOpen}
|
||||
toggleSidebar={toggleSidebar}
|
||||
theme={theme}
|
||||
taxonomyId={currentTaxonomyId}
|
||||
articleSlug={currentArticleSlug}
|
||||
onArticleClick={handleArticleClick}
|
||||
/>
|
||||
)}
|
||||
|
||||
<DocumentationPage
|
||||
isSidebarOpen={isSidebarOpen}
|
||||
toggleSidebar={toggleSidebar}
|
||||
theme={theme}
|
||||
taxonomyId={taxonomyId || null}
|
||||
articleSlug={articleSlug || null}
|
||||
onArticleClick={handleArticleClick}
|
||||
/>
|
||||
{/* Mobile overlay */}
|
||||
{isSidebarOpen && currentPage === 'documentation' && (
|
||||
{isSidebarOpen && (
|
||||
<div
|
||||
className="fixed inset-0 bg-black opacity-50 z-30 lg:hidden"
|
||||
onClick={toggleSidebar}
|
||||
@@ -75,3 +51,33 @@ export default function App() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function HomePageWrapper() {
|
||||
const [theme, setTheme] = useState<'light' | 'dark'>('light');
|
||||
|
||||
const toggleTheme = () => {
|
||||
setTheme(theme === 'light' ? 'dark' : 'light');
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={`min-h-screen ${theme === 'dark' ? 'bg-[#1a1a1a] text-white' : 'bg-white text-gray-900'}`}>
|
||||
<Header
|
||||
toggleSidebar={() => {}}
|
||||
theme={theme}
|
||||
toggleTheme={toggleTheme}
|
||||
/>
|
||||
<HomePage theme={theme} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/" element={<HomePageWrapper />} />
|
||||
<Route path="/contents" element={<DocumentationPageWrapper />} />
|
||||
<Route path="/contents/:taxonomyId" element={<DocumentationPageWrapper />} />
|
||||
<Route path="/contents/:taxonomyId/:articleSlug" element={<DocumentationPageWrapper />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user