This commit is contained in:
john 2025-05-03 23:43:59 +02:00
parent 1c2d6d60a6
commit 9f8f6b55a5

View file

@ -21,13 +21,11 @@ export default function FeedView({ loadPosts }: FeedViewProps) {
const [hasMore, setHasMore] = useState(true) const [hasMore, setHasMore] = useState(true)
const cursor = useRef<string | null>(null) const cursor = useRef<string | null>(null)
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
const loading = useRef(false)
const loading = useRef(false)
const loadNextPage = useCallback(async () => { const loadNextPage = useCallback(async () => {
if (loading.current) return if (loading.current) return
loading.current = true loading.current = true
setIsLoading(true)
try { try {
const page = await loadPosts(cursor.current, PageSize) const page = await loadPosts(cursor.current, PageSize)
@ -40,8 +38,8 @@ export default function FeedView({ loadPosts }: FeedViewProps) {
setPages((prev) => [...prev, page]) setPages((prev) => [...prev, page])
} finally { } finally {
setIsLoading(false)
loading.current = false loading.current = false
setIsLoading(false)
} }
}, [loadPosts]) }, [loadPosts])
@ -55,20 +53,20 @@ export default function FeedView({ loadPosts }: FeedViewProps) {
} }
}, [loadNextPage]) }, [loadNextPage])
const loadButtonState = isLoading ? 'loading' : hasMore ? 'ready' : 'done'
return ( return (
<main className={`w-full max-w-full px-12 py-6`}> <main className={`w-full max-w-full px-12 py-6`}>
<div className={`col-start-1`}> <div className={`col-start-1`}>
<PostsFeed posts={posts} /> <PostsFeed posts={posts} />
<LoadMoreButton state={loadButtonState} onClick={loadNextPage} /> {hasMore && (
<LoadMoreButton state={isLoading ? 'loading' : 'ready'} onClick={loadNextPage} />
)}
</div> </div>
</main> </main>
) )
} }
interface LoadMoreButtonProps { interface LoadMoreButtonProps {
state: 'ready' | 'loading' | 'done' state: 'ready' | 'loading'
onClick: () => void onClick: () => void
} }
@ -76,8 +74,6 @@ function LoadMoreButton({ state, onClick }: LoadMoreButtonProps) {
const buttonClasses = const buttonClasses =
'w-full py-3 px-4 bg-gray-100 hover:bg-gray-200 text-gray-800 rounded-md flex items-center justify-center disabled:opacity-70' 'w-full py-3 px-4 bg-gray-100 hover:bg-gray-200 text-gray-800 rounded-md flex items-center justify-center disabled:opacity-70'
switch (state) { switch (state) {
case 'done':
return <div className="text-center text-gray-400 py-4">that's all...</div>
case 'loading': case 'loading':
return ( return (
<button disabled={true} className={buttonClasses}> <button disabled={true} className={buttonClasses}>