import { useMutation } from "@tanstack/react-query"; import { createView, updateView, deleteView, } from "@/ee/base/services/base-service"; import { IBase, IBaseView, CreateViewInput, UpdateViewInput, DeleteViewInput, ViewConfig, ViewConfigPatch, } from "@/ee/base/types/base.types"; function applyConfigPatch( existing: ViewConfig | undefined, patch: ViewConfigPatch | undefined, ): ViewConfig { const merged: Record = { ...(existing ?? {}) }; for (const [key, value] of Object.entries(patch ?? {})) { if (value === null) delete merged[key]; else if (value !== undefined) merged[key] = value; } return merged as ViewConfig; } import { notifications } from "@mantine/notifications"; import { queryClient } from "@/main"; import { useTranslation } from "react-i18next"; export function useCreateViewMutation() { const { t } = useTranslation(); return useMutation({ mutationFn: (data) => createView(data), onSuccess: (newView) => { queryClient.setQueryData( ["bases", newView.pageId], (old) => { if (!old) return old; return { ...old, views: [...old.views, newView], }; }, ); }, onError: () => { notifications.show({ message: t("Failed to create view"), color: "red", }); }, }); } export function useUpdateViewMutation() { const { t } = useTranslation(); return useMutation({ mutationFn: (data) => updateView(data), onMutate: async (variables) => { await queryClient.cancelQueries({ queryKey: ["bases", variables.pageId], }); const previous = queryClient.getQueryData([ "bases", variables.pageId, ]); queryClient.setQueryData( ["bases", variables.pageId], (old) => { if (!old) return old; return { ...old, views: old.views.map((v) => v.id === variables.viewId ? { ...v, ...(variables.name !== undefined && { name: variables.name, }), ...(variables.type !== undefined && { type: variables.type, }), ...(variables.config !== undefined && { config: applyConfigPatch(v.config, variables.config), }), ...(variables.position !== undefined && { position: variables.position, }), } : v, ), }; }, ); return { previous }; }, onError: (_, variables, context) => { if (context?.previous) { queryClient.setQueryData( ["bases", variables.pageId], context.previous, ); } notifications.show({ message: t("Failed to update view"), color: "red", }); }, onSuccess: (updatedView) => { queryClient.setQueryData( ["bases", updatedView.pageId], (old) => { if (!old) return old; return { ...old, views: old.views.map((v) => v.id === updatedView.id ? updatedView : v, ), }; }, ); }, }); } export function useDeleteViewMutation() { const { t } = useTranslation(); return useMutation({ mutationFn: (data) => deleteView(data), onSuccess: (_, variables) => { queryClient.setQueryData( ["bases", variables.pageId], (old) => { if (!old) return old; return { ...old, views: old.views.filter((v) => v.id !== variables.viewId), }; }, ); }, onError: () => { notifications.show({ message: t("Failed to delete view"), color: "red", }); }, }); }