useApi hook implementation outside of conditional block

Signed-off-by: hram_wh <hakkam@Hakkams-MacBook-Pro.local>
This commit is contained in:
hram_wh
2022-09-23 12:51:12 +05:30
parent 5c25ce6d9e
commit 15bed4655e
2 changed files with 25 additions and 43 deletions
@@ -33,51 +33,37 @@ import {
import { useApi } from '@backstage/core-plugin-api';
const Body = (props: { exploreTools?: Array<ExploreTool> }) => {
var exploreTools;
if (!props?.exploreTools) {
const exploreToolsConfigApi = useApi(exploreToolsConfigRef);
const {
value: tools,
loading,
error,
} = useAsync(async () => {
return await exploreToolsConfigApi.getTools();
}, [exploreToolsConfigApi]);
const exploreToolsConfigApi = useApi(exploreToolsConfigRef);
const {
value: tools,
loading,
error,
} = useAsync(async () => {
if (props?.exploreTools) return props?.exploreTools;
return await exploreToolsConfigApi.getTools();
}, [exploreToolsConfigApi]);
if (loading) {
return <Progress />;
}
if (loading) {
return <Progress />;
}
if (error) {
return <WarningPanel title="Failed to load tools" />;
}
if (error) {
return <WarningPanel title="Failed to load tools" />;
}
if (!tools?.length) {
return (
<EmptyState
missing="info"
title="No tools to display"
description="You haven't added any tools yet."
/>
);
}
exploreTools = tools;
} else if (props?.exploreTools) {
exploreTools = props?.exploreTools;
if (!props?.exploreTools?.length) {
return (
<EmptyState
missing="info"
title="No tools to display"
description="You haven't added any tools yet."
/>
);
}
if (!tools?.length) {
return (
<EmptyState
missing="info"
title="No tools to display"
description="You haven't added any tools yet."
/>
);
}
return (
<ItemCardGrid>
{exploreTools?.map((tool, index) => (
{tools?.map((tool, index) => (
<ToolCard key={index} card={tool} />
))}
</ItemCardGrid>