mirror of
https://github.com/docmost/docmost.git
synced 2026-07-25 10:44:42 +10:00
4e5bff6d55
Table and kanban UI, formula engine package, and the base-embed editor extension
127 lines
3.5 KiB
TypeScript
127 lines
3.5 KiB
TypeScript
import {
|
|
useMutation,
|
|
useQuery,
|
|
UseQueryResult,
|
|
} from "@tanstack/react-query";
|
|
import {
|
|
createBase,
|
|
getBaseInfo,
|
|
updateBase,
|
|
deleteBase,
|
|
convertPageToBase,
|
|
} from "@/ee/base/services/base-service";
|
|
import {
|
|
IBase,
|
|
CreateBaseInput,
|
|
UpdateBaseInput,
|
|
} from "@/ee/base/types/base.types";
|
|
import { notifications } from "@mantine/notifications";
|
|
import { queryClient } from "@/main";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useAtom } from "jotai";
|
|
import { treeDataAtom } from "@/features/page/tree/atoms/tree-data-atom";
|
|
import { treeModel } from "@/features/page/tree/model/tree-model";
|
|
import { SpaceTreeNode } from "@/features/page/tree/types";
|
|
import { socketAtom } from "@/features/websocket/atoms/socket-atom";
|
|
|
|
export function useBaseQuery(
|
|
pageId: string | undefined,
|
|
): UseQueryResult<IBase, Error> {
|
|
return useQuery({
|
|
queryKey: ["bases", pageId],
|
|
queryFn: () => getBaseInfo(pageId!),
|
|
enabled: !!pageId,
|
|
staleTime: 5 * 60 * 1000,
|
|
});
|
|
}
|
|
|
|
export function useCreateBaseMutation() {
|
|
const { t } = useTranslation();
|
|
return useMutation<IBase, Error, CreateBaseInput>({
|
|
mutationFn: (data) => createBase(data),
|
|
onSuccess: (data) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: ["bases", "list", data.spaceId],
|
|
});
|
|
},
|
|
onError: () => {
|
|
notifications.show({
|
|
message: t("Failed to create base"),
|
|
color: "red",
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useConvertPageToBaseMutation() {
|
|
const { t } = useTranslation();
|
|
const [, setTreeData] = useAtom(treeDataAtom);
|
|
const [socket] = useAtom(socketAtom);
|
|
|
|
return useMutation<IBase, Error, { pageId: string; template?: "kanban" }>({
|
|
mutationFn: ({ pageId, template }) => convertPageToBase(pageId, template),
|
|
onSuccess: (base) => {
|
|
queryClient.invalidateQueries({ queryKey: ["pages"] });
|
|
queryClient.invalidateQueries({
|
|
queryKey: ["root-sidebar-pages", base.spaceId],
|
|
});
|
|
queryClient.invalidateQueries({ queryKey: ["sidebar-pages"] });
|
|
setTreeData((prev) =>
|
|
treeModel.update(prev, base.id, { isBase: true } as Partial<SpaceTreeNode>),
|
|
);
|
|
socket?.emit("message", {
|
|
operation: "updateOne",
|
|
spaceId: base.spaceId,
|
|
entity: ["pages"],
|
|
id: base.id,
|
|
payload: { isBase: true },
|
|
});
|
|
},
|
|
onError: () => {
|
|
notifications.show({
|
|
message: t("Failed to create base"),
|
|
color: "red",
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateBaseMutation() {
|
|
const { t } = useTranslation();
|
|
return useMutation<IBase, Error, UpdateBaseInput>({
|
|
mutationFn: (data) => updateBase(data),
|
|
onSuccess: (data) => {
|
|
queryClient.setQueryData<IBase>(["bases", data.id], (old) => {
|
|
if (!old) return old;
|
|
return { ...old, ...data };
|
|
});
|
|
},
|
|
onError: () => {
|
|
notifications.show({
|
|
message: t("Failed to update base"),
|
|
color: "red",
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteBaseMutation() {
|
|
const { t } = useTranslation();
|
|
return useMutation<void, Error, { pageId: string; spaceId: string }>({
|
|
mutationFn: ({ pageId }) => deleteBase(pageId),
|
|
onSuccess: (_, { pageId, spaceId }) => {
|
|
queryClient.removeQueries({ queryKey: ["bases", pageId] });
|
|
queryClient.invalidateQueries({
|
|
queryKey: ["bases", "list", spaceId],
|
|
});
|
|
notifications.show({ message: t("Base deleted") });
|
|
},
|
|
onError: () => {
|
|
notifications.show({
|
|
message: t("Failed to delete base"),
|
|
color: "red",
|
|
});
|
|
},
|
|
});
|
|
}
|