32 lines
894 B
TypeScript
32 lines
894 B
TypeScript
import type { ReactNode } from "react";
|
|
|
|
import type { UseQueryResult } from "@tanstack/react-query";
|
|
|
|
interface MatchQueryStatusOptions {
|
|
Pending: ReactNode;
|
|
Empty: ReactNode;
|
|
Success: () => ReactNode;
|
|
Error?: ReactNode;
|
|
}
|
|
|
|
export function matchQueryStatus<T>(
|
|
query: UseQueryResult<T>,
|
|
options: MatchQueryStatusOptions,
|
|
): ReactNode {
|
|
if (query.isPending) return options.Pending;
|
|
if (query.isError) return options.Error ?? null;
|
|
|
|
const isEmpty =
|
|
query.data == null ||
|
|
(Array.isArray(query.data) && query.data.length === 0) ||
|
|
(typeof query.data === "object" &&
|
|
query.data !== null &&
|
|
"content" in query.data &&
|
|
Array.isArray((query.data as { content: unknown[] }).content) &&
|
|
(query.data as { content: unknown[] }).content.length === 0);
|
|
|
|
if (isEmpty) return options.Empty;
|
|
|
|
return options.Success();
|
|
}
|