Skip to content

Commit

Permalink
fix: features initialize unexpectedly in the new UI
Browse files Browse the repository at this point in the history
now they run expectedly in the both old and new UI
  • Loading branch information
tyn1998 committed Aug 21, 2023
1 parent bcf2c47 commit 71b1f48
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dom-loaded": "^3.0.0",
"echarts": "^5.3.0",
"element-ready": "^6.2.1",
"github-url-detection": "^6.1.0",
"github-url-detection": "^8.1.0",
"jquery": "^3.6.0",
"lodash-es": "^4.17.21",
"office-ui-fabric-react": "^7.183.0",
Expand Down
28 changes: 17 additions & 11 deletions src/helpers/get-repo-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ export function getRepoName() {
return pageDetect.utils.getRepositoryInfo(window.location)!.nameWithOwner;
}

// check if the repository is public
export function hasRepoContainerHeader() {
const headerElement = $('#repository-container-header');
return headerElement && !headerElement.attr('hidden');
}

export async function isRepoRoot() {
return pageDetect.isRepoRoot();
}

/**
* check if the repository is public
*/
export async function isPublicRepo() {
// another selector that also works
// const repoLabel = $('strong[itemprop="name"]').siblings('span.Label.Label--secondary').text();
await elementReady('#repository-container-header');
const repoLabel = $('#repository-container-header')
.find('span.Label.Label--secondary:first')
.text();
return (
pageDetect.isRepo() &&
(repoLabel === 'Public' || repoLabel === 'Public template')
);
const selector = 'meta[name="octolytics-dimension-repository_public"]';
await elementReady(selector);
// <meta name="octolytics-dimension-repository_public" content="true/false">
const isPublic = $(selector).attr('content') === 'true';
return pageDetect.isRepo() && isPublic;
}

export async function isPublicRepoWithMeta() {
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/is-perceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const isPerceptor = (): boolean =>
window.location.search.includes('?redirect=perceptor');
const isPerceptor = (): boolean => {
return window.location.search.includes('?redirect=perceptor');
};

export default isPerceptor;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import features from '../../../../feature-manager';
import {
getRepoName,
isPublicRepoWithMeta,
isRepoRoot,
} from '../../../../helpers/get-repo-info';
import { getActivity, getOpenrank } from '../../../../api/repo';
import { RepoMeta, metaStore } from '../../../../api/common';
Expand Down Expand Up @@ -65,7 +66,7 @@ const restore = async () => {
};

features.add(featureId, {
asLongAs: [isPublicRepoWithMeta],
asLongAs: [isPublicRepoWithMeta, isRepoRoot],
awaitDomReady: true,
init,
restore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import $ from 'jquery';

import features from '../../../../feature-manager';
import isPerceptor from '../../../../helpers/is-perceptor';
import { getRepoName } from '../../../../helpers/get-repo-info';
import {
getRepoName,
isPublicRepoWithMeta,
} from '../../../../helpers/get-repo-info';
import { getActivityDetails } from '../../../../api/repo';
import View from './view';
import DataNotFound from '../repo-networks/DataNotFound';
Expand Down Expand Up @@ -52,7 +55,7 @@ const restore = async () => {
};

features.add(featureId, {
asLongAs: [isPerceptor],
asLongAs: [isPerceptor, isPublicRepoWithMeta],
awaitDomReady: false,
init,
restore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import features from '../../../../feature-manager';
import getGithubTheme from '../../../../helpers/get-github-theme';
import {
getRepoName,
hasRepoContainerHeader,
isPublicRepoWithMeta,
} from '../../../../helpers/get-repo-info';
import { getForks } from '../../../../api/repo';
Expand Down Expand Up @@ -65,7 +66,7 @@ const restore = async () => {
};

features.add(featureId, {
asLongAs: [isPublicRepoWithMeta],
asLongAs: [isPublicRepoWithMeta, hasRepoContainerHeader],
awaitDomReady: false,
init,
restore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import $ from 'jquery';
import features from '../../../../feature-manager';
import {
getRepoName,
hasRepoContainerHeader,
isPublicRepoWithMeta,
} from '../../../../helpers/get-repo-info';
import { getActivity, getOpenrank, getParticipant } from '../../../../api/repo';
Expand Down Expand Up @@ -65,7 +66,7 @@ const restore = async () => {
};

features.add(featureId, {
asLongAs: [isPublicRepoWithMeta],
asLongAs: [isPublicRepoWithMeta, hasRepoContainerHeader],
awaitDomReady: false,
init,
restore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const init = async (): Promise<void> => {
const container = document.createElement('div');
container.id = featureId;
renderTo(container);
(await elementReady('#repository-container-header'))?.append(container);
(await elementReady('nav.js-repo-nav'))?.append(container);
};

const restore = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const init = async (): Promise<void> => {
const container = document.createElement('div');
container.id = featureId;
renderTo(container);
(await elementReady('#repository-container-header'))?.append(container);
(await elementReady('nav.js-repo-nav'))?.append(container);
};

const restore = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import features from '../../../../feature-manager';
import getGithubTheme from '../../../../helpers/get-github-theme';
import {
getRepoName,
hasRepoContainerHeader,
isPublicRepoWithMeta,
} from '../../../../helpers/get-repo-info';
import { getStars } from '../../../../api/repo';
Expand Down Expand Up @@ -67,7 +68,7 @@ const restore = async () => {
};

features.add(featureId, {
asLongAs: [isPublicRepoWithMeta],
asLongAs: [isPublicRepoWithMeta, hasRepoContainerHeader],
awaitDomReady: false,
init,
restore,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3204,10 +3204,10 @@ get-stream@^6.0.0, get-stream@^6.0.1:
resolved "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz#a262d8eef67aced57c2852ad6167526a43cbf7b7"
integrity sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==

github-url-detection@^6.1.0:
version "6.1.0"
resolved "https://registry.npmjs.org/github-url-detection/-/github-url-detection-6.1.0.tgz#b5a478fa7cb36f673d3da52cd5b3075fb3a43b3a"
integrity sha512-Z2z3WmR38cbHegKHN3jiyo2wDBEPRlEn/8HHK05iidiHtNF8KltmjKiaNzB3QdGU+OxF2QhgwT1q4rLT15Z08w==
github-url-detection@^8.1.0:
version "8.1.0"
resolved "https://registry.npmjs.org/github-url-detection/-/github-url-detection-8.1.0.tgz#f415564f39c8b92ca2e823c22d1386bd0ac3fdf8"
integrity sha512-GvKgbLdIP6++wzBMCeJFJhoysFoUunE52m2B1K3ltfVfL3LO4TGQ/lv4utWoVCmVP85dVk+ZWkYA7IB3+fPt7A==

glob-parent@^5.1.1, glob-parent@^5.1.2, glob-parent@~5.1.2:
version "5.1.2"
Expand Down

0 comments on commit 71b1f48

Please sign in to comment.