mirror of
https://github.com/docmost/docmost.git
synced 2026-05-13 02:34:05 +08:00
641ce142df
* SCIM - init (EE) * accept db transaction * sync * Content parser support for scim+json * patch scimmy * sync * return early if userIds is empty * sync * SCIM db table * fixes * scim tokens * backfill * feat(audit): add scim token events * rename scim migration * fix * fix translation * cleanup
102 lines
2.7 KiB
TypeScript
102 lines
2.7 KiB
TypeScript
import { IPagination, QueryParams } from "@/lib/types.ts";
|
|
import {
|
|
keepPreviousData,
|
|
useMutation,
|
|
useQuery,
|
|
useQueryClient,
|
|
UseQueryResult,
|
|
} from "@tanstack/react-query";
|
|
import {
|
|
createApiKey,
|
|
getApiKeys,
|
|
IApiKey,
|
|
ICreateApiKeyRequest,
|
|
IUpdateApiKeyRequest,
|
|
revokeApiKey,
|
|
updateApiKey,
|
|
} from "@/ee/api-key";
|
|
import { notifications } from "@mantine/notifications";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
export function useGetApiKeysQuery(
|
|
params?: QueryParams,
|
|
): UseQueryResult<IPagination<IApiKey>, Error> {
|
|
return useQuery({
|
|
queryKey: ["api-key-list", params],
|
|
queryFn: () => getApiKeys(params),
|
|
staleTime: 0,
|
|
gcTime: 0,
|
|
placeholderData: keepPreviousData,
|
|
});
|
|
}
|
|
|
|
export function useRevokeApiKeyMutation() {
|
|
const queryClient = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation<
|
|
void,
|
|
Error,
|
|
{
|
|
apiKeyId: string;
|
|
}
|
|
>({
|
|
mutationFn: (data) => revokeApiKey(data),
|
|
onSuccess: (data, variables) => {
|
|
notifications.show({ message: t("Revoked successfully") });
|
|
queryClient.invalidateQueries({
|
|
predicate: (item) =>
|
|
["api-key-list"].includes(item.queryKey[0] as string),
|
|
});
|
|
},
|
|
onError: (error) => {
|
|
const errorMessage = error["response"]?.data?.message;
|
|
notifications.show({ message: errorMessage, color: "red" });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useCreateApiKeyMutation() {
|
|
const queryClient = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation<IApiKey, Error, ICreateApiKeyRequest>({
|
|
mutationFn: (data) => createApiKey(data),
|
|
onSuccess: () => {
|
|
notifications.show({
|
|
message: t("{{credential}} created successfully", {
|
|
credential: t("API key"),
|
|
}),
|
|
});
|
|
queryClient.invalidateQueries({
|
|
predicate: (item) =>
|
|
["api-key-list"].includes(item.queryKey[0] as string),
|
|
});
|
|
},
|
|
onError: (error) => {
|
|
const errorMessage = error["response"]?.data?.message;
|
|
notifications.show({ message: errorMessage, color: "red" });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateApiKeyMutation() {
|
|
const queryClient = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation<IApiKey, Error, IUpdateApiKeyRequest>({
|
|
mutationFn: (data) => updateApiKey(data),
|
|
onSuccess: (data, variables) => {
|
|
notifications.show({ message: t("Updated successfully") });
|
|
queryClient.invalidateQueries({
|
|
predicate: (item) =>
|
|
["api-key-list"].includes(item.queryKey[0] as string),
|
|
});
|
|
},
|
|
onError: (error) => {
|
|
const errorMessage = error["response"]?.data?.message;
|
|
notifications.show({ message: errorMessage, color: "red" });
|
|
},
|
|
});
|
|
}
|