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";
|
|
|
|
|
2022-04-14 21:49:51 +00:00
|
|
|
import { trpc } from "@lib/trpc";
|
|
|
|
|
2021-10-01 15:12:47 +00:00
|
|
|
import Loader from "@components/Loader";
|
|
|
|
|
2022-04-14 21:49:51 +00:00
|
|
|
import type { AppRouter } from "@server/routers/_app";
|
|
|
|
import type { TRPCClientErrorLike } from "@trpc/client";
|
|
|
|
import type { UseTRPCQueryOptions } from "@trpc/react";
|
|
|
|
// import type { inferProcedures } from "@trpc/react/src/createReactQueryHooks";
|
|
|
|
import type {
|
|
|
|
inferHandlerInput,
|
|
|
|
inferProcedureInput,
|
|
|
|
inferProcedureOutput,
|
|
|
|
ProcedureRecord,
|
|
|
|
} from "@trpc/server";
|
|
|
|
|
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>;
|
|
|
|
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;
|
|
|
|
}
|
2022-04-14 21:49:51 +00:00
|
|
|
|
|
|
|
type inferProcedures<TObj extends ProcedureRecord<any, any, any, any, any, any>> = {
|
|
|
|
[TPath in keyof TObj]: {
|
|
|
|
input: inferProcedureInput<TObj[TPath]>;
|
|
|
|
output: inferProcedureOutput<TObj[TPath]>;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
type TQueryValues = inferProcedures<AppRouter["_def"]["queries"]>;
|
|
|
|
type TQueries = AppRouter["_def"]["queries"];
|
|
|
|
type TError = TRPCClientErrorLike<AppRouter>;
|
|
|
|
|
|
|
|
const withQuery = <TPath extends keyof TQueryValues & string>(
|
|
|
|
pathAndInput: [path: TPath, ...args: inferHandlerInput<TQueries[TPath]>],
|
|
|
|
params?: UseTRPCQueryOptions<TPath, TQueryValues[TPath]["input"], TQueryValues[TPath]["output"], TError>
|
|
|
|
) => {
|
|
|
|
return function WithQuery(
|
|
|
|
opts: Omit<
|
|
|
|
Partial<QueryCellOptionsWithEmpty<TQueryValues[TPath]["output"], TError>> &
|
|
|
|
QueryCellOptionsNoEmpty<TQueryValues[TPath]["output"], TError>,
|
|
|
|
"query"
|
|
|
|
>
|
|
|
|
) {
|
|
|
|
const query = trpc.useQuery(pathAndInput, params);
|
|
|
|
return <QueryCell query={query} {...opts} />;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export { withQuery };
|