2023-06-30 14:17:38 +03:00
|
|
|
import { useState } from "react";
|
2023-08-07 15:13:41 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-06-30 14:17:38 +03:00
|
|
|
|
|
|
|
import { useSupabase } from "@/lib/context/SupabaseProvider";
|
|
|
|
import { useToast } from "@/lib/hooks";
|
2023-09-18 16:12:50 +03:00
|
|
|
import { useEventTracking } from "@/services/analytics/june/useEventTracking";
|
2023-06-30 14:17:38 +03:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2023-10-30 16:52:47 +03:00
|
|
|
export const useLogoutModal = () => {
|
2023-06-30 14:17:38 +03:00
|
|
|
const { supabase } = useSupabase();
|
2023-10-30 16:52:47 +03:00
|
|
|
const [isLoggingOut, setIsLoggingOut] = useState(false);
|
|
|
|
const [isLogoutModalOpened, setIsLogoutModalOpened] = useState(false);
|
2023-06-30 14:17:38 +03:00
|
|
|
const { track } = useEventTracking();
|
|
|
|
|
2023-08-07 15:13:41 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2023-10-30 16:52:47 +03:00
|
|
|
const { t } = useTranslation(["translation", "logout"]);
|
2023-08-07 15:13:41 +03:00
|
|
|
|
2023-06-30 14:17:38 +03:00
|
|
|
const { publish } = useToast();
|
|
|
|
|
|
|
|
const handleLogout = async () => {
|
2023-10-30 16:52:47 +03:00
|
|
|
setIsLoggingOut(true);
|
2023-06-30 14:17:38 +03:00
|
|
|
const { error } = await supabase.auth.signOut();
|
|
|
|
void track("LOGOUT");
|
2023-07-18 19:28:44 +03:00
|
|
|
localStorage.clear();
|
|
|
|
|
2023-06-30 14:17:38 +03:00
|
|
|
if (error) {
|
|
|
|
console.error("Error logging out:", error.message);
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
2023-09-18 16:12:50 +03:00
|
|
|
text: t("error", { errorMessage: error.message, ns: "logout" }),
|
2023-06-30 14:17:38 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
publish({
|
|
|
|
variant: "success",
|
2023-09-18 16:12:50 +03:00
|
|
|
text: t("loggedOut", { ns: "logout" }),
|
2023-06-30 14:17:38 +03:00
|
|
|
});
|
2024-02-07 03:05:07 +03:00
|
|
|
window.location.href = "/";
|
2023-06-30 14:17:38 +03:00
|
|
|
}
|
2023-10-30 16:52:47 +03:00
|
|
|
setIsLoggingOut(false);
|
2023-06-30 14:17:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
handleLogout,
|
2023-10-30 16:52:47 +03:00
|
|
|
isLoggingOut,
|
|
|
|
isLogoutModalOpened,
|
|
|
|
setIsLogoutModalOpened,
|
2023-06-30 14:17:38 +03:00
|
|
|
};
|
|
|
|
};
|