Skip to content
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

ui: use route location instead of window.location (PROJQUAY-5392) #1844

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 17 additions & 11 deletions web/src/routes/NavigationPath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ export enum NavigationPath {
tagDetail = '/repository/:organizationName/:repositoryName/tag/:tagName',
}

export function getRepoDetailPath(org: string, repo: string) {
export function getRepoDetailPath(
currentRoute: string,
org: string,
repo: string,
) {
// return relative path to repository detail page from repo list table
let repoPath = NavigationPath.repositoryDetail.toString();
repoPath = repoPath.replace(':organizationName', org);
repoPath = repoPath.replace('*', repo);
return domainRoute(repoPath);
return domainRoute(currentRoute, repoPath);
}

export function getTagDetailPath(
currentRoute: string,
org: string,
repo: string,
tagName: string,
Expand All @@ -67,51 +72,52 @@ export function getTagDetailPath(
}
tagPath = tagPath + '?' + params.join('&');
}
return domainRoute(tagPath);
return domainRoute(currentRoute, tagPath);
}

export function getDomain() {
return process.env.REACT_APP_QUAY_DOMAIN || 'quay.io';
}

function domainRoute(definedRoute) {
function domainRoute(currentRoute, definedRoute) {
/***
* This function returns prefix + route.
Eg:If quay is hosted on https://stage.foo.redhat.com:1337/settings/quay/organization,
window.location.pathname here is `/settings/quay/organization`,
the regex removes everything after organization and returns /settings/quay.
So, the function returns /settings/quay/<route> .
***/
const currentRoute = window.location.pathname;
return (
currentRoute.replace(/\/(organization|repository|signin)(?!.*\1).*/, '') +
definedRoute
);
}

const currentRoute = window.location.pathname;

const NavigationRoutes = [
{
path: domainRoute(NavigationPath.organizationsList),
path: domainRoute(currentRoute, NavigationPath.organizationsList),
Component: <OrganizationsList />,
breadcrumb: Breadcrumb.organizationsListBreadcrumb,
},
{
path: domainRoute(NavigationPath.organizationDetail),
path: domainRoute(currentRoute, NavigationPath.organizationDetail),
Component: <Organization />,
breadcrumb: Breadcrumb.organizationDetailBreadcrumb,
},
{
path: domainRoute(NavigationPath.repositoriesList),
Component: <RepositoriesList />,
path: domainRoute(currentRoute, NavigationPath.repositoriesList),
Component: <RepositoriesList organizationName="" />,
breadcrumb: Breadcrumb.repositoriesListBreadcrumb,
},
{
path: domainRoute(NavigationPath.repositoryDetail),
path: domainRoute(currentRoute, NavigationPath.repositoryDetail),
Component: <RepositoryDetails />,
breadcrumb: Breadcrumb.repositoryDetailBreadcrumb,
},
{
path: domainRoute(NavigationPath.tagDetail),
path: domainRoute(currentRoute, NavigationPath.tagDetail),
Component: <TagDetails />,
breadcrumb: Breadcrumb.tagDetailBreadcrumb,
},
Expand Down
27 changes: 23 additions & 4 deletions web/src/routes/RepositoriesList/RepositoriesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {
Tbody,
Td,
} from '@patternfly/react-table';
import {useRecoilState, useRecoilValue} from 'recoil';
import {useRecoilState} from 'recoil';
import {IRepository} from 'src/resources/RepositoryResource';
import {ReactElement, useState} from 'react';
import {Link, useLocation} from 'react-router-dom';
import CreateRepositoryModalTemplate from 'src/components/modals/CreateRepoModalTemplate';
import {getRepoDetailPath} from 'src/routes/NavigationPath';
import {selectedReposState, searchRepoState} from 'src/atoms/RepositoryState';
import {selectedReposState} from 'src/atoms/RepositoryState';
import {formatDate, formatSize} from 'src/libs/utils';
import {BulkDeleteModalTemplate} from 'src/components/modals/BulkDeleteModalTemplate';
import {RepositoryToolBar} from 'src/routes/RepositoriesList/RepositoryToolBar';
Expand Down Expand Up @@ -69,6 +69,9 @@ export default function RepositoriesList(props: RepositoriesListProps) {
const [makePublicModalOpen, setmakePublicModal] = useState(false);
const [makePrivateModalOpen, setmakePrivateModal] = useState(false);
const [err, setErr] = useState<string[]>();
const location = useLocation();

console.log('current route location', location.pathname);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove the console.log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed


const quayConfig = useQuayConfig();
const {user} = useCurrentUser();
Expand Down Expand Up @@ -363,11 +366,23 @@ export default function RepositoriesList(props: RepositoriesListProps) {
/>
<Td dataLabel={RepositoryListColumnNames.name}>
{currentOrg == null ? (
<Link to={getRepoDetailPath(repo.namespace, repo.name)}>
<Link
to={getRepoDetailPath(
location.pathname,
repo.namespace,
repo.name,
)}
>
{repo.namespace}/{repo.name}
</Link>
) : (
<Link to={getRepoDetailPath(repo.namespace, repo.name)}>
<Link
to={getRepoDetailPath(
location.pathname,
repo.namespace,
repo.name,
)}
>
{repo.name}
</Link>
)}
Expand Down Expand Up @@ -414,3 +429,7 @@ interface RepoListTableItem {
size: number;
last_modified: number;
}

interface RepositoriesListProps {
organizationName: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason for adding the props here? I don't see the organizationName getting used in the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see RepositoriesListProps used in src/routes/RepositoriesList/RepositoriesList.tsx:65 but it's not defined so I'm defining it

}
27 changes: 23 additions & 4 deletions web/src/routes/RepositoryDetails/Tags/SecurityDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
SecurityDetailsResponse,
getSecurityDetails,
} from 'src/resources/TagResource';
import {Link} from 'react-router-dom';
import {Link, useLocation} from 'react-router-dom';
import {Skeleton} from '@patternfly/react-core';
import {getTagDetailPath} from 'src/routes/NavigationPath';
import {TabIndex} from 'src/routes/TagDetails/Types';
Expand Down Expand Up @@ -34,6 +34,7 @@ export default function SecurityDetails(props: SecurityDetailsProps) {
const [err, setErr] = useState<string>();
const setGlobalErr = useSetRecoilState(SecurityDetailsErrorState);
const setGlobalData = useSetRecoilState(SecurityDetailsState);
const location = useLocation();

const severityOrder = [
VulnerabilitySeverity.Critical,
Expand Down Expand Up @@ -117,7 +118,13 @@ export default function SecurityDetails(props: SecurityDetailsProps) {
if (vulnCount.size === 0) {
return (
<Link
to={getTagDetailPath(props.org, props.repo, props.tag, queryParams)}
to={getTagDetailPath(
location.pathname,
props.org,
props.repo,
props.tag,
queryParams,
)}
className={'pf-u-display-inline-flex pf-u-align-items-center'}
style={{textDecoration: 'none'}}
>
Expand All @@ -137,7 +144,13 @@ export default function SecurityDetails(props: SecurityDetailsProps) {
const highestSeverity: VulnerabilitySeverity = getHighestSeverity();
return (
<Link
to={getTagDetailPath(props.org, props.repo, props.tag, queryParams)}
to={getTagDetailPath(
location.pathname,
props.org,
props.repo,
props.tag,
queryParams,
)}
className={'pf-u-display-inline-flex pf-u-align-items-center'}
style={{textDecoration: 'none'}}
>
Expand Down Expand Up @@ -178,7 +191,13 @@ export default function SecurityDetails(props: SecurityDetailsProps) {
});
return (
<Link
to={getTagDetailPath(props.org, props.repo, props.tag, queryParams)}
to={getTagDetailPath(
location.pathname,
props.org,
props.repo,
props.tag,
queryParams,
)}
style={{textDecoration: 'none'}}
>
{counts}
Expand Down
8 changes: 7 additions & 1 deletion web/src/routes/RepositoryDetails/Tags/TagsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function SubRow(props: SubRowProps) {
<ExpandableRowContent>
<Link
to={getTagDetailPath(
location.pathname,
props.org,
props.repo,
props.tag.name,
Expand Down Expand Up @@ -126,7 +127,12 @@ function TagsTableRow(props: RowProps) {
/>
<Td dataLabel={ColumnNames.name}>
<Link
to={getTagDetailPath(props.org, props.repo, tag.name)}
to={getTagDetailPath(
location.pathname,
props.org,
props.repo,
tag.name,
)}
onClick={resetSecurityDetails}
>
{tag.name}
Expand Down
3 changes: 2 additions & 1 deletion web/src/routes/TagDetails/TagDetailsTabs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Tabs, Tab, TabTitleText} from '@patternfly/react-core';
import {useSearchParams, useNavigate} from 'react-router-dom';
import {useSearchParams, useNavigate, useLocation} from 'react-router-dom';
import {useState} from 'react';
import Details from './Details/Details';
import SecurityReport from './SecurityReport/SecurityReport';
Expand All @@ -20,6 +20,7 @@ function getTabIndex(tab: string) {
export default function TagTabs(props: TagTabsProps) {
const [activeTabKey, setActiveTabKey] = useState<TabIndex>(TabIndex.Details);
const navigate = useNavigate();
const location = useLocation();
Copy link
Contributor

Choose a reason for hiding this comment

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

location here doesn't seem to getting used in the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's used in src/routes/TagDetails/TagDetailsTabs.tsx:35 but not defined in the context which causes issues


// Navigate to the correct tab
const [searchParams] = useSearchParams();
Expand Down