Skip to content

Commit 994c446

Browse files
committed
fix: load readme relative to path, link to readme
1 parent 74ed196 commit 994c446

8 files changed

Lines changed: 55 additions & 20 deletions

File tree

angular-ngrx-scss/src/app/app-routing.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ const routes: Routes = [
1818

1919
@NgModule({
2020
imports: [
21-
RouterModule.forRoot(routes, { paramsInheritanceStrategy: 'always' }),
21+
RouterModule.forRoot(routes, {
22+
paramsInheritanceStrategy: 'always',
23+
scrollPositionRestoration: 'enabled',
24+
anchorScrolling: 'enabled',
25+
}),
2226
],
2327
exports: [RouterModule],
2428
})

angular-ngrx-scss/src/app/file-viewer/components/file-explorer-about/file-explorer-about.component.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ <h2>About</h2>
2020
</div>
2121

2222
<div class="mt-1">
23-
<!-- TODO: this should scroll down to the readme section of the repository when it is clicked-->
24-
<!-- The id anchor tag approach is modelled after the current behaviour of similar link on github.com-->
25-
<a class="readMeLink" routerLink="./" fragment="readme">
23+
<a class="readMeLink" [routerLink]="['/', owner, name]" fragment="readme">
2624
<span class="icon" appOcticon="book"></span> Readme
2725
</a>
2826
</div>

angular-ngrx-scss/src/app/file-viewer/components/file-explorer-about/file-explorer-about.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export class FileExplorerAboutComponent {
1010
@Input() description: string | undefined;
1111
@Input() homepageUrl!: string;
1212
@Input() topics!: string[];
13+
@Input() owner!: string;
14+
@Input() name!: string;
1315
}

angular-ngrx-scss/src/app/file-viewer/file-explorer/file-explorer.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
[description]="repo.description"
2323
[homepageUrl]="repo.website"
2424
[topics]="repo.tags"
25+
[owner]="owner"
26+
[name]="repoName"
2527
></app-file-explorer-about>
2628
</section>
2729

28-
<section class="col-span-9">
30+
<section class="col-span-9" id="readme" #readme>
2931
<app-read-me [readme]="repo.readme"></app-read-me>
3032
</section>
3133
</div>

angular-ngrx-scss/src/app/file-viewer/file-explorer/file-explorer.component.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ describe('FileExplorerComponent', () => {
4949
}
5050
},
5151
}),
52+
snapshot: {
53+
fragment: '',
54+
},
5255
};
5356

5457
beforeEach(async () => {

angular-ngrx-scss/src/app/file-viewer/file-explorer/file-explorer.component.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import { Component, OnDestroy } from '@angular/core';
1+
import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
22
import { Store } from '@ngrx/store';
33
import { ActivatedRoute } from '@angular/router';
44
import { RepoContents, fetchRepository, selectedRepository } from '../../state/repository';
5-
import { map, takeWhile, tap } from 'rxjs';
5+
import { map, take, takeWhile, tap } from 'rxjs';
66

77
@Component({
88
selector: 'app-file-explorer',
99
templateUrl: './file-explorer.component.html',
1010
styleUrls: ['./file-explorer.component.scss'],
1111
})
12-
export class FileExplorerComponent implements OnDestroy {
12+
export class FileExplorerComponent implements OnInit, OnDestroy {
13+
@ViewChild('readme') readmeContainer: ElementRef | undefined;
14+
15+
private componentActive = true;
16+
1317
owner = '';
1418
repoName = '';
1519
path = '';
@@ -29,10 +33,18 @@ export class FileExplorerComponent implements OnDestroy {
2933

3034
return { ...repo, tree: dirItems.concat(fileItems) };
3135
}),
36+
tap(() => {
37+
// make sure the readme is scrolled into view if the fragment is set
38+
// we need to wait for the readme to be rendered before we can scroll to it
39+
this.zone.onStable.pipe(take(1)).subscribe(() => {
40+
if (this.route.snapshot.fragment === 'readme') {
41+
this.readmeContainer?.nativeElement?.scrollIntoView();
42+
}
43+
});
44+
}),
3245
);
33-
private componentActive = true;
3446

35-
constructor(private route: ActivatedRoute, private store: Store) { }
47+
constructor(private route: ActivatedRoute, private store: Store, private zone: NgZone) { }
3648

3749

3850
ngOnInit() {
@@ -57,7 +69,8 @@ export class FileExplorerComponent implements OnDestroy {
5769
.subscribe();
5870
}
5971

60-
ngOnDestroy(): void {
72+
ngOnDestroy() {
6173
this.componentActive = false;
6274
}
75+
6376
}

angular-ngrx-scss/src/app/repository/services/repository.service.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HttpClient, HttpParams } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3-
import { Observable, map } from 'rxjs';
3+
import { Observable, catchError, map, of } from 'rxjs';
44
import {
55
FileContentsApiResponse,
66
IssueAPIResponse,
@@ -373,21 +373,33 @@ export class RepositoryService {
373373
* Gets the contents of the repository's readme file
374374
* @param owner who the repo belongs to
375375
* @param repoName name of the repo
376+
* @param path (optional) if provided, the path to retrieve the readme from; defaults to the root directory
376377
* @returns the readme file for the repository
377378
*/
378379
getRepositoryReadme(
379380
repoOwner: string,
380381
repoName: string,
381-
): Observable<ReadmeApiResponse> {
382+
path?: string | null,
383+
): Observable<ReadmeApiResponse | null> {
382384
const owner = encodeURIComponent(repoOwner);
383385
const name = encodeURIComponent(repoName);
384-
const url = `${environment.githubUrl}/repos/${owner}/${name}/readme`;
386+
path = path ?? '';
387+
const url = `${environment.githubUrl}/repos/${owner}/${name}/readme/${path}`;
385388

386-
return this.http.get<ReadmeApiResponse>(url, {
387-
headers: {
388-
Accept: 'application/vnd.github.v3+json',
389-
},
390-
});
389+
return this.http
390+
.get<ReadmeApiResponse>(url, {
391+
headers: {
392+
Accept: 'application/vnd.github.v3+json',
393+
},
394+
})
395+
.pipe(
396+
catchError((err) => {
397+
if (err.status === 404) {
398+
return of(null);
399+
}
400+
throw err;
401+
}),
402+
);
391403
}
392404

393405
private extractTotalFromLinkHeader(linkHeader: string | null): number {

angular-ngrx-scss/src/app/state/repository/repository.effects.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class RepositoryEffects {
4242
const repoReadme$ = this.repoService.getRepositoryReadme(
4343
owner,
4444
repoName,
45+
path,
4546
);
4647

4748
const repoMilestones$ = this.repoService.getRepositoryMilestones(
@@ -83,7 +84,7 @@ export class RepositoryEffects {
8384
visibility: info.visibility,
8485
watchCount: info.watchers_count,
8586
website: info.homepage,
86-
readme: readme.content || '',
87+
readme: readme?.content || '',
8788
milestones: milestones || [],
8889
labels: labels || [],
8990
};

0 commit comments

Comments
 (0)