import { ChangeDetectionStrategy, Component, ElementRef, OnDestroy, OnInit, computed, effect, inject, signal, viewChild, } from '@angular/core'; import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop'; import { Router, NavigationEnd } from '@angular/router'; import { filter } from 'rxjs/operators'; import { Subscription } from 'rxjs'; import { AiChatInputComponent } from '../../shared/ui/ai-chat-input/ai-chat-input.component'; import { VoiceStreamPanelComponent } from '../../shared/ui/voice-stream-panel/voice-stream-panel.component'; import { VoiceTranscriptComponent } from '../../shared/ui/voice-transcript/voice-transcript.component'; import { VoiceStreamService } from '../../services/voice-stream/voice-stream.service'; import { ThemeService } from '../../services/theme/theme.service'; const HOME_TITLES = [ 'چه چیزی می‌خواهید بدانید؟', 'سوالتان را بپرسید، با شما هستم...', 'هر سوالی دارید، از من بپرسید...', 'از هر چیزی که می‌خواهید شروع کنید...', 'سوالتان را مطرح کنید، پاسخ می‌دهم...', 'بپرسید؛ با هم به پاسخ می‌رسیم...', 'هر سوالی در ذهن دارید، بپرسید...', 'دنبال یک پاسخ دقیق هستید؟', 'سوالتان را همین‌جا مطرح کنید...', 'آماده‌ام به سوالتان پاسخ بدهم...', ]; /** Keeps the call bar mounted this long after the call ends so it can fade/collapse out (matches the CSS). */ const CALL_DOCK_EXIT_MS = 400; /** Within this distance of the bottom the list keeps following new text. */ const STICK_TO_BOTTOM_PX = 80; @Component({ selector: 'app-home', standalone: true, imports: [AiChatInputComponent, VoiceStreamPanelComponent, VoiceTranscriptComponent], changeDetection: ChangeDetectionStrategy.OnPush, templateUrl: './home.page.html', styleUrl: './home.page.css', }) export class HomePage implements OnInit, OnDestroy { private readonly themeService = inject(ThemeService); private readonly router = inject(Router); private readonly voiceStream = inject(VoiceStreamService); readonly isDarkMode = this.themeService.isDarkMode; readonly logoSrc = () => this.isDarkMode() ? 'assets/images/RaymandLogo.png' : 'assets/images/login.png'; // ---- conversation state --------------------------------------------------- readonly hasMessages = signal(false); readonly callOpen = this.voiceStream.isOpen; readonly voiceMessages = this.voiceStream.messages; /** Anything on screen that belongs in the chat column: text sent, a live call, or a finished call's transcript. */ readonly hasConversation = computed( () => this.hasMessages() || this.callOpen() || this.voiceMessages().length > 0, ); /** The call bar stays mounted briefly after the call ends so it can animate out. */ readonly callDockMounted = signal(false); readonly jumpVisible = signal(false); // ---- title typing --------------------------------------------------------- readonly homeTitle = signal(''); readonly streamedText = signal(''); readonly isStreaming = signal(false); private readonly listRef = viewChild>('transcriptList'); private stickToBottom = true; private scrollFrame: number | null = null; private dockTimer: ReturnType | null = null; private typingTimer: ReturnType | null = null; private routerSubscription?: Subscription; constructor() { this.routerSubscription = this.router.events .pipe(filter((event) => event instanceof NavigationEnd)) .subscribe((event: NavigationEnd) => { if (event.url === '/' || event.url === '') { this.hasMessages.set(false); this.setRandomTitle(); } }); // Mount the call bar when a call starts; unmount it after the exit animation. toObservable(this.callOpen) .pipe(takeUntilDestroyed()) .subscribe((open) => { this.clearDockTimer(); if (open) { this.callDockMounted.set(true); } else if (this.callDockMounted()) { this.dockTimer = setTimeout(() => { this.dockTimer = null; this.callDockMounted.set(false); }, CALL_DOCK_EXIT_MS); } }); // Follow new transcript text, unless the user scrolled up to read. effect(() => { this.voiceMessages(); this.callOpen(); if (this.stickToBottom) this.scheduleScrollToBottom(); }); } ngOnInit(): void { this.setRandomTitle(); } ngOnDestroy(): void { this.stopTyping(); this.clearDockTimer(); if (this.scrollFrame !== null) cancelAnimationFrame(this.scrollFrame); this.routerSubscription?.unsubscribe(); } // ---- transcript list scrolling ------------------------------------------- onTranscriptScroll(): void { const el = this.listRef()?.nativeElement; if (!el) return; const distance = el.scrollHeight - el.scrollTop - el.clientHeight; this.stickToBottom = distance < STICK_TO_BOTTOM_PX; this.jumpVisible.set(!this.stickToBottom && this.hasConversation()); } scrollToLatest(): void { this.stickToBottom = true; this.jumpVisible.set(false); this.listRef()?.nativeElement.scrollTo({ top: this.listRef()!.nativeElement.scrollHeight, behavior: 'smooth', }); } private scheduleScrollToBottom(): void { if (this.scrollFrame !== null) return; this.scrollFrame = requestAnimationFrame(() => { this.scrollFrame = null; const el = this.listRef()?.nativeElement; if (el) el.scrollTo({ top: el.scrollHeight, behavior: 'instant' }); }); } private clearDockTimer(): void { if (this.dockTimer !== null) { clearTimeout(this.dockTimer); this.dockTimer = null; } } // ---- title typing (unchanged) --------------------------------------------- private setRandomTitle(): void { this.stopTyping(); const index = Math.floor(Math.random() * HOME_TITLES.length); const title = HOME_TITLES[index]; this.homeTitle.set(title); this.startTyping(title); } private startTyping(text: string): void { this.streamedText.set(''); this.isStreaming.set(true); let index = 0; const typeNextCharacter = () => { if (index >= text.length) { this.streamedText.set(text); this.isStreaming.set(false); this.typingTimer = null; return; } index++; this.streamedText.set(text.substring(0, index)); // Slightly random timing makes it feel more natural const delay = 16 + Math.random() * 8; this.typingTimer = setTimeout(typeNextCharacter, delay); }; typeNextCharacter(); } private stopTyping(): void { if (this.typingTimer !== null) { clearTimeout(this.typingTimer); this.typingTimer = null; } this.isStreaming.set(false); } // ---- composer events ------------------------------------------------------ onMessageSent(message: string): void { this.hasMessages.set(true); console.log('Message sent:', message); } onFileSelected(file: File): void { console.log('File selected:', file.name); } onVoiceToggled(active: boolean): void { console.log('Voice mode:', active ? 'active' : 'inactive'); } }