|
1 | 1 | import { Trans, t } from '@lingui/macro';
|
2 | 2 | import { Bullseye, DataList } from '@patternfly/react-core';
|
3 | 3 | import React, { ReactNode, useEffect, useState } from 'react';
|
4 |
| -import { Link } from 'react-router-dom'; |
| 4 | +import { Link, Navigate } from 'react-router-dom'; |
5 | 5 | import NotFoundImage from 'src/../static/images/not_found.svg';
|
6 | 6 | import { CollectionVersionAPI, LegacyRoleAPI } from 'src/api';
|
7 | 7 | import {
|
@@ -39,19 +39,60 @@ export const Dispatch = (props: RouteProps) => {
|
39 | 39 |
|
40 | 40 | const [collections, setCollections] = useState(null);
|
41 | 41 | const [roles, setRoles] = useState(null);
|
| 42 | + const [redirect, setRedirect] = useState(null); |
42 | 43 |
|
43 | 44 | 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 | + ); |
47 | 52 |
|
48 | 53 | 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 | + ); |
52 | 59 | }
|
| 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 | + }); |
53 | 90 | }, [pathname]);
|
54 | 91 |
|
| 92 | + if (redirect) { |
| 93 | + return <Navigate to={redirect} />; |
| 94 | + } |
| 95 | + |
55 | 96 | return (
|
56 | 97 | <>
|
57 | 98 | <BaseHeader title={t`404 - Page not found`} />
|
|
0 commit comments