Skip to content

Commit 4c69c7b

Browse files
committed
更新
1 parent 5fb6eb5 commit 4c69c7b

4 files changed

Lines changed: 44 additions & 14 deletions

File tree

js/app.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const DevPanel = ({ onToggle }) => {
1414
const App = () => {
1515
const [currentPage, setCurrentPage] = useState('home');
1616
const [isDark, setIsDark] = useState(false);
17+
const [openContent, setOpenContent] = useState(null);
1718

1819
useEffect(() => {
1920
const savedTheme = localStorage.getItem('theme');
@@ -39,6 +40,15 @@ const App = () => {
3940

4041
const handleNavigate = (page) => {
4142
setCurrentPage(page);
43+
setOpenContent(null);
44+
window.location.hash = page;
45+
window.scrollTo({ top: 0, behavior: 'smooth' });
46+
};
47+
48+
const handleOpenContent = (type, slug) => {
49+
const page = type === 'article' ? 'articles' : 'works';
50+
setCurrentPage(page);
51+
setOpenContent({ type, slug });
4252
window.location.hash = page;
4353
window.scrollTo({ top: 0, behavior: 'smooth' });
4454
};
@@ -57,13 +67,13 @@ const App = () => {
5767
const renderPage = () => {
5868
switch (currentPage) {
5969
case 'works':
60-
return <WorksPage />;
70+
return <WorksPage openContent={openContent} />;
6171
case 'articles':
62-
return <ArticlesPage />;
72+
return <ArticlesPage openContent={openContent} />;
6373
case 'about':
6474
return <AboutPage />;
6575
default:
66-
return <HomePage onNavigate={handleNavigate} />;
76+
return <HomePage onNavigate={handleNavigate} onOpenContent={handleOpenContent} />;
6777
}
6878
};
6979

js/pages/ArticlesPage.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const ArticlesPage = () => {
1+
const ArticlesPage = ({ openContent }) => {
22
const [articles, setArticles] = useState([]);
33
const [selectedArticle, setSelectedArticle] = useState(null);
44
const [loading, setLoading] = useState(true);
@@ -18,6 +18,15 @@ const ArticlesPage = () => {
1818
}
1919
}, [selectedArticle]);
2020

21+
useEffect(() => {
22+
if (openContent && openContent.type === 'article' && articles.length > 0) {
23+
const article = articles.find(a => a.slug === openContent.slug);
24+
if (article) {
25+
setSelectedArticle(article);
26+
}
27+
}
28+
}, [openContent, articles]);
29+
2130
useEffect(() => {
2231
const urlParams = new URLSearchParams(window.location.search);
2332
const openParam = urlParams.get('open');

js/pages/HomePage.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const HomePage = ({ onNavigate }) => {
1+
const HomePage = ({ onNavigate, onOpenContent }) => {
22
const [articles, setArticles] = useState([]);
33
const [works, setWorks] = useState([]);
44
const [loading, setLoading] = useState(true);
@@ -42,10 +42,11 @@ const HomePage = ({ onNavigate }) => {
4242
<div className="card-grid">
4343
{articles.map((article) => (
4444
<Card key={article.id} tag={article.tag} title={article.title} description={article.description} meta={article.date} onClick={() => {
45-
const url = new URL(window.location.href);
46-
url.searchParams.set('open', `article:${article.slug}`);
47-
url.hash = '#articles';
48-
window.location.href = url.toString();
45+
if (onOpenContent) {
46+
onOpenContent('article', article.slug);
47+
} else {
48+
onNavigate('articles');
49+
}
4950
}} />
5051
))}
5152
</div>
@@ -65,10 +66,11 @@ const HomePage = ({ onNavigate }) => {
6566
<div className="card-grid">
6667
{works.map((work, index) => (
6768
<Card key={work.id} title={work.title} description={work.description} tag={work.techStack[0]} onClick={() => {
68-
const url = new URL(window.location.href);
69-
url.searchParams.set('open', `work:${work.slug}`);
70-
url.hash = '#works';
71-
window.location.href = url.toString();
69+
if (onOpenContent) {
70+
onOpenContent('work', work.slug);
71+
} else {
72+
onNavigate('works');
73+
}
7274
}} />
7375
))}
7476
</div>

js/pages/WorksPage.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const WorksPage = () => {
1+
const WorksPage = ({ openContent }) => {
22
const [works, setWorks] = useState([]);
33
const [selectedWork, setSelectedWork] = useState(null);
44
const [loading, setLoading] = useState(true);
@@ -31,6 +31,15 @@ const WorksPage = () => {
3131
}
3232
}, [selectedWork]);
3333

34+
useEffect(() => {
35+
if (!loading && openContent && openContent.type === 'work') {
36+
const work = works.find(w => w.slug === openContent.slug);
37+
if (work) {
38+
setSelectedWork(work);
39+
}
40+
}
41+
}, [loading, openContent, works]);
42+
3443
useEffect(() => {
3544
const urlParams = new URLSearchParams(window.location.search);
3645
const openParam = urlParams.get('open');

0 commit comments

Comments
 (0)