Skip to content

Commit e60b827

Browse files
author
Joshua Eilers
authored
fix logic for multiple entities found and clean up messy code (#8113)
1 parent 798ce3d commit e60b827

File tree

4 files changed

+42
-55
lines changed

4 files changed

+42
-55
lines changed

datahub-web-react/src/app/embed/lookup/EmbedLookup.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import React from 'react';
2-
import { useParams } from 'react-router';
1+
import React, { useEffect } from 'react';
2+
import { useHistory, useParams } from 'react-router';
33
import styled from 'styled-components';
4+
import { LoadingOutlined } from '@ant-design/icons';
45
import { ErrorSection } from '../../shared/error/ErrorSection';
56
import useGetEntityByUrl from './useGetEntityByUrl';
67
import LookupNotFound from './LookupNotFound';
78
import LookupFoundMultiple from './LookupFoundMultiple';
8-
import LookupLoading from './LookupLoading';
9-
import GoToLookup from './GoToLookup';
10-
11-
type RouteParams = {
12-
url: string;
13-
};
149

1510
const PageContainer = styled.div`
1611
display: flex;
@@ -19,17 +14,29 @@ const PageContainer = styled.div`
1914
height: 85vh;
2015
`;
2116

17+
const LookupLoading = styled(LoadingOutlined)`
18+
font-size: 50px;
19+
`;
20+
21+
type RouteParams = {
22+
url: string;
23+
};
24+
2225
const EmbedLookup = () => {
26+
const history = useHistory();
2327
const { url: encodedUrl } = useParams<RouteParams>();
2428
const decodedUrl = decodeURIComponent(encodedUrl);
25-
const { count, entity, error, loading } = useGetEntityByUrl(decodedUrl);
29+
const { embedUrl, notFound, foundMultiple, error } = useGetEntityByUrl(decodedUrl);
30+
31+
useEffect(() => {
32+
if (embedUrl) history.push(embedUrl);
33+
}, [embedUrl, history]);
2634

2735
const getContent = () => {
28-
if (loading) return <LookupLoading />;
2936
if (error) return <ErrorSection />;
30-
if (count === 0 || !entity) return <LookupNotFound url={encodedUrl} />;
31-
if (count > 1) return <LookupFoundMultiple url={encodedUrl} />;
32-
return <GoToLookup entityType={entity.type} entityUrn={entity.urn} />;
37+
if (notFound) return <LookupNotFound url={encodedUrl} />;
38+
if (foundMultiple) return <LookupFoundMultiple url={encodedUrl} />;
39+
return <LookupLoading />;
3340
};
3441

3542
return <PageContainer>{getContent()}</PageContainer>;

datahub-web-react/src/app/embed/lookup/GoToLookup.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

datahub-web-react/src/app/embed/lookup/LookupLoading.tsx

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { useMemo } from 'react';
21
import { useGetSearchResultsForMultipleQuery } from '../../../graphql/search.generated';
32
import { FilterOperator } from '../../../types.generated';
43
import { UnionType } from '../../search/utils/constants';
54
import { generateOrFilters } from '../../search/utils/generateOrFilters';
5+
import { PageRoutes } from '../../../conf/Global';
6+
import { useEntityRegistry } from '../../useEntityRegistry';
7+
import { urlEncodeUrn } from '../../entity/shared/utils';
68

79
const URL_FIELDS = ['externalUrl', 'chartUrl', 'dashboardUrl'] as const;
810

911
const useGetEntityByUrl = (externalUrl: string) => {
10-
const { data, loading, error } = useGetSearchResultsForMultipleQuery({
12+
const registry = useEntityRegistry();
13+
const { data, error } = useGetSearchResultsForMultipleQuery({
1114
variables: {
1215
input: {
1316
query: '*',
@@ -25,20 +28,24 @@ const useGetEntityByUrl = (externalUrl: string) => {
2528
},
2629
});
2730

28-
const entities = data?.searchAcrossEntities?.searchResults.map((result) => result.entity) ?? [];
29-
const count = entities.length;
30-
const entity = count === 1 ? entities[0] : null;
31+
const getLookupData = () => {
32+
if (!data) return {} as const;
3133

32-
return useMemo(
33-
() =>
34-
({
35-
count,
36-
entity,
37-
loading,
38-
error,
39-
} as const),
40-
[count, entity, error, loading],
41-
);
34+
const entities = data.searchAcrossEntities?.searchResults.map((result) => result.entity) ?? [];
35+
const notFound = entities.length === 0;
36+
const foundMultiple = entities.length > 1;
37+
const entity = entities.length === 1 ? entities[0] : null;
38+
const embedUrl = entity
39+
? `${PageRoutes.EMBED}/${registry.getPathName(entity.type)}/${urlEncodeUrn(entity.urn)}`
40+
: null;
41+
42+
return { notFound, foundMultiple, embedUrl } as const;
43+
};
44+
45+
return {
46+
error,
47+
...getLookupData(),
48+
} as const;
4249
};
4350

4451
export default useGetEntityByUrl;

0 commit comments

Comments
 (0)