UNPKG

@airplane/views

Version:

A React library for building Airplane views. Views components are optimized in style and functionality to produce internal apps that are easy to build and maintain.

86 lines (85 loc) 2.87 kB
import { QueryObserverResult } from "@tanstack/react-query"; import { DefaultOutput, DefaultParams, ExecuteTaskError, ExecuteTaskSuccess, ParamValues } from "../../client/executeTask"; import { ExecuteError, TaskQuery } from "../../components/query"; export type UseTaskQueryOptions<TParams extends ParamValues | undefined = DefaultParams, TOutput = DefaultOutput> = { /** * The params of the task to execute. */ params?: TParams; /** * If set to true, the task will be executed automatically. * @default true */ enabled?: boolean; /** * If set, the task will be refetched every refetchInterval milliseconds. */ refetchInterval?: number; /** * If set, queries with identical inputs within the configured age * (in seconds) may get cached results. */ allowCachedMaxAge?: number; /** * If set to true, the task will be executed on mount. * @default true */ executeOnMount?: boolean; /** * If set to true, the task will be executed on window focus. * @default true */ executeOnWindowFocus?: boolean; /** * If set to true, the task will be executed on reconnect. * @default true */ executeOnReconnect?: boolean; /** * Callback on successful task execution. */ onSuccess?: (output: TOutput, runID: string) => void; /** * Callback on failed task execution. */ onError?: (output: TOutput | undefined, error: ExecuteError, runID?: string) => void; }; export type UseTaskQueryResult<TOutput = DefaultOutput> = { /** * The output of the last successfully executed task. */ output?: TOutput; /** * True when the task is executing for the first time. */ loading?: boolean; /** * True anytime the task is executing. This includes the first time the task is executed (loading = true) * and anytime the task is refetching. * * You usually want to use `loading` instead of `executing` unless you want to show an indicator when the task is refetching. */ executing?: boolean; /** * Will be set with the error message if the task failed to execute. */ error?: ExecuteError; /** * Will be set with a function that executes the task. */ refetch: () => Promise<QueryObserverResult<ExecuteTaskSuccess<TOutput>, ExecuteTaskError<TOutput>>>; /** * The ID of the run. */ runID?: string; }; /** * useTaskQuery executes a task. * * This should be used for tasks that queries for data. * * Additionally, useTaskQuery can: * - Cache the output * - Automatically refetch to keep the output up to date */ export declare const useTaskQuery: <TParams extends ParamValues | undefined = DefaultParams, TOutput = any>(query: TaskQuery<TParams, TOutput>) => UseTaskQueryResult<TOutput>;