'use client' import { Suspense } from 'react' import { Chip, cn, Loader } from '@sim/emcn' import { getErrorMessage } from '@sim/utils/errors' import { useSearchParams } from 'next/navigation' import type { UnsubscribeType } from '@/lib/api/contracts/user' import { AuthSubmitButton } from '@/app/(auth)/components' import { AUTH_BUTTON_CLASS } from '@/app/(auth)/components/constants' import { InviteLayout } from '@/app/invite/components' import { useUnsubscribe, useUnsubscribeMutation } from '@/hooks/queries/unsubscribe' function UnsubscribeContent() { const searchParams = useSearchParams() const email = searchParams.get('email') const token = searchParams.get('token') const hasParams = Boolean(email) && Boolean(token) const query = useUnsubscribe(email ?? undefined, token ?? undefined) const unsubscribe = useUnsubscribeMutation() const data = query.data ?? null const loading = hasParams && query.isLoading const processing = unsubscribe.isPending const unsubscribed = unsubscribe.isSuccess const error = !hasParams ? 'Missing email or token in URL' : query.isError ? getErrorMessage(query.error, 'Failed to validate unsubscribe link') : unsubscribe.isError ? getErrorMessage(unsubscribe.error, 'Failed to process unsubscribe request') : null const handleUnsubscribe = (type: UnsubscribeType) => { if (!email || !token) return unsubscribe.mutate({ email, token, type }) } if (loading) { return (

Loading

Validating your unsubscribe link…

) } if (error) { return (

Invalid Unsubscribe Link

{error}

window.history.back()} loadingLabel=''> Go Back
) } if (data?.isTransactional) { return (

Important Account Emails

Transactional emails like password resets, account confirmations, and security alerts cannot be unsubscribed from as they contain essential information for your account.

window.close()} loadingLabel=''> Close
) } if (unsubscribed) { return (

Successfully Unsubscribed

You have been unsubscribed from our emails. You will stop receiving emails within 48 hours.

window.close()} loadingLabel=''> Close
) } const isAlreadyUnsubscribedFromAll = data?.currentPreferences.unsubscribeAll return (

Email Preferences

Choose which emails you'd like to stop receiving.

{data?.email}

handleUnsubscribe('all')} disabled={isAlreadyUnsubscribedFromAll} loading={processing} loadingLabel='Unsubscribing…' > {isAlreadyUnsubscribedFromAll ? 'Unsubscribed from All Emails' : 'Unsubscribe from All Marketing Emails'}
or choose specific types
handleUnsubscribe('marketing')} disabled={ processing || isAlreadyUnsubscribedFromAll || data?.currentPreferences.unsubscribeMarketing } className={cn(AUTH_BUTTON_CLASS, 'border border-[var(--border-1)]')} > {data?.currentPreferences.unsubscribeMarketing ? 'Unsubscribed from Marketing' : 'Unsubscribe from Marketing Emails'} handleUnsubscribe('updates')} disabled={ processing || isAlreadyUnsubscribedFromAll || data?.currentPreferences.unsubscribeUpdates } className={cn(AUTH_BUTTON_CLASS, 'border border-[var(--border-1)]')} > {data?.currentPreferences.unsubscribeUpdates ? 'Unsubscribed from Updates' : 'Unsubscribe from Product Updates'} handleUnsubscribe('notifications')} disabled={ processing || isAlreadyUnsubscribedFromAll || data?.currentPreferences.unsubscribeNotifications } className={cn(AUTH_BUTTON_CLASS, 'border border-[var(--border-1)]')} > {data?.currentPreferences.unsubscribeNotifications ? 'Unsubscribed from Notifications' : 'Unsubscribe from Notifications'}

You'll continue receiving important account emails like password resets and security alerts.

) } export default function Unsubscribe() { return (

Loading

Validating your unsubscribe link…

} >
) }