import { type UseQueryResult } from "@tanstack/react-query"; import type { PageableList } from "../model"; /** * Match the state of a query to a set of components. * * Useful for rendering different UI based on the state of a query. */ export function matchQueryStatus( query: UseQueryResult>, options: { Loading?: JSX.Element; Errored?: JSX.Element | ((error: unknown) => JSX.Element); Empty?: JSX.Element; Success: ( data: UseQueryResult> & { data: NonNullable["data"]>; }, ) => JSX.Element; }, ): JSX.Element; export function matchQueryStatus( query: UseQueryResult>, options: { Loading?: JSX.Element; Errored?: JSX.Element | ((error: unknown) => JSX.Element); Success: (data: UseQueryResult>) => JSX.Element; }, ): JSX.Element; export function matchQueryStatus( query: UseQueryResult>, { Loading, Errored, Empty, Success, }: { Loading?: JSX.Element; Errored?: JSX.Element | ((error: unknown) => JSX.Element); Empty?: JSX.Element; Success: (data: UseQueryResult>) => JSX.Element; }, ): JSX.Element { if (query.isLoading && Loading) { return Loading; } if (query.isError && Errored) { if (typeof Errored === "function") { return Errored(query.error); } return Errored; } const isEmpty = query.data?.content === undefined || query.data?.content === null || (Array.isArray(query.data?.content) && query.data?.content.length === 0); if (isEmpty && Empty) { return Empty; } return Success(query); }