mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-24 17:48:30 +02:00
Fix SVG viewer crashing
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import type { HttpResponse } from '@yaakapp-internal/models';
|
import type { HttpResponse } from '@yaakapp-internal/models';
|
||||||
import React from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { useResponseBodyText } from '../../hooks/useResponseBodyText';
|
import { useResponseBodyText } from '../../hooks/useResponseBodyText';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -8,7 +8,23 @@ interface Props {
|
|||||||
|
|
||||||
export function SvgViewer({ response }: Props) {
|
export function SvgViewer({ response }: Props) {
|
||||||
const rawTextBody = useResponseBodyText(response);
|
const rawTextBody = useResponseBodyText(response);
|
||||||
if (rawTextBody.data == null) return null;
|
const [src, setSrc] = useState<string | null>(null);
|
||||||
const src = `data:image/svg+xml;base64,${btoa(rawTextBody.data)}`;
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!rawTextBody.data) {
|
||||||
|
return setSrc(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
const blob = new Blob([rawTextBody.data], { type: 'image/svg+xml;charset=utf-8' });
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
setSrc(url);
|
||||||
|
|
||||||
|
return () => URL.revokeObjectURL(url);
|
||||||
|
}, [rawTextBody.data]);
|
||||||
|
|
||||||
|
if (src == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return <img src={src} alt="Response preview" className="max-w-full max-h-full pb-2" />;
|
return <img src={src} alt="Response preview" className="max-w-full max-h-full pb-2" />;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user