import { SafeReturn } from 'p-safe'; type AsyncFunc = (...args: any[]) => Promise; interface UseActionOptions { /** * The initial result of the action. */ initialResult?: Awaited>; /** * A callback function that is called when an error occurs during the action execution. */ onError?: (error: E) => any; /** * Whether to ignore errors and not update the error state. */ ignoreErrors?: boolean; } interface Action { /** * The result of the most recent successful action execution. */ result: Awaited> | undefined; /** * The error that occurred during the most recent failed action execution. */ error: E | undefined; /** * A boolean indicating whether the action is currently in progress. */ isLoading: boolean; /** * Triggers the asynchronous action and manages the loading state. * * @param args - The arguments to be passed to the asynchronous function. * @returns A Promise that resolves with the safe result of the asynchronous function. */ dispatch: (...args: Parameters) => Promise>, E>>; /** * Resets the action state to its initial state. */ reset: () => void; } /** * A custom React hook that wraps an asynchronous action with loading state management. * * @template T - The type of the asynchronous function being wrapped. * @template E - The type of the error that might occur. * @param actionKey * @param {T} action - The asynchronous function to be wrapped. * @param options * @returns {Action} - An object containing the action state and methods to manage the action. * @example * export default function Page() { * const { result, error, isLoading, dispatch, reset } = useAction( * 'fetchUser', * async (id: number) => { * const response = await fetch(`https://api.example.com/users/${id}`); * return response.json(); * } * ); * * if (isLoading) { * return
Loading...
; * } * * if (error) { * return
Error: {error.message}
; * } * * return ( *
* {result &&
User: {result.name}
} * * *
* ); * } */ declare function useAction(actionKey: string, action: T, options?: UseActionOptions): Action; export { type Action, type UseActionOptions, useAction };