refactor: overhaul home UI and audio playback reliability
Redesign the home page and voice stream panel into a compact dock layout to improve the chat experience. Implement a flush mechanism in the PCM playback worklet to handle the resampler tail, preventing premature session termination due to phantom outstanding playback. Fix document filter visibility in settings to ensure controls remain accessible when no results match, and refactor transcript insertion to maintain correct user/assistant ordering.
This commit is contained in:
+100
-21
@@ -1,20 +1,24 @@
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
ElementRef,
|
||||
OnDestroy,
|
||||
OnInit,
|
||||
computed,
|
||||
effect,
|
||||
inject,
|
||||
signal,
|
||||
computed,
|
||||
OnInit,
|
||||
OnDestroy,
|
||||
viewChild,
|
||||
} from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
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 { VoiceStreamService } from '../../services/voice-stream/voice-stream.service';
|
||||
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
|
||||
import { Router, NavigationEnd } from '@angular/router';
|
||||
import { filter } from 'rxjs/operators';
|
||||
import { ThemeService } from '../../services/theme/theme.service';
|
||||
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 = [
|
||||
'چه چیزی میخواهید بدانید؟',
|
||||
@@ -24,15 +28,20 @@ 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: [CommonModule, AiChatInputComponent, VoiceStreamPanelComponent],
|
||||
imports: [AiChatInputComponent, VoiceStreamPanelComponent, VoiceTranscriptComponent],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
templateUrl: './home.page.html',
|
||||
styleUrl: './home.page.css',
|
||||
@@ -47,26 +56,29 @@ export class HomePage implements OnInit, OnDestroy {
|
||||
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;
|
||||
|
||||
/**
|
||||
* Conversation owns the layout when there is a live voice call,
|
||||
* leftover voice transcript, or text messages — same idea as Claude's
|
||||
* chat column switching from empty state to transcript list.
|
||||
*/
|
||||
readonly voiceHasTranscript = computed(
|
||||
() => this.voiceStream.isOpen() || this.voiceStream.messages().length > 0,
|
||||
/** 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,
|
||||
);
|
||||
|
||||
readonly hasConversation = computed(() => this.hasMessages() || this.voiceHasTranscript());
|
||||
|
||||
/** @deprecated use hasConversation — kept for any external bindings */
|
||||
readonly voicePanelVisible = this.hasConversation;
|
||||
/** 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<ElementRef<HTMLElement>>('transcriptList');
|
||||
private stickToBottom = true;
|
||||
private scrollFrame: number | null = null;
|
||||
private dockTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private typingTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private routerSubscription?: Subscription;
|
||||
|
||||
@@ -79,6 +91,28 @@ export class HomePage implements OnInit, OnDestroy {
|
||||
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 {
|
||||
@@ -87,9 +121,48 @@ export class HomePage implements OnInit, OnDestroy {
|
||||
|
||||
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();
|
||||
|
||||
@@ -115,9 +188,12 @@ export class HomePage implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
@@ -129,9 +205,12 @@ export class HomePage implements OnInit, OnDestroy {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user