@@ -28,12 +28,16 @@ import { NotConfiguredErrorBanner } from '../notConfiguredErrorBanner';
2828import useCaptureEvent from '@/hooks/useCaptureEvent' ;
2929import { SignInPromptBanner } from './signInPromptBanner' ;
3030import { DuplicateChatDialog } from '@/app/[domain]/chat/components/duplicateChatDialog' ;
31+ import { LoginModal } from '@/app/components/loginModal' ;
32+ import type { IdentityProviderMetadata } from '@/lib/identityProviders' ;
3133import { useParams } from 'next/navigation' ;
3234
3335type ChatHistoryState = {
3436 scrollOffset ?: number ;
3537}
3638
39+ const PENDING_MESSAGE_STORAGE_KEY = "askgh_chat_pending_message" ;
40+
3741interface ChatThreadProps {
3842 id ?: string | undefined ;
3943 initialMessages ?: SBChatMessage [ ] ;
@@ -46,6 +50,8 @@ interface ChatThreadProps {
4650 isOwner ?: boolean ;
4751 isAuthenticated ?: boolean ;
4852 chatName ?: string ;
53+ providers ?: IdentityProviderMetadata [ ] ;
54+ isAskGhEnabled ?: boolean ;
4955}
5056
5157export const ChatThread = ( {
@@ -60,6 +66,8 @@ export const ChatThread = ({
6066 isOwner = true ,
6167 isAuthenticated = false ,
6268 chatName,
69+ providers = [ ] ,
70+ isAskGhEnabled = false ,
6371} : ChatThreadProps ) => {
6472 const [ isErrorBannerVisible , setIsErrorBannerVisible ] = useState ( false ) ;
6573 const scrollAreaRef = useRef < HTMLDivElement > ( null ) ;
@@ -71,6 +79,8 @@ export const ChatThread = ({
7179 const params = useParams < { domain : string } > ( ) ;
7280 const [ isContextSelectorOpen , setIsContextSelectorOpen ] = useState ( false ) ;
7381 const [ isDuplicateDialogOpen , setIsDuplicateDialogOpen ] = useState ( false ) ;
82+ const [ isLoginModalOpen , setIsLoginModalOpen ] = useState ( false ) ;
83+ const hasRestoredPendingMessage = useRef ( false ) ;
7484 const captureEvent = useCaptureEvent ( ) ;
7585
7686 // Initial state is from attachments that exist in in the chat history.
@@ -200,6 +210,38 @@ export const ChatThread = ({
200210 hasSubmittedInputMessage . current = true ;
201211 } , [ inputMessage , sendMessage ] ) ;
202212
213+ // Restore pending message after OAuth redirect (askgh login wall)
214+ useEffect ( ( ) => {
215+ if ( ! isAskGhEnabled || ! isAuthenticated || ! isOwner || hasRestoredPendingMessage . current ) {
216+ return ;
217+ }
218+
219+ const stored = sessionStorage . getItem ( PENDING_MESSAGE_STORAGE_KEY ) ;
220+ if ( ! stored ) {
221+ return ;
222+ }
223+
224+ hasRestoredPendingMessage . current = true ;
225+ sessionStorage . removeItem ( PENDING_MESSAGE_STORAGE_KEY ) ;
226+
227+ try {
228+ const { chatId : storedChatId , children } = JSON . parse ( stored ) as { chatId : string ; children : Descendant [ ] } ;
229+
230+ // Only restore if we're on the same chat that stored the pending message
231+ if ( storedChatId !== chatId ) {
232+ return ;
233+ }
234+
235+ const text = slateContentToString ( children ) ;
236+ const mentions = getAllMentionElements ( children ) ;
237+ const message = createUIMessage ( text , mentions . map ( ( { data } ) => data ) , selectedSearchScopes ) ;
238+ sendMessage ( message ) ;
239+ setIsAutoScrollEnabled ( true ) ;
240+ } catch ( error ) {
241+ console . error ( 'Failed to restore pending message:' , error ) ;
242+ }
243+ } , [ isAskGhEnabled , isAuthenticated , isOwner , chatId , sendMessage , selectedSearchScopes ] ) ;
244+
203245 // Track scroll position changes.
204246 useEffect ( ( ) => {
205247 const scrollElement = scrollAreaRef . current ?. querySelector ( '[data-radix-scroll-area-viewport]' ) as HTMLElement ;
@@ -288,6 +330,13 @@ export const ChatThread = ({
288330 } , [ error ] ) ;
289331
290332 const onSubmit = useCallback ( ( children : Descendant [ ] , editor : CustomEditor ) => {
333+ if ( isAskGhEnabled && ! isAuthenticated ) {
334+ captureEvent ( 'wa_askgh_login_wall_prompted' , { } ) ;
335+ sessionStorage . setItem ( PENDING_MESSAGE_STORAGE_KEY , JSON . stringify ( { chatId, children } ) ) ;
336+ setIsLoginModalOpen ( true ) ;
337+ return ;
338+ }
339+
291340 const text = slateContentToString ( children ) ;
292341 const mentions = getAllMentionElements ( children ) ;
293342
@@ -297,7 +346,7 @@ export const ChatThread = ({
297346 setIsAutoScrollEnabled ( true ) ;
298347
299348 resetEditor ( editor ) ;
300- } , [ sendMessage , selectedSearchScopes ] ) ;
349+ } , [ sendMessage , selectedSearchScopes , isAskGhEnabled , isAuthenticated , captureEvent , chatId ] ) ;
301350
302351 const onDuplicate = useCallback ( async ( newName : string ) : Promise < string | null > => {
303352 if ( ! defaultChatId ) {
@@ -449,6 +498,15 @@ export const ChatThread = ({
449498 </ div >
450499 ) }
451500 </ div >
501+
502+ { isAskGhEnabled && (
503+ < LoginModal
504+ isOpen = { isLoginModalOpen }
505+ onOpenChange = { setIsLoginModalOpen }
506+ providers = { providers }
507+ callbackUrl = { typeof window !== 'undefined' ? window . location . href : '' }
508+ />
509+ ) }
452510 </ >
453511 ) ;
454512}
0 commit comments