-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathConnectButton.tsx
99 lines (88 loc) · 2.96 KB
/
ConnectButton.tsx
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React, { useState } from 'react';
import { useCurrentUser } from '@site/src/hooks/use-current-user';
import { Button } from '@site/src/ui/design-system/src/lib/Components/Button';
import Dropdown from '@site/src/ui/design-system/src/lib/Components/Dropdown';
import ProgressModal from '@site/src/components/ProgressModal';
import ProfileModal from '@site/src/components/ProfileModal';
import { useProgress } from '../hooks/use-progress';
import { faUser } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { useProfile } from '../hooks/use-profile';
import { useGithubAvatar } from '../hooks/use-github-avatar';
import { SocialType } from '../types/gold-star';
const shortenAddress = (address: string) => {
if (!address) return '';
return `${address.slice(0, 4)}...${address.slice(-3)}`;
};
const ConnectButton: React.FC = () => {
const { user, logIn, logOut } = useCurrentUser();
const { profile } = useProfile(user.addr);
const { avatar } = useGithubAvatar(profile?.socials?.[SocialType.GITHUB]);
const [isProgressModalOpen, setIsProgressModalOpen] = useState(false);
const [isProfileModalOpen, setIsProfileModalOpen] = useState(false);
const { getProgress } = useProgress();
const handleOpenProgress = () => {
setIsProgressModalOpen(true);
};
const handleCloseProgressModal = () => {
setIsProgressModalOpen(false);
};
const handleOpenProfileModal = () => {
setIsProfileModalOpen(true);
};
const handleCloseProfileModal = () => {
setIsProfileModalOpen(false);
};
if (!user.loggedIn) {
return (
<Button size="sm" className="mr-2" onClick={logIn}>
Connect
</Button>
);
}
const formattedProgressValue = Math.floor(getProgress() * 100);
const dropdownItems = [
{
label: `Progress (${formattedProgressValue}%)`,
onClick: handleOpenProgress,
},
{ label: 'Profile', onClick: handleOpenProfileModal },
{ label: 'Disconnect', onClick: logOut },
];
return (
<div className="hide-connect-on-mobile">
<Dropdown
buttonLabel={
<div className="flex items-center gap-2">
{avatar ? (
<img
src={avatar}
alt="Github Avatar"
className="w-6 h-6 rounded-full"
/>
) : (
<FontAwesomeIcon icon={faUser} size="md" className="mx-auto" />
)}
<span className="text-sm text-gray-500">
{formattedProgressValue}%
</span>
</div>
}
items={dropdownItems}
/>
<ProgressModal
isOpen={isProgressModalOpen}
onClose={handleCloseProgressModal}
onOpenProfileModal={() => {
handleCloseProgressModal();
handleOpenProfileModal();
}}
/>
<ProfileModal
isOpen={isProfileModalOpen}
onClose={handleCloseProfileModal}
/>
</div>
);
};
export default ConnectButton;