feat: 调整前端blog列表为服务端渲染模式
This commit is contained in:
@@ -1,9 +1,5 @@
|
|||||||
"use client"
|
|
||||||
|
|
||||||
import { Skeleton } from "@/components/ui/skeleton";
|
|
||||||
import { BlogApi } from "@/lib/api";
|
import { BlogApi } from "@/lib/api";
|
||||||
import { useCallback } from "react"
|
import { useCallback } from "react"
|
||||||
import useSWR from "swr";
|
|
||||||
import {
|
import {
|
||||||
Alert,
|
Alert,
|
||||||
AlertDescription,
|
AlertDescription,
|
||||||
@@ -12,7 +8,7 @@ import {
|
|||||||
import { AlertCircle } from "lucide-react";
|
import { AlertCircle } from "lucide-react";
|
||||||
import { base62 } from "@/lib/utils";
|
import { base62 } from "@/lib/utils";
|
||||||
|
|
||||||
export default function Blog() {
|
export default async function Blog() {
|
||||||
const formatNumber = useCallback((num: number) => {
|
const formatNumber = useCallback((num: number) => {
|
||||||
if (num >= 1000) {
|
if (num >= 1000) {
|
||||||
return (num / 1000).toFixed(1) + 'K';
|
return (num / 1000).toFixed(1) + 'K';
|
||||||
@@ -22,29 +18,18 @@ export default function Blog() {
|
|||||||
return num.toString();
|
return num.toString();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const { data: blogs, error, isLoading } = useSWR(
|
let errorMsg = '';
|
||||||
'/api/blogs',
|
const blogs = await BlogApi.list().catch(e => { errorMsg = `${e}`; return null });
|
||||||
() => BlogApi.list(),
|
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="max-w-120 w-auto mx-auto my-10 flex flex-col gap-8">
|
<div className="max-w-120 w-auto mx-auto my-10 flex flex-col gap-8">
|
||||||
{
|
{
|
||||||
isLoading && (
|
errorMsg && (
|
||||||
<div className="w-full">
|
|
||||||
<Skeleton className="w-full h-5" />
|
|
||||||
<Skeleton className="w-full h-10 mt-1" />
|
|
||||||
<Skeleton className="w-full h-5 mt-5" />
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
{
|
|
||||||
error && (
|
|
||||||
<Alert variant="destructive" className="w-full">
|
<Alert variant="destructive" className="w-full">
|
||||||
<AlertCircle className="h-4 w-4" />
|
<AlertCircle className="h-4 w-4" />
|
||||||
<AlertTitle>出错啦</AlertTitle>
|
<AlertTitle>出错啦</AlertTitle>
|
||||||
<AlertDescription>
|
<AlertDescription>
|
||||||
{error.message}
|
{errorMsg}
|
||||||
</AlertDescription>
|
</AlertDescription>
|
||||||
</Alert>
|
</Alert>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Blog } from "@/lib/types/blog";
|
import { Blog } from "@/lib/types/blog";
|
||||||
import fetcher from "../fetcher";
|
import { apiFetch } from "../client";
|
||||||
|
|
||||||
export async function list() {
|
export async function list() {
|
||||||
return fetcher<Blog[]>('/api/blog');
|
return apiFetch<Blog[]>('/api/blog');
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user