-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathclient.tsx
More file actions
213 lines (191 loc) · 6.84 KB
/
client.tsx
File metadata and controls
213 lines (191 loc) · 6.84 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"use client";
import { useCallback, useEffect, useState } from "react";
import { AnimatePresence, motion } from "framer-motion";
import Image from "next/image";
import { Github, Loader, X } from "lucide-react";
import { cn } from "@/lib/utils";
interface GitHubModalProps {
onClose: () => void;
}
interface GitHubProfile {
login: string;
avatar_url: string;
name: string;
bio: string;
}
// Debounce function to limit API calls
function useDebounce<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(timer);
};
}, [value, delay]);
return debouncedValue;
}
export default function GitHubModal({ onClose }: GitHubModalProps) {
const [username, setUsername] = useState("");
const [isValidating, setIsValidating] = useState(false);
const [error, setError] = useState("");
const [profile, setProfile] = useState<GitHubProfile | null>(null);
const [loading, setLoading] = useState(false);
const redirectToProfilePage = () => {
if (!profile) return;
setLoading(true);
// Get current search params and preserve them
const currentParams = new URLSearchParams(window.location.search);
currentParams.set('ref', 'modal');
// Use window.location for instant navigation
window.location.href = `/${profile?.login}?${currentParams.toString()}`;
};
const redirectToProfilePageFromCard = () => {
if (!profile) return;
// Get current search params and preserve them
const currentParams = new URLSearchParams(window.location.search);
currentParams.set('ref', 'modelv2');
// Use window.location for instant navigation
window.location.href = `/${profile?.login}?${currentParams.toString()}`;
};
// Debounce the username input to prevent excessive API calls
const debouncedUsername = useDebounce(username, 500);
const validateGithubUsername = useCallback(
async (usernameToValidate: string) => {
if (!usernameToValidate) {
setError("");
setProfile(null);
return;
}
setIsValidating(true);
setError("");
try {
const response = await fetch(
`https://api.github.com/users/${usernameToValidate}`
);
if (!response.ok) {
throw new Error("GitHub user not found");
}
const data = await response.json();
setProfile({
login: data.login,
avatar_url: data.avatar_url,
name: data.name || data.login,
bio: data.bio || "No bio available",
});
} catch (err) {
console.log(err);
setError("Invalid GitHub username");
setProfile(null);
} finally {
setIsValidating(false);
}
},
[]
);
// Effect to trigger validation when debounced username changes
useEffect(() => {
if (debouncedUsername) {
validateGithubUsername(debouncedUsername);
} else {
setProfile(null);
setError("");
}
}, [debouncedUsername, validateGithubUsername]);
return (
<AnimatePresence>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50 p-4"
onClick={(e) => {
if (e.target === e.currentTarget) onClose();
}}
>
<motion.div
initial={{ scale: 0.95, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.95, opacity: 0 }}
className="bg-white rounded-2xl p-8 max-w-md w-full mx-auto shadow-xl"
>
<div className="flex justify-between items-center mb-6">
<div className="flex items-center gap-3">
<Github size={32} className="text-black" />
<h2 className="text-2xl font-bold">GitHub Username</h2>
</div>
<button
onClick={onClose}
className="text-gray-500 hover:text-gray-700 transition-colors"
>
<X size={24} />
</button>
</div>
<div className="space-y-6">
<div>
<div className="relative">
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Enter GitHub username"
className="w-full px-4 py-3 rounded-lg border border-gray-200 focus:outline-none focus:ring-2 focus:ring-[#CCFF00] focus:border-transparent text-lg"
/>
{isValidating && (
<div className="absolute right-3 top-1/2 -translate-y-1/2">
<Loader className={"animate-spin"} />
</div>
)}
</div>
{error && <p className="mt-2 text-red-500 text-sm">{error}</p>}
</div>
{profile && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
onClick={redirectToProfilePageFromCard}
className="bg-gray-50 rounded-lg p-4 flex items-center gap-4 cursor-pointer hover:bg-gray-100 transition-colors"
>
<Image
src={profile.avatar_url}
alt={profile.name}
width={60}
height={60}
className="rounded-full"
/>
<div>
<h3 className="font-semibold text-lg">{profile.name}</h3>
<p className="text-gray-600 text-sm">{profile.bio}</p>
</div>
</motion.div>
)}
<div className="flex gap-3">
<button
disabled={!profile || loading}
onClick={redirectToProfilePage}
className={cn(
"flex-1 bg-[#CCFF00] text-black px-6 py-3 rounded-lg font-semibold hover:bg-[#b8e600] transition-colors flex items-center justify-center gap-2",
(!profile || loading) && "opacity-50 cursor-not-allowed"
)}
>
{loading ? (
// 🔹 Tailwind-only spinner (no external libs)
<div className="h-5 w-5 border-2 border-black border-t-transparent rounded-full animate-spin" />
) : (
"View Profile"
)}
</button>
<button
onClick={onClose}
className="px-6 py-3 border border-gray-200 rounded-lg font-semibold hover:bg-gray-50 transition-colors"
>
Cancel
</button>
</div>
</div>
</motion.div>
</motion.div>
</AnimatePresence>
);
}