4e21ca937a
Deploy Documentation / deploy (push) Has been cancelled
Canary / Prepare Version (push) Has been cancelled
Canary / build-cli (push) Has been cancelled
Canary / Upload Install Script (push) Has been cancelled
Canary / bundle-desktop (push) Has been cancelled
Canary / bundle-desktop-intel (push) Has been cancelled
Canary / bundle-desktop-linux (push) Has been cancelled
Canary / bundle-desktop-windows (push) Has been cancelled
Canary / bundle-desktop-windows-cuda (push) Has been cancelled
Canary / Release (push) Has been cancelled
Unused Dependencies / machete (push) Has been cancelled
CI / changes (push) Has been cancelled
CI / Check Rust Code Format (push) Has been cancelled
CI / Build and Test Rust Project (push) Has been cancelled
CI / Build Rust Project on Windows (push) Has been cancelled
CI / Check MSRV (push) Has been cancelled
CI / Lint Rust Code (push) Has been cancelled
CI / Check Generated Schemas are Up-to-Date (push) Has been cancelled
CI / Test and Lint Electron Desktop App (push) Has been cancelled
CI / H5 Plaza Tests and Build (push) Has been cancelled
Live Provider Tests / check-fork (push) Has been cancelled
Live Provider Tests / changes (push) Has been cancelled
Live Provider Tests / Build Binary (push) Has been cancelled
Live Provider Tests / Smoke Tests (push) Has been cancelled
Live Provider Tests / Smoke Tests (Code Execution) (push) Has been cancelled
Live Provider Tests / Compaction Tests (push) Has been cancelled
Live Provider Tests / goose server HTTP integration tests (push) Has been cancelled
Publish Ask AI Bot Docker Image / docker (push) Has been cancelled
Publish Docker Image / docker (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Fork goose with custom MCP widgets, platform extensions (aider, git, web, search), MindSpace H5 backend/frontend, Plaza/Ops UIs, and deploy scripts for tkmind.cn. Co-authored-by: Cursor <cursoragent@cursor.com>
94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
'use client';
|
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
import type { PlazaPost } from '@/types/plaza';
|
|
import { PostGrid } from '@/components/feed/PostGrid';
|
|
|
|
async function loadFeedPage(url: string) {
|
|
const response = await fetch(url);
|
|
const payload = await response.json();
|
|
if (!response.ok) throw new Error(payload?.error?.message ?? '加载失败');
|
|
return payload.data as {
|
|
posts: PlazaPost[];
|
|
next_cursor: string | null;
|
|
has_more: boolean;
|
|
};
|
|
}
|
|
|
|
export function FeedLoadMore({
|
|
initialPosts,
|
|
initialCursor,
|
|
initialHasMore,
|
|
sort,
|
|
categorySlug,
|
|
}: {
|
|
initialPosts: PlazaPost[];
|
|
initialCursor: string | null;
|
|
initialHasMore: boolean;
|
|
sort: 'hot' | 'new';
|
|
categorySlug?: string;
|
|
}) {
|
|
const [posts, setPosts] = useState(initialPosts);
|
|
const [cursor, setCursor] = useState(initialCursor);
|
|
const [hasMore, setHasMore] = useState(initialHasMore);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const sentinelRef = useRef<HTMLDivElement | null>(null);
|
|
const loadingRef = useRef(false);
|
|
|
|
const loadMore = useCallback(async () => {
|
|
if (!hasMore || loadingRef.current || !cursor) return;
|
|
loadingRef.current = true;
|
|
setLoading(true);
|
|
setError(null);
|
|
try {
|
|
const query = new URLSearchParams({ sort, limit: '20', cursor });
|
|
if (categorySlug) query.set('category', categorySlug);
|
|
const data = await loadFeedPage(`/api/plaza/v1/feed?${query.toString()}`);
|
|
setPosts((prev) => [...prev, ...data.posts]);
|
|
setCursor(data.next_cursor);
|
|
setHasMore(data.has_more);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : '加载失败');
|
|
} finally {
|
|
loadingRef.current = false;
|
|
setLoading(false);
|
|
}
|
|
}, [categorySlug, cursor, hasMore, sort]);
|
|
|
|
useEffect(() => {
|
|
if (!hasMore || !cursor) return;
|
|
const node = sentinelRef.current;
|
|
if (!node) return;
|
|
const observer = new IntersectionObserver(
|
|
(entries) => {
|
|
if (entries.some((entry) => entry.isIntersecting)) {
|
|
void loadMore();
|
|
}
|
|
},
|
|
{ rootMargin: '240px' },
|
|
);
|
|
observer.observe(node);
|
|
return () => observer.disconnect();
|
|
}, [cursor, hasMore, loadMore]);
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<PostGrid posts={posts} />
|
|
{error ? <p className="text-center text-sm text-red-700">{error}</p> : null}
|
|
{hasMore ? (
|
|
<div ref={sentinelRef} className="flex justify-center pb-8">
|
|
<button
|
|
type="button"
|
|
onClick={() => void loadMore()}
|
|
disabled={loading || !cursor}
|
|
className="rounded-full border border-[#2f6f57] px-6 py-2.5 text-sm font-semibold text-[#2f6f57] transition hover:bg-[#2f6f57] hover:text-white disabled:opacity-60"
|
|
>
|
|
{loading ? '加载中…' : '加载更多'}
|
|
</button>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|