mirror of
https://github.com/docmost/docmost.git
synced 2026-07-25 16:14:44 +10:00
fix base cache
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
|||||||
CreateBaseInput,
|
CreateBaseInput,
|
||||||
UpdateBaseInput,
|
UpdateBaseInput,
|
||||||
} from "@/ee/base/types/base.types";
|
} from "@/ee/base/types/base.types";
|
||||||
|
import { IPage } from "@/features/page/types/page.types";
|
||||||
import { notifications } from "@mantine/notifications";
|
import { notifications } from "@mantine/notifications";
|
||||||
import { queryClient } from "@/main";
|
import { queryClient } from "@/main";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
@@ -62,6 +63,10 @@ export function useConvertPageToBaseMutation() {
|
|||||||
return useMutation<IBase, Error, { pageId: string; template?: "kanban" }>({
|
return useMutation<IBase, Error, { pageId: string; template?: "kanban" }>({
|
||||||
mutationFn: ({ pageId, template }) => convertPageToBase(pageId, template),
|
mutationFn: ({ pageId, template }) => convertPageToBase(pageId, template),
|
||||||
onSuccess: (base) => {
|
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: ["pages"] });
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
queryKey: ["root-sidebar-pages", base.spaceId],
|
queryKey: ["root-sidebar-pages", base.spaceId],
|
||||||
@@ -75,7 +80,7 @@ export function useConvertPageToBaseMutation() {
|
|||||||
spaceId: base.spaceId,
|
spaceId: base.spaceId,
|
||||||
entity: ["pages"],
|
entity: ["pages"],
|
||||||
id: base.id,
|
id: base.id,
|
||||||
payload: { isBase: true },
|
payload: { isBase: true, slugId: base.slugId },
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
|
|||||||
@@ -235,6 +235,7 @@ export type IBaseView = {
|
|||||||
|
|
||||||
export type IBase = {
|
export type IBase = {
|
||||||
id: string;
|
id: string;
|
||||||
|
slugId: string;
|
||||||
name: string;
|
name: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
icon?: string;
|
icon?: string;
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ export const useQuerySubscription = () => {
|
|||||||
const data: WebSocketEvent = event;
|
const data: WebSocketEvent = event;
|
||||||
|
|
||||||
let entity = null;
|
let entity = null;
|
||||||
let queryKeyId = null;
|
|
||||||
|
|
||||||
switch (data.operation) {
|
switch (data.operation) {
|
||||||
case "invalidate":
|
case "invalidate":
|
||||||
@@ -106,21 +105,23 @@ export const useQuerySubscription = () => {
|
|||||||
case "deleteTreeNode":
|
case "deleteTreeNode":
|
||||||
invalidateOnDeletePage(data.payload.node.id);
|
invalidateOnDeletePage(data.payload.node.id);
|
||||||
break;
|
break;
|
||||||
case "updateOne":
|
case "updateOne": {
|
||||||
entity = data.entity[0];
|
entity = data.entity[0];
|
||||||
if (entity === "pages") {
|
const keyIds =
|
||||||
// we have to do this because the usePageQuery cache key is the slugId.
|
entity === "pages" ? [data.payload.slugId, data.id] : [data.id];
|
||||||
queryKeyId = data.payload.slugId;
|
|
||||||
} else {
|
|
||||||
queryKeyId = data.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// only update if data was already in cache
|
for (const keyId of keyIds) {
|
||||||
if (queryClient.getQueryData([...data.entity, queryKeyId])) {
|
if (!keyId) continue;
|
||||||
queryClient.setQueryData([...data.entity, queryKeyId], {
|
const cached = queryClient.getQueryData<Record<string, unknown>>([
|
||||||
...queryClient.getQueryData([...data.entity, queryKeyId]),
|
...data.entity,
|
||||||
...data.payload,
|
keyId,
|
||||||
});
|
]);
|
||||||
|
if (cached) {
|
||||||
|
queryClient.setQueryData([...data.entity, keyId], {
|
||||||
|
...cached,
|
||||||
|
...data.payload,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entity === "pages") {
|
if (entity === "pages") {
|
||||||
@@ -132,20 +133,8 @@ export const useQuerySubscription = () => {
|
|||||||
data.payload.icon,
|
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;
|
break;
|
||||||
|
}
|
||||||
case "refetchRootTreeNodeEvent": {
|
case "refetchRootTreeNodeEvent": {
|
||||||
const spaceId = data.spaceId;
|
const spaceId = data.spaceId;
|
||||||
queryClient.refetchQueries({
|
queryClient.refetchQueries({
|
||||||
|
|||||||
Reference in New Issue
Block a user