Skip to content

Commit a2b1648

Browse files
committed
fix: Fetching new logs no longer fetches current page after scrolling
1 parent 14283c5 commit a2b1648

1 file changed

Lines changed: 38 additions & 5 deletions

File tree

src/LogPort.UI/src/App.tsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function App() {
4141
}, [page])
4242

4343
useEffect(() => {
44-
fetchLogs()
44+
fetchLatestLogs()
4545
getMetadata().then(setMetadata).catch(err => console.error('Failed to load metadata', err))
4646
return () => {
4747
wsRef.current?.close()
@@ -54,14 +54,14 @@ function App() {
5454
const nearBottom = window.innerHeight + window.scrollY >= document.body.offsetHeight - 300
5555
if (nearBottom) {
5656
console.log('Reached bottom, fetching next page...')
57-
fetchLogs()
57+
fetchLogsPage()
5858
}
5959
}
6060
window.addEventListener('scroll', handleScroll)
6161
return () => window.removeEventListener('scroll', handleScroll)
6262
}, [hasMore])
6363

64-
const fetchLogs = async () => {
64+
const fetchLogsPage = async () => {
6565
if (loadingRef.current) return
6666
loadingRef.current = true
6767
setLoading(true)
@@ -101,12 +101,45 @@ function App() {
101101
}
102102
}
103103

104+
const fetchLatestLogs = async () => {
105+
if (loadingRef.current) return
106+
loadingRef.current = true
107+
setLoading(true)
108+
109+
try {
110+
const params: LogQueryParameters = {
111+
...queryParamsRef.current,
112+
page: 1,
113+
}
114+
115+
const newLogs = await getLogs(params)
116+
setLogs(newLogs)
117+
setPage(2)
118+
pageRef.current = 2
119+
setHasMore(true)
120+
121+
const latest = newLogs.reduce((max, log) => {
122+
const ts = log.timestamp ? new Date(log.timestamp) : new Date(0)
123+
return ts > max ? ts : max
124+
}, lastUpdatedRef.current ?? new Date(0))
125+
lastUpdatedRef.current = latest
126+
127+
const histogramData = await getHistogramData({})
128+
setHistogram(histogramData)
129+
} catch (err) {
130+
console.error('Failed to fetch latest logs', err)
131+
} finally {
132+
loadingRef.current = false
133+
setLoading(false)
134+
}
135+
}
136+
104137
const applyFilters = () => {
105138
setLogs([])
106139
setHasMore(true)
107140
setPage(1)
108141
pageRef.current = 1
109-
fetchLogs()
142+
fetchLatestLogs()
110143
}
111144

112145
const enableTailing = () => {
@@ -192,7 +225,7 @@ function App() {
192225
</div>
193226

194227
<div style={{ display: 'flex', alignItems: 'center', marginBottom: '8px' }}>
195-
<button onClick={fetchLogs} disabled={loading} style={{ marginRight: '12px' }}>
228+
<button onClick={fetchLatestLogs} disabled={loading} style={{ marginRight: '12px' }}>
196229
{loading ? 'Loading...' : 'Fetch New Logs'}
197230
</button>
198231
<button onClick={enableTailing} disabled={tailing}>

0 commit comments

Comments
 (0)