import dayjs from 'dayjs' import { Edit, Unlink } from 'lucide-react' import Image from 'next/image' import Link from 'next/link' import { useRouter } from 'next/router' import { useEffect, useState } from 'react' import { toast } from 'sonner' import { Badge, Button, Card, CardContent, cn, Dialog, DialogContent, DialogHeader, DialogTitle, Tooltip, TooltipContent, TooltipTrigger, } from 'ui' import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal' import { PageSection, PageSectionContent, PageSectionDescription, PageSectionMeta, PageSectionSummary, PageSectionTitle, } from 'ui-patterns/PageSection' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { ChangeEmailAddressForm, GitHubChangeEmailAddress, SSOChangeEmailAddress, } from './ChangeEmailAddress' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { useProfileIdentitiesQuery } from '@/data/profile/profile-identities-query' import { useUnlinkIdentityMutation } from '@/data/profile/profile-unlink-identity-mutation' import { BASE_PATH } from '@/lib/constants' const getProviderName = (provider: string) => provider === 'github' ? 'GitHub' : provider.startsWith('sso') ? 'SSO' : provider.replaceAll('_', ' ') export const AccountIdentities = () => { const router = useRouter() const { data, isPending: isLoading, isSuccess } = useProfileIdentitiesQuery() const identities = data?.identities ?? [] const isChangeExpired = data?.email_change_sent_at ? dayjs().utc().diff(dayjs(data?.email_change_sent_at).utc(), 'minute') > 10 : false const [selectedProviderUnlink, setSelectedProviderUnlink] = useState() const [selectedProviderUpdateEmail, setSelectedProviderUpdateEmail] = useState() const { mutate: unlinkIdentity, isPending: isUnlinking } = useUnlinkIdentityMutation({ onSuccess: () => { toast.success( `Successfully unlinked ${getProviderName(selectedProviderUnlink ?? '')} identity!` ) setSelectedProviderUnlink(undefined) }, }) const [, message] = router.asPath.split('#message=') const onConfirmUnlinkIdentity = async () => { const identity = identities.find((i) => i.provider === selectedProviderUnlink) if (identity) unlinkIdentity(identity) } useEffect(() => { if (message) toast.success(message.replaceAll('+', ' ')) }, [message]) return ( Account identities Manage the providers linked to your Briven account and update their details. {isLoading && ( )} {isSuccess && (
{identities.map((identity) => { const { identity_id, provider } = identity const username = identity.identity_data?.user_name const providerName = getProviderName(provider) const iconKey = provider === 'github' ? 'github-icon' : provider === 'email' ? 'email-icon2' : 'saml-icon' return (
{`${identity.provider}

{providerName}

{provider === 'email' && data.new_email && !isChangeExpired && ( Pending change Changing to {data.new_email} )}

{!!username ? {username} ยท : null} {identity.email}

{provider === 'email' && ( )} } className="w-7" onClick={() => setSelectedProviderUpdateEmail(provider)} tooltip={{ content: { side: 'bottom', text: 'Update email address' } }} /> {identities.length > 1 && ( } className="w-7" onClick={() => setSelectedProviderUnlink(provider)} tooltip={{ content: { side: 'bottom', text: 'Unlink identity' } }} /> )}
) })}
)}
{ if (!open) setSelectedProviderUpdateEmail(undefined) }} > {selectedProviderUpdateEmail !== 'email' ? `Updating email address for ${getProviderName(selectedProviderUpdateEmail ?? '')} identity` : 'Update email address'} {selectedProviderUpdateEmail === 'github' ? ( ) : selectedProviderUpdateEmail?.startsWith('sso') ? ( ) : ( setSelectedProviderUpdateEmail(undefined)} /> )} setSelectedProviderUnlink(undefined)} onConfirm={onConfirmUnlinkIdentity} confirmLabel="Unlink identity" confirmLabelLoading="Unlinking identity" alert={{ base: { variant: 'warning' }, title: `Confirm to disconnect your ${getProviderName(selectedProviderUnlink ?? '')} identity`, description: `After disconnecting, you will only be able to sign in via ${selectedProviderUnlink === 'github' ? 'email and password' : 'your GitHub identity'}`, }} />
) }