fix base cache

This commit is contained in:
Philipinho
2026-06-15 22:07:50 +01:00
parent 398bd499cc
commit e9e6fba886
3 changed files with 23 additions and 28 deletions
@@ -15,6 +15,7 @@ import {
CreateBaseInput,
UpdateBaseInput,
} from "@/ee/base/types/base.types";
import { IPage } from "@/features/page/types/page.types";
import { notifications } from "@mantine/notifications";
import { queryClient } from "@/main";
import { useTranslation } from "react-i18next";
@@ -62,6 +63,10 @@ export function useConvertPageToBaseMutation() {
return useMutation<IBase, Error, { pageId: string; template?: "kanban" }>({
mutationFn: ({ pageId, template }) => convertPageToBase(pageId, template),
onSuccess: (base) => {
const markAsBase = (old?: IPage) => (old ? { ...old, isBase: true } : old);
queryClient.setQueryData<IPage>(["pages", base.id], markAsBase);
queryClient.setQueryData<IPage>(["pages", base.slugId], markAsBase);
queryClient.invalidateQueries({ queryKey: ["pages"] });
queryClient.invalidateQueries({
queryKey: ["root-sidebar-pages", base.spaceId],
@@ -75,7 +80,7 @@ export function useConvertPageToBaseMutation() {
spaceId: base.spaceId,
entity: ["pages"],
id: base.id,
payload: { isBase: true },
payload: { isBase: true, slugId: base.slugId },
});
},
onError: (error) => {
@@ -235,6 +235,7 @@ export type IBaseView = {
export type IBase = {
id: string;
slugId: string;
name: string;
description?: string;
icon?: string;
@@ -23,7 +23,6 @@ export const useQuerySubscription = () => {
const data: WebSocketEvent = event;
let entity = null;
let queryKeyId = null;
switch (data.operation) {
case "invalidate":
@@ -106,21 +105,23 @@ export const useQuerySubscription = () => {
case "deleteTreeNode":
invalidateOnDeletePage(data.payload.node.id);
break;
case "updateOne":
case "updateOne": {
entity = data.entity[0];
if (entity === "pages") {
// we have to do this because the usePageQuery cache key is the slugId.
queryKeyId = data.payload.slugId;
} else {
queryKeyId = data.id;
}
const keyIds =
entity === "pages" ? [data.payload.slugId, data.id] : [data.id];
// only update if data was already in cache
if (queryClient.getQueryData([...data.entity, queryKeyId])) {
queryClient.setQueryData([...data.entity, queryKeyId], {
...queryClient.getQueryData([...data.entity, queryKeyId]),
...data.payload,
});
for (const keyId of keyIds) {
if (!keyId) continue;
const cached = queryClient.getQueryData<Record<string, unknown>>([
...data.entity,
keyId,
]);
if (cached) {
queryClient.setQueryData([...data.entity, keyId], {
...cached,
...data.payload,
});
}
}
if (entity === "pages") {
@@ -132,20 +133,8 @@ export const useQuerySubscription = () => {
data.payload.icon,
);
}
/*
queryClient.setQueriesData(
{ queryKey: [data.entity, data.id] },
(oldData: any) => {
const update = (entity: Record<string, unknown>) =>
entity.id === data.id ? { ...entity, ...data.payload } : entity;
return Array.isArray(oldData)
? oldData.map(update)
: update(oldData as Record<string, unknown>);
},
);
*/
break;
}
case "refetchRootTreeNodeEvent": {
const spaceId = data.spaceId;
queryClient.refetchQueries({