File size: 5,025 Bytes
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
import { Component, EventEmitter, Output, inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http';

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

@Component({
  selector: 'app-activity-log',
  standalone: true,
  imports: [CommonModule],
  template: `

    <div class="activity-log-dropdown" (click)="$event.stopPropagation()">

      <div class="activity-header">

        <h3>πŸ”” Recent Activities</h3>

        <button class="close-btn" (click)="close.emit()">Γ—</button>

      </div>

      <div class="activity-list">

        @if (loading) {

          <div class="loading">Loading...</div>

        } @else if (activities.length === 0) {

          <div class="empty">No recent activities</div>

        } @else {

          @for (activity of activities; track activity.id) {

            <div class="activity-item">

              <div class="activity-time">{{ getRelativeTime(activity.timestamp) }}</div>

              <div class="activity-content">

                <strong>{{ activity.user }}</strong> {{ getActionText(activity) }}

                <em>{{ activity.entity_name }}</em>

              </div>

            </div>

          }

        }

      </div>

      <div class="activity-footer">

        <button class="btn btn-secondary" (click)="loadMore()">View All Activities</button>

      </div>

    </div>

  `,
  styles: [`

    .activity-log-dropdown {

      position: absolute;

      top: 100%;

      right: 0;

      width: 350px;

      background: white;

      border: 1px solid #dee2e6;

      border-radius: 8px;

      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);

      z-index: 1000;

      margin-top: 0.5rem;

    }



    .activity-header {

      padding: 1rem;

      border-bottom: 1px solid #dee2e6;

      display: flex;

      justify-content: space-between;

      align-items: center;



      h3 {

        margin: 0;

        font-size: 1.1rem;

      }



      .close-btn {

        background: none;

        border: none;

        font-size: 1.5rem;

        cursor: pointer;

        color: #6c757d;



        &:hover {

          color: #333;

        }

      }

    }



    .activity-list {

      max-height: 300px;

      overflow-y: auto;

    }



    .activity-item {

      padding: 0.75rem 1rem;

      border-bottom: 1px solid #f0f0f0;



      &:hover {

        background-color: #f8f9fa;

      }



      .activity-time {

        font-size: 0.85rem;

        color: #6c757d;

        margin-bottom: 0.25rem;

      }



      .activity-content {

        font-size: 0.9rem;



        em {

          color: #007bff;

          font-style: normal;

        }

      }

    }



    .activity-footer {

      padding: 0.75rem;

      border-top: 1px solid #dee2e6;

      text-align: center;

    }



    .loading, .empty {

      padding: 2rem;

      text-align: center;

      color: #6c757d;

    }

  `]
})
export class ActivityLogComponent implements OnInit {
  @Output() close = new EventEmitter<void>();

  private http = inject(HttpClient);

  activities: ActivityLog[] = [];
  loading = true;

  ngOnInit() {
    this.loadActivities();
  }

  loadActivities() {
    this.loading = true;
    this.http.get<ActivityLog[]>('/api/activity-log?limit=10')
      .subscribe({
        next: (data) => {
          this.activities = data;
          this.loading = false;
        },
        error: () => {
          this.loading = false;
        }
      });
  }

  loadMore() {
    // TODO: Implement full activity log view
    console.log('Load more activities');
  }

  getRelativeTime(timestamp: string): string {
    const date = new Date(timestamp);
    const now = new Date();
    const diff = now.getTime() - date.getTime();

    const minutes = Math.floor(diff / 60000);
    const hours = Math.floor(diff / 3600000);
    const days = Math.floor(diff / 86400000);

    if (minutes < 1) return 'just now';
    if (minutes < 60) return `${minutes} min ago`;
    if (hours < 24) return `${hours} hour${hours > 1 ? 's' : ''} ago`;
    return `${days} day${days > 1 ? 's' : ''} ago`;
  }

  getActionText(activity: ActivityLog): string {
    const actions: Record<string, string> = {
      'CREATE_PROJECT': 'created project',
      'UPDATE_PROJECT': 'updated project',
      'DELETE_PROJECT': 'deleted project',
      'PUBLISH_VERSION': 'published version',
      'CREATE_VERSION': 'created version',
      'UPDATE_VERSION': 'updated version',
      'CREATE_API': 'created API',
      'UPDATE_API': 'updated API',
      'DELETE_API': 'deleted API',
      'UPDATE_ENVIRONMENT': 'updated environment',
      'IMPORT_PROJECT': 'imported project'
    };

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