import { Component, OnInit, OnDestroy } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Router, RouterLink, RouterOutlet } from '@angular/router'; import { Subscription } from 'rxjs'; import { MatIcon } from '@angular/material/icon'; import { MatButtonModule } from '@angular/material/button'; import { MatMenuModule } from '@angular/material/menu'; import { MatTooltipModule } from '@angular/material/tooltip'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { AuthService } from './services/auth.service'; import { EventService } from './services/event.service'; import { SessionService } from './services/session.service'; import { WindowService } from './services/window.service'; import { ConfigService } from 'base-core'; import { NewProjectModalComponent } from './components/new-project-modal.component'; @Component({ selector: 'app-root', imports: [CommonModule, RouterOutlet, RouterLink, MatIcon, MatButtonModule, MatMenuModule, MatTooltipModule, MatProgressSpinnerModule, NewProjectModalComponent], templateUrl: './app.component.html', styleUrl: './app.component.scss' }) export class AppComponent implements OnInit, OnDestroy { showNewProjectModal = false; private subscriptions: Subscription = new Subscription(); isEventStreamConnected = false; constructor( private router: Router, public authService: AuthService, private config: ConfigService, private eventService: EventService, private sessionService: SessionService, private windowService: WindowService ) {} ngOnInit() { console.log('AppComponent初始化'); // 禁用模拟数据,使用真实API this.config.useMockData = false; this.config.apiBaseUrl = '/api'; console.log('使用真实API,配置:', { useMockData: this.config.useMockData, apiBaseUrl: this.config.apiBaseUrl }); // 订阅事件流连接状态 this.subscriptions.add( this.eventService.connectionStatus$.subscribe(connected => { this.isEventStreamConnected = connected; console.log('事件流连接状态:', connected ? '已连接' : '已断开'); }) ); // 如果已登录,加载项目 if (this.authService.isAuthenticated()) { this.loadProjects(); } else { // 订阅认证状态变化 this.subscriptions.add( this.authService.authState$.subscribe(authState => { if (authState.isAuthenticated) { console.log('用户已认证,加载项目'); this.loadProjects(); } }) ); } } // 加载项目列表 loadProjects() { this.sessionService.getSessions().subscribe({ next: (sessions: any[]) => { // 从会话中提取项目信息 const projects = sessions .filter(session => session.project_id) .map(session => ({ id: session.project_id!, title: session.title, description: session.description, agent: session.agent_name, created_at: session.created_at, updated_at: session.updated_at })); // 去重:相同project_id只保留最新 const uniqueProjects = Array.from( new Map(projects.map(p => [p.id, p])).values() ); console.log('加载项目列表:', uniqueProjects.length); // 可以在这里更新标签页服务中的项目数据(如果需要) // this.tabService.updateProjects(uniqueProjects); }, error: (error) => { console.error('加载项目失败:', error); } }); } get isLoginPage(): boolean { return this.router.url.includes('/login') || this.router.url.includes('/project/'); } // 导航到主页 goHome() { this.router.navigate(['/home']); } // 打开新建项目模态框 openNewProjectModal() { console.log('🔍 [AppComponent] 打开新建项目模态框,当前状态:', this.showNewProjectModal); this.showNewProjectModal = true; console.log('🔍 [AppComponent] 设置后状态:', this.showNewProjectModal); } // 处理项目创建 onProjectCreated(project: any) { // 在新浏览器标签页中打开项目 const projectId = project.project_id || project.id; console.log('🔍 [AppComponent] 新项目创建,打开标签页:', projectId); const success = this.windowService.openProjectTab(projectId); if (!success) { console.error('🔍 [AppComponent] 打开项目标签页失败'); } } logout() { this.authService.logout(); } get username(): string { return this.authService.getCurrentUser()?.username || '用户'; } ngOnDestroy() { this.subscriptions.unsubscribe(); } }