Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions www/components/github-modal/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ export default function GitHubModal({ onClose }: GitHubModalProps) {
const [error, setError] = useState("");
const [profile, setProfile] = useState<GitHubProfile | null>(null);
const router = useRouter();
const [loading, setLoading] = useState(false);

const redirectToProfilePage = async () => {
if (!profile) return;
setLoading(true);
await router.push(`/${profile?.login}?ref=modal`);

// no need to setLoading(false) because navigation will replace this page
};
Comment on lines +46 to +52
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function is redefined on every render. To optimize performance and maintain consistency with other memoized callbacks in this component (e.g., validateGithubUsername), you should wrap redirectToProfilePage in useCallback.

Also, the optional chaining in profile?.login is redundant due to the if (!profile) check above it. Removing it will make the code slightly cleaner.

Suggested change
const redirectToProfilePage = async () => {
if (!profile) return;
setLoading(true);
await router.push(`/${profile?.login}?ref=modal`);
// no need to setLoading(false) because navigation will replace this page
};
const redirectToProfilePage = useCallback(async () => {
if (!profile) return;
setLoading(true);
await router.push(`/${profile.login}?ref=modal`);
// no need to setLoading(false) because navigation will replace this page
}, [profile, router]);

// Debounce the username input to prevent excessive API calls
const debouncedUsername = useDebounce(username, 500);

Expand All @@ -58,7 +66,7 @@ export default function GitHubModal({ onClose }: GitHubModalProps) {

try {
const response = await fetch(
`https://api.github.com/users/${usernameToValidate}`,
`https://api.github.com/users/${usernameToValidate}`
);
if (!response.ok) {
throw new Error("GitHub user not found");
Expand All @@ -78,7 +86,7 @@ export default function GitHubModal({ onClose }: GitHubModalProps) {
setIsValidating(false);
}
},
[],
[]
);

// Effect to trigger validation when debounced username changes
Expand Down Expand Up @@ -162,14 +170,19 @@ export default function GitHubModal({ onClose }: GitHubModalProps) {

<div className="flex gap-3">
<button
disabled={!profile}
onClick={() => router.push(`/${profile?.login}?ref=modal`)}
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",
!profile && "opacity-50 cursor-not-allowed",
"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"
)}
>
View Profile
{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}
Expand Down