/
api-navigation.component.ts
417 lines (383 loc) · 15.4 KB
/
api-navigation.component.ts
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, Inject, OnDestroy, OnInit } from '@angular/core';
import { flatMap } from 'lodash';
import { filter, map, shareReplay, switchMap, takeUntil, tap } from 'rxjs/operators';
import { GIO_DIALOG_WIDTH, GioBannerTypes, GioMenuSearchService, GioMenuService, MenuSearchItem } from '@gravitee/ui-particles-angular';
import { combineLatest, Observable, Subject } from 'rxjs';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import {
ApiConfirmDeploymentDialogComponent,
ApiConfirmDeploymentDialogData,
ApiConfirmDeploymentDialogResult,
} from './api-confirm-deployment-dialog/api-confirm-deployment-dialog.component';
import { ApiReviewDialogComponent, ApiReviewDialogData, ApiReviewDialogResult } from './api-review-dialog/api-review-dialog.component';
import { MenuGroupItem, MenuItem, MenuItemHeader } from './MenuGroupItem';
import { ApiV4MenuService } from './api-v4-menu.service';
import { ApiV1V2MenuService } from './api-v1-v2-menu.service';
import { cleanRouterLink, getPathFromRoot } from '../../../util/router-link.util';
import { GioPermissionService } from '../../../shared/components/gio-permission/gio-permission.service';
import { Constants } from '../../../entities/Constants';
import { ApiV2Service } from '../../../services-ngx/api-v2.service';
import { Api } from '../../../entities/management-api-v2';
import { ApiService } from '../../../services-ngx/api.service';
import { SnackBarService } from '../../../services-ngx/snack-bar.service';
type TopBanner = {
title: string;
body?: string;
type: GioBannerTypes;
action?: {
btnText: string;
onClick: () => void;
};
};
@Component({
selector: 'api-navigation',
templateUrl: './api-navigation.component.html',
styleUrls: ['./api-navigation.component.scss'],
providers: [ApiV1V2MenuService, ApiV4MenuService],
})
export class ApiNavigationComponent implements OnInit, OnDestroy {
public currentApi: Api;
public subMenuItems: MenuItem[] = [];
public groupItems: MenuGroupItem[] = [];
public selectedItemWithTabs: MenuItem = undefined;
public selectedItemHeader: MenuItemHeader;
public bannerState: string;
public hasBreadcrumb = false;
public breadcrumbItems: string[] = [];
public banners$: Observable<TopBanner[]> = combineLatest([
this.apiV2Service.getLastApiFetch(this.activatedRoute.snapshot.params.apiId),
this.apiV2Service.verifyDeploy(this.activatedRoute.snapshot.params.apiId),
]).pipe(
map(([api, verifyDeploymentResponse]) => {
const banners: TopBanner[] = [];
if (api.definitionVersion == null || api.definitionVersion === 'V1') {
banners.push({
title: 'API version out-of-date',
type: 'warning',
body:
'<div>We no longer support path-based APIs. To continue using all features, you will need to update your API.</div>\n' +
'<a href="https://www.gravitee.io/blog/gravitee-api-definitions" target="_blank" rel="noopener">Learn more</a>\n',
action: {
btnText: 'Update API version',
onClick: () => {
this.legacyApiService
.migrateApiToPolicyStudio(this.currentApi.id)
.pipe(takeUntil(this.unsubscribe$))
.subscribe({
error: ({ error }) => {
this.snackBarService.error(error.message);
},
});
},
},
});
}
const isApiReviewer = this.permissionService.hasAnyMatching(['api-reviews-u']);
// Only for API reviewer
if (isApiReviewer && this.constants.env.settings?.apiReview?.enabled && api.workflowState === 'IN_REVIEW') {
// 'api-reviews-u'
banners.push({
title: 'As an API reviewer, there are some changes made on this API that required a review before been applied.',
type: 'info',
action: {
btnText: 'Review changes',
onClick: () => {
this.matDialog
.open<ApiReviewDialogComponent, ApiReviewDialogData, ApiReviewDialogResult>(ApiReviewDialogComponent, {
data: {
apiId: api.id,
},
role: 'alertdialog',
id: 'gioApiReviewDialog',
width: GIO_DIALOG_WIDTH.MEDIUM,
})
.afterClosed()
.pipe(takeUntil(this.unsubscribe$))
.subscribe();
},
},
});
}
if (isApiReviewer && this.constants.env.settings?.apiReview?.enabled && api.workflowState === 'REVIEW_OK') {
banners.push({
title: 'As an API reviewer, you have accepted the changes made on this API.',
type: 'success',
action: {
btnText: 'Review changes',
onClick: () => {
this.matDialog
.open<ApiReviewDialogComponent, ApiReviewDialogData, ApiReviewDialogResult>(ApiReviewDialogComponent, {
data: {
apiId: api.id,
},
role: 'alertdialog',
id: 'gioApiReviewDialog',
width: GIO_DIALOG_WIDTH.MEDIUM,
})
.afterClosed()
.pipe(takeUntil(this.unsubscribe$))
.subscribe();
},
},
});
}
if (isApiReviewer && this.constants.env.settings?.apiReview?.enabled && api.workflowState === 'REQUEST_FOR_CHANGES') {
banners.push({
title: 'As an API reviewer, you have rejected the changes made on this API.',
type: 'warning',
action: {
btnText: 'Review changes',
onClick: () => {
this.matDialog
.open<ApiReviewDialogComponent, ApiReviewDialogData, ApiReviewDialogResult>(ApiReviewDialogComponent, {
data: {
apiId: api.id,
},
role: 'alertdialog',
id: 'gioApiReviewDialog',
width: GIO_DIALOG_WIDTH.MEDIUM,
})
.afterClosed()
.pipe(takeUntil(this.unsubscribe$))
.subscribe();
},
},
});
}
// Only for not API reviewer
if (!isApiReviewer && this.constants.env.settings?.apiReview?.enabled && api.workflowState === 'IN_REVIEW') {
// not 'api-reviews-u'
banners.push({
title: 'The API reviewer has been asked to review the changes.',
type: 'info',
});
}
if (!isApiReviewer && this.constants.env.settings?.apiReview?.enabled && api.workflowState === 'REVIEW_OK') {
// not 'api-reviews-u'
banners.push({
title: 'The API reviewer has accepted the changes made on this API.',
type: 'success',
});
}
if (!isApiReviewer && this.constants.env.settings?.apiReview?.enabled && api.workflowState === 'REQUEST_FOR_CHANGES') {
// not 'api-reviews-u'
banners.push({
title: 'The API reviewer has asked for changes to be made on this API.',
type: 'warning',
});
}
// Other banners
if (this.constants.env.settings?.apiReview?.enabled && api.workflowState === 'DRAFT') {
banners.push({
title: 'This API is a draft.',
type: 'info',
});
}
const apiReviewIsOKOrNotNeeded =
(this.constants.env.settings?.apiReview?.enabled && api.workflowState === 'REVIEW_OK') ||
!this.constants.env.settings?.apiReview?.enabled;
const canUpdateApiDefinition = this.permissionService.hasAnyMatching(['api-definition-u']);
if (api.deploymentState === 'NEED_REDEPLOY' && apiReviewIsOKOrNotNeeded && verifyDeploymentResponse.ok) {
banners.push(
canUpdateApiDefinition
? {
title: 'This API is out of sync.',
type: 'warning',
action: {
btnText: 'Deploy API',
onClick: () => {
this.matDialog
.open<ApiConfirmDeploymentDialogComponent, ApiConfirmDeploymentDialogData, ApiConfirmDeploymentDialogResult>(
ApiConfirmDeploymentDialogComponent,
{
data: {
apiId: api.id,
},
role: 'alertdialog',
id: 'gioApiConfirmDeploymentDialog',
width: GIO_DIALOG_WIDTH.MEDIUM,
},
)
.afterClosed()
.pipe(takeUntil(this.unsubscribe$))
.subscribe();
},
},
}
: {
title: 'This API is out of sync.',
type: 'warning',
},
);
}
if (api.lifecycleState === 'DEPRECATED') {
banners.push({
title: 'This API is deprecated.',
type: 'error',
});
}
if (!verifyDeploymentResponse.ok) {
banners.push({
title: 'This API cannot be deployed.',
body: 'The current configuration uses features not in your license.',
type: 'error',
});
}
return banners;
}),
shareReplay(1),
);
public hasBanner$ = this.banners$.pipe(map((banners) => banners.length > 0));
private unsubscribe$ = new Subject();
constructor(
private readonly router: Router,
private readonly activatedRoute: ActivatedRoute,
private readonly permissionService: GioPermissionService,
@Inject(Constants) private readonly constants: Constants,
private readonly gioMenuService: GioMenuService,
private readonly apiV2Service: ApiV2Service,
private readonly legacyApiService: ApiService,
private readonly matDialog: MatDialog,
private readonly apiNgV1V2MenuService: ApiV1V2MenuService,
private readonly apiNgV4MenuService: ApiV4MenuService,
private readonly snackBarService: SnackBarService,
private readonly gioMenuSearchService: GioMenuSearchService,
) {}
ngOnInit() {
this.gioMenuService.reduced$.pipe(takeUntil(this.unsubscribe$)).subscribe((reduced) => {
this.hasBreadcrumb = reduced;
});
this.bannerState = localStorage.getItem('gv-api-navigation-banner');
this.apiV2Service
.getLastApiFetch(this.activatedRoute.snapshot.params.apiId)
.pipe(
tap((api) => (this.currentApi = api)),
tap((api) => {
const menu = api.definitionVersion !== 'V4' ? this.apiNgV1V2MenuService.getMenu(api) : this.apiNgV4MenuService.getMenu(api);
this.groupItems = menu.groupItems;
this.subMenuItems = menu.subMenuItems;
this.gioMenuSearchService.addMenuSearchItems(this.getApiNavigationSearchItems());
this.breadcrumbItems = this.computeBreadcrumbItems();
this.selectedItemWithTabs = this.findMenuItemWithTabs();
this.selectedItemHeader = this.findActiveMenuItemHeader();
}),
switchMap(() => this.router.events),
filter((event) => event instanceof NavigationEnd),
map((event: NavigationEnd) => event),
tap(() => {
this.selectedItemWithTabs = this.findMenuItemWithTabs();
this.selectedItemHeader = this.findActiveMenuItemHeader();
}),
takeUntil(this.unsubscribe$),
)
.subscribe();
}
ngOnDestroy() {
this.unsubscribe$?.next(true);
this.unsubscribe$?.unsubscribe();
this.gioMenuSearchService.removeMenuSearchItems([this.currentApi?.id]);
}
private findMenuItemWithTabs(): MenuItem {
let item: MenuItem = this.findActiveMenuItem(this.subMenuItems);
if (item) {
return item;
}
for (const groupItem of this.groupItems) {
item = this.findActiveMenuItem(groupItem.items);
if (item) {
return item;
}
}
}
private findActiveMenuItem(items: MenuItem[]) {
return items.filter((item) => item?.tabs).find((item) => this.isTabActive(item?.tabs));
}
private findActiveMenuItemHeader() {
return this.subMenuItems.find((item) => this.isActive(item) || this.isTabActive(item.tabs))?.header;
}
isActive(item: MenuItem): boolean {
if (!item.routerLink) {
return false;
}
return this.router.isActive(this.router.createUrlTree([item.routerLink], { relativeTo: this.activatedRoute }), {
paths: item.routerLinkActiveOptions?.exact ? 'exact' : 'subset',
queryParams: 'subset',
fragment: 'ignored',
matrixParams: 'ignored',
});
}
isTabActive(tabs: MenuItem[]): boolean {
return flatMap(tabs, (tab) => tab).some((tab) => this.isActive(tab));
}
public computeBreadcrumbItems(): string[] {
const breadcrumbItems: string[] = [];
this.groupItems.forEach((groupItem) => {
groupItem.items.forEach((item) => {
if (this.isActive(item)) {
breadcrumbItems.push(groupItem.title);
breadcrumbItems.push(item.displayName);
} else if (item.tabs && this.isTabActive(item.tabs)) {
breadcrumbItems.push(groupItem.title);
breadcrumbItems.push(item.displayName);
}
});
});
return breadcrumbItems;
}
private getApiNavigationSearchItems() {
const environmentId = this.activatedRoute.snapshot.params.envHrid;
const apiId = this.currentApi.id;
const parentRouterLink = getPathFromRoot(this.activatedRoute);
return this.mapToMenuSearchItem(environmentId, apiId, parentRouterLink, this.subMenuItems).concat(
this.mapToMenuSearchItem(
environmentId,
apiId,
parentRouterLink,
this.groupItems.flatMap((item) => item.items),
),
);
}
private mapToMenuSearchItem(environmentId: string, apiId: string, parentRouterLink: string, items: MenuItem[]): MenuSearchItem[] {
return items.reduce((acc: MenuSearchItem[], item: MenuItem) => {
const cleanItemLink = cleanRouterLink(item.routerLink);
const routerLink = cleanItemLink || cleanRouterLink(item.tabs?.[0]?.routerLink) || '';
// check if parent route is empty ('') and if the first tab has the same name than the parent.
const isUniqueItem = cleanItemLink || item.displayName !== item.tabs?.[0]?.displayName;
if (routerLink !== 'DISABLED' && isUniqueItem) {
acc.push({
name: item.displayName,
routerLink: `${parentRouterLink}/${routerLink}`,
category: `Apis`,
groupIds: [environmentId, apiId],
});
}
item.tabs?.forEach((tab) => {
if (tab.routerLink !== 'DISABLED') {
acc.push({
name: tab.displayName,
routerLink: `${parentRouterLink}/${cleanRouterLink(tab.routerLink)}`,
category: `Apis / ${item.displayName}`,
groupIds: [environmentId, apiId],
});
}
});
return acc;
}, []);
}
}