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:
@@ -435,7 +435,7 @@
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (documents().length > 0) {
|
||||
@if (documents().length > 0 || hasActiveFilters()) {
|
||||
<div class="documents-filters mb-3 flex items-center justify-between gap-2">
|
||||
<div class="flex items-center gap-2">
|
||||
<select
|
||||
@@ -456,22 +456,43 @@
|
||||
</label>
|
||||
<span class="text-xs text-[var(--text-muted)]">فقط فعال</span>
|
||||
</div>
|
||||
<button type="button" class="btn btn--outline" (click)="refreshDocuments()" [disabled]="documentsLoading()">
|
||||
<svg
|
||||
class="w-4 h-4"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M20 7v5h-5" />
|
||||
<path d="M4 17v-5h5" />
|
||||
<path d="M5.6 9a7 7 0 0 1 11.55-2.6L20 12" />
|
||||
<path d="M4 12l2.85 5.6A7 7 0 0 0 18.4 15" />
|
||||
</svg>
|
||||
<span>بهروزرسانی</span>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn--outline"
|
||||
(click)="refreshDocuments()"
|
||||
[disabled]="documentsLoading()"
|
||||
[attr.aria-busy]="documentsLoading()"
|
||||
>
|
||||
@if (documentsLoading()) {
|
||||
<svg
|
||||
class="w-4 h-4 animate-spin"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<circle cx="12" cy="12" r="9" stroke-opacity="0.2" />
|
||||
<path d="M21 12a9 9 0 0 0-9-9" />
|
||||
</svg>
|
||||
} @else {
|
||||
<svg
|
||||
class="w-4 h-4"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M20 7v5h-5" />
|
||||
<path d="M4 17v-5h5" />
|
||||
<path d="M5.6 9a7 7 0 0 1 11.55-2.6L20 12" />
|
||||
<path d="M4 12l2.85 5.6A7 7 0 0 0 18.4 15" />
|
||||
</svg>
|
||||
}
|
||||
<span>{{ documentsLoading() ? 'در حال بهروزرسانی…' : 'بهروزرسانی' }}</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -614,18 +635,29 @@
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
} @empty {
|
||||
@if (!documentsLoading()) {
|
||||
<tr>
|
||||
<td
|
||||
colspan="7"
|
||||
class="px-3 py-10 text-center text-sm text-[var(--text-muted)]"
|
||||
>
|
||||
هیچ سندی با فیلتر انتخابشده مطابقت ندارد.
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
@if (!hasMoreDocuments() && !documentsLoading()) {
|
||||
@if (documents().length > 0 && !hasMoreDocuments() && !documentsLoading()) {
|
||||
<p class="text-xs text-[var(--text-muted)] text-center mt-3">
|
||||
{{ documents().length }} سند ثبت شده
|
||||
</p>
|
||||
}
|
||||
|
||||
@if (hasMoreDocuments() && !documentsLoading()) {
|
||||
@if (documents().length > 0 && hasMoreDocuments() && !documentsLoading()) {
|
||||
<div class="documents-pagination mt-3 text-center">
|
||||
<button type="button" class="btn btn--outline" (click)="loadMoreDocuments()">
|
||||
بیشتر بارگذاری
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { provideZonelessChangeDetection } from '@angular/core';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
import { provideRouter } from '@angular/router';
|
||||
import { of, Subject, type Observable } from 'rxjs';
|
||||
import { vi } from 'vitest';
|
||||
import { SettingsPage } from './settings.page';
|
||||
import { DocumentsService } from '../../services/documents/documents.service';
|
||||
import {
|
||||
DocumentListItem,
|
||||
DocumentListResponse,
|
||||
} from '../../services/documents/documents.types';
|
||||
import { AuthService } from '../../services/auth/auth.service';
|
||||
import { HttpService } from '../../services/http-request/http.service';
|
||||
import { ThemeService } from '../../services/theme/theme.service';
|
||||
import { ToastrService } from 'ngx-toastr';
|
||||
|
||||
function readyDoc(): DocumentListItem {
|
||||
return {
|
||||
id: 'doc-1',
|
||||
title: 'سند آزمون',
|
||||
filename: 'guide.pdf',
|
||||
status: 'ready',
|
||||
is_active: true,
|
||||
searchable: true,
|
||||
chunk_count: 12,
|
||||
created_at: '2024-01-01T10:00:00Z',
|
||||
job: { id: 'job-1', status: 'ready', stage: null, attempt_count: 0, retry_generation: 0, can_retry: false, error: null },
|
||||
review_required: false,
|
||||
};
|
||||
}
|
||||
|
||||
const filterSelect = (el: HTMLElement): HTMLSelectElement | null => el.querySelector('.documents-filters select');
|
||||
|
||||
describe('SettingsPage — document filters', () => {
|
||||
let getDocuments: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Server-side filtering: `all` returns one ready doc; every other status matches none.
|
||||
getDocuments = vi.fn((params: { status?: string } = {}): Observable<DocumentListResponse> => {
|
||||
const items = params.status ? [] : [readyDoc()];
|
||||
return of({ items, next_cursor: null, knowledge_revision: 0 });
|
||||
});
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [SettingsPage],
|
||||
providers: [
|
||||
provideZonelessChangeDetection(),
|
||||
provideHttpClient(),
|
||||
provideRouter([]),
|
||||
{
|
||||
provide: DocumentsService,
|
||||
useValue: {
|
||||
getConfiguration: () => of({}),
|
||||
getDocuments,
|
||||
getKnowledgeSelection: () => of({ revision: 0, document_ids: ['doc-1'] }),
|
||||
},
|
||||
},
|
||||
{ provide: AuthService, useValue: { getCurrentUserValue: () => ({ role: 'admin' }) } },
|
||||
{ provide: HttpService, useValue: { apiUrl: '/api/v1' } },
|
||||
{ provide: ThemeService, useValue: { isDarkMode: () => false, toggleDarkMode: () => {} } },
|
||||
{ provide: ToastrService, useValue: { success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() } },
|
||||
],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
it('renders the filter select and one document row initially', () => {
|
||||
const fixture = TestBed.createComponent(SettingsPage);
|
||||
fixture.detectChanges(); // ngOnInit → load all documents
|
||||
|
||||
expect(filterSelect(fixture.nativeElement as HTMLElement)).not.toBeNull();
|
||||
expect((fixture.nativeElement as HTMLElement).querySelectorAll('.documents-table tbody tr').length).toBe(1);
|
||||
});
|
||||
|
||||
it('keeps the filter select and table shell when a filter matches no rows', () => {
|
||||
const fixture = TestBed.createComponent(SettingsPage);
|
||||
fixture.detectChanges();
|
||||
const el = fixture.nativeElement as HTMLElement;
|
||||
|
||||
// Select a status that has zero documents.
|
||||
const component = fixture.componentInstance;
|
||||
component.activeFilter.set('failed');
|
||||
component.applyDocumentFilters();
|
||||
fixture.detectChanges();
|
||||
|
||||
// Regression: the whole panel used to vanish; the select + table must stay so the user
|
||||
// can change the filter back, and an in-table "no match" message is shown instead.
|
||||
expect(filterSelect(el)).not.toBeNull();
|
||||
expect(el.querySelector('.documents-table tbody tr')).not.toBeNull();
|
||||
expect(el.textContent).toContain('هیچ سندی با فیلتر انتخابشده مطابقت ندارد.');
|
||||
expect(el.textContent).not.toContain('هنوز سندی آپلود نشده است.');
|
||||
// The "N سند ثبت شده" footer must not claim 0 documents.
|
||||
expect(el.textContent).not.toContain('سند ثبت شده');
|
||||
});
|
||||
|
||||
it('restores the rows when the filter is reset back to all', () => {
|
||||
const fixture = TestBed.createComponent(SettingsPage);
|
||||
fixture.detectChanges();
|
||||
const component = fixture.componentInstance;
|
||||
|
||||
component.activeFilter.set('processing'); // empty
|
||||
component.applyDocumentFilters();
|
||||
fixture.detectChanges();
|
||||
expect((fixture.nativeElement as HTMLElement).querySelectorAll('.documents-table tbody tr').length).toBe(1); // just the empty row
|
||||
|
||||
component.activeFilter.set('all'); // back to everything
|
||||
component.applyDocumentFilters();
|
||||
fixture.detectChanges();
|
||||
const rows = (fixture.nativeElement as HTMLElement).querySelectorAll('.documents-table tbody tr');
|
||||
expect(rows.length).toBe(1);
|
||||
expect(rows[0].textContent).toContain('guide.pdf');
|
||||
});
|
||||
|
||||
it('hasActiveFilters reflects the active status/active-only filters', () => {
|
||||
const component = TestBed.createComponent(SettingsPage).componentInstance;
|
||||
expect(component.hasActiveFilters()).toBe(false);
|
||||
component.activeFilter.set('failed');
|
||||
expect(component.hasActiveFilters()).toBe(true);
|
||||
component.activeFilter.set('all');
|
||||
component.activeOnly.set(true);
|
||||
expect(component.hasActiveFilters()).toBe(true);
|
||||
});
|
||||
|
||||
it('shows a loading spinner on the بهروزرسانی button while documents are being fetched', () => {
|
||||
// The initial load resolves immediately; the refresh load stays pending so we can observe
|
||||
// the in-flight state (previously the button only disabled with no visible feedback).
|
||||
let call = 0;
|
||||
let pending!: Subject<DocumentListResponse>;
|
||||
getDocuments.mockReset();
|
||||
getDocuments.mockImplementation((): Observable<DocumentListResponse> => {
|
||||
call += 1;
|
||||
if (call === 1) return of({ items: [readyDoc()], next_cursor: null, knowledge_revision: 0 });
|
||||
pending = new Subject<DocumentListResponse>();
|
||||
return pending;
|
||||
});
|
||||
|
||||
const fixture = TestBed.createComponent(SettingsPage);
|
||||
fixture.detectChanges(); // initial load
|
||||
const el = fixture.nativeElement as HTMLElement;
|
||||
const refresh = el.querySelector('.documents-filters button') as HTMLButtonElement;
|
||||
expect(refresh.textContent).toContain('بهروزرسانی');
|
||||
expect(refresh.querySelector('.animate-spin')).toBeNull();
|
||||
|
||||
refresh.click(); // start refresh → stays pending
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(refresh.disabled).toBe(true);
|
||||
expect(refresh.getAttribute('aria-busy')).toBe('true');
|
||||
expect(refresh.textContent).toContain('در حال بهروزرسانی');
|
||||
expect(refresh.querySelector('.animate-spin')).not.toBeNull();
|
||||
|
||||
pending.next({ items: [readyDoc()], next_cursor: null, knowledge_revision: 0 });
|
||||
fixture.detectChanges();
|
||||
expect(refresh.disabled).toBe(false);
|
||||
expect(refresh.textContent).toContain('بهروزرسانی');
|
||||
expect(refresh.querySelector('.animate-spin')).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -203,6 +203,13 @@ export class SettingsPage implements OnInit, OnDestroy {
|
||||
readonly activeFilter = signal<DocumentStatus | 'all'>('all');
|
||||
readonly activeOnly = signal<boolean>(false);
|
||||
|
||||
/**
|
||||
* True when a document filter (status other than "all", or active-only) is applied. Keeps
|
||||
* the filter controls + table shell visible even when the filter matches zero rows, so the
|
||||
* user can change/reset the filter instead of the whole panel vanishing.
|
||||
*/
|
||||
readonly hasActiveFilters = computed(() => this.activeFilter() !== 'all' || this.activeOnly());
|
||||
|
||||
readonly hasActiveDocuments = computed(() => this.documents().some((d) => d.is_active));
|
||||
|
||||
readonly currentUserRole = computed<'admin' | 'user' | null>(() => {
|
||||
|
||||
Reference in New Issue
Block a user