File size: 9,785 Bytes
66d5a73
 
 
 
 
 
 
e01b7e8
 
 
66d5a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e01b7e8
 
 
 
66d5a73
 
ce589a9
 
 
 
 
 
 
 
 
 
 
 
 
 
66d5a73
 
ce589a9
 
e01b7e8
 
 
ce589a9
 
 
e01b7e8
 
 
 
 
 
66d5a73
 
ce589a9
 
 
66d5a73
e01b7e8
ce589a9
 
 
 
e01b7e8
ce589a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66d5a73
 
e01b7e8
 
66d5a73
 
e01b7e8
66d5a73
 
e01b7e8
66d5a73
 
 
e01b7e8
3b6a1f2
 
 
e01b7e8
66d5a73
 
 
e01b7e8
 
3b6a1f2
 
e01b7e8
 
 
 
3b6a1f2
66d5a73
 
3b6a1f2
 
 
 
66d5a73
 
e01b7e8
66d5a73
 
e01b7e8
66d5a73
 
 
 
 
e01b7e8
66d5a73
e01b7e8
 
 
 
66d5a73
e01b7e8
 
66d5a73
 
 
e01b7e8
 
66d5a73
 
e01b7e8
 
 
 
66d5a73
e01b7e8
 
 
 
 
 
66d5a73
e01b7e8
 
 
 
66d5a73
 
 
 
e01b7e8
 
 
66d5a73
e01b7e8
 
66d5a73
 
e01b7e8
66d5a73
e01b7e8
66d5a73
 
 
e01b7e8
 
66d5a73
e01b7e8
66d5a73
e01b7e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66d5a73
 
e01b7e8
 
 
 
 
 
 
 
 
 
 
66d5a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d519b
 
 
 
 
66d5a73
 
d0d519b
 
 
 
 
 
 
 
 
 
66d5a73
 
 
 
d0d519b
66d5a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e01b7e8
 
 
 
66d5a73
e01b7e8
 
 
 
66d5a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e01b7e8
 
 
 
 
 
 
 
 
 
 
 
d9633b3
 
 
 
 
 
 
 
3b93905
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { Component, EventEmitter, Output, inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatPaginatorModule, PageEvent } from '@angular/material/paginator';
import { MatListModule } from '@angular/material/list';
import { MatCardModule } from '@angular/material/card';
import { MatDividerModule } from '@angular/material/divider';

interface ActivityLog {
  id: number;
  timestamp: string;
  user: string;
  action: string;
  entity_type: string;
  entity_id: any;
  entity_name: string;
  details?: string;
}

interface ActivityLogResponse {
  items: ActivityLog[];
  total: number;
  page: number;
  limit: number;
  pages: number;
}

@Component({
  selector: 'app-activity-log',
  standalone: true,
  imports: [
    CommonModule, 
    MatProgressSpinnerModule,
    MatButtonModule,
    MatIconModule,
    MatPaginatorModule,
    MatListModule,
    MatCardModule,
    MatDividerModule
  ],
  template: `
    <mat-card class="activity-log-dropdown" (click)="$event.stopPropagation()">
      <mat-card-header>
        <mat-card-title>
          <mat-icon>notifications</mat-icon>
          Recent Activities
        </mat-card-title>
        <button mat-icon-button (click)="close.emit()">
          <mat-icon>close</mat-icon>
        </button>
      </mat-card-header>
      
      <mat-card-content>
        @if (loading && activities.length === 0) {
          <div class="loading">
            <mat-spinner diameter="30"></mat-spinner>
          </div>
        } @else if (activities.length === 0) {
          <div class="empty">
            <mat-icon>inbox</mat-icon>
            <p>No activities found</p>
          </div>
        } @else {
          <mat-list class="activity-list">
            @for (activity of activities; track activity.id) {
              <mat-list-item>
                <mat-icon matListItemIcon>{{ getActivityIcon(activity.action) }}</mat-icon>
                <div matListItemTitle>
                  <span class="activity-time">{{ getRelativeTime(activity.timestamp) }}</span>
                </div>
                <div matListItemLine>
                  <strong>{{ activity.user }}</strong> {{ getActionText(activity) }}
                  <em>{{ activity.entity_name }}</em>
                  @if (activity.details) {
                    <span class="details">• {{ activity.details }}</span>
                  }
                </div>
              </mat-list-item>
              @if (!$last) {
                <mat-divider></mat-divider>
              }
            }
          </mat-list>
        }
      </mat-card-content>
      
      <mat-card-actions *ngIf="totalItems > pageSize">
        <mat-paginator
          [length]="totalItems"
          [pageSize]="pageSize"
          [pageIndex]="currentPage - 1"
          [pageSizeOptions]="[10, 25, 50]"
          (page)="onPageChange($event)"
          showFirstLastButtons>
        </mat-paginator>
      </mat-card-actions>
      
      <mat-card-actions *ngIf="totalItems <= pageSize && activities.length > 0">
        <button mat-button (click)="openFullView()" class="full-width">
          <mat-icon>open_in_new</mat-icon>
          View All Activities
        </button>
      </mat-card-actions>
    </mat-card>
  `,
  styles: [`
    .activity-log-dropdown {
      width: 450px;
      max-height: 600px;
      display: flex;
      flex-direction: column;
      overflow: hidden;
    }

    mat-card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 16px;
      background-color: #424242;  // Koyu gri (önceden #f5f5f5 idi)
      color: white;  // Yazıları beyaz yap
    
      mat-card-title {
        margin: 0;
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 18px;
        color: white;  // Title'ı da beyaz yap
    
        mat-icon {
          font-size: 24px;
          width: 24px;
          height: 24px;
          color: white;  // Icon'u da beyaz yap
        }
      }
    
      button {
        color: white;  // Close button'u beyaz yap
      }
    }

    mat-card-content {
      flex: 1;
      overflow-y: auto;
      padding: 0;
      min-height: 200px;
      max-height: 400px;
    }

    .activity-list {
      padding: 0;

      mat-list-item {
        height: auto;
        min-height: 72px;
        padding: 12px 16px;

        &:hover {
          background-color: #f5f5f5;
        }

        .activity-time {
          font-size: 12px;
          color: #666;
        }

        strong {
          color: #1976d2;
          margin-right: 4px;
        }

        em {
          color: #673ab7;
          font-style: normal;
          font-weight: 500;
          margin: 0 4px;
        }

        .details {
          color: #666;
          font-size: 12px;
          margin-left: 4px;
        }
      }
    }

    mat-card-actions {
      padding: 0;
      margin: 0;

      mat-paginator {
        background: transparent;
      }

      .full-width {
        width: 100%;
        margin: 0;
      }
    }

    .loading, .empty {
      padding: 60px 20px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      color: #666;

      mat-icon {
        font-size: 48px;
        width: 48px;
        height: 48px;
        color: #e0e0e0;
        margin-bottom: 16px;
      }

      p {
        margin: 0;
        font-size: 14px;
      }
    }

    ::ng-deep {
      .mat-mdc-list-item-unscoped-content {
        display: block;
      }

      .mat-mdc-paginator {
        .mat-mdc-paginator-container {
          padding: 8px;
          justify-content: center;
        }
      }
    }
  `]
})
export class ActivityLogComponent implements OnInit {
  @Output() close = new EventEmitter<void>();

  private http = inject(HttpClient);

  activities: ActivityLog[] = [];
  loading = false;
  currentPage = 1;
  pageSize = 10;
  totalItems = 0;
  totalPages = 0;

  ngOnInit() {
    this.loadActivities();
  }

  loadActivities(page: number = 1) {
    this.loading = true;
    this.currentPage = page;
    
    // Backend sadece limit parametresi alıyor, page almıyor
    const limit = this.pageSize * page; // Toplam kaç kayıt istediğimizi hesapla
    
    this.http.get<ActivityLog[]>(
      `/api/activity-log?limit=${limit}`
    ).subscribe({
      next: (response) => {
        // Response direkt array olarak geliyor
        const allActivities = response || [];
        
        // Manuel pagination yap
        const startIndex = (page - 1) * this.pageSize;
        const endIndex = startIndex + this.pageSize;
        
        this.activities = allActivities.slice(startIndex, endIndex);
        this.totalItems = allActivities.length;
        this.totalPages = Math.ceil(allActivities.length / this.pageSize);
        this.loading = false;
      },
      error: (error) => {
        console.error('Failed to load activities:', error);
        this.activities = []; // Hata durumunda boş array
        this.loading = false;
      }
    });
  }

  onPageChange(event: PageEvent) {
    this.pageSize = event.pageSize;
    this.loadActivities(event.pageIndex + 1);
  }

  openFullView() {
    // TODO: Implement full activity log view
    console.log('Open full activity log view');
    this.close.emit();
  }

  getRelativeTime(timestamp: string): string {
    const date = new Date(timestamp);
    const now = new Date();
    const diffMs = now.getTime() - date.getTime();
    const diffMins = Math.floor(diffMs / 60000);
    const diffHours = Math.floor(diffMs / 3600000);
    const diffDays = Math.floor(diffMs / 86400000);

    if (diffMins < 1) return 'just now';
    if (diffMins < 60) return `${diffMins} min ago`;
    if (diffHours < 24) return `${diffHours} hour${diffHours > 1 ? 's' : ''} ago`;
    if (diffDays < 7) return `${diffDays} day${diffDays > 1 ? 's' : ''} ago`;

    return date.toLocaleDateString();
  }

  getActionText(activity: ActivityLog): string {
    const actions: Record<string, string> = {
      'CREATE_PROJECT': 'created project',
      'UPDATE_PROJECT': 'updated project',
      'DELETE_PROJECT': 'deleted project',
      'ENABLE_PROJECT': 'enabled project',
      'DISABLE_PROJECT': 'disabled project',
      'PUBLISH_VERSION': 'published version of',
      'CREATE_VERSION': 'created version for',
      'UPDATE_VERSION': 'updated version of',
      'DELETE_VERSION': 'deleted version from',
      'CREATE_API': 'created API',
      'UPDATE_API': 'updated API',
      'DELETE_API': 'deleted API',
      'UPDATE_ENVIRONMENT': 'updated environment',
      'IMPORT_PROJECT': 'imported project',
      'CHANGE_PASSWORD': 'changed password'
    };

    return actions[activity.action] || activity.action.toLowerCase().replace(/_/g, ' ');
  }

  getActivityIcon(action: string): string {
    if (action.includes('CREATE')) return 'add_circle';
    if (action.includes('UPDATE')) return 'edit';
    if (action.includes('DELETE')) return 'delete';
    if (action.includes('ENABLE')) return 'check_circle';
    if (action.includes('DISABLE')) return 'cancel';
    if (action.includes('PUBLISH')) return 'publish';
    if (action.includes('IMPORT')) return 'cloud_upload';
    if (action.includes('PASSWORD')) return 'lock';
    return 'info';
  }

  trackByActivityId(index: number, activity: ActivityLog): number {
    return activity.id;
  }
  
  isLast(activity: ActivityLog): boolean {
    return this.activities.indexOf(activity) === this.activities.length - 1;
  }
}