* Move sidebar pages from workspace to space level * Replace array sorting with lexicographical fractional indexing * Fixes and updates
99 lines
2.6 KiB
TypeScript
99 lines
2.6 KiB
TypeScript
import {
|
|
useInfiniteQuery,
|
|
useMutation,
|
|
useQuery,
|
|
UseQueryResult,
|
|
} from "@tanstack/react-query";
|
|
import {
|
|
createPage,
|
|
deletePage,
|
|
getPageById,
|
|
getSidebarPages,
|
|
getRecentChanges,
|
|
updatePage,
|
|
movePage,
|
|
} from "@/features/page/services/page-service";
|
|
import {
|
|
IMovePage,
|
|
IPage,
|
|
SidebarPagesParams,
|
|
} from "@/features/page/types/page.types";
|
|
import { notifications } from "@mantine/notifications";
|
|
import { IPagination } from "@/lib/types.ts";
|
|
|
|
const RECENT_CHANGES_KEY = ["recentChanges"];
|
|
|
|
export function usePageQuery(pageId: string): UseQueryResult<IPage, Error> {
|
|
return useQuery({
|
|
queryKey: ["pages", pageId],
|
|
queryFn: () => getPageById(pageId),
|
|
enabled: !!pageId,
|
|
staleTime: 5 * 60 * 1000,
|
|
});
|
|
}
|
|
|
|
export function useRecentChangesQuery(): UseQueryResult<IPage[], Error> {
|
|
return useQuery({
|
|
queryKey: RECENT_CHANGES_KEY,
|
|
queryFn: () => getRecentChanges(),
|
|
refetchOnMount: true,
|
|
});
|
|
}
|
|
|
|
export function useCreatePageMutation() {
|
|
return useMutation<IPage, Error, Partial<IPage>>({
|
|
mutationFn: (data) => createPage(data),
|
|
onSuccess: (data) => {},
|
|
onError: (error) => {
|
|
notifications.show({ message: "Failed to create page", color: "red" });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdatePageMutation() {
|
|
return useMutation<IPage, Error, Partial<IPage>>({
|
|
mutationFn: (data) => updatePage(data),
|
|
onSuccess: (data) => {},
|
|
});
|
|
}
|
|
|
|
export function useDeletePageMutation() {
|
|
return useMutation({
|
|
mutationFn: (pageId: string) => deletePage(pageId),
|
|
onSuccess: () => {
|
|
notifications.show({ message: "Page deleted successfully" });
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({ message: "Failed to delete page", color: "red" });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useMovePageMutation() {
|
|
return useMutation<void, Error, IMovePage>({
|
|
mutationFn: (data) => movePage(data),
|
|
});
|
|
}
|
|
|
|
export function useGetSidebarPagesQuery(
|
|
data: SidebarPagesParams,
|
|
): UseQueryResult<IPagination<IPage>, Error> {
|
|
return useQuery({
|
|
queryKey: ["sidebar-pages", data],
|
|
queryFn: () => getSidebarPages(data),
|
|
});
|
|
}
|
|
|
|
export function useGetRootSidebarPagesQuery(data: SidebarPagesParams) {
|
|
return useInfiniteQuery({
|
|
queryKey: ["root-sidebar-pages", data.spaceId],
|
|
queryFn: async ({ pageParam }) => {
|
|
return getSidebarPages({ spaceId: data.spaceId, page: pageParam });
|
|
},
|
|
initialPageParam: 1,
|
|
getPreviousPageParam: (firstPage) =>
|
|
firstPage.meta.hasPrevPage ? firstPage.meta.page - 1 : undefined,
|
|
getNextPageParam: (lastPage) =>
|
|
lastPage.meta.hasNextPage ? lastPage.meta.page + 1 : undefined,
|
|
});
|
|
}
|