Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 21 additions & 33 deletions source/features/repo-age.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as pageDetect from 'github-url-detection';

import features from '.';
import * as api from '../github-helpers/api';
import looseParseInt from '../helpers/loose-parse-int';
import {getRepoURL, getRepoGQL} from '../github-helpers';

const dateFormatter = new Intl.DateTimeFormat('en-US', {
Expand Down Expand Up @@ -43,34 +42,28 @@ const getRepoAge = async (commitSha: string, commitsCount: number): Promise<[str
return [committedDate, resourcePath];
};

const getFirstCommit = cache.function(async (): Promise<[string, string] | undefined> => {
const commitInfo = await elementReady<HTMLAnchorElement | HTMLScriptElement>([
// "Repository refresh" layout
'.Box-header--blue .hx_avatar_stack_commit + div a[href*="/commit/"]',
'include-fragment[aria-label="Loading latest commit"]',

// Pre "Repository refresh" layout
'a.commit-tease-sha',
'include-fragment.commit-tease'
].join());
const commitUrl = commitInfo instanceof HTMLAnchorElement ? commitInfo.href : commitInfo!.src;
const commitSha = commitUrl.split('/').pop()!;

// In "Repository refresh" layout, the number of commits may not be rendered yet
const commitsCountElement = select('li.commits .num') ?? (await elementReady('.Box-header--blue [aria-label^="Commits on "]'))!.parentElement;
const commitsCount = looseParseInt(commitsCountElement!.textContent!);

// Returning undefined will make sure that it is not cached. It will check again for commits on the next load.
// Reference: https://github.com/fregante/webext-storage-cache/#getter
if (commitsCount === 0) {
Copy link
Member

@fregante fregante Aug 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that this was removed. Why? It shouldn't be

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since until now the element could have not been ready on time so it would be undefined. Now the only case would be on a brand new repo.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the only case would be on a brand new repo.

Yes. Brand new repos still exist and now I think this would throw in those cases because repository.defaultBranchRef.target is null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The easiest way is to add isEmptyRepoRoot to excludes

TODO lint <-- search this next week :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol

return;
}
const getFirstCommit = cache.function(async (): Promise<[string, string]> => {
const {repository} = await api.v4(`
repository(${getRepoGQL()}) {
defaultBranchRef {
target {
... on Commit {
oid
committedDate
resourcePath
history {
totalCount
}
}
}
}
}
`);

const {oid: commitSha, history, committedDate, resourcePath} = repository.defaultBranchRef.target;
const commitsCount = history.totalCount;
if (commitsCount === 1) {
return [select([
'.commit-tease-sha + span relative-time', // Pre "Repository refresh" layout
'.js-details-container a relative-time'
])!.attributes.datetime.value, commitUrl];
return [committedDate, resourcePath];
}

return getRepoAge(commitSha, commitsCount);
Expand All @@ -79,12 +72,7 @@ const getFirstCommit = cache.function(async (): Promise<[string, string] | undef
});

async function init(): Promise<void> {
const [firstCommitDate, firstCommitHref] = await getFirstCommit() ?? [];

if (!firstCommitDate) {
return;
}

const [firstCommitDate, firstCommitHref] = await getFirstCommit()!;
const date = new Date(firstCommitDate);

// `twas` could also return `an hour ago` or `just now`
Expand Down