-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
useful-not-found-page - Improve style and reliability
#9812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dcd077f
e30b031
96f7d29
254e994
fdeb58a
a4c941a
efcf07c
107f462
289ba36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /* Missing file */ | ||
| .flex-items-center a[aria-label='go to Overview'], | ||
| /* Missing branch */ | ||
| .flex-items-center a[aria-label='go to default branch'] { | ||
| display: none; | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This drops their useless green buttons that just point to repo root. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| <script lang="ts"> | ||
| import api from '../github-helpers/api.js'; | ||
| import getDefaultBranch from '../github-helpers/get-default-branch.js'; | ||
| import GitHubFileUrl from '../github-helpers/github-file-url.js'; | ||
| import {isUrlReachable} from '../github-helpers/index.js'; | ||
| import GetLatestCommitToFile from './useful-not-found-page.gql'; | ||
|
|
||
| type File = { | ||
| previous_filename?: string; | ||
| filename: string; | ||
| status: string; | ||
| blob_url: string; | ||
| }; | ||
|
|
||
| type FileChanges = { | ||
| file: File; | ||
| commit: { | ||
| parentSha: string; | ||
| date: Date; | ||
| url: string; | ||
| }; | ||
| }; | ||
|
|
||
| type GitHistory = { | ||
| oldFilename: string; | ||
| lastVersionUrl: string; | ||
| status: string; | ||
| movedUrl: string; | ||
| commitUrl: string; | ||
| commitDate: Date; | ||
| }; | ||
|
|
||
| function getType(): string { | ||
| return location.pathname.split('/').pop()!.includes('.') ? 'file' : 'object'; | ||
| } | ||
|
|
||
| async function getLatestCommitToFile( | ||
| branch: string, | ||
| filePath: string, | ||
| ): Promise<string> { | ||
| const {repository} = await api.v4(GetLatestCommitToFile, { | ||
| variables: {branch, filePath}, | ||
| }); | ||
|
|
||
| return repository.object | ||
| // Missing if the ref doesn't exist | ||
| ?.history.nodes[0] | ||
| // Missing if the ref exists but the file never existed | ||
| ?.oid; | ||
| } | ||
|
|
||
| async function getChangesToFileInCommit( | ||
| sha: string, | ||
| filePath: string, | ||
| ): Promise<FileChanges | undefined> { | ||
| const commit = await api.v3(`commits/${sha}`); | ||
| for (const fileInfo of commit.files as File[]) { | ||
| if ([fileInfo.filename, fileInfo.previous_filename].includes(filePath)) { | ||
| return { | ||
| commit: { | ||
| parentSha: commit.parents[0].sha, | ||
| date: commit.commit.committer.date, | ||
| url: commit.html_url, | ||
| }, | ||
| file: fileInfo, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| async function getUrlToFileOnDefaultBranch(): Promise<string | undefined> { | ||
| const parsedUrl = new GitHubFileUrl(location.href); | ||
| if (!parsedUrl.branch) { | ||
| return undefined; | ||
| } | ||
|
|
||
| parsedUrl.assign({branch: await getDefaultBranch()}); | ||
| const urlOnDefault = parsedUrl.href; | ||
| if (urlOnDefault !== location.href && await isUrlReachable(urlOnDefault)) { | ||
| return urlOnDefault; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| async function getGitHistory(): Promise<GitHistory | undefined> { | ||
| const url = new GitHubFileUrl(location.href); | ||
| if (!url.branch || !url.filePath) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const commitSha = await getLatestCommitToFile(url.branch, url.filePath); | ||
| if (!commitSha) { | ||
| // Never existed | ||
| return undefined; | ||
| } | ||
|
|
||
| const fileChanges = await getChangesToFileInCommit(commitSha, url.filePath); | ||
| if (!fileChanges) { | ||
| return undefined; | ||
| } | ||
|
Comment on lines
+94
to
+103
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to handle these two states separately but I've worked on this component enough already. I left a TODO in the template. |
||
|
|
||
| return { | ||
| oldFilename: fileChanges.file.previous_filename ?? fileChanges.file.filename, | ||
| lastVersionUrl: fileChanges.file.status === 'removed' | ||
| ? fileChanges.file.blob_url | ||
| : url.href, | ||
| status: fileChanges.file.status, | ||
| movedUrl: decodeURIComponent(fileChanges.file.blob_url), // Why is the API returning dir%2Ffile.js??! | ||
| commitUrl: fileChanges.commit.url, | ||
| commitDate: fileChanges.commit.date, | ||
| }; | ||
| } | ||
|
|
||
| const type = getType(); | ||
| </script> | ||
|
|
||
| <div class="color-fg-muted rgh-hide-if-empty"> | ||
| {#await getGitHistory()} | ||
| Loading history of this {type}... | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adds some feedback to tell the user something is loading. This is the kind of things we just don't do without components. Not the best of styles but better than nothing. |
||
| {:then gitHistory} | ||
| {#if gitHistory} | ||
| <span class="commit-ref"> | ||
| <a href={gitHistory.commitUrl}> | ||
| {new GitHubFileUrl(gitHistory.commitUrl).branch.slice(0, 8)} | ||
| </a> | ||
| </span> | ||
| {gitHistory.status} | ||
| <a href={gitHistory.lastVersionUrl}>{gitHistory.oldFilename}</a> | ||
| {#if gitHistory.status !== 'removed'} | ||
| to <a href={gitHistory.movedUrl}>{ | ||
| gitHistory.movedUrl.split('/').slice(7).join('/') | ||
| }</a> | ||
| {/if} | ||
| <relative-time datetime={gitHistory.commitDate}></relative-time>. | ||
| {:else} | ||
| <!-- TODO: Handle scenario. Can be because branch OR file is 404 --> | ||
| {/if} | ||
| {/await} | ||
| </div> | ||
|
|
||
| <div class="color-fg-muted rgh-hide-if-empty"> | ||
| {#await getUrlToFileOnDefaultBranch()} | ||
| Loading default branch... | ||
| {:then defaultBranchUrl} | ||
| {#if defaultBranchUrl} | ||
| <a href={defaultBranchUrl}>{ | ||
| new GitHubFileUrl(defaultBranchUrl).filePath | ||
| }</a> exists on the default branch. | ||
| {/if} | ||
| {/await} | ||
| </div> | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isRepoFile404github-url-detection#251