2021-10-01 15:12:47 +00:00
|
|
|
import {
|
|
|
|
QueryObserverLoadingErrorResult,
|
|
|
|
QueryObserverLoadingResult,
|
|
|
|
QueryObserverRefetchErrorResult,
|
|
|
|
QueryObserverSuccessResult,
|
|
|
|
UseQueryResult,
|
2022-09-29 16:58:29 +00:00
|
|
|
} from "@tanstack/react-query";
|
|
|
|
import { ReactNode } from "react";
|
2021-10-01 15:12:47 +00:00
|
|
|
|
2022-09-05 19:06:34 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import type { TRPCClientErrorLike } from "@calcom/trpc/client";
|
2022-11-10 23:40:01 +00:00
|
|
|
import type { DecorateProcedure } from "@calcom/trpc/react/shared";
|
|
|
|
import type { AnyQueryProcedure, inferProcedureInput, inferProcedureOutput } from "@calcom/trpc/server";
|
2022-07-22 17:27:06 +00:00
|
|
|
import type { AppRouter } from "@calcom/trpc/server/routers/_app";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Alert, Loader } from "@calcom/ui";
|
2022-04-14 21:49:51 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
import type { UseTRPCQueryOptions } from "@trpc/react-query/shared";
|
2022-09-29 16:58:29 +00:00
|
|
|
|
2021-10-01 15:12:47 +00:00
|
|
|
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>;
|
2022-04-25 17:01:51 +00:00
|
|
|
customLoader?: ReactNode;
|
2021-10-01 15:12:47 +00:00
|
|
|
error?: (
|
|
|
|
query: QueryObserverLoadingErrorResult<TData, TError> | QueryObserverRefetchErrorResult<TData, TError>
|
2021-10-30 15:54:21 +00:00
|
|
|
) => JSXElementOrNull;
|
2022-09-05 19:06:34 +00:00
|
|
|
loading?: (query: QueryObserverLoadingResult<TData, TError> | null) => 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;
|
2022-08-26 09:32:58 +00:00
|
|
|
/** @deprecated Use `trpc.useQuery` instead. */
|
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;
|
2022-09-05 19:06:34 +00:00
|
|
|
const { isLocaleReady } = useLocale();
|
|
|
|
const StatusLoader = opts.customLoader || <Loader />; // Fixes edge case where this can return null form query cell
|
|
|
|
|
|
|
|
if (query.status === "loading" || !isLocaleReady) {
|
|
|
|
return opts.loading?.(query.status === "loading" ? query : null) ?? StatusLoader;
|
|
|
|
}
|
|
|
|
|
2021-10-01 15:12:47 +00:00
|
|
|
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
|
|
|
}
|
2022-09-05 19:06:34 +00:00
|
|
|
|
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} />
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-04-26 00:26:13 +00:00
|
|
|
|
2021-10-01 15:12:47 +00:00
|
|
|
// impossible state
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-14 21:49:51 +00:00
|
|
|
|
|
|
|
type TError = TRPCClientErrorLike<AppRouter>;
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const withQuery = <
|
|
|
|
TQuery extends AnyQueryProcedure,
|
|
|
|
TInput = inferProcedureInput<TQuery>,
|
|
|
|
TOutput = inferProcedureOutput<TQuery>
|
|
|
|
>(
|
2023-02-10 20:05:02 +00:00
|
|
|
queryProcedure: DecorateProcedure<TQuery, any, any>,
|
2022-11-10 23:40:01 +00:00
|
|
|
input?: TInput,
|
|
|
|
params?: UseTRPCQueryOptions<any, TInput, TOutput, TOutput, TError>
|
2022-04-14 21:49:51 +00:00
|
|
|
) => {
|
|
|
|
return function WithQuery(
|
|
|
|
opts: Omit<
|
2022-11-10 23:40:01 +00:00
|
|
|
Partial<QueryCellOptionsWithEmpty<TOutput, TError>> & QueryCellOptionsNoEmpty<TOutput, TError>,
|
2022-04-14 21:49:51 +00:00
|
|
|
"query"
|
|
|
|
>
|
|
|
|
) {
|
2022-11-10 23:40:01 +00:00
|
|
|
const query = queryProcedure.useQuery(input, params);
|
2022-04-14 21:49:51 +00:00
|
|
|
return <QueryCell query={query} {...opts} />;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export { withQuery };
|