"use client"; import React, { useRef, useCallback, useEffect } from "react"; import { Button, Card, CardBody, CardHeader, Chip, Divider, Listbox, ListboxItem, Spinner } from "@nextui-org/react"; import { useTranslation } from "@/shared/lib/useTranslation"; import { Plus } from "lucide-react"; // 资源组类型定义 export interface ResourceGroup { id: string; name: string; description?: string; isDefault?: boolean; serviceCount?: number; createdAt?: string; } interface GroupListProps { groups: ResourceGroup[]; selectedGroup: ResourceGroup | null; onSelectGroup: (group: ResourceGroup) => void; onCreateGroup: () => void; onLoadMore?: () => void; hasMore?: boolean; loading?: boolean; shouldScrollToTop?: boolean; onScrolledToTop?: () => void; } const GroupList: React.FC = ({ groups, selectedGroup, onSelectGroup, onCreateGroup, onLoadMore, hasMore = false, loading = false, shouldScrollToTop = false, onScrolledToTop, }) => { const { t } = useTranslation(); const scrollRef = useRef(null); const observerRef = useRef(null); const loadMoreTriggerRef = useRef(null); const hasLoadedMoreRef = useRef(false); // 使用 IntersectionObserver 监听滚动到底部 const handleObserver = useCallback( (entries: IntersectionObserverEntry[]) => { const [target] = entries; if (target.isIntersecting && hasMore && !loading && onLoadMore) { hasLoadedMoreRef.current = true; // 标记已触发过加载更多 onLoadMore(); } }, [hasMore, loading, onLoadMore] ); useEffect(() => { const element = loadMoreTriggerRef.current; if (!element || !onLoadMore) return; // 创建 IntersectionObserver observerRef.current = new IntersectionObserver(handleObserver, { root: scrollRef.current, rootMargin: "100px", // 提前 100px 触发加载 threshold: 0.1, }); observerRef.current.observe(element); return () => { if (observerRef.current && element) { observerRef.current.unobserve(element); } }; }, [handleObserver, onLoadMore]); // 监听 shouldScrollToTop 变化,滚动到顶部 useEffect(() => { if (shouldScrollToTop && scrollRef.current) { scrollRef.current.scrollTo({ top: 0, behavior: 'smooth' }); // 通知父组件已完成滚动 onScrolledToTop?.(); } }, [shouldScrollToTop, onScrolledToTop]); return (

{t("Group List")}

:
{t("No groups found")}
} selectionMode="single" selectedKeys={selectedGroup ? [selectedGroup.id] : []} onSelectionChange={(keys) => { const selectedId = Array.from(keys)[0]; const selectedKey = (groups || []).find( (k) => k.id === selectedId ); if (selectedKey) onSelectGroup(selectedKey); }} itemClasses={{ title: "text-md font-medium", base: "bg-white p-6 transition-colors border-1 data-[selected=true]:focus:bg-primary-50 data-[selected=true]:data-[hover=true]:bg-primary-50 data-[selected=true]:border-primary-500 data-[selected=true]:bg-primary-50 data-[hover=true]:bg-default/50 data-[selectable=true]:focus:bg-default/50 [&>span]:w-full", }} classNames={{ list: "gap-4", }} hideSelectedIcon > {(group) => (
{group.name} {group.isDefault && ( {t("Default")} )}
)}
{/* 加载更多触发器 */} {onLoadMore && (
{loading && hasMore && ( )} {!hasMore && groups.length > 0 && hasLoadedMoreRef.current && (

{t("No more groups")}

)}
)}
); }; export default GroupList;