mirror of
https://github.com/docmost/docmost.git
synced 2026-08-30 18:36:26 +08:00
fix: reactive subpage list
This commit is contained in:
@@ -333,6 +333,22 @@ export function useDeletedPagesQuery(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getChildrenCacheKeys(
|
||||||
|
parentPageId: string | null,
|
||||||
|
spaceId: string,
|
||||||
|
): QueryKey[] {
|
||||||
|
if (parentPageId === null) {
|
||||||
|
return [["root-sidebar-pages", spaceId]];
|
||||||
|
}
|
||||||
|
return queryClient
|
||||||
|
.getQueriesData({
|
||||||
|
predicate: (query) =>
|
||||||
|
query.queryKey[0] === "sidebar-pages" &&
|
||||||
|
(query.queryKey[1] as { pageId?: string })?.pageId === parentPageId,
|
||||||
|
})
|
||||||
|
.map(([key]) => key);
|
||||||
|
}
|
||||||
|
|
||||||
export function invalidateOnCreatePage(data: Partial<IPage>) {
|
export function invalidateOnCreatePage(data: Partial<IPage>) {
|
||||||
const newPage: Partial<IPage> = {
|
const newPage: Partial<IPage> = {
|
||||||
creatorId: data.creatorId,
|
creatorId: data.creatorId,
|
||||||
@@ -346,17 +362,8 @@ export function invalidateOnCreatePage(data: Partial<IPage>) {
|
|||||||
title: data.title,
|
title: data.title,
|
||||||
};
|
};
|
||||||
|
|
||||||
let queryKey: QueryKey = null;
|
|
||||||
if (data.parentPageId === null) {
|
|
||||||
queryKey = ["root-sidebar-pages", data.spaceId];
|
|
||||||
} else {
|
|
||||||
queryKey = [
|
|
||||||
"sidebar-pages",
|
|
||||||
{ pageId: data.parentPageId, spaceId: data.spaceId },
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
//update all sidebar pages
|
//update all sidebar pages
|
||||||
|
getChildrenCacheKeys(data.parentPageId, data.spaceId).forEach((queryKey) => {
|
||||||
queryClient.setQueryData<InfiniteData<IPagination<Partial<IPage>>>>(
|
queryClient.setQueryData<InfiniteData<IPagination<Partial<IPage>>>>(
|
||||||
queryKey,
|
queryKey,
|
||||||
(old) => {
|
(old) => {
|
||||||
@@ -375,6 +382,7 @@ export function invalidateOnCreatePage(data: Partial<IPage>) {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
//update sidebar haschildren
|
//update sidebar haschildren
|
||||||
if (data.parentPageId !== null) {
|
if (data.parentPageId !== null) {
|
||||||
@@ -438,13 +446,8 @@ export function invalidateOnUpdatePage(
|
|||||||
title: string,
|
title: string,
|
||||||
icon: string,
|
icon: string,
|
||||||
) {
|
) {
|
||||||
let queryKey: QueryKey = null;
|
|
||||||
if (parentPageId === null) {
|
|
||||||
queryKey = ["root-sidebar-pages", spaceId];
|
|
||||||
} else {
|
|
||||||
queryKey = ["sidebar-pages", { pageId: parentPageId, spaceId: spaceId }];
|
|
||||||
}
|
|
||||||
//update all sidebar pages
|
//update all sidebar pages
|
||||||
|
getChildrenCacheKeys(parentPageId, spaceId).forEach((queryKey) => {
|
||||||
queryClient.setQueryData<InfiniteData<IPagination<IPage>>>(
|
queryClient.setQueryData<InfiniteData<IPagination<IPage>>>(
|
||||||
queryKey,
|
queryKey,
|
||||||
(old) => {
|
(old) => {
|
||||||
@@ -466,6 +469,7 @@ export function invalidateOnUpdatePage(
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
//update recent changes
|
//update recent changes
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
@@ -481,11 +485,7 @@ export function updateCacheOnMovePage(
|
|||||||
pageData: Partial<IPage>,
|
pageData: Partial<IPage>,
|
||||||
) {
|
) {
|
||||||
// Remove page from old parent's cache
|
// Remove page from old parent's cache
|
||||||
const oldQueryKey =
|
getChildrenCacheKeys(oldParentId, spaceId).forEach((oldQueryKey) => {
|
||||||
oldParentId === null
|
|
||||||
? ["root-sidebar-pages", spaceId]
|
|
||||||
: ["sidebar-pages", { pageId: oldParentId, spaceId }];
|
|
||||||
|
|
||||||
queryClient.setQueryData<InfiniteData<IPagination<IPage>>>(
|
queryClient.setQueryData<InfiniteData<IPagination<IPage>>>(
|
||||||
oldQueryKey,
|
oldQueryKey,
|
||||||
(old) => {
|
(old) => {
|
||||||
@@ -499,6 +499,7 @@ export function updateCacheOnMovePage(
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// Update old parent's hasChildren flag if it has no more children
|
// Update old parent's hasChildren flag if it has no more children
|
||||||
if (oldParentId !== null) {
|
if (oldParentId !== null) {
|
||||||
@@ -540,11 +541,7 @@ export function updateCacheOnMovePage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add page to new parent's cache
|
// Add page to new parent's cache
|
||||||
const newQueryKey =
|
getChildrenCacheKeys(newParentId, spaceId).forEach((newQueryKey) => {
|
||||||
newParentId === null
|
|
||||||
? ["root-sidebar-pages", spaceId]
|
|
||||||
: ["sidebar-pages", { pageId: newParentId, spaceId }];
|
|
||||||
|
|
||||||
queryClient.setQueryData<InfiniteData<IPagination<Partial<IPage>>>>(
|
queryClient.setQueryData<InfiniteData<IPagination<Partial<IPage>>>>(
|
||||||
newQueryKey,
|
newQueryKey,
|
||||||
(old) => {
|
(old) => {
|
||||||
@@ -570,6 +567,7 @@ export function updateCacheOnMovePage(
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// Update new parent's hasChildren flag
|
// Update new parent's hasChildren flag
|
||||||
if (newParentId !== null) {
|
if (newParentId !== null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user