Skip to content

Commit d4684bf

Browse files
SebasianDevAndyvargtz
authored andcommitted
Enhance UserButton component to format email display
- Introduced useMemo to format the user's email into local part and domain for better readability. - Updated the rendering logic to conditionally display the formatted email or a fallback message when no email is available.
1 parent bfbda2b commit d4684bf

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

components/login/user-button/UserButton.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@ import { signOut, useSession } from 'next-auth/react';
1111
import Image from 'next/image';
1212
import Link from 'next/link';
1313
import SignOutComponent from '../sign-out/SignOut';
14-
import { useState } from 'react';
14+
import { useState, useMemo } from 'react';
1515
import { CircleUserRound } from 'lucide-react';
1616
import { Separator } from '@radix-ui/react-dropdown-menu';
1717
export function UserButton() {
1818
const { data: session, status } = useSession() ?? {};
1919
const [isDialogOpen, setIsDialogOpen] = useState(false);
2020
const isAuthenticated = status === 'authenticated';
21+
22+
// Dividir el correo por @ para evitar cortes no deseados
23+
const formattedEmail = useMemo(() => {
24+
const email = session?.user?.email;
25+
if (!email) return null;
26+
27+
const atIndex = email.indexOf('@');
28+
if (atIndex === -1) return { localPart: email, domain: null };
29+
30+
return {
31+
localPart: email.substring(0, atIndex),
32+
domain: email.substring(atIndex), // Incluye el @
33+
};
34+
}, [session?.user?.email]);
2135
const handleSignOut = (): void => {
2236
// Clean up any stored redirect URLs before logout
2337
if (typeof window !== "undefined") {
@@ -66,9 +80,18 @@ export function UserButton() {
6680
shadow-lg p-1 rounded-md w-48'
6781
>
6882
<div className="px-2 py-1.5">
69-
<p className="text-sm break-words">
70-
{session.user.email || 'No email available'}
71-
</p>
83+
{formattedEmail ? (
84+
<div className="text-sm">
85+
<div className="break-words">{formattedEmail.localPart}</div>
86+
{formattedEmail.domain && (
87+
<div className="break-words">{formattedEmail.domain}</div>
88+
)}
89+
</div>
90+
) : (
91+
<p className="text-sm break-words">
92+
{session.user.email || 'No email available'}
93+
</p>
94+
)}
7295

7396
<p className="text-sm break-words mt-1">
7497
{session.user.name || 'No name available'}

0 commit comments

Comments
 (0)