"use client";

import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { SESSION_EXPIRED_EVENT } from "@/lib/api";

/**
 * Sends the user to /login when the API rejects their token (lib/api.ts has already cleared it).
 * Uses the app router rather than window.location so it also works in the desktop build's app:// pages.
 */
export function useSessionExpiredRedirect() {
    const router = useRouter();
    useEffect(() => {
        const onExpired = () => router.push("/login");
        window.addEventListener(SESSION_EXPIRED_EVENT, onExpired);
        return () => window.removeEventListener(SESSION_EXPIRED_EVENT, onExpired);
    }, [router]);
}
