import type { EventObject, Service, Machine } from "@zag-js/core" export type SortDirection = "ascending" | "descending" export interface SortDescriptor { column: keyof T direction: SortDirection } export interface LoadDetails { signal: AbortSignal | undefined filterText: string cursor?: C | undefined sortDescriptor?: SortDescriptor | undefined } export interface LoadResult { items: T[] cursor?: C | undefined } export interface SortDetails { items: T[] descriptor: SortDescriptor filterText: string } export type LoadDependency = string | number | boolean | undefined | null export interface AsyncListProps { /** * The function to call when the list is loaded */ load: (args: LoadDetails) => Promise> /** * The function to call when the list is sorted */ sort?: ((args: SortDetails) => Promise<{ items: T[] }> | { items: T[] } | undefined) | undefined /** * The initial items to display */ initialItems?: T[] | undefined /** * The initial sort descriptor to use */ initialSortDescriptor?: SortDescriptor | undefined /** * The initial filter text to use */ initialFilterText?: string | undefined /** * The dependencies to watch for changes */ dependencies?: LoadDependency[] | undefined /** * Whether to automatically reload the list when the dependencies change */ autoReload?: boolean | undefined /** * The function to call when the list is loaded successfully */ onSuccess?: ((details: { items: T[] }) => void | undefined) | undefined /** * The function to call when the list fails to load */ onError?: ((details: { error: Error }) => void | undefined) | undefined } export interface AsyncListSchema { state: "idle" | "loading" | "sorting" props: AsyncListProps context: { items: T[] filterText: string cursor?: C | undefined sortDescriptor?: SortDescriptor | undefined error?: any | undefined } refs: { abort: AbortController | null seq: number } event: EventObject action: string guard: string effect: string } export type AsyncListService = Service> export type AsyncListMachine = Machine> export interface AsyncListApi { /** * The items in the list. */ items: T[] /** * The filter text. */ filterText: string /** * The cursor. */ cursor: C | undefined /** * The sort descriptor. */ sortDescriptor: SortDescriptor | undefined /** * Whether the list is loading. */ loading: boolean /** * Whether the list is sorting. */ sorting: boolean /** * Whether the list is empty. */ empty: boolean /** * Whether there are more items to load. */ hasMore: boolean /** * The error instance returned by the last fetch. */ error: any | undefined /** * Function to abort the current fetch. */ abort: VoidFunction /** * Function to reload the list */ reload: VoidFunction /** * Function to load more items */ loadMore: VoidFunction /** * Function to sort the list */ sort: (sortDescriptor: SortDescriptor) => void /** * Function to set the filter text */ setFilterText: (filterText: string) => void /** * Function to clear the filter text */ clearFilter: VoidFunction }