import { notifyManager, replaceEqualDeep } from '@tanstack/query-core' import { useCallback, useState } from 'preact/hooks' import { useQueryClient } from './QueryClientProvider' import { useSyncExternalStore } from './utils' import type { Mutation, MutationCache, MutationFilters, MutationState, QueryClient, } from '@tanstack/query-core' /** * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending` * (useful for app-wide loading indicators). * @param filters - The {@link MutationFilters} to narrow down the matched mutations. * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will * be used. * @returns Will be the `number` of the mutations that your application currently has `pending`. * @example * ```tsx * import { useIsMutating } from '@tanstack/preact-query' * * function PostsMutatingIndicator() { * // How many mutations matching the posts prefix are in progress? * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] }) * * return isMutatingPosts ? Saving posts... : null * } * ``` */ export function useIsMutating( filters?: Omit, queryClient?: QueryClient, ): number { const client = useQueryClient(queryClient) return useMutationState( { filters: { ...filters, status: 'pending' } }, client, ).length } type MutationTypeFromResult = [TResult] extends [ MutationState< infer TData, infer TError, infer TVariables, infer TOnMutateResult >, ] ? Mutation : Mutation type MutationStateOptions< TResult = MutationState, TMutation extends Mutation = MutationTypeFromResult, > = { filters?: MutationFilters select?: (mutation: TMutation) => TResult } /** * Creates snapshot getters that share equal selected results across renders. * A new getter is created on each render. Each getter caches its snapshot until the matching mutations * or their states change. * @returns A factory for snapshot getters with a shared result cache. */ function createSnapshotFactory< TResult = MutationState, TMutation extends Mutation = MutationTypeFromResult, >() { let previousResult: Array | undefined return ( mutationCache: MutationCache, options: MutationStateOptions, ) => { let inputs: Array = [] let snapshot: Array | undefined return (): Array => { const mutations = mutationCache.findAll(options.filters) const nextInputs = mutations.flatMap((mutation) => [ mutation, mutation.state, ]) if ( !snapshot || nextInputs.length !== inputs.length || nextInputs.some((input, index) => !Object.is(input, inputs[index])) ) { inputs = nextInputs snapshot = replaceEqualDeep( previousResult, mutations.map( (mutation): TResult => (options.select ? options.select(mutation as TMutation) : mutation.state) as TResult, ), ) previousResult = snapshot } return snapshot } } } /** * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation * state. * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the * mutation state. * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will * be used. * @returns Will be an Array of whatever `select` returns for each matching mutation. * @example * Get all variables of all running mutations: * ```tsx * import { useMutationState } from '@tanstack/preact-query' * * function PendingPosts() { * const pendingVariables = useMutationState({ * filters: { status: 'pending' }, * select: (mutation) => mutation.state.variables, * }) * * return <>{pendingVariables.length} posts saving... * } * ``` * @example * Get all data for specific mutations via the `mutationKey`: * ```tsx * import { useMutation, useMutationState } from '@tanstack/preact-query' * * const mutationKey = ['posts'] * * function Posts() { * // Some mutation that we want to get the state for * const mutation = useMutation({ * mutationKey, * mutationFn: createPosts, * }) * * const savedPosts = useMutationState({ * // this mutation key needs to match the mutation key of the given mutation (see above) * filters: { mutationKey, status: 'success' }, * select: (mutation) => mutation.state.data, * }) * * return ( * * ) * } * ``` * @example * Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new * entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the * last item that `useMutationState` returns to get the latest successful invocation: * ```tsx * import { useMutationState } from '@tanstack/preact-query' * * function LatestPost() { * const savedPosts = useMutationState({ * filters: { mutationKey: ['posts'], status: 'success' }, * select: (mutation) => mutation.state.data, * }) * * const latestSavedPost = savedPosts[savedPosts.length - 1] * * return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'} * } * ``` */ export function useMutationState< TResult = MutationState, TMutation extends Mutation = MutationTypeFromResult, >( options: MutationStateOptions = {}, queryClient?: QueryClient, ): Array { const mutationCache = useQueryClient(queryClient).getMutationCache() const [createSnapshot] = useState(() => createSnapshotFactory(), ) const getSnapshot = createSnapshot(mutationCache, options) return useSyncExternalStore( useCallback( (onStoreChange) => mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)), [mutationCache], ), getSnapshot, ) }