File size: 1,507 Bytes
c7fbc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use client";

import { useState } from "react";
import { Unlink, Loader2, Trash2 } from "lucide-react";
import { disconnectAccount } from "@/app/dashboard/settings/actions";
import { useRouter } from "next/navigation";

interface DisconnectButtonProps {
    accountId: string;
    username: string;
}

export default function DisconnectButton({ accountId, username }: DisconnectButtonProps) {
    const [isPending, setIsPending] = useState(false);
    const router = useRouter();

    const handleDisconnect = async () => {
        if (!confirm(`Are you sure you want to disconnect @${username}?`)) return;

        setIsPending(true);
        try {
            await disconnectAccount(accountId);
            router.refresh();
        } catch (error) {
            console.error("Failed to disconnect:", error);
            alert("Failed to disconnect account. Please try again.");
        } finally {
            setIsPending(false);
        }
    };

    return (
        <button
            onClick={handleDisconnect}
            disabled={isPending}
            className="group flex items-center justify-center p-2 rounded-lg text-gray-400 hover:text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
            title="Disconnect Account"
        >
            {isPending ? (
                <Loader2 size={18} className="animate-spin" />
            ) : (
                <Trash2 size={18} />
            )}
        </button>
    );
}