2021-10-01 15:12:47 +00:00
|
|
|
import {
|
|
|
|
QueryObserverIdleResult,
|
|
|
|
QueryObserverLoadingErrorResult,
|
|
|
|
QueryObserverLoadingResult,
|
|
|
|
QueryObserverRefetchErrorResult,
|
|
|
|
QueryObserverSuccessResult,
|
|
|
|
UseQueryResult,
|
|
|
|
} from "react-query";
|
|
|
|
|
2022-03-16 23:36:43 +00:00
|
|
|
import { Alert } from "@calcom/ui/Alert";
|
|
|
|
|
2021-10-01 15:12:47 +00:00
|
|
|
import Loader from "@components/Loader";
|
|
|
|
|
|
|
|
type ErrorLike = {
|
|
|
|
message: string;
|
|
|
|
};
|
2021-10-30 15:54:21 +00:00
|
|
|
type JSXElementOrNull = JSX.Element | null;
|
2021-10-04 09:48:40 +00:00
|
|
|
|
|
|
|
interface QueryCellOptionsBase<TData, TError extends ErrorLike> {
|
2021-10-01 15:12:47 +00:00
|
|
|
query: UseQueryResult<TData, TError>;
|
|
|
|
error?: (
|
|
|
|
query: QueryObserverLoadingErrorResult<TData, TError> | QueryObserverRefetchErrorResult<TData, TError>
|
2021-10-30 15:54:21 +00:00
|
|
|
) => JSXElementOrNull;
|
|
|
|
loading?: (query: QueryObserverLoadingResult<TData, TError>) => JSXElementOrNull;
|
|
|
|
idle?: (query: QueryObserverIdleResult<TData, TError>) => JSXElementOrNull;
|
2021-10-04 09:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface QueryCellOptionsNoEmpty<TData, TError extends ErrorLike>
|
|
|
|
extends QueryCellOptionsBase<TData, TError> {
|
2021-10-30 15:54:21 +00:00
|
|
|
success: (query: QueryObserverSuccessResult<TData, TError>) => JSXElementOrNull;
|
2021-10-04 09:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface QueryCellOptionsWithEmpty<TData, TError extends ErrorLike>
|
|
|
|
extends QueryCellOptionsBase<TData, TError> {
|
2021-10-30 15:54:21 +00:00
|
|
|
success: (query: QueryObserverSuccessResult<NonNullable<TData>, TError>) => JSXElementOrNull;
|
2021-10-01 15:12:47 +00:00
|
|
|
/**
|
|
|
|
* If there's no data (`null`, `undefined`, or `[]`), render this component
|
|
|
|
*/
|
2021-10-30 15:54:21 +00:00
|
|
|
empty: (query: QueryObserverSuccessResult<TData, TError>) => JSXElementOrNull;
|
2021-10-01 15:12:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 09:48:40 +00:00
|
|
|
export function QueryCell<TData, TError extends ErrorLike>(
|
|
|
|
opts: QueryCellOptionsWithEmpty<TData, TError>
|
2021-10-30 15:54:21 +00:00
|
|
|
): JSXElementOrNull;
|
2021-10-04 09:48:40 +00:00
|
|
|
export function QueryCell<TData, TError extends ErrorLike>(
|
|
|
|
opts: QueryCellOptionsNoEmpty<TData, TError>
|
2021-10-30 15:54:21 +00:00
|
|
|
): JSXElementOrNull;
|
2021-10-04 09:48:40 +00:00
|
|
|
export function QueryCell<TData, TError extends ErrorLike>(
|
|
|
|
opts: QueryCellOptionsNoEmpty<TData, TError> | QueryCellOptionsWithEmpty<TData, TError>
|
|
|
|
) {
|
2021-10-01 15:12:47 +00:00
|
|
|
const { query } = opts;
|
|
|
|
|
|
|
|
if (query.status === "success") {
|
2021-10-04 09:48:40 +00:00
|
|
|
if ("empty" in opts && (query.data == null || (Array.isArray(query.data) && query.data.length === 0))) {
|
2021-10-01 15:12:47 +00:00
|
|
|
return opts.empty(query);
|
|
|
|
}
|
2021-10-04 09:48:40 +00:00
|
|
|
return opts.success(query as any);
|
2021-10-01 15:12:47 +00:00
|
|
|
}
|
|
|
|
if (query.status === "error") {
|
|
|
|
return (
|
|
|
|
opts.error?.(query) ?? (
|
|
|
|
<Alert severity="error" title="Something went wrong" message={query.error.message} />
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (query.status === "loading") {
|
|
|
|
return opts.loading?.(query) ?? <Loader />;
|
|
|
|
}
|
|
|
|
if (query.status === "idle") {
|
2021-10-14 10:57:49 +00:00
|
|
|
return opts.idle?.(query) ?? <Loader />;
|
2021-10-01 15:12:47 +00:00
|
|
|
}
|
|
|
|
// impossible state
|
|
|
|
return null;
|
|
|
|
}
|