mirror of
https://github.com/docmost/docmost.git
synced 2026-07-23 17:42:47 +10:00
803f1f0b81
* user session management * WIP * cleanup * license * cleanup * don't cache index * rename current device property * fix
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import {
|
|
useMutation,
|
|
useQuery,
|
|
useQueryClient,
|
|
UseQueryResult,
|
|
} from "@tanstack/react-query";
|
|
import {
|
|
getSessions,
|
|
revokeSession,
|
|
revokeAllSessions,
|
|
} from "@/features/session/services/session-service";
|
|
import { ISession } from "@/features/session/types/session.types";
|
|
import { notifications } from "@mantine/notifications";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
export function useGetSessionsQuery(): UseQueryResult<ISession[], Error> {
|
|
return useQuery({
|
|
queryKey: ["session-list"],
|
|
queryFn: () => getSessions(),
|
|
});
|
|
}
|
|
|
|
export function useRevokeSessionMutation() {
|
|
const queryClient = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation<void, Error, { sessionId: string }>({
|
|
mutationFn: (data) => revokeSession(data),
|
|
onSuccess: () => {
|
|
notifications.show({ message: t("Session revoked") });
|
|
queryClient.invalidateQueries({ queryKey: ["session-list"] });
|
|
},
|
|
onError: (error) => {
|
|
const errorMessage = error["response"]?.data?.message;
|
|
notifications.show({ message: errorMessage, color: "red" });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useRevokeAllSessionsMutation() {
|
|
const queryClient = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation<void, Error, void>({
|
|
mutationFn: () => revokeAllSessions(),
|
|
onSuccess: () => {
|
|
notifications.show({ message: t("All other sessions revoked") });
|
|
queryClient.invalidateQueries({ queryKey: ["session-list"] });
|
|
},
|
|
onError: (error) => {
|
|
const errorMessage = error["response"]?.data?.message;
|
|
notifications.show({ message: errorMessage, color: "red" });
|
|
},
|
|
});
|
|
}
|