{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"lib-load-more","type":"registry:lib","title":"load-more helpers","description":"Loading the next rows of a long list as its end comes near (Data Table and List, scrolled with virtual). One load","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/dashral/lib/load-more.ts","type":"registry:file","target":"components/dashral/lib/load-more.ts","content":"\"use client\"\n\n// Loading the next rows of a long list as its end comes near (Data Table and List, scrolled with virtual). One load\n// at a time; a load that throws waits for \"Try again\" rather than retrying on its own.\n\nimport * as React from \"react\"\n\nexport type LoadMoreStatus = \"idle\" | \"loading\" | \"error\"\n\n/**\n * Calls onLoadMore when `near` (the end is in view) while there's more to load, once at a time. It doesn't call again\n * for the same row count, so a load that adds nothing doesn't loop: it waits for the rows to change, or for retry.\n */\nexport function useLoadMore({\n  near,\n  count,\n  hasMore,\n  onLoadMore,\n}: {\n  near: boolean\n  /** How many rows there are now. */\n  count: number\n  hasMore: boolean\n  onLoadMore?: () => Promise<void> | void\n}) {\n  const [status, setStatus] = React.useState<LoadMoreStatus>(\"idle\")\n  const running = React.useRef(false)\n  const loadedAt = React.useRef<number | null>(null)\n  const latest = React.useRef(onLoadMore)\n  latest.current = onLoadMore\n\n  const load = React.useCallback(async (force = false) => {\n    if (!latest.current || running.current) return\n    if (!force && loadedAt.current === count) return\n    running.current = true\n    loadedAt.current = count\n    setStatus(\"loading\")\n    try {\n      await latest.current()\n      setStatus(\"idle\")\n    } catch {\n      setStatus(\"error\")\n    } finally {\n      running.current = false\n    }\n  }, [count])\n\n  React.useEffect(() => {\n    if (near && hasMore && status === \"idle\") void load()\n  }, [near, hasMore, status, load])\n\n  return { status, retry: () => void load(true) }\n}\n"}]}