Skip to content

Commit 6b003ab

Browse files
committed
1 parent a6b6734 commit 6b003ab

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

src/containers/not-found/dispatch.tsx

+48-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Trans, t } from '@lingui/macro';
22
import { Bullseye, DataList } from '@patternfly/react-core';
33
import React, { ReactNode, useEffect, useState } from 'react';
4-
import { Link } from 'react-router-dom';
4+
import { Link, Navigate } from 'react-router-dom';
55
import NotFoundImage from 'src/../static/images/not_found.svg';
66
import { CollectionVersionAPI, LegacyRoleAPI } from 'src/api';
77
import {
@@ -39,19 +39,60 @@ export const Dispatch = (props: RouteProps) => {
3939

4040
const [collections, setCollections] = useState(null);
4141
const [roles, setRoles] = useState(null);
42+
const [redirect, setRedirect] = useState(null);
4243

4344
useEffect(() => {
44-
CollectionVersionAPI.list({ namespace, name, is_highest: true })
45-
.then(({ data: { data } }) => setCollections(data || []))
46-
.catch(() => setCollections([]));
45+
const wait = [];
46+
47+
wait.push(
48+
CollectionVersionAPI.list({ namespace, name, is_highest: true })
49+
.then(({ data: { data } }) => setCollections(data || []))
50+
.catch(() => setCollections([])),
51+
);
4752

4853
if (featureFlags.legacy_roles) {
49-
LegacyRoleAPI.list({ github_user: namespace, name })
50-
.then(({ data: { results } }) => setRoles(results || []))
51-
.catch(() => setRoles([]));
54+
wait.push(
55+
LegacyRoleAPI.list({ github_user: namespace, name })
56+
.then(({ data: { results } }) => setRoles(results || []))
57+
.catch(() => setRoles([])),
58+
);
5259
}
60+
61+
Promise.all(wait).then(() => {
62+
if (collections.length === 1 && !roles?.length) {
63+
const {
64+
collection_version: { name: collection, namespace },
65+
repository: { name: repo },
66+
} = collections[0];
67+
setRedirect(
68+
formatPath(Paths.collectionByRepo, {
69+
collection,
70+
namespace,
71+
repo,
72+
}),
73+
);
74+
}
75+
if (roles.length === 1 && !collections.length) {
76+
const {
77+
name,
78+
summary_fields: {
79+
namespace: { name: username },
80+
},
81+
} = roles[0];
82+
setRedirect(
83+
formatPath(Paths.legacyRole, {
84+
username,
85+
name,
86+
}),
87+
);
88+
}
89+
});
5390
}, [pathname]);
5491

92+
if (redirect) {
93+
return <Navigate to={redirect} />;
94+
}
95+
5596
return (
5697
<>
5798
<BaseHeader title={t`404 - Page not found`} />

0 commit comments

Comments
 (0)