diff options
| author | Dhravya Shah <[email protected]> | 2025-09-13 22:09:46 -0700 |
|---|---|---|
| committer | Dhravya Shah <[email protected]> | 2025-09-13 22:09:46 -0700 |
| commit | 8de2099e148ae0928a47d48d76479865ecf17f74 (patch) | |
| tree | f20ef0dd4613e06df4782373d39903e6732cd155 /packages | |
| parent | update: Readme (diff) | |
| parent | fix: autumn caching issue (#421) (diff) | |
| download | supermemory-8de2099e148ae0928a47d48d76479865ecf17f74.tar.xz supermemory-8de2099e148ae0928a47d48d76479865ecf17f74.zip | |
Merge branch 'main' of https://github.com/supermemoryai/supermemory
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/lib/queries.ts | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/packages/lib/queries.ts b/packages/lib/queries.ts index 36338445..0902f378 100644 --- a/packages/lib/queries.ts +++ b/packages/lib/queries.ts @@ -1,5 +1,12 @@ -import { useQuery } from "@tanstack/react-query" +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import type { useCustomer } from "autumn-js/react" +import { $fetch } from "./api" +import { toast } from "sonner" +import type { DocumentsWithMemoriesResponseSchema } from "../validation/api" +import type { z } from "zod" + +type DocumentsResponse = z.infer<typeof DocumentsWithMemoriesResponseSchema> +type DocumentWithMemories = DocumentsResponse["documents"][0] export const fetchSubscriptionStatus = ( autumn: ReturnType<typeof useCustomer>, @@ -84,3 +91,68 @@ export const fetchProProduct = (autumn: ReturnType<typeof useCustomer>) => staleTime: 30 * 1000, // 30 seconds gcTime: 5 * 60 * 1000, // 5 minutes }) + +export const useDeleteDocument = (selectedProject: string) => { + const queryClient = useQueryClient() + + return useMutation({ + mutationFn: async (documentId: string) => { + // context for LLM: delete/memories/:documentId is documents delete endpoint not memories delete endpoint + const response = await $fetch(`@delete/memories/${documentId}`) + if (response.error) { + throw new Error(response.error?.message || "Failed to delete document") + } + return response.data + }, + onMutate: async (documentId: string) => { + await queryClient.cancelQueries({ + queryKey: ["documents-with-memories", selectedProject], + }) + + const previousData = queryClient.getQueryData([ + "documents-with-memories", + selectedProject, + ]) + + queryClient.setQueryData( + ["documents-with-memories", selectedProject], + (old: unknown) => { + if (!old || typeof old !== "object") return old + const typedOld = old as { + pages?: Array<{ documents?: DocumentWithMemories[] }> + } + return { + ...typedOld, + pages: typedOld.pages?.map((page) => ({ + ...page, + documents: page.documents?.filter( + (doc: DocumentWithMemories) => doc.id !== documentId, + ), + })), + } + }, + ) + + return { previousData } + }, + onSuccess: () => { + toast.success("Memory deleted successfully") + }, + onError: (error, _documentId, context) => { + if (context?.previousData) { + queryClient.setQueryData( + ["documents-with-memories", selectedProject], + context.previousData, + ) + } + toast.error("Failed to delete memory", { + description: error instanceof Error ? error.message : "Unknown error", + }) + }, + onSettled: () => { + queryClient.invalidateQueries({ + queryKey: ["documents-with-memories", selectedProject], + }) + }, + }) +} |