"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { cn } from "@suphark/ui/lib/utils"; import { type ReactNode, useEffect, useRef } from "react"; export interface LoadMoreProps { /** ยังมีรายการให้โหลดอีกไหม — false = แสดง "ครบทุกรายการแล้ว" */ hasMore: boolean; /** กำลังโหลด (ปุ่มหมุน กดซ้ำไม่ได้) */ isLoading?: boolean; /** โหลดรอบล่าสุดไม่สำเร็จ — แสดงข้อความ + ปุ่มลองใหม่ (เรียก `onLoadMore` อีกครั้ง) */ error?: boolean; onLoadMore: () => void; /** จำนวนที่แสดงอยู่ / ทั้งหมด — ส่งมาครบทั้งคู่ = แสดง "แสดง N จาก M" */ shown?: number; total?: number; /** จำนวนที่จะโหลดต่อครั้ง (แสดงบนปุ่ม "โหลดเพิ่ม 20 รายการ") */ step?: number; className?: string; } /** * ปุ่ม "โหลดเพิ่ม" ที่รู้สถานะ (พร้อม / กำลังโหลด / ผิดพลาด / ครบแล้ว) — ใช้ท้ายรายการยาวแทนเลขหน้า (เหมาะกับมือถือ) * อยากให้โหลดเองเมื่อเลื่อนถึงท้ายใช้ `InfiniteList` * * */ export function LoadMore({ hasMore, isLoading = false, error = false, onLoadMore, shown, total, step, className, }: LoadMoreProps) { const text = useUiText().loadMore; const counter = shown !== undefined && total !== undefined ? (text?.shown ?? "แสดง {shown} จาก {total}") .replace("{shown}", String(shown)) .replace("{total}", String(total)) : null; let body: ReactNode; if (error) { body = ( <>

{text?.failed ?? "โหลดต่อไม่สำเร็จ ลองใหม่อีกครั้ง"}

); } else if (!hasMore) { body = (

{text?.done ?? "ครบทุกรายการแล้ว"} {total !== undefined && · {total}}

); } else { body = ( <> {counter &&

{counter}

} ); } return (
{body}
); } export interface InfiniteListProps extends Omit { items: ReadonlyArray; renderItem: (item: T, index: number) => ReactNode; getKey: (item: T, index: number) => string | number; /** * โหลดเองเมื่อเลื่อนถึงท้ายได้กี่ครั้งติดกันแล้วหยุดให้กดปุ่มเอง (กันโหลดไม่จบและให้ผู้ใช้ถึง footer ได้) * ค่าเริ่มต้น 3 · 0 = ไม่โหลดเอง (ปุ่มอย่างเดียว) · กดปุ่มแล้วนับใหม่ */ autoLoad?: number; /** ระยะก่อนถึงท้ายที่เริ่มโหลด (IntersectionObserver rootMargin) ค่าเริ่มต้น "200px" */ rootMargin?: string; /** แท็กของรายการ ค่าเริ่มต้น ul (แต่ละรายการถูกห่อด้วย li) */ as?: "ul" | "ol" | "div"; className?: string; listClassName?: string; } /** * รายการที่โหลดต่อเองเมื่อเลื่อนถึงท้าย (IntersectionObserver) + ปุ่ม `LoadMore` สำรองเสมอ * รายการที่โหลดแล้วไม่ถูกล้างเมื่อโหลดรอบถัดไปล้มเหลว * * r.id} renderItem={(r) => } * hasMore={hasNextPage} isLoading={isFetching} error={isError} onLoadMore={fetchNextPage} total={count} /> */ export function InfiniteList({ items, renderItem, getKey, autoLoad = 3, rootMargin = "200px", as = "ul", className, listClassName, ...loadMore }: InfiniteListProps) { const sentinel = useRef(null); const autoCount = useRef(0); const latest = useRef(loadMore); latest.current = loadMore; useEffect(() => { const el = sentinel.current; if (!el || autoLoad <= 0 || typeof IntersectionObserver === "undefined") return; const observer = new IntersectionObserver( (entries) => { const { hasMore, isLoading, error, onLoadMore } = latest.current; if (!entries.some((e) => e.isIntersecting)) return; if (!hasMore || isLoading || error || autoCount.current >= autoLoad) return; autoCount.current += 1; onLoadMore(); }, { rootMargin }, ); observer.observe(el); return () => observer.disconnect(); // สร้าง observer ใหม่เมื่อโหลดเสร็จ/จำนวนเปลี่ยน — observer ยิงเฉพาะตอนสถานะ "เห็น" เปลี่ยน ถ้ารายการสั้นจน // เครื่องหมายท้ายยังอยู่บนจอหลังโหลด จะไม่ยิงอีกเลย (โหลดค้าง) · observe ใหม่ = ยิงสถานะปัจจุบันทันที }, [autoLoad, rootMargin, loadMore.isLoading, items.length]); const List = as; const Item = as === "div" ? "div" : "li"; return (
{items.map((item, index) => ( {renderItem(item, index)} ))} ); }