All files / app/shared/ipfs-status-indicator ipfs-status-indicator.component.ts

87.5% Statements 35/40
77.27% Branches 17/22
100% Functions 11/11
87.5% Lines 35/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                                                                                                                                                                        1x 18x 18x 18x 18x 18x 18x     18x 18x       15x   10x     10x     12x   2x 2x       12x 12x 7x               22x 22x           18x 18x       10x 10x 8x 6x     2x 2x         2x       30x 28x 2x 2x           2x 2x            
import { Component, OnInit, OnDestroy, Inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Subject, timer, of } from 'rxjs';
import { switchMap, takeUntil, catchError } from 'rxjs/operators';
import { StorageType, STORAGE_TYPE, STORAGE_PROVIDER } from '../../core/services/storage-provider.factory';
import { IStorageProvider } from '../../core/domain/interfaces/storage.interface';
 
@Component({
  selector: 'app-ipfs-status-indicator',
  standalone: true,
  imports: [CommonModule],
  template: `
    <div class="ipfs-status" *ngIf="storageType === StorageType.IPFS || storageType === StorageType.HELIA">
      <div class="status-indicator" [class.connected]="isConnected" [class.disconnected]="!isConnected">
        <span class="status-dot"></span>
        <span class="status-text">{{ getStatusText() }}</span>
      </div>
      <div class="status-details" *ngIf="showDetails">
        <p *ngIf="isConnected">{{ getHealthyMessage() }}</p>
        <p *ngIf="!isConnected && errorMessage">{{ errorMessage }}</p>
      </div>
    </div>
  `,
  styles: [`
    .ipfs-status {
      display: inline-block;
      padding: 8px 12px;
      background-color: #f0f0f0;
      border-radius: 4px;
      font-size: 14px;
    }
 
    .status-indicator {
      display: flex;
      align-items: center;
      gap: 8px;
      cursor: pointer;
    }
 
    .status-dot {
      width: 10px;
      height: 10px;
      border-radius: 50%;
      transition: background-color 0.3s;
    }
 
    .connected .status-dot {
      background-color: #27ae60;
      animation: pulse 2s infinite;
    }
 
    .disconnected .status-dot {
      background-color: #e74c3c;
    }
 
    .status-text {
      font-weight: 500;
    }
 
    .connected .status-text {
      color: #27ae60;
    }
 
    .disconnected .status-text {
      color: #e74c3c;
    }
 
    .status-details {
      margin-top: 8px;
      padding-top: 8px;
      border-top: 1px solid #ddd;
      font-size: 12px;
      color: #666;
    }
 
    .status-details p {
      margin: 0;
    }
 
    @keyframes pulse {
      0% {
        box-shadow: 0 0 0 0 rgba(39, 174, 96, 0.4);
      }
      70% {
        box-shadow: 0 0 0 10px rgba(39, 174, 96, 0);
      }
      100% {
        box-shadow: 0 0 0 0 rgba(39, 174, 96, 0);
      }
    }
  `]
})
export class IPFSStatusIndicatorComponent implements OnInit, OnDestroy {
  StorageType = StorageType;
  isConnected = false;
  showDetails = false;
  errorMessage = '';
  private destroy$ = new Subject<void>();
  private checkInterval = 30000; // Check every 30 seconds
 
  constructor(
    @Inject(STORAGE_TYPE) public storageType: StorageType,
    @Inject(STORAGE_PROVIDER) private storageProvider: IStorageProvider
  ) {}
 
  ngOnInit() {
    if (this.storageType === StorageType.IPFS || this.storageType === StorageType.HELIA) {
      // Initial check
      this.checkConnection();
 
      // Set up periodic checks
      timer(0, this.checkInterval)
        .pipe(
          takeUntil(this.destroy$),
          switchMap(() => this.checkProviderHealth()),
          catchError(error => {
            this.errorMessage = error.message || 'Connection failed';
            return of(false);
          })
        )
        .subscribe(isHealthy => {
          this.isConnected = isHealthy;
          if (isHealthy) {
            this.errorMessage = '';
          }
        });
    }
  }
 
  private async checkProviderHealth(): Promise<boolean> {
    // Check if the storage provider has isHealthy method
    if ('isHealthy' in this.storageProvider && typeof this.storageProvider.isHealthy === 'function') {
      return await this.storageProvider.isHealthy();
    }
    return false;
  }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  private async checkConnection() {
    try {
      this.isConnected = await this.checkProviderHealth();
      if (this.isConnected) {
        this.errorMessage = '';
      }
    } catch (error) {
      this.isConnected = false;
      this.errorMessage = error instanceof Error ? error.message : 'Connection failed';
    }
  }
 
  toggleDetails() {
    this.showDetails = !this.showDetails;
  }
 
  getStatusText(): string {
    if (this.storageType === StorageType.IPFS) {
      return `IPFS: ${this.isConnected ? 'Connected' : 'Disconnected'}`;
    } else if (this.storageType === StorageType.HELIA) {
      return `Helia: ${this.isConnected ? 'Active' : 'Inactive'}`;
    }
    return '';
  }
 
  getHealthyMessage(): string {
    if (this.storageType === StorageType.IPFS) {
      return 'Node is healthy';
    } else IEif (this.storageType === StorageType.HELIA) {
      return 'Browser IPFS is running';
    }
    return '';
  }
}