-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile-layout.component.ts
More file actions
236 lines (200 loc) · 7.56 KB
/
profile-layout.component.ts
File metadata and controls
236 lines (200 loc) · 7.56 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright The Linux Foundation and each contributor to LFX.
// SPDX-License-Identifier: MIT
import { CommonModule } from '@angular/common';
import { Component, computed, inject, input, Signal, signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { RouterModule } from '@angular/router';
import { CombinedProfile } from '@lfx-one/shared/interfaces';
import { UserService } from '@services/user.service';
import { AvatarComponent } from '@shared/components/avatar/avatar.component';
import { BreadcrumbComponent } from '@shared/components/breadcrumb/breadcrumb.component';
import { HeaderComponent } from '@shared/components/header/header.component';
import { MenuItem } from 'primeng/api';
import { ChipModule } from 'primeng/chip';
import { finalize } from 'rxjs';
import { ProfileStatsComponent } from './components/profile-stats/profile-stats.component';
@Component({
selector: 'lfx-profile-layout',
standalone: true,
imports: [CommonModule, RouterModule, HeaderComponent, AvatarComponent, BreadcrumbComponent, ChipModule, ProfileStatsComponent],
templateUrl: './profile-layout.component.html',
styleUrl: './profile-layout.component.scss',
})
export class ProfileLayoutComponent {
private readonly userService = inject(UserService);
// Load current user profile data
public loading = signal<boolean>(true);
public profile = this.initializeProfile();
// Computed profile data
public readonly profileTitle = this.initializeProfileTitle();
public readonly profileSubtitle = this.initializeProfileSubtitle();
public readonly profileLocation = this.initializeProfileLocation();
public readonly userInitials = this.initializeUserInitials();
public readonly memberSince = this.initializeMemberSince();
public readonly lastActive = this.initializeLastActive();
// Loading state
public readonly breadcrumbItems = input<MenuItem[]>([
{
label: 'Home',
routerLink: '/',
icon: 'fa-light fa-chevron-left',
routerLinkActiveOptions: { exact: false },
},
]);
// Menu items for profile sections
public readonly menuItems: Signal<MenuItem[]> = this.initializeMenuItems();
private initializeProfile(): Signal<CombinedProfile | null> {
return toSignal(this.userService.getCurrentUserProfile().pipe(finalize(() => this.loading.set(false))), { initialValue: null });
}
private initializeUserInitials(): Signal<string> {
return computed(() => {
const profile = this.profile();
if (!profile?.user) return '';
const user = profile.user;
const firstName = user.first_name;
const lastName = user.last_name;
const email = user.email;
const username = user.username;
// Priority: first/last name > username > email
if (firstName && lastName) {
return `${firstName.charAt(0)}${lastName.charAt(0)}`.toUpperCase();
}
if (firstName) {
return firstName.charAt(0).toUpperCase();
}
if (username) {
return username.charAt(0).toUpperCase();
}
return email.charAt(0).toUpperCase();
});
}
private initializeMenuItems(): Signal<MenuItem[]> {
return computed(() => [
{
label: 'Edit Profile',
icon: 'fa-light fa-user-edit text-blue-500',
routerLink: '/profile',
routerLinkActiveOptions: { exact: true },
},
{
label: 'Password',
icon: 'fa-light fa-key text-amber-500',
routerLink: '/profile/password',
routerLinkActiveOptions: { exact: true },
},
{
label: 'Email Settings',
icon: 'fa-light fa-envelope text-green-500',
routerLink: '/profile/email',
routerLinkActiveOptions: { exact: true },
},
{
label: 'Developer Settings',
icon: 'fa-light fa-code text-purple-500',
routerLink: '/profile/developer',
routerLinkActiveOptions: { exact: true },
},
]);
}
private initializeProfileTitle(): Signal<string> {
return computed(() => {
const profile = this.profile();
if (!profile?.user) return '';
const user = profile.user;
const firstName = user.first_name;
const lastName = user.last_name;
// If the user has a first name and last name, use it
if (firstName && lastName) {
return `${firstName} ${lastName}`;
}
// If the user has a first name, use it
if (firstName) {
return firstName;
}
// If the user has a username, use it
if (user.username) {
return user.username;
}
// If the user has an email, use it
return user.email;
});
}
private initializeProfileSubtitle(): Signal<string> {
return computed(() => {
const profile = this.profile();
if (!profile?.profile) return '';
return profile.profile.job_title || '';
});
}
private initializeProfileLocation(): Signal<string> {
return computed(() => {
const profile = this.profile();
if (!profile?.profile) return '';
const parts = [];
if (profile.profile.city) parts.push(profile.profile.city);
if (profile.profile.state_province) parts.push(profile.profile.state_province);
if (profile.profile.country) parts.push(profile.profile.country);
return parts.join(', ');
});
}
private initializeMemberSince(): Signal<string> {
return computed(() => {
const profile = this.profile();
if (!profile?.user) return '';
return this.calculateMemberSince(profile.user.created_at);
});
}
private initializeLastActive(): Signal<string> {
return computed(() => {
const profile = this.profile();
if (!profile?.user) return '';
return this.calculateLastActive(profile.user.updated_at);
});
}
/**
* Calculate how long the user has been a member
*/
private calculateMemberSince(createdAt: string): string {
const created = new Date(createdAt);
const now = new Date();
const diffTime = Math.abs(now.getTime() - created.getTime());
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays < 30) {
return `${diffDays} day${diffDays === 1 ? '' : 's'}`;
} else if (diffDays < 365) {
const months = Math.floor(diffDays / 30);
return `${months} month${months === 1 ? '' : 's'}`;
}
const years = Math.floor(diffDays / 365);
const remainingMonths = Math.floor((diffDays % 365) / 30);
if (remainingMonths > 0) {
return `${years} year${years === 1 ? '' : 's'}, ${remainingMonths} month${remainingMonths === 1 ? '' : 's'}`;
}
return `${years} year${years === 1 ? '' : 's'}`;
}
/**
* Calculate last active time
*/
private calculateLastActive(updatedAt: string): string {
const updated = new Date(updatedAt);
const now = new Date();
const diffTime = Math.abs(now.getTime() - updated.getTime());
const diffMinutes = Math.floor(diffTime / (1000 * 60));
const diffHours = Math.floor(diffTime / (1000 * 60 * 60));
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
if (diffMinutes < 1) {
return 'Just now';
} else if (diffMinutes < 60) {
return `${diffMinutes} minute${diffMinutes === 1 ? '' : 's'} ago`;
} else if (diffHours < 24) {
return `${diffHours} hour${diffHours === 1 ? '' : 's'} ago`;
} else if (diffDays < 30) {
return `${diffDays} day${diffDays === 1 ? '' : 's'} ago`;
} else if (diffDays < 365) {
const months = Math.floor(diffDays / 30);
return `${months} month${months === 1 ? '' : 's'} ago`;
}
const years = Math.floor(diffDays / 365);
return `${years} year${years === 1 ? '' : 's'} ago`;
}
}