fc7de7fffe
* feat: support manual add and edit for memory facts * fix: restore memory updater save helper * fix: address memory fact review feedback * fix: remove duplicate memory fact edit action * docs: simplify memory fact review setup * docs: relax memory review startup instructions * fix: clear rebase marker in memory settings page * fix: address memory fact review and format issues * fix: address memory fact review feedback * refactor: make memory fact updates explicit patch semantics --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
73 lines
1.6 KiB
TypeScript
73 lines
1.6 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import {
|
|
clearMemory,
|
|
createMemoryFact,
|
|
deleteMemoryFact,
|
|
loadMemory,
|
|
updateMemoryFact,
|
|
} from "./api";
|
|
import type {
|
|
MemoryFactInput,
|
|
MemoryFactPatchInput,
|
|
UserMemory,
|
|
} from "./types";
|
|
|
|
export function useMemory() {
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ["memory"],
|
|
queryFn: () => loadMemory(),
|
|
});
|
|
return { memory: data ?? null, isLoading, error };
|
|
}
|
|
|
|
export function useClearMemory() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: () => clearMemory(),
|
|
onSuccess: (memory) => {
|
|
queryClient.setQueryData<UserMemory>(["memory"], memory);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteMemoryFact() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (factId: string) => deleteMemoryFact(factId),
|
|
onSuccess: (memory) => {
|
|
queryClient.setQueryData<UserMemory>(["memory"], memory);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useCreateMemoryFact() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (input: MemoryFactInput) => createMemoryFact(input),
|
|
onSuccess: (memory) => {
|
|
queryClient.setQueryData<UserMemory>(["memory"], memory);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateMemoryFact() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
factId,
|
|
input,
|
|
}: {
|
|
factId: string;
|
|
input: MemoryFactPatchInput;
|
|
}) => updateMemoryFact(factId, input),
|
|
onSuccess: (memory) => {
|
|
queryClient.setQueryData<UserMemory>(["memory"], memory);
|
|
},
|
|
});
|
|
}
|