feat(voice): implement new pill-style provider picker

Replace the legacy select dropdown in the home page with a modern,
Claude-style provider picker component. This improves the user
experience and encapsulates provider loading and selection logic
within the new component.
This commit is contained in:
PeymanHonarjoo
2026-10-17 10:47:21 +03:30
parent dc23dbeb69
commit 4dac437cc7
6 changed files with 555 additions and 31 deletions
+41 -4
View File
@@ -244,6 +244,47 @@
margin-bottom: 0.5rem; margin-bottom: 0.5rem;
} }
/* ---------- footnote row under the chat input: disclaimer + provider pill ---------- */
.composer-footnote {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 0.75rem;
padding: 0.4rem 0.75rem 0;
max-width: 800px;
margin-inline: auto;
}
/* Fallback state (provider list failed to load, no pill to show): the footnote
stacks as centered lines under the input instead of a squeezed side row. */
.composer-footnote:has(.provider-picker.is-fallback) {
flex-direction: column;
align-items: center;
text-align: center;
gap: 0.25rem;
}
.composer-footnote:has(.provider-picker.is-fallback) .composer-disclaimer {
flex: none;
}
.composer-footnote:has(.provider-picker.is-fallback) app-voice-provider-picker {
width: 100%;
}
.composer-disclaimer {
margin: 0;
flex: 1;
min-width: 0;
font-size: 0.72rem;
line-height: 1.8;
color: var(--muted);
}
.composer-footnote app-voice-provider-picker {
flex-shrink: 0;
}
/* ---------- jump to latest ---------- */ /* ---------- jump to latest ---------- */
.jump-to-latest { .jump-to-latest {
position: absolute; position: absolute;
@@ -331,7 +372,3 @@
animation: none !important; animation: none !important;
} }
} }
.voice-provider-choice { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; padding: .5rem 1rem; font-size: .85rem; }
.voice-provider-choice select { font: inherit; color: inherit; background: var(--surface-color, transparent); border: 1px solid currentColor; border-radius: .5rem; padding: .3rem .6rem; }
.voice-provider-choice small { opacity: .75; }
.voice-provider-error { margin: .25rem 1rem; font-size: .8rem; }
+6 -14
View File
@@ -37,20 +37,6 @@
<!-- composer: call bar (while a call runs) + the chat input --> <!-- composer: call bar (while a call runs) + the chat input -->
<div class="composer"> <div class="composer">
@if (providerOptions().length) {
<div class="voice-provider-choice" dir="rtl">
<label for="voice-provider">ارائه‌دهندهٔ صدا</label>
<select id="voice-provider" [value]="selectedProvider() ?? ''" [disabled]="providerSwitching()" (change)="onProviderChange($event)">
@if (!selectedProvider()) { <option value="">پیش‌فرض سرویس</option> }
@for (provider of providerOptions(); track provider.id) {
<option [value]="provider.id" [disabled]="!provider.can_start">{{ provider.label }}{{ provider.can_start ? '' : ' — فعلاً در دسترس نیست' }}</option>
}
</select>
@if (callOpen()) { <small>تغییر ارائه‌دهنده تماس فعلی را پایان می‌دهد.</small> }
</div>
}
@if (providerChoiceError()) { <p class="voice-provider-error" role="status">{{ providerChoiceError() }}</p> }
@if (jumpVisible()) { @if (jumpVisible()) {
<button <button
type="button" type="button"
@@ -75,6 +61,12 @@
(fileSelected)="onFileSelected($event)" (fileSelected)="onFileSelected($event)"
(voiceToggled)="onVoiceToggled($event)" (voiceToggled)="onVoiceToggled($event)"
/> />
<!-- footnote row under the input: disclaimer + selected provider pill -->
<div class="composer-footnote" dir="rtl">
<p class="composer-disclaimer">دستیار هوش مصنوعی ممکن است اشتباه کند؛ لطفاً پاسخ‌ها را بازبینی کنید.</p>
<app-voice-provider-picker />
</div>
</div> </div>
</div> </div>
+7 -13
View File
@@ -17,7 +17,7 @@ import { Subscription } from 'rxjs';
import { AiChatInputComponent } from '../../shared/ui/ai-chat-input/ai-chat-input.component'; 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 { VoiceStreamPanelComponent } from '../../shared/ui/voice-stream-panel/voice-stream-panel.component';
import { VoiceTranscriptComponent } from '../../shared/ui/voice-transcript/voice-transcript.component'; import { VoiceTranscriptComponent } from '../../shared/ui/voice-transcript/voice-transcript.component';
import { VoiceProvider } from '../../services/voice-stream/voice-api.service'; import { VoiceProviderPickerComponent } from '../../shared/ui/voice-provider-picker/voice-provider-picker.component';
import { VoiceStreamService } from '../../services/voice-stream/voice-stream.service'; import { VoiceStreamService } from '../../services/voice-stream/voice-stream.service';
import { ThemeService } from '../../services/theme/theme.service'; import { ThemeService } from '../../services/theme/theme.service';
@@ -42,7 +42,12 @@ const STICK_TO_BOTTOM_PX = 80;
@Component({ @Component({
selector: 'app-home', selector: 'app-home',
standalone: true, standalone: true,
imports: [AiChatInputComponent, VoiceStreamPanelComponent, VoiceTranscriptComponent], imports: [
AiChatInputComponent,
VoiceStreamPanelComponent,
VoiceTranscriptComponent,
VoiceProviderPickerComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush, changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './home.page.html', templateUrl: './home.page.html',
styleUrl: './home.page.css', styleUrl: './home.page.css',
@@ -61,16 +66,6 @@ export class HomePage implements OnInit, OnDestroy {
readonly hasMessages = signal(false); readonly hasMessages = signal(false);
readonly callOpen = this.voiceStream.isOpen; readonly callOpen = this.voiceStream.isOpen;
readonly voiceMessages = this.voiceStream.messages; readonly voiceMessages = this.voiceStream.messages;
readonly providerOptions = this.voiceStream.providerOptions;
readonly selectedProvider = this.voiceStream.selectedProvider;
readonly providerSwitching = this.voiceStream.providerSwitching;
readonly providerChoiceError = this.voiceStream.providerChoiceError;
async onProviderChange(event: Event): Promise<void> {
const select = event.target as HTMLSelectElement;
await this.voiceStream.selectProvider(select.value as VoiceProvider);
select.value = this.selectedProvider() ?? "";
}
/** Anything on screen that belongs in the chat column: text sent, a live call, or a finished call's transcript. */ /** Anything on screen that belongs in the chat column: text sent, a live call, or a finished call's transcript. */
readonly hasConversation = computed( readonly hasConversation = computed(
@@ -94,7 +89,6 @@ export class HomePage implements OnInit, OnDestroy {
private routerSubscription?: Subscription; private routerSubscription?: Subscription;
constructor() { constructor() {
void this.voiceStream.loadProviders();
this.routerSubscription = this.router.events this.routerSubscription = this.router.events
.pipe(filter((event) => event instanceof NavigationEnd)) .pipe(filter((event) => event instanceof NavigationEnd))
.subscribe((event: NavigationEnd) => { .subscribe((event: NavigationEnd) => {
@@ -0,0 +1,262 @@
:host {
display: block;
flex: none;
}
.provider-picker {
position: relative;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 0.375rem;
}
.provider-pill-row {
display: flex;
align-items: center;
justify-content: flex-end;
}
/* Fallback state (provider list failed to load, no pill): the error is the only
content, so center it instead of pinning it to the end. */
.provider-picker.is-fallback {
align-items: center;
text-align: center;
}
/* ---------- Pill trigger (like Claude's "Sonnet 5.5 Medium" badge) ---------- */
.provider-pill {
display: inline-flex;
align-items: center;
gap: 0.375rem;
padding: 0.2rem 0.6rem;
border: 1px solid var(--surface-border);
border-radius: 0.5rem;
background: var(--sidebar-hover);
color: var(--text-secondary);
font-family: inherit;
font-size: 0.75rem;
font-weight: 500;
line-height: 1.6;
white-space: nowrap;
cursor: pointer;
transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
user-select: none;
}
.provider-pill:hover:not(:disabled) {
background: var(--surface-border);
color: var(--text-primary);
}
.provider-pill.is-open {
border-color: var(--color-primary);
color: var(--text-primary);
}
.provider-pill:disabled {
opacity: 0.6;
cursor: wait;
}
.provider-pill:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.provider-pill-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--color-success, #22c55e);
flex-shrink: 0;
}
.provider-pill-dot.is-busy {
background: var(--color-warning, #f59e0b);
animation: pillPulse 1s ease-in-out infinite;
}
@keyframes pillPulse {
0%,
100% {
opacity: 1;
}
50% {
opacity: 0.35;
}
}
.provider-pill-chevron {
opacity: 0.6;
transition: transform 0.18s ease;
flex-shrink: 0;
}
.provider-pill.is-open .provider-pill-chevron {
transform: rotate(180deg);
}
/* ---------- Popover menu (opens upward, like the Claude screenshot) ---------- */
.provider-menu {
position: absolute;
bottom: calc(100% + 0.5rem);
inset-inline-end: 0;
min-width: 290px;
max-width: min(340px, calc(100vw - 2rem));
background: var(--surface-bg);
border: 1px solid var(--surface-border);
border-radius: 0.875rem;
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.16), 0 2px 8px rgba(0, 0, 0, 0.08);
overflow: hidden;
z-index: 60;
animation: providerMenuIn 0.16s ease;
}
@keyframes providerMenuIn {
from {
opacity: 0;
transform: translateY(6px) scale(0.98);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
}
}
.provider-item {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
width: 100%;
padding: 0.7rem 1rem;
border: none;
border-bottom: 1px solid var(--surface-border);
background: transparent;
color: var(--text-primary);
font-family: inherit;
text-align: start;
cursor: pointer;
transition: background 0.13s ease;
}
.provider-item:last-of-type {
border-bottom: none;
}
.provider-item:hover:not(:disabled),
.provider-item:focus-visible:not(:disabled) {
background: var(--sidebar-hover);
}
.provider-item:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: -2px;
}
.provider-item:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.provider-item-text {
display: flex;
flex-direction: column;
gap: 0.1rem;
min-width: 0;
}
.provider-item-name {
font-size: 0.875rem;
font-weight: 600;
line-height: 1.5;
}
.provider-item-reason {
font-size: 0.75rem;
color: var(--muted);
line-height: 1.6;
}
.provider-item-check {
display: inline-flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
color: var(--color-primary);
flex-shrink: 0;
}
.provider-menu-foot {
padding: 0.55rem 1rem;
border-top: 1px solid var(--surface-border);
background: var(--sidebar-hover);
color: var(--muted);
font-size: 0.72rem;
line-height: 1.7;
}
/* ---------- Inline error under the pill ---------- */
.provider-error {
display: inline-flex;
align-items: center;
gap: 0.375rem;
max-width: 100%;
padding: 0.2rem 0.55rem;
border: 1px solid rgba(239, 68, 68, 0.4);
border-radius: 0.5rem;
background: rgba(239, 68, 68, 0.08);
color: #dc2626;
font-size: 0.72rem;
line-height: 1.6;
}
/* Fallback state (provider list failed to load, no pill): the error is the only
content, so it stretches full-width and centers like a footnote line under
the chat input instead of hugging the side. */
.provider-picker.is-fallback .provider-error {
display: flex;
width: 100%;
justify-content: center;
text-align: center;
border: none;
background: transparent;
color: var(--muted);
padding-inline: 0.75rem;
}
.provider-error-close {
display: inline-flex;
align-items: center;
justify-content: center;
width: 18px;
height: 18px;
border: none;
border-radius: 50%;
background: transparent;
color: inherit;
cursor: pointer;
flex-shrink: 0;
}
.provider-error-close:hover {
background: rgba(239, 68, 68, 0.15);
}
@media (max-width: 640px) {
.provider-menu {
min-width: min(270px, calc(100vw - 2rem));
}
}
@media (prefers-reduced-motion: reduce) {
.provider-pill,
.provider-pill-chevron,
.provider-menu,
.provider-item {
transition: none !important;
animation: none !important;
}
}
@@ -0,0 +1,100 @@
<div class="provider-picker" #container dir="rtl" [class.is-fallback]="!options().length">
@if (options().length) {
<!-- Pill trigger pinned to the end of the footnote row (bottom-right in LTR screenshots, bottom-left in RTL) -->
<div class="provider-pill-row">
<button
#trigger
type="button"
class="provider-pill"
[class.is-open]="isOpen()"
[disabled]="switching()"
[attr.aria-expanded]="isOpen()"
aria-haspopup="menu"
aria-controls="voice-provider-menu"
[attr.aria-label]="'ارائه‌دهنده: ' + selectedLabel() + ' — برای تغییر کلیک کنید'"
(click)="toggleMenu()"
>
<span class="provider-pill-dot" [class.is-busy]="switching()" aria-hidden="true"></span>
<span class="provider-pill-label">{{ switching() ? 'در حال تغییر…' : selectedLabel() }}</span>
<svg
class="provider-pill-chevron"
width="14"
height="14"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="m6 9 6 6 6-6" />
</svg>
</button>
</div>
@if (isOpen()) {
<div
#menu
id="voice-provider-menu"
class="provider-menu"
role="menu"
aria-label="انتخاب ارائه‌دهندهٔ صدا"
(keydown)="onMenuKeydown($event)"
>
@for (provider of options(); track provider.id) {
<button
type="button"
class="provider-item"
role="menuitemradio"
[attr.aria-checked]="provider.id === selectedId()"
[disabled]="!provider.can_start"
[title]="!provider.can_start && provider.reason ? provider.reason : provider.label"
(click)="choose(provider.id)"
>
<span class="provider-item-text">
<span class="provider-item-name">{{ provider.label }}</span>
@if (!provider.can_start) {
<span class="provider-item-reason">{{ provider.reason || 'فعلاً در دسترس نیست' }}</span>
} @else if (provider.id === selectedId()) {
<span class="provider-item-reason">فعال برای گفتگوی بعدی</span>
} @else {
<span class="provider-item-reason">کلیک برای انتخاب</span>
}
</span>
<span class="provider-item-check" aria-hidden="true">
@if (provider.id === selectedId()) {
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
<path d="M20 6 9 17l-5-5" />
</svg>
}
</span>
</button>
}
<div class="provider-menu-foot">
@if (callOpen()) {
<span>تغییر ارائه‌دهنده تماس فعلی را پایان می‌دهد.</span>
} @else {
<span>انتخاب شما برای گفتگوی بعدی استفاده می‌شود.</span>
}
</div>
</div>
}
}
<!-- Load/switch failures render here: inside the footnote row, under the chat input.
Kept outside the options guard on purpose — this error fires exactly when the
provider list failed to load (options empty), so gating on options would hide it. -->
@if (choiceError(); as err) {
<div class="provider-error" role="status">
<span>{{ err }}</span>
<button type="button" class="provider-error-close" aria-label="بستن پیام" (click)="dismissError()">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<line x1="18" x2="6" y1="6" y2="18" />
<line x1="6" x2="18" y1="6" y2="18" />
</svg>
</button>
</div>
}
</div>
@@ -0,0 +1,139 @@
import {
ChangeDetectionStrategy,
Component,
ElementRef,
Injector,
OnInit,
afterNextRender,
computed,
effect,
inject,
signal,
viewChild,
} from '@angular/core';
import { VoiceProvider } from '../../../services/voice-stream/voice-api.service';
import { VoiceStreamService } from '../../../services/voice-stream/voice-stream.service';
/**
* Claude-style provider picker that lives under the chat input.
* A small pill shows the selected provider; clicking it opens a popover
* menu with every provider option (checkmark on the active one).
* All state lives in `VoiceStreamService` — this component only renders it.
*/
@Component({
selector: 'app-voice-provider-picker',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './voice-provider-picker.component.html',
styleUrl: './voice-provider-picker.component.css',
})
export class VoiceProviderPickerComponent implements OnInit {
private readonly injector = inject(Injector);
private readonly voice = inject(VoiceStreamService);
readonly options = this.voice.providerOptions;
readonly selectedId = this.voice.selectedProvider;
readonly switching = this.voice.providerSwitching;
readonly choiceError = this.voice.providerChoiceError;
readonly callOpen = this.voice.isOpen;
readonly isOpen = signal(false);
readonly selectedOption = computed(() =>
this.options().find((row) => row.id === this.selectedId()) ?? null,
);
/** Pill text: backend label, or the legacy "service default" fallback. */
readonly selectedLabel = computed(() => this.selectedOption()?.label ?? 'پیش‌فرض سرویس');
private readonly containerRef = viewChild<ElementRef<HTMLElement>>('container');
private readonly menuRef = viewChild<ElementRef<HTMLDivElement>>('menu');
private readonly triggerRef = viewChild<ElementRef<HTMLButtonElement>>('trigger');
constructor() {
// Close on any pointer press outside the pill + menu, only while open.
effect((onCleanup) => {
if (!this.isOpen()) return;
const onPointerDown = (event: PointerEvent): void => {
const target = event.target as Node | null;
if (target && this.containerRef()?.nativeElement.contains(target)) return;
this.closeMenu();
};
document.addEventListener('pointerdown', onPointerDown);
onCleanup(() => document.removeEventListener('pointerdown', onPointerDown));
});
}
ngOnInit(): void {
void this.voice.loadProviders();
}
toggleMenu(): void {
if (this.switching()) return;
const opening = !this.isOpen();
this.isOpen.set(opening);
if (opening) {
afterNextRender(() => this.menuItems()[0]?.focus(), { injector: this.injector });
}
}
closeMenu(focusTrigger = false): void {
if (!this.isOpen()) return;
this.isOpen.set(false);
if (focusTrigger) this.triggerRef()?.nativeElement.focus();
}
async choose(provider: VoiceProvider): Promise<void> {
if (provider === this.selectedId()) {
this.closeMenu(true);
return;
}
const ok = await this.voice.selectProvider(provider);
// Keep the menu open on failure so the error stays visible next to it.
if (ok) this.closeMenu(true);
}
onMenuKeydown(event: KeyboardEvent): void {
if (event.key === 'Escape') {
event.preventDefault();
this.closeMenu(true);
return;
}
const items = this.menuItems();
if (!items.length) return;
const index = items.indexOf(document.activeElement as HTMLElement);
let next = -1;
switch (event.key) {
case 'ArrowDown':
next = (index + 1) % items.length;
break;
case 'ArrowUp':
next = (index - 1 + items.length) % items.length;
break;
case 'Home':
next = 0;
break;
case 'End':
next = items.length - 1;
break;
case 'Tab':
this.closeMenu();
return;
default:
return;
}
event.preventDefault();
items[next].focus();
}
dismissError(): void {
this.choiceError.set('');
}
private menuItems(): HTMLElement[] {
const menu = this.menuRef()?.nativeElement;
return menu
? Array.from(menu.querySelectorAll<HTMLElement>('[role="menuitemradio"]:not(:disabled)'))
: [];
}
}