Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fetch target images in auth envs #1420

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useState, useEffect } from "react";
import axios from "axios";
import DefaultImage from "@app/images/Icon-Red_Hat-Virtual_server_stack-A-Black-RGB.svg";
import { Target } from "@app/api/models";
import { FILES } from "@app/api/rest";

const useFetchImageDataUrl = (target: Target) => {
const [imageDataUrl, setImageDataUrl] = useState<string | null>(null);

Check warning on line 8 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L8

Added line #L8 was not covered by tests

useEffect(() => {

Check warning on line 10 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L10

Added line #L10 was not covered by tests
const imagePath = target?.image?.id
? `${FILES}/${target?.image.id}`
: DefaultImage;

Check warning on line 13 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L12-L13

Added lines #L12 - L13 were not covered by tests

(async () => {
try {
const response = await axios.get(imagePath, {

Check warning on line 17 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L15-L17

Added lines #L15 - L17 were not covered by tests
headers: {
Accept: "application/octet-stream",
},
responseType: "arraybuffer",
});
const contentType = response.headers["content-type"];

Check warning on line 23 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L23

Added line #L23 was not covered by tests

let imageData;

if (contentType === "image/svg+xml") {
const text = new TextDecoder().decode(response.data);
imageData = `data:${contentType},${encodeURIComponent(text)}`;
} else {
const base64 = btoa(

Check warning on line 31 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L28-L31

Added lines #L28 - L31 were not covered by tests
new Uint8Array(response.data).reduce(
(data, byte) => data + String.fromCharCode(byte),

Check warning on line 33 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L33

Added line #L33 was not covered by tests
""
)
);
imageData = `data:${contentType};base64,${base64}`;

Check warning on line 37 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L37

Added line #L37 was not covered by tests
}

setImageDataUrl(imageData);

Check warning on line 40 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L40

Added line #L40 was not covered by tests
} catch (error) {
console.error("There was an issue fetching the image:", error);

Check warning on line 42 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L42

Added line #L42 was not covered by tests
}
})();
}, [target]);

return imageDataUrl;

Check warning on line 47 in client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts#L47

Added line #L47 was not covered by tests
};

export default useFetchImageDataUrl;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "./target-card.css";
import * as React from "react";
import {
EmptyState,
Expand All @@ -24,14 +25,14 @@
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 { KebabDropdown } from "../KebabDropdown";
import DefaultImage from "@app/images/Icon-Red_Hat-Virtual_server_stack-A-Black-RGB.svg";
import { Target, TargetLabel } from "@app/api/models";
import "./TargetCard.css";
import useFetchImageDataUrl from "./hooks/useFetchImageDataUrl";

export interface TargetCardProps {
item: Target;
Expand Down Expand Up @@ -67,6 +68,7 @@
}) => {
const { t } = useTranslation();
const [isCardSelected, setCardSelected] = React.useState(cardSelected);
const imageDataUrl = useFetchImageDataUrl(target);

Check warning on line 71 in client/src/app/components/target-card/target-card.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/target-card.tsx#L71

Added line #L71 was not covered by tests

const prevSelectedLabel =
formLabels?.find((formLabel) => {
Expand Down Expand Up @@ -105,24 +107,6 @@
}
};

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" }}
/>
);
}

return result;
};

return (
<Card
onClick={handleCardClick}
Expand Down Expand Up @@ -176,7 +160,18 @@
variant={EmptyStateVariant.sm}
className="select-card__component__empty-state"
>
<EmptyStateIcon icon={getImage()} />
<EmptyStateIcon
icon={() => (
<img

Check warning on line 165 in client/src/app/components/target-card/target-card.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/target-card.tsx#L165

Added line #L165 was not covered by tests
src={imageDataUrl || DefaultImage}
alt="Card logo"
style={{ height: 80, pointerEvents: "none" }}
onError={(e) => {
e.currentTarget.src = DefaultImage;

Check warning on line 170 in client/src/app/components/target-card/target-card.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/target-card/target-card.tsx#L169-L170

Added lines #L169 - L170 were not covered by tests
}}
/>
)}
/>
<Title headingLevel="h4" size="md">
{target.name}
</Title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { useTranslation } from "react-i18next";
import { useFormContext } from "react-hook-form";

import { TargetCard } from "@app/components/TargetCard";
import { TargetCard } from "@app/components/target-card/target-card";
import { AnalysisWizardFormValues } from "./schema";
import { useSetting } from "@app/queries/settings";
import { useFetchTargets } from "@app/queries/targets";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { forwardRef } from "react";
import { TargetCard } from "@app/components/TargetCard";
import { TargetCard } from "@app/components/target-card/target-card";
import { useFetchTargets } from "@app/queries/targets";

interface ItemProps {
Expand Down
Loading