Skip to content

Commit 2b2ae1b

Browse files
committed
fix: load readme relative to path, link to readme
1 parent 73043b6 commit 2b2ae1b

File tree

8 files changed

+64
-19
lines changed

8 files changed

+64
-19
lines changed

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: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
import { Component, OnDestroy, OnInit } from '@angular/core';
1+
import {
2+
Component,
3+
ElementRef,
4+
NgZone,
5+
OnDestroy,
6+
OnInit,
7+
ViewChild,
8+
} from '@angular/core';
29
import { Store } from '@ngrx/store';
310
import { ActivatedRoute } from '@angular/router';
411
import {
512
RepoContents,
613
fetchRepository,
714
selectedRepository,
815
} from '../../state/repository';
9-
import { map, takeWhile, tap } from 'rxjs';
16+
import { map, take, takeWhile, tap } from 'rxjs';
1017

1118
@Component({
1219
selector: 'app-file-explorer',
1320
templateUrl: './file-explorer.component.html',
1421
styleUrls: ['./file-explorer.component.scss'],
1522
})
1623
export class FileExplorerComponent implements OnInit, OnDestroy {
24+
@ViewChild('readme') readmeContainer: ElementRef | undefined;
25+
26+
private componentActive = true;
27+
1728
owner = '';
1829
repoName = '';
1930
path = '';
@@ -33,10 +44,22 @@ export class FileExplorerComponent implements OnInit, OnDestroy {
3344

3445
return { ...repo, tree: dirItems.concat(fileItems) };
3546
}),
47+
tap(() => {
48+
// make sure the readme is scrolled into view if the fragment is set
49+
// we need to wait for the readme to be rendered before we can scroll to it
50+
this.zone.onStable.pipe(take(1)).subscribe(() => {
51+
if (this.route.snapshot.fragment === 'readme') {
52+
this.readmeContainer?.nativeElement?.scrollIntoView();
53+
}
54+
});
55+
}),
3656
);
37-
private componentActive = true;
3857

39-
constructor(private route: ActivatedRoute, private store: Store) {}
58+
constructor(
59+
private route: ActivatedRoute,
60+
private store: Store,
61+
private zone: NgZone,
62+
) {}
4063

4164
ngOnInit() {
4265
this.route.paramMap
@@ -60,7 +83,7 @@ export class FileExplorerComponent implements OnInit, OnDestroy {
6083
.subscribe();
6184
}
6285

63-
ngOnDestroy(): void {
86+
ngOnDestroy() {
6487
this.componentActive = false;
6588
}
6689
}

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, HttpHeaders, 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,
@@ -337,21 +337,33 @@ export class RepositoryService {
337337
* Gets the contents of the repository's readme file
338338
* @param owner who the repo belongs to
339339
* @param repoName name of the repo
340+
* @param path (optional) if provided, the path to retrieve the readme from; defaults to the root directory
340341
* @returns the readme file for the repository
341342
*/
342343
getRepositoryReadme(
343344
repoOwner: string,
344345
repoName: string,
345-
): Observable<ReadmeApiResponse> {
346+
path?: string | null,
347+
): Observable<ReadmeApiResponse | null> {
346348
const owner = encodeURIComponent(repoOwner);
347349
const name = encodeURIComponent(repoName);
348-
const url = `${environment.githubUrl}/repos/${owner}/${name}/readme`;
350+
path = path ?? '';
351+
const url = `${environment.githubUrl}/repos/${owner}/${name}/readme/${path}`;
349352

350-
return this.http.get<ReadmeApiResponse>(url, {
351-
headers: {
352-
Accept: 'application/vnd.github.v3+json',
353-
},
354-
});
353+
return this.http
354+
.get<ReadmeApiResponse>(url, {
355+
headers: {
356+
Accept: 'application/vnd.github.v3+json',
357+
},
358+
})
359+
.pipe(
360+
catchError((err) => {
361+
if (err.status === 404) {
362+
return of(null);
363+
}
364+
throw err;
365+
}),
366+
);
355367
}
356368

357369
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)