Skip to content

Commit 84abf1c

Browse files
committed
fix: build error
1 parent b94bd88 commit 84abf1c

5 files changed

Lines changed: 94 additions & 78 deletions

File tree

src/app/logs/page.tsx

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,47 @@ export default function LogsPage() {
2626
const [currentPage, setCurrentPage] = useState(1)
2727

2828
// 分页获取日志
29-
const fetchLogs = async (page = 1, append = false) => {
30-
try {
31-
if (append) {
32-
setLoadingMore(true)
33-
} else {
34-
setLoading(true)
35-
}
36-
37-
const params = new URLSearchParams({
38-
page: page.toString(),
39-
limit: '10',
40-
})
41-
42-
const response = await fetch(`/api/logs?${params}`)
43-
const data = await response.json()
44-
45-
if (response.ok) {
29+
const fetchLogs = useCallback(
30+
async (page = 1, append = false) => {
31+
try {
4632
if (append) {
47-
setLogs((prev) => [...prev, ...data.logs])
33+
setLoadingMore(true)
4834
} else {
49-
setLogs(data.logs)
35+
setLoading(true)
5036
}
5137

52-
// 检查是否还有更多页面
53-
setHasMore(data.logs.length === 10 && data.pagination.totalPages > page)
54-
} else {
55-
errorToast('获取日志失败', { description: data.error })
38+
const params = new URLSearchParams({
39+
page: page.toString(),
40+
limit: '10',
41+
})
42+
43+
const response = await fetch(`/api/logs?${params}`)
44+
const data = await response.json()
45+
46+
if (response.ok) {
47+
if (append) {
48+
setLogs((prev) => [...prev, ...data.logs])
49+
} else {
50+
setLogs(data.logs)
51+
}
52+
53+
// 检查是否还有更多页面
54+
setHasMore(
55+
data.logs.length === 10 && data.pagination.totalPages > page
56+
)
57+
} else {
58+
errorToast('获取日志失败', { description: data.error })
59+
}
60+
} catch (error) {
61+
console.error('获取日志失败:', error)
62+
errorToast('获取日志失败', { description: '网络错误,请稍后重试' })
63+
} finally {
64+
setLoading(false)
65+
setLoadingMore(false)
5666
}
57-
} catch (error) {
58-
console.error('获取日志失败:', error)
59-
errorToast('获取日志失败', { description: '网络错误,请稍后重试' })
60-
} finally {
61-
setLoading(false)
62-
setLoadingMore(false)
63-
}
64-
}
67+
},
68+
[errorToast]
69+
)
6570

6671
// 分页加载更多日志
6772
const loadMore = useCallback(() => {
@@ -70,7 +75,7 @@ export default function LogsPage() {
7075
setCurrentPage(nextPage)
7176
fetchLogs(nextPage, true)
7277
}
73-
}, [currentPage, loadingMore, hasMore])
78+
}, [currentPage, loadingMore, hasMore, fetchLogs])
7479

7580
// 分页滚动加载更多日志
7681
useEffect(() => {
@@ -99,7 +104,7 @@ export default function LogsPage() {
99104
useEffect(() => {
100105
// 初始化加载第一页日志
101106
fetchLogs(1, false)
102-
}, [])
107+
}, [fetchLogs])
103108

104109
const formatTime = (dateString: string) => {
105110
const date = new Date(dateString)

src/components/admin/log-manage.tsx

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useState, useEffect } from 'react'
3+
import { useState, useEffect, useCallback } from 'react'
44
import { useRouter } from 'next/navigation'
55
import Link from 'next/link'
66
import { Button } from '@/components/ui/button'
@@ -99,38 +99,41 @@ export default function LogsPage() {
9999
}, [user?.id])
100100

101101
// Fetch logs
102-
const fetchLogs = async (page = 1) => {
103-
try {
104-
setLoading(true)
105-
const params = new URLSearchParams({
106-
page: page.toString(),
107-
limit: '10',
108-
})
109-
if (searchTerm) params.append('search', searchTerm)
102+
const fetchLogs = useCallback(
103+
async (page = 1) => {
104+
try {
105+
setLoading(true)
106+
const params = new URLSearchParams({
107+
page: page.toString(),
108+
limit: '10',
109+
})
110+
if (searchTerm) params.append('search', searchTerm)
110111

111-
const response = await fetch(`/api/logs?${params}`)
112-
const data = await response.json()
112+
const response = await fetch(`/api/logs?${params}`)
113+
const data = await response.json()
113114

114-
if (response.ok) {
115-
setLogs(data.logs)
116-
console.log('获取日志成功:', data)
117-
setPagination(data.pagination)
118-
} else {
119-
errorToast('获取日志失败', { description: data.error })
115+
if (response.ok) {
116+
setLogs(data.logs)
117+
console.log('获取日志成功:', data)
118+
setPagination(data.pagination)
119+
} else {
120+
errorToast('获取日志失败', { description: data.error })
121+
}
122+
} catch (error) {
123+
console.error('获取日志失败:', error)
124+
errorToast('获取日志失败', { description: '网络错误,请稍后重试' })
125+
} finally {
126+
setLoading(false)
120127
}
121-
} catch (error) {
122-
console.error('获取日志失败:', error)
123-
errorToast('获取日志失败', { description: '网络错误,请稍后重试' })
124-
} finally {
125-
setLoading(false)
126-
}
127-
}
128+
},
129+
[searchTerm, errorToast]
130+
)
128131

129132
useEffect(() => {
130133
if (isAdminUser) {
131134
fetchLogs(currentPage)
132135
}
133-
}, [isAdminUser, currentPage, searchTerm])
136+
}, [isAdminUser, currentPage, searchTerm, fetchLogs])
134137

135138
const handleSearch = () => {
136139
setCurrentPage(1)

src/components/app-sidebar.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,31 @@ export function AppSidebar() {
4747
const [categories, setCategories] = useState<WebCategory[]>([])
4848
const [isLoading, setIsLoading] = useState(true)
4949
const [isAdminUser, setIsAdminUser] = useState(false)
50+
const [hasLoadedCategories, setHasLoadedCategories] = useState(false)
5051
const { errorToast } = useToast()
5152

52-
const loadCategories = useCallback(async () => {
53-
try {
54-
// const data = await getCategories()
55-
const response = await fetch('/api/categories')
56-
const data = await response.json()
57-
setCategories(data)
58-
} catch (error) {
59-
console.error('加载分类失败:', error)
60-
errorToast('分类加载失败', {
61-
description: error as string,
62-
})
63-
} finally {
64-
setIsLoading(false)
53+
useEffect(() => {
54+
if (hasLoadedCategories) return
55+
56+
const loadCategories = async () => {
57+
try {
58+
// const data = await getCategories()
59+
const response = await fetch('/api/categories')
60+
const data = await response.json()
61+
setCategories(data)
62+
} catch (error) {
63+
console.error('加载分类失败:', error)
64+
errorToast('分类加载失败', {
65+
description: error as string,
66+
})
67+
} finally {
68+
setIsLoading(false)
69+
setHasLoadedCategories(true)
70+
}
6571
}
66-
}, [errorToast])
6772

68-
useEffect(() => {
6973
loadCategories()
70-
}, []) // 空依赖数组,只在组件挂载时运行一次
74+
}, [hasLoadedCategories, errorToast]) // eslint-disable-line react-hooks/exhaustive-deps
7175

7276
// Check admin status when user changes
7377
useEffect(() => {

src/components/message-board/markdown-content.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useState, useEffect } from 'react'
44
import ReactMarkdown from 'react-markdown'
5+
import Image from 'next/image'
56

67
interface MarkdownContentProps {
78
content: string
@@ -35,11 +36,13 @@ export default function MarkdownContent({
3536
/>
3637
),
3738
img: ({ node, ...props }) => (
38-
<img
39-
{...props}
40-
className="max-w-full h-auto rounded-md my-2"
41-
loading="lazy"
39+
<Image
40+
src={props.src || ''}
4241
alt={props.alt || 'Image'}
42+
width={800}
43+
height={600}
44+
className="max-w-full h-auto rounded-md my-2"
45+
unoptimized
4346
/>
4447
),
4548
code: ({ node, ...props }) => (

src/components/message-board/rich-text-editor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export default function RichTextEditor({
215215
className="h-8 w-8 p-0"
216216
onClick={insertImage}
217217
disabled={disabled}
218+
aria-label="插入图片"
218219
>
219220
<Image className="h-4 w-4" />
220221
</Button>

0 commit comments

Comments
 (0)