Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

project-page.component.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { Component, OnInit, OnDestroy } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { ActivatedRoute, Router } from '@angular/router';
  4. import { Subscription } from 'rxjs';
  5. import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
  6. import { MatIconModule } from '@angular/material/icon';
  7. import { MatButtonModule } from '@angular/material/button';
  8. import { MatTooltipModule } from '@angular/material/tooltip';
  9. import { ProjectTabComponent } from '../../components/project-tab.component';
  10. import { SessionService } from '../../services/session.service';
  11. import { IndependentEventService } from '../../services/independent-event.service';
  12. import { Session } from '../../models/session.model';
  13. @Component({
  14. selector: 'app-project-page',
  15. standalone: true,
  16. imports: [
  17. CommonModule,
  18. MatProgressSpinnerModule,
  19. MatIconModule,
  20. MatButtonModule,
  21. MatTooltipModule,
  22. ProjectTabComponent
  23. ],
  24. templateUrl: './project-page.component.html',
  25. styleUrl: './project-page.component.scss',
  26. providers: [IndependentEventService] // 组件级独立事件服务
  27. })
  28. export class ProjectPageComponent implements OnInit, OnDestroy {
  29. projectId: string | null = null;
  30. project: Session | null = null;
  31. instanceId: string = '';
  32. isLoading = true;
  33. error: string | null = null;
  34. // 公开独立事件服务供模板使用
  35. get independentEventService(): IndependentEventService {
  36. return this._independentEventService;
  37. }
  38. private routeSub?: Subscription;
  39. private sessionsSub?: Subscription;
  40. constructor(
  41. private route: ActivatedRoute,
  42. private router: Router,
  43. private sessionService: SessionService,
  44. private _independentEventService: IndependentEventService
  45. ) {}
  46. ngOnInit() {
  47. console.log('🔍 [ProjectPageComponent] 初始化');
  48. this.routeSub = this.route.paramMap.subscribe(params => {
  49. const id = params.get('projectId');
  50. console.log('🔍 [ProjectPageComponent] 路由参数变化,新ID:', id, '旧ID:', this.projectId);
  51. if (id && id !== this.projectId) {
  52. this.projectId = id;
  53. this.loadProject(id);
  54. } else if (id === this.projectId) {
  55. console.log('🔍 [ProjectPageComponent] 项目ID未变化,跳过重新加载');
  56. } else {
  57. console.log('🔍 [ProjectPageComponent] 无项目ID或ID为空');
  58. }
  59. });
  60. }
  61. loadProject(projectId: string) {
  62. console.log('🔍 [ProjectPageComponent] 加载项目:', projectId);
  63. // 取消之前的订阅
  64. if (this.sessionsSub) {
  65. this.sessionsSub.unsubscribe();
  66. this.sessionsSub = undefined;
  67. }
  68. // 重置状态
  69. this.isLoading = true;
  70. this.error = null;
  71. this.project = null;
  72. // 使用项目ID+时间戳确保实例ID唯一,避免组件复用问题
  73. this.instanceId = `project-${projectId}-${Date.now()}`;
  74. // 订阅会话列表变化,查找对应项目
  75. this.sessionsSub = this.sessionService.sessions$.subscribe(sessions => {
  76. const session = sessions.find(s => s.project_id === projectId || s.id === projectId);
  77. if (session) {
  78. console.log('🔍 [ProjectPageComponent] 找到项目会话:', session.title);
  79. this.project = session;
  80. this.isLoading = false;
  81. // 建立独立SSE连接(使用会话ID)
  82. console.log('🔍 [ProjectPageComponent] 建立独立SSE连接,会话ID:', session.id);
  83. this._independentEventService.connect(session.id);
  84. } else {
  85. console.log('🔍 [ProjectPageComponent] 未找到项目,重新加载会话列表');
  86. // 如果没有找到,尝试加载会话
  87. this.sessionService.loadSessions();
  88. }
  89. });
  90. // 初始加载会话
  91. this.sessionService.loadSessions();
  92. }
  93. goBack() {
  94. this.router.navigate(['/home']);
  95. }
  96. ngOnDestroy() {
  97. if (this.routeSub) {
  98. this.routeSub.unsubscribe();
  99. }
  100. if (this.sessionsSub) {
  101. this.sessionsSub.unsubscribe();
  102. }
  103. // 断开独立SSE连接
  104. console.log('🔍 [ProjectPageComponent] 组件销毁,断开独立SSE连接');
  105. this._independentEventService.disconnect();
  106. }
  107. }