2022-04-25 17:01:51 +00:00
|
|
|
import React, { ReactNode } from "react";
|
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>;
|
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;
|
|
|
|
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} />
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-04-26 00:26:13 +00:00
|
|
|
const StatusLoader = opts.customLoader || <Loader />; // Fixes edge case where this can return null form query cell
|
|
|
|
|
2021-10-01 15:12:47 +00:00
|
|
|
if (query.status === "loading") {
|
2022-04-26 00:26:13 +00:00
|
|
|
return opts.loading?.(query) ?? StatusLoader;
|
2021-10-01 15:12:47 +00:00
|
|
|
}
|
|
|
|
if (query.status === "idle") {
|
2022-04-26 00:26:13 +00:00
|
|
|
return opts.idle?.(query) ?? StatusLoader;
|
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]>],
|
2022-06-19 15:02:00 +00:00
|
|
|
params?: UseTRPCQueryOptions<
|
|
|
|
TPath,
|
|
|
|
TQueryValues[TPath]["input"],
|
|
|
|
TQueryValues[TPath]["output"],
|
|
|
|
TQueryValues[TPath]["output"],
|
|
|
|
TError
|
|
|
|
>
|
2022-04-14 21:49:51 +00:00
|
|
|
) => {
|
|
|
|
return function WithQuery(
|
|
|
|
opts: Omit<
|
|
|
|
Partial<QueryCellOptionsWithEmpty<TQueryValues[TPath]["output"], TError>> &
|
|
|
|
QueryCellOptionsNoEmpty<TQueryValues[TPath]["output"], TError>,
|
|
|
|
"query"
|
|
|
|
>
|
|
|
|
) {
|
|
|
|
const query = trpc.useQuery(pathAndInput, params);
|
2022-04-25 17:01:51 +00:00
|
|
|
|
2022-04-14 21:49:51 +00:00
|
|
|
return <QueryCell query={query} {...opts} />;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export { withQuery };
|