-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprofile-developer.component.ts
More file actions
84 lines (72 loc) · 2.85 KB
/
Copy pathprofile-developer.component.ts
File metadata and controls
84 lines (72 loc) · 2.85 KB
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
// Copyright The Linux Foundation and each contributor to LFX.
// SPDX-License-Identifier: MIT
import { Clipboard } from '@angular/cdk/clipboard';
import { Component, computed, inject, Signal, signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { ButtonComponent } from '@components/button/button.component';
import { CardComponent } from '@components/card/card.component';
import { MessageComponent } from '@components/message/message.component';
import { UserService } from '@services/user.service';
import { MessageService } from 'primeng/api';
import { ToastModule } from 'primeng/toast';
import { finalize } from 'rxjs';
@Component({
selector: 'lfx-profile-developer',
imports: [ButtonComponent, CardComponent, MessageComponent, ToastModule],
templateUrl: './profile-developer.component.html',
})
export class ProfileDeveloperComponent {
private readonly userService = inject(UserService);
private readonly messageService = inject(MessageService);
private readonly clipboard = inject(Clipboard);
// Loading state
public loading = signal<boolean>(true);
// Token data using toSignal pattern
public tokenInfo = this.initializeTokenInfo();
// Loading state computed from tokenInfo
public readonly isLoading = computed(() => this.loading());
// API token computed from tokenInfo
public readonly apiToken = computed(() => this.tokenInfo()?.token || '');
// Token visibility toggle
public maskToken = signal<boolean>(true);
// Computed masked token
public readonly maskedToken = computed(() => {
const token = this.apiToken();
if (!token) return '';
if (token.length <= 8) return '*'.repeat(token.length);
// Show first 4 chars + fixed number of asterisks + last 4 chars for better UX
return token.substring(0, 4) + '••••••••••••' + token.substring(token.length - 4);
});
public toggleTokenVisibility(): void {
this.maskToken.set(!this.maskToken());
}
public copyToken(): void {
const token = this.apiToken();
if (!token) {
this.messageService.add({
severity: 'warn',
summary: 'No Token',
detail: 'No API token available to copy.',
});
return;
}
const success = this.clipboard.copy(token);
if (success) {
this.messageService.add({
severity: 'success',
summary: 'Copied',
detail: 'API token copied to clipboard successfully.',
});
} else {
this.messageService.add({
severity: 'error',
summary: 'Copy Failed',
detail: 'Failed to copy token to clipboard. Please try again.',
});
}
}
private initializeTokenInfo(): Signal<{ token: string; type: string } | null> {
this.loading.set(true);
return toSignal(this.userService.getDeveloperTokenInfo().pipe(finalize(() => this.loading.set(false))), { initialValue: null });
}
}