Files
lincodex/frontend/src/features/sign-out/model/use-sign-out.ts
T

31 lines
1.2 KiB
TypeScript

import { useMutation, useQueryClient } from "@tanstack/react-query";
import { getGetCurrentSessionQueryKey } from "../../../shared/api/endpoints/identity/identity";
import type { Session } from "../../../shared/api/schemas";
import { paths } from "../../../shared/config";
import { requestSignOut } from "./request-sign-out";
/**
* Sign-out command for a cabinet shell. On success the page is replaced by a
* full load of the sign-in screen: that drops every in-memory copy of the
* account's data (the query cache, form drafts), which a client-side
* navigation would keep until the cache is cleared piece by piece. The CSRF
* token is read from the cached session when the command runs, so the shell
* does not subscribe to the session query.
*/
export function useSignOut() {
const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: () =>
requestSignOut(queryClient.getQueryData<Session>(getGetCurrentSessionQueryKey())),
onSuccess: () => {
window.location.assign(paths.login);
},
});
return {
signOut: () => mutation.mutate(),
isPending: mutation.isPending || mutation.isSuccess,
failed: mutation.isError,
};
}