From 6fc2b70c32a887cd4a3b340f01b22e64570afaa2 Mon Sep 17 00:00:00 2001 From: Martin Hradil Date: Tue, 24 Oct 2023 22:00:39 +0000 Subject: [PATCH] Dispatch: redirect on 1 item https://forum.ansible.com/t/improvements-to-new-galaxy-s-404-error-pages-and-redirects/1888 No-Issue --- src/containers/not-found/dispatch.tsx | 57 +++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/containers/not-found/dispatch.tsx b/src/containers/not-found/dispatch.tsx index 0b337c51bb..77847ab914 100644 --- a/src/containers/not-found/dispatch.tsx +++ b/src/containers/not-found/dispatch.tsx @@ -28,10 +28,10 @@ const SectionTitle = ({ children }: { children: ReactNode }) => (

{children}

); -export const Dispatch = (props: RouteProps) => { +export const Dispatch = ({ location, navigate }: RouteProps) => { const { featureFlags } = useContext(); - const { pathname } = ParamHelper.parseParamString(props.location.search) as { + const { pathname } = ParamHelper.parseParamString(location.search) as { pathname: string; }; @@ -41,15 +41,56 @@ export const Dispatch = (props: RouteProps) => { const [roles, setRoles] = useState(null); useEffect(() => { - CollectionVersionAPI.list({ namespace, name, is_highest: true }) - .then(({ data: { data } }) => setCollections(data || [])) - .catch(() => setCollections([])); + const wait = []; + + wait.push( + CollectionVersionAPI.list({ namespace, name, is_highest: true }) + .then(({ data: { data } }) => data || []) + .catch(() => []) + .then((c) => (setCollections(c), c)), + ); if (featureFlags.legacy_roles) { - LegacyRoleAPI.list({ github_user: namespace, name }) - .then(({ data: { results } }) => setRoles(results || [])) - .catch(() => setRoles([])); + wait.push( + LegacyRoleAPI.list({ github_user: namespace, name }) + .then(({ data: { results } }) => results || []) + .catch(() => []) + .then((r) => (setRoles(r), r)), + ); } + + Promise.all(wait).then(([collections, roles]) => { + if (collections.length === 1 && !roles?.length) { + const { + collection_version: { name: collection, namespace }, + repository: { name: repo }, + } = collections[0]; + + navigate( + formatPath(Paths.collectionByRepo, { + collection, + namespace, + repo, + }), + ); + } + + if (roles.length === 1 && !collections.length) { + const { + name, + summary_fields: { + namespace: { name: username }, + }, + } = roles[0]; + + navigate( + formatPath(Paths.legacyRole, { + username, + name, + }), + ); + } + }); }, [pathname]); return (