-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
211 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
useEffect(() => { | ||
const imagePath = target?.image?.id | ||
? `${FILES}/${target?.image.id}` | ||
: DefaultImage; | ||
|
||
(async () => { | ||
try { | ||
const response = await axios.get(imagePath, { | ||
headers: { | ||
Accept: "application/octet-stream", | ||
}, | ||
responseType: "arraybuffer", | ||
}); | ||
const contentType = response.headers["content-type"]; | ||
|
||
let imageData; | ||
|
||
if (contentType === "image/svg+xml") { | ||
const text = new TextDecoder().decode(response.data); | ||
imageData = `data:${contentType},${encodeURIComponent(text)}`; | ||
} else { | ||
const base64 = btoa( | ||
new Uint8Array(response.data).reduce( | ||
(data, byte) => data + String.fromCharCode(byte), | ||
"" | ||
) | ||
); | ||
imageData = `data:${contentType};base64,${base64}`; | ||
} | ||
|
||
setImageDataUrl(imageData); | ||
} catch (error) { | ||
console.error("There was an issue fetching the image:", error); | ||
} | ||
})(); | ||
}, [target]); | ||
|
||
return imageDataUrl; | ||
}; | ||
|
||
export default useFetchImageDataUrl; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.