Skip to content

Commit

Permalink
refactor(blog): use SSR to render index
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulike committed Sep 4, 2024
1 parent 1f69c83 commit 06cd138
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
14 changes: 5 additions & 9 deletions apps/blog/src/app/(index)/Container.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
'use client';

import type {Metadata} from 'next';

import {IndexView} from '@/src/app/(index)/View';
import {useArticlesWithAbstract} from '@/src/hooks/useArticlesWithAbstract';

import {IndexViewModel} from './IndexViewModel';

export const metadata: Metadata = {
title: 'Soulike 的博客',
};

export function Index() {
const {loading, articlesWithAbstract} = useArticlesWithAbstract();
export async function Index() {
const articlesWithAbstract = await IndexViewModel.getArticlesWithAbstract();

return (
<IndexView
articlesWithAbstract={articlesWithAbstract ?? []}
loading={loading}
/>
<IndexView articlesWithAbstract={articlesWithAbstract} loading={false} />
);
}
14 changes: 14 additions & 0 deletions apps/blog/src/app/(index)/IndexViewModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Article} from '@website/classes';

import {Article as ArticleApi} from '@/src/apis';

export class IndexViewModel {
public static async getArticlesWithAbstract(): Promise<Article[]> {
const response = await ArticleApi.getAllWithAbstract();
if (response.isSuccessful) {
return response.data;
} else {
throw new Error(response.message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use client';

import {type Category} from '@website/classes';
import {useMathJax, useMdConverter} from '@website/hooks';
import {useMdConverter} from '@website/hooks';

import {ArticlePreviewCardView} from './View';

Expand All @@ -18,11 +20,9 @@ export function ArticlePreviewCard(props: IArticlePreviewCardProps) {
const {loading: mdConverterLoading, html: articleBriefTextHtml} =
useMdConverter(articleBriefTextMarkdown);

const {loading: mathJaxLoading} = useMathJax([articleBriefTextHtml]);

return (
<ArticlePreviewCardView
loading={mdConverterLoading || mathJaxLoading}
loading={mdConverterLoading}
articleBriefTextHtml={articleBriefTextHtml ?? ''}
{...restProps}
/>
Expand Down
16 changes: 12 additions & 4 deletions apps/blog/src/components/ArticleList/Container.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import {type Article, type Category} from '@website/classes';
import {useMathJax} from '@website/hooks';
import type {PaginationConfig} from 'antd/lib/pagination';
import {useCallback, useMemo, useRef, useState} from 'react';

Expand All @@ -15,7 +16,7 @@ interface IProps {

export function ArticleList(props: IProps) {
const viewRef = useRef<HTMLDivElement>(null);
const [, setPageNumber] = useState(1);
const [pageNumber, setPageNumber] = useState(1);
const {categories, loading: categoriesIsLoading} = useCategories();

const {articleList, loading} = props;
Expand All @@ -38,12 +39,19 @@ export function ArticleList(props: IProps) {

viewRef.current.scrollIntoView(true);
},
[viewRef.current],
[],
);

// TODO: bug: math render when page changes
const {loading: mathJaxLoading} = useMathJax([
articleList,
categoryMap,
pageNumber,
]);

const isLoading = useMemo(
() => loading || categoriesIsLoading,
[categoriesIsLoading, loading],
() => loading || categoriesIsLoading || mathJaxLoading,
[categoriesIsLoading, loading, mathJaxLoading],
);

return (
Expand Down
5 changes: 1 addition & 4 deletions packages/hooks/src/useMathJax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ export function useMathJax(deps: Readonly<DependencyList>) {
const [loading, setLoading] = useState(true);

useEffect(() => {
if (!document) {
return;
}
setLoading(true);
TexRenderer.renderAllTex().finally(() => {
setLoading(false);
});
}, [...deps, document]);
}, [...deps]);

return {loading};
}

0 comments on commit 06cd138

Please sign in to comment.