Skip to content

Commit

Permalink
🐛 Fix img fetching logic
Browse files Browse the repository at this point in the history
Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 committed Sep 29, 2023
1 parent 8130ba2 commit d74e17c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion client/src/app/components/KeycloakProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { deleteCookie, getCookie, setCookie } from "@app/queries/cookies";
import { AppPlaceholder } from "./AppPlaceholder";
import { Flex, FlexItem, Spinner } from "@patternfly/react-core";
import { ReactKeycloakProvider } from "@react-keycloak/web";
import React, { Suspense, useEffect } from "react";
import React, { Suspense } from "react";

interface IKeycloakProviderProps {
children: React.ReactNode;
Expand Down
45 changes: 28 additions & 17 deletions client/src/app/components/TargetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import {
SelectVariant,
SelectOptionObject,
} from "@patternfly/react-core/deprecated";
import { CubesIcon, GripVerticalIcon } from "@patternfly/react-icons";
import { GripVerticalIcon } from "@patternfly/react-icons";
import spacing from "@patternfly/react-styles/css/utilities/Spacing/spacing";
import { useTranslation } from "react-i18next";

import { KebabDropdown } from "./KebabDropdown";
import DefaultRulesetIcon from "@app/images/Icon-Red_Hat-Virtual_server_stack-A-Black-RGB.svg";
import { Target, TargetLabel } from "@app/api/models";
import "./TargetCard.css";
import axios from "axios";

export interface TargetCardProps {
item: Target;
Expand Down Expand Up @@ -68,6 +69,8 @@ export const TargetCard: React.FC<TargetCardProps> = ({
const { t } = useTranslation();
const [isCardSelected, setCardSelected] = React.useState(cardSelected);

const [imageDataUrl, setImageDataUrl] = React.useState<string | null>(null);

Check warning on line 72 in client/src/app/components/TargetCard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/TargetCard.tsx#L72

Added line #L72 was not covered by tests

const prevSelectedLabel =
formLabels?.find((formLabel) => {
const labelNames = target?.labels?.map((label) => label.name);
Expand Down Expand Up @@ -105,23 +108,23 @@ export const TargetCard: React.FC<TargetCardProps> = ({
}
};

const getImage = (): React.ComponentType => {
let result: React.ComponentType<any> = CubesIcon;
const imagePath = target?.image?.id
? `/hub/files/${target?.image.id}`
: DefaultRulesetIcon;
if (target.image) {
result = () => (
<img
src={imagePath}
alt="Card logo"
style={{ height: 80, pointerEvents: "none" }}
/>
);
const fetchImageAndSetDataUrl = async (imagePath: string) => {
try {
const imageDataResponse = await axios.get(imagePath, {

Check warning on line 113 in client/src/app/components/TargetCard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/TargetCard.tsx#L111-L113

Added lines #L111 - L113 were not covered by tests
headers: {
Accept: "application/octet-stream",
},
});
const encodedSvg = encodeURIComponent(imageDataResponse.data);
setImageDataUrl(`data:image/svg+xml,${encodedSvg}`);

Check warning on line 119 in client/src/app/components/TargetCard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/TargetCard.tsx#L118-L119

Added lines #L118 - L119 were not covered by tests
} catch (error) {
console.error("There was an issue fetching the image:", error);

Check warning on line 121 in client/src/app/components/TargetCard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/TargetCard.tsx#L121

Added line #L121 was not covered by tests
}

return result;
};
const imagePath = target?.image?.id
? `/hub/files/${target?.image.id}`
: DefaultRulesetIcon;
fetchImageAndSetDataUrl(imagePath);

Check warning on line 127 in client/src/app/components/TargetCard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/TargetCard.tsx#L125-L127

Added lines #L125 - L127 were not covered by tests

return (
<Card
Expand Down Expand Up @@ -176,7 +179,15 @@ export const TargetCard: React.FC<TargetCardProps> = ({
variant={EmptyStateVariant.sm}
className="select-card__component__empty-state"
>
<EmptyStateIcon icon={getImage()} />
<EmptyStateIcon
icon={() => (
<img

Check warning on line 184 in client/src/app/components/TargetCard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/TargetCard.tsx#L184

Added line #L184 was not covered by tests
src={imageDataUrl || DefaultRulesetIcon}
alt="Card logo"
style={{ height: 80, pointerEvents: "none" }}
/>
)}
/>
<Title headingLevel="h4" size="md">
{target.name}
</Title>
Expand Down

0 comments on commit d74e17c

Please sign in to comment.