11'use client' ;
22
33import { Button } from '@/components/ui/button' ;
4+ import { captureEvent } from '@/hooks/useCaptureEvent' ;
45import { X } from 'lucide-react' ;
56import Link from 'next/link' ;
67import { usePathname } from 'next/navigation' ;
@@ -9,48 +10,57 @@ import { useState, useEffect } from 'react';
910const DISMISSED_KEY = 'sb.chat-sign-in-prompt-dismissed' ;
1011
1112interface SignInPromptBannerProps {
13+ chatId : string ;
1214 isAuthenticated : boolean ;
1315 isOwner : boolean ;
1416 hasMessages : boolean ;
1517 isStreaming : boolean ;
1618}
1719
1820export const SignInPromptBanner = ( {
21+ chatId,
1922 isAuthenticated,
2023 isOwner,
2124 hasMessages,
2225 isStreaming,
2326} : SignInPromptBannerProps ) => {
2427 const pathname = usePathname ( ) ;
2528 const [ isDismissed , setIsDismissed ] = useState ( true ) ; // Start as true to avoid flash
26- const [ hasShownOnce , setHasShownOnce ] = useState ( false ) ;
29+ const [ hasDisplayedEventFired , setHasDisplayedEventFired ] = useState ( false ) ;
2730
2831 // Check sessionStorage on mount
2932 useEffect ( ( ) => {
3033 const dismissed = sessionStorage . getItem ( DISMISSED_KEY ) === 'true' ;
3134 setIsDismissed ( dismissed ) ;
3235 } , [ ] ) ;
3336
34- // Show the banner after first response completes
37+ const isBannerVisible =
38+ ! isDismissed &&
39+ ! isAuthenticated &&
40+ isOwner &&
41+ hasMessages &&
42+ ! isStreaming ;
43+
44+ // Show the banner after first response completes and track display
3545 useEffect ( ( ) => {
36- if ( ! isAuthenticated && isOwner && hasMessages && ! isStreaming && ! hasShownOnce ) {
37- setHasShownOnce ( true ) ;
46+ if ( isBannerVisible && ! hasDisplayedEventFired ) {
47+ setHasDisplayedEventFired ( true ) ;
48+ captureEvent ( 'wa_chat_sign_in_banner_displayed' , { chatId } ) ;
3849 }
39- } , [ isAuthenticated , isOwner , hasMessages , isStreaming , hasShownOnce ] ) ;
50+ } , [ isBannerVisible , chatId , hasDisplayedEventFired ] ) ;
51+
4052
4153 const handleDismiss = ( ) => {
54+ captureEvent ( 'wa_chat_sign_in_banner_dismissed' , { chatId } ) ;
4255 setIsDismissed ( true ) ;
4356 sessionStorage . setItem ( DISMISSED_KEY , 'true' ) ;
4457 } ;
4558
46- // Don't show if:
47- // - User is authenticated
48- // - User doesn't own this chat
49- // - Banner was dismissed
50- // - No messages yet (haven't had first interaction)
51- // - Still streaming (wait for response to complete)
52- // - Haven't triggered the "show" condition yet
53- if ( isAuthenticated || ! isOwner || isDismissed || ! hasMessages || isStreaming || ! hasShownOnce ) {
59+ const handleSignInClick = ( ) => {
60+ captureEvent ( 'wa_chat_sign_in_banner_clicked' , { chatId } ) ;
61+ } ;
62+
63+ if ( ! isBannerVisible ) {
5464 return null ;
5565 }
5666
@@ -64,6 +74,7 @@ export const SignInPromptBanner = ({
6474 variant = "default"
6575 size = "sm"
6676 asChild
77+ onClick = { handleSignInClick }
6778 >
6879 < Link href = { `/login?callbackUrl=${ encodeURIComponent ( pathname ) } ` } >
6980 Sign in
0 commit comments