diff --git a/app/(main)/settings/account/actions.ts b/app/(main)/settings/account/actions.ts index ebdf5b61..1ad49966 100644 --- a/app/(main)/settings/account/actions.ts +++ b/app/(main)/settings/account/actions.ts @@ -5,7 +5,7 @@ import { createClient, getUser } from "@/lib/supabase"; import { revalidatePath } from "next/cache"; import { redirect } from "next/navigation"; -export async function authorizeGitHub() { +export async function connectGitHubIdentity() { const supabase = await createClient(); const { data, error } = await supabase.auth.linkIdentity({ provider: "github", @@ -23,7 +23,7 @@ export async function authorizeGitHub() { } } -export async function unlinkIdentity() { +export async function disconnectGitHubIdentity() { const supabaseUser = await getUser(); const supabase = await createClient(); if (!supabaseUser.identities) { diff --git a/app/(main)/settings/components/github-connection-button.tsx b/app/(main)/settings/components/github-connection-button.tsx index 02a93b1c..42d40042 100644 --- a/app/(main)/settings/components/github-connection-button.tsx +++ b/app/(main)/settings/components/github-connection-button.tsx @@ -1,43 +1,46 @@ +"use client"; + import { Button } from "@/components/ui/button"; +import { cn } from "@/lib/utils"; +import { useActionState } from "react"; + +type ButtonWithActionProps = { + action: () => Promise; +}; -export type GitHubConnectButtonMode = "connect" | "disconnect" | "hidden"; +const buttonClassNames = "w-fit font-avenir text-sm font-medium"; -export function GitHubConnectButton({ - mode, - connectAction, - disconnectAction, -}: { - mode: GitHubConnectButtonMode; - connectAction: () => Promise; - disconnectAction: () => Promise; -}) { - const className = "w-fit font-avenir text-sm font-medium"; - switch (mode) { - case "connect": - return ( -
- -
- ); - case "disconnect": - return ( -
- -
- ); - case "hidden": - return null; - } +export function GitHubConnectButton({ action }: ButtonWithActionProps) { + const [_, formAction, isPending] = useActionState(async () => { + await action(); + }, null); + return ( +
+ +
+ ); +} +export function GitHubDisconnectButton({ action }: ButtonWithActionProps) { + const [_, formAction, isPending] = useActionState(async () => { + await action(); + }, null); + return ( +
+ +
+ ); } diff --git a/app/(main)/settings/components/github-connection.tsx b/app/(main)/settings/components/github-connection.tsx index a41e3f98..ceace905 100644 --- a/app/(main)/settings/components/github-connection.tsx +++ b/app/(main)/settings/components/github-connection.tsx @@ -3,10 +3,13 @@ import { getUser } from "@/lib/supabase"; import type { GitHubUserClient } from "@/services/external/github/user-client"; import { SiGithub } from "@icons-pack/react-simple-icons"; import Link from "next/link"; -import { authorizeGitHub, unlinkIdentity } from "../account/actions"; +import { + connectGitHubIdentity, + disconnectGitHubIdentity, +} from "../account/actions"; import { GitHubConnectButton, - type GitHubConnectButtonMode, + GitHubDisconnectButton, } from "./github-connection-button"; type GitHubUser = Awaited>; @@ -16,16 +19,20 @@ export async function GitHubConnection({ }: { gitHubUser?: GitHubUser; }) { - const supabaseUser = await getUser(); - const unlinkable = - supabaseUser.identities && supabaseUser.identities.length > 1; + const ConnectButton = async () => { + if (!gitHubUser) { + return ; + } + + const supabaseUser = await getUser(); + const unlinkable = + supabaseUser.identities && supabaseUser.identities.length > 1; + if (unlinkable) { + return ; + } - let connectMode: GitHubConnectButtonMode = "hidden"; - if (!gitHubUser) { - connectMode = "connect"; - } else if (unlinkable) { - connectMode = "disconnect"; - } + return null; + }; return (
@@ -45,11 +52,7 @@ export async function GitHubConnection({ )}
- + {ConnectButton()} );