import { useQuery } from "@tanstack/react-query"; import { getMemoryUnit } from "@/services/memory"; import { queryKeys } from "@/lib/query-keys"; import { hasPreciseScope } from "@/lib/memory"; import { useRuntimeContext } from "@/providers/runtime-context-provider"; import type { MemoryScope } from "@/types/memory"; export function useMemoryUnit( memoryStoreName: string, memoryId: string | undefined, scope: MemoryScope | null, enabled = true, ) { const { context, ready } = useRuntimeContext(); const exactScope = scope && hasPreciseScope(scope) ? scope : null; return useQuery({ queryKey: queryKeys.memoryUnit( context.dataScopeKey, memoryStoreName, memoryId, exactScope, ), queryFn: () => getMemoryUnit({ memoryStoreName, memoryId: memoryId!, ...(exactScope ? { scope: exactScope } : {}), }), enabled: ready && enabled && !!memoryStoreName && !!memoryId && memoryId !== "-", }); }