2023-11-20 14:01:19 +00:00
|
|
|
// react
|
2023-01-26 18:12:20 +00:00
|
|
|
import { useEffect, useState, FC } from "react";
|
2023-11-20 14:01:19 +00:00
|
|
|
// next
|
2023-01-26 18:12:20 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
import Image from "next/image";
|
|
|
|
import { useRouter } from "next/router";
|
2023-07-17 07:30:44 +00:00
|
|
|
import { useTheme } from "next-themes";
|
2023-01-26 18:12:20 +00:00
|
|
|
// images
|
2023-11-23 08:15:00 +00:00
|
|
|
import githubLightModeImage from "/public/logos/github-black.png";
|
|
|
|
import githubDarkModeImage from "/public/logos/github-dark.svg";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-12-11 10:22:21 +00:00
|
|
|
type Props = {
|
2023-01-26 18:12:20 +00:00
|
|
|
handleSignIn: React.Dispatch<string>;
|
2023-10-04 13:25:29 +00:00
|
|
|
clientId: string;
|
2024-01-19 15:25:03 +00:00
|
|
|
type: "sign_in" | "sign_up";
|
2023-12-11 10:22:21 +00:00
|
|
|
};
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-12-11 10:22:21 +00:00
|
|
|
export const GitHubSignInButton: FC<Props> = (props) => {
|
2024-01-19 15:25:03 +00:00
|
|
|
const { handleSignIn, clientId, type } = props;
|
2023-10-04 13:25:29 +00:00
|
|
|
// states
|
2023-07-17 07:30:44 +00:00
|
|
|
const [loginCallBackURL, setLoginCallBackURL] = useState(undefined);
|
|
|
|
const [gitCode, setGitCode] = useState<null | string>(null);
|
2023-10-04 13:25:29 +00:00
|
|
|
// router
|
2023-01-26 18:12:20 +00:00
|
|
|
const {
|
|
|
|
query: { code },
|
|
|
|
} = useRouter();
|
2023-10-04 13:25:29 +00:00
|
|
|
// theme
|
2023-11-23 08:15:00 +00:00
|
|
|
const { resolvedTheme } = useTheme();
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-06-09 19:56:38 +00:00
|
|
|
if (code && !gitCode) {
|
|
|
|
setGitCode(code.toString());
|
2023-01-26 18:12:20 +00:00
|
|
|
handleSignIn(code.toString());
|
|
|
|
}
|
2023-06-09 19:56:38 +00:00
|
|
|
}, [code, gitCode, handleSignIn]);
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-11-08 15:01:46 +00:00
|
|
|
const origin = typeof window !== "undefined" && window.location.origin ? window.location.origin : "";
|
2023-05-30 13:44:35 +00:00
|
|
|
setLoginCallBackURL(`${origin}/` as any);
|
2023-01-26 18:12:20 +00:00
|
|
|
}, []);
|
|
|
|
return (
|
2023-12-06 08:52:59 +00:00
|
|
|
<div className="w-full">
|
2023-03-21 07:16:12 +00:00
|
|
|
<Link
|
2023-10-04 13:25:29 +00:00
|
|
|
href={`https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${loginCallBackURL}&scope=read:user,user:email`}
|
2023-03-21 07:16:12 +00:00
|
|
|
>
|
2023-11-20 14:01:19 +00:00
|
|
|
<button
|
2023-12-10 10:18:10 +00:00
|
|
|
className={`flex h-[42px] w-full items-center justify-center gap-2 rounded border px-2 text-sm font-medium text-custom-text-100 duration-300 hover:bg-onboarding-background-300 ${
|
|
|
|
resolvedTheme === "dark" ? "border-[#43484F] bg-[#2F3135]" : "border-[#D9E4FF]"
|
2023-11-23 08:15:00 +00:00
|
|
|
}`}
|
2023-11-20 14:01:19 +00:00
|
|
|
>
|
2023-07-17 07:30:44 +00:00
|
|
|
<Image
|
2023-11-23 08:15:00 +00:00
|
|
|
src={resolvedTheme === "dark" ? githubDarkModeImage : githubLightModeImage}
|
2024-04-29 06:42:33 +00:00
|
|
|
height={18}
|
|
|
|
width={18}
|
2023-07-17 07:30:44 +00:00
|
|
|
alt="GitHub Logo"
|
|
|
|
/>
|
2024-01-19 15:25:03 +00:00
|
|
|
<span className="text-onboarding-text-200">{type === "sign_in" ? "Sign-in" : "Sign-up"} with GitHub</span>
|
2023-03-21 07:16:12 +00:00
|
|
|
</button>
|
|
|
|
</Link>
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
};
|