import { Component, OnInit, inject, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ActivatedRoute, Router } from '@angular/router';
import { ApiService, Service } from '../../core/services/api.service';
@Component({
selector: 'app-service-detail',
standalone: true,
imports: [CommonModule],
template: `
@if (loading()) {
} @else if (service()) {
{{ service()!.name }}
{{ service()!.status }}
Service Details
- Image
- {{ service()!.image }}
- Port
- {{ service()!.port }}
@if (service()!.domain) {
}
@if (service()!.containerID) {
- Container ID
- {{ service()!.containerID?.substring(0, 12) }}
}
- Created
- {{ formatDate(service()!.createdAt) }}
- Updated
- {{ formatDate(service()!.updatedAt) }}
@if (Object.keys(service()!.envVars).length > 0) {
Environment Variables
@for (entry of Object.entries(service()!.envVars); track entry[0]) {
{{ entry[0] }}
{{ entry[1] }}
}
}
Actions
@if (service()!.status === 'stopped') {
}
@if (service()!.status === 'running') {
}
Logs
@if (loadingLogs()) {
} @else {
{{ logs() || 'No logs available' }}
}
}
`,
})
export class ServiceDetailComponent implements OnInit {
private apiService = inject(ApiService);
private route = inject(ActivatedRoute);
private router = inject(Router);
service = signal(null);
logs = signal('');
loading = signal(true);
loadingLogs = signal(false);
Object = Object;
ngOnInit(): void {
const name = this.route.snapshot.paramMap.get('name')!;
this.loadService(name);
this.loadLogs(name);
}
loadService(name: string): void {
this.loading.set(true);
this.apiService.getService(name).subscribe({
next: (response) => {
if (response.success && response.data) {
this.service.set(response.data);
}
this.loading.set(false);
},
error: () => {
this.loading.set(false);
this.router.navigate(['/services']);
},
});
}
loadLogs(name: string): void {
this.loadingLogs.set(true);
this.apiService.getServiceLogs(name).subscribe({
next: (response) => {
if (response.success && response.data) {
this.logs.set(response.data);
}
this.loadingLogs.set(false);
},
error: () => {
this.loadingLogs.set(false);
},
});
}
refreshLogs(): void {
this.loadLogs(this.service()!.name);
}
startService(): void {
this.apiService.startService(this.service()!.name).subscribe({
next: () => {
this.loadService(this.service()!.name);
},
});
}
stopService(): void {
this.apiService.stopService(this.service()!.name).subscribe({
next: () => {
this.loadService(this.service()!.name);
},
});
}
restartService(): void {
this.apiService.restartService(this.service()!.name).subscribe({
next: () => {
this.loadService(this.service()!.name);
},
});
}
deleteService(): void {
if (confirm(`Are you sure you want to delete ${this.service()!.name}?`)) {
this.apiService.deleteService(this.service()!.name).subscribe({
next: () => {
this.router.navigate(['/services']);
},
});
}
}
formatDate(timestamp: number): string {
return new Date(timestamp).toLocaleString();
}
}