-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfile-explorer.component.ts
More file actions
89 lines (81 loc) · 2.23 KB
/
file-explorer.component.ts
File metadata and controls
89 lines (81 loc) · 2.23 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
import {
Component,
ElementRef,
NgZone,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';
import { Store } from '@ngrx/store';
import { ActivatedRoute } from '@angular/router';
import {
RepoContents,
fetchRepository,
selectedRepository,
} from '../../state/repository';
import { map, take, takeWhile, tap } from 'rxjs';
@Component({
selector: 'app-file-explorer',
templateUrl: './file-explorer.component.html',
styleUrls: ['./file-explorer.component.scss'],
})
export class FileExplorerComponent implements OnInit, OnDestroy {
@ViewChild('readme') readmeContainer: ElementRef | undefined;
private componentActive = true;
owner = '';
repoName = '';
path = '';
branch = '';
repo$ = this.store.select(selectedRepository).pipe(
map((repo) => {
const fileItems: RepoContents[] = [];
const dirItems: RepoContents[] = [];
repo.tree.forEach((item) => {
if (item.type === 'dir') {
dirItems.push(item);
} else {
fileItems.push(item);
}
});
return { ...repo, tree: dirItems.concat(fileItems) };
}),
tap(() => {
// make sure the readme is scrolled into view if the fragment is set
// we need to wait for the readme to be rendered before we can scroll to it
this.zone.onStable.pipe(take(1)).subscribe(() => {
if (this.route.snapshot.fragment === 'readme') {
this.readmeContainer?.nativeElement?.scrollIntoView();
}
});
}),
);
constructor(
private route: ActivatedRoute,
private store: Store,
private zone: NgZone,
) {}
ngOnInit() {
this.route.paramMap
.pipe(
takeWhile(() => this.componentActive),
tap((params) => {
this.owner = params.get('owner') as string;
this.repoName = params.get('repo') as string;
this.branch = params.get('branch') as string;
this.path = params.get('path') as string;
this.store.dispatch(
fetchRepository({
owner: this.owner,
repoName: this.repoName,
path: this.path,
branch: this.branch,
}),
);
}),
)
.subscribe();
}
ngOnDestroy() {
this.componentActive = false;
}
}