diff --git a/frontend/utils/base64ToImage.ts b/frontend/utils/base64ToImage.ts new file mode 100644 index 00000000..9ec9ac34 --- /dev/null +++ b/frontend/utils/base64ToImage.ts @@ -0,0 +1,20 @@ +/** + * Converts a Base64 string to a valid image source URL. + * + * @param base64String - The "image" field from the learning path JSON response. + * @returns A properly formatted data URL for use in an tag. + * + * @example + * // Fetch the learning path data and extract the image + * const response = await fetch( learning path route ); + * const data = await response.json(); + * const base64String = data.image; + * + * // Use in an element + * Learning Path Image + */ +export function convertBase64ToImageSrc(base64String: string): string { + return base64String.startsWith("data:image") + ? base64String + : `data:image/png;base64,${base64String}`; +}