Skip to content

Commit

Permalink
[Frontend] feat: show login notice for user (CDMD-3557) (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
r4zendev authored Jul 8, 2024
1 parent 82eb71a commit ea09bd5
Showing 1 changed file with 86 additions and 15 deletions.
101 changes: 86 additions & 15 deletions apps/frontend/components/TokenBuilder.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { useCallback, useEffect, useRef, useState } from "react";

import {
ACCESS_TOKEN_COMMANDS,
Expand All @@ -10,17 +10,48 @@ import {
} from "@/constants";
import { useAuth, useUser } from "@clerk/nextjs";
import { populateLoginIntent } from "@studio/api/populateLoginIntent";
import {
Dialog,
DialogContent,
DialogTrigger,
} from "@studio/components/ui/dialog";
import { SEARCH_PARAMS_KEYS } from "@studio/store/getInitialState";

export const TokenBuilder = () => {
const { getToken } = useAuth();
const { isLoaded, isSignedIn } = useUser();
const router = useRouter();

const [isOpen, setIsOpen] = useState(false);
const [timeleft, setTimeleft] = useState(5);
const [result, setResult] = useState<"fail" | "success" | null>(null);

const intervalRef = useRef<NodeJS.Timeout | null>(null);

const onLoginIntentPopulated = useCallback((result: "fail" | "success") => {
setIsOpen(true);
setResult(result);

intervalRef.current = setInterval(() => {
setTimeleft((prev) => prev - 1);
}, 1000);
}, []);

useEffect(() => {
if (timeleft === 0) {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
setIsOpen(false);
window.close();
}
}, [timeleft]);

useEffect(() => {
if (!isLoaded || !isSignedIn) {
return;
}

(async () => {
const clerkToken = await getToken();
if (clerkToken === null) {
Expand All @@ -43,15 +74,26 @@ export const TokenBuilder = () => {
?.split(",") || [];

// Polling should pick it up
await populateLoginIntent({
clerkToken,
sessionId,
iv,
});
try {
await populateLoginIntent({
clerkToken,
sessionId,
iv,
});
onLoginIntentPopulated("success");
} catch (err) {
onLoginIntentPopulated("fail");
}
}
ACCESS_TOKEN_COMMANDS.forEach((key) => localStorage.removeItem(key));
})();
}, [isSignedIn, isLoaded, getToken]);

return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
};
}, [isSignedIn, isLoaded, getToken, onLoginIntentPopulated]);

useEffect(() => {
const searchParams = new URLSearchParams(window.location.search);
Expand All @@ -76,12 +118,16 @@ export const TokenBuilder = () => {
const iv = searchParams.get(SEARCH_PARAMS_KEYS.IV);

// Polling should pick it up
await populateLoginIntent({
clerkToken,
sessionId,
iv,
});
return;
try {
await populateLoginIntent({
clerkToken,
sessionId,
iv,
});
onLoginIntentPopulated("success");
} catch (err) {
onLoginIntentPopulated("fail");
}
}
})();
return;
Expand All @@ -97,7 +143,32 @@ export const TokenBuilder = () => {
}

router.push("/auth/sign-in");
}, [getToken, isSignedIn, isLoaded, router]);

return null;
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
};
}, [getToken, isSignedIn, isLoaded, router, onLoginIntentPopulated]);

return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogContent className="max-w-2xl bg-white text-gray-dark m-auto flex-col flex items-center gap-2">
{result === "success" ? (
<>
<span>Login successful.</span>
<span>You can return to the CLI now.</span>
<span>
This tab will close in{" "}
<span className="text-red-500">{timeleft} seconds</span>...
</span>
</>
) : (
<span className="text-red-500">
Login failed. Please contact Codemod team.
</span>
)}
</DialogContent>
</Dialog>
);
};

0 comments on commit ea09bd5

Please sign in to comment.