refactor: Migrate App.tsx to use React Router

This commit is contained in:
dahyeon committed 2025-11-15 14:46:23 +00:00
1 parent d7847bb621
commit f64680578d
1 file changed
+42 -36
+42 -36
View File
@@ -2,13 +2,13 @@ import { Header } from './components/Header';
import { HomePage } from './components/HomePage'; import { HomePage } from './components/HomePage';
import { DocumentationPage } from './components/DocumentationPage'; import { DocumentationPage } from './components/DocumentationPage';
import { useState } from 'react'; import { useState } from 'react';
import { Routes, Route, useParams, useNavigate } from 'react-router-dom';
export default function App() { function DocumentationPageWrapper() {
const [currentPage, setCurrentPage] = useState<'home' | 'documentation'>('home'); const { taxonomyId, articleSlug } = useParams<{ taxonomyId?: string; articleSlug?: string }>();
const [isSidebarOpen, setIsSidebarOpen] = useState(false); const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const [theme, setTheme] = useState<'light' | 'dark'>('light'); const [theme, setTheme] = useState<'light' | 'dark'>('light');
const [currentTaxonomyId, setCurrentTaxonomyId] = useState<string | null>(null); const navigate = useNavigate();
const [currentArticleSlug, setCurrentArticleSlug] = useState<string | null>(null);
const toggleSidebar = () => { const toggleSidebar = () => {
setIsSidebarOpen(!isSidebarOpen); setIsSidebarOpen(!isSidebarOpen);
@@ -18,27 +18,12 @@ export default function App() {
setTheme(theme === 'light' ? 'dark' : 'light'); 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) => { const handleArticleClick = (slug: string) => {
setCurrentArticleSlug(slug); if (taxonomyId) {
navigate(`/contents/${taxonomyId}/${slug}`);
} else {
navigate(`/contents/article/${slug}`);
}
}; };
return ( return (
@@ -47,26 +32,17 @@ export default function App() {
toggleSidebar={toggleSidebar} toggleSidebar={toggleSidebar}
theme={theme} theme={theme}
toggleTheme={toggleTheme} toggleTheme={toggleTheme}
navigateToHome={navigateToHome}
navigateToDocumentation={navigateToDocumentation}
onTaxonomyClick={handleTaxonomyClick}
/> />
{currentPage === 'home' ? (
<HomePage theme={theme} navigateToDocumentation={navigateToDocumentation} />
) : (
<DocumentationPage <DocumentationPage
isSidebarOpen={isSidebarOpen} isSidebarOpen={isSidebarOpen}
toggleSidebar={toggleSidebar} toggleSidebar={toggleSidebar}
theme={theme} theme={theme}
taxonomyId={currentTaxonomyId} taxonomyId={taxonomyId || null}
articleSlug={currentArticleSlug} articleSlug={articleSlug || null}
onArticleClick={handleArticleClick} onArticleClick={handleArticleClick}
/> />
)}
{/* Mobile overlay */} {/* Mobile overlay */}
{isSidebarOpen && currentPage === 'documentation' && ( {isSidebarOpen && (
<div <div
className="fixed inset-0 bg-black opacity-50 z-30 lg:hidden" className="fixed inset-0 bg-black opacity-50 z-30 lg:hidden"
onClick={toggleSidebar} onClick={toggleSidebar}
@@ -75,3 +51,33 @@ export default function App() {
</div> </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>
);
}