fix: Copy JSON button form submission

This commit is contained in:
Francisco Veiras 2025-07-06 12:08:20 -03:00
commit 9842a6278a

View file

@ -252,28 +252,32 @@ const DynamicJsonForm = ({
}, [shouldUseJsonMode, isJsonMode]); }, [shouldUseJsonMode, isJsonMode]);
const handleCopyJson = useCallback(() => { const handleCopyJson = useCallback(() => {
try { const copyToClipboard = async () => {
navigator.clipboard try {
.writeText(JSON.stringify(value, null, 2) ?? "[]") await navigator.clipboard.writeText(
.then(() => { JSON.stringify(value, null, 2) ?? "[]",
setCopiedJson(true); );
setCopiedJson(true);
toast({ toast({
title: "JSON copied", title: "JSON copied",
description: description:
"The JSON data has been successfully copied to your clipboard.", "The JSON data has been successfully copied to your clipboard.",
});
setTimeout(() => {
setCopiedJson(false);
}, 2000);
})
.catch((error) => {
reportError(error);
}); });
} catch (error) {
reportError(error); setTimeout(() => {
} setCopiedJson(false);
}, 2000);
} catch (error) {
toast({
title: "Error",
description: `Failed to copy JSON: ${error instanceof Error ? error.message : String(error)}`,
variant: "destructive",
});
}
};
copyToClipboard();
}, [toast, value]); }, [toast, value]);
return ( return (
@ -281,7 +285,12 @@ const DynamicJsonForm = ({
<div className="flex justify-end space-x-2"> <div className="flex justify-end space-x-2">
{isJsonMode && ( {isJsonMode && (
<> <>
<Button variant="outline" size="sm" onClick={handleCopyJson}> <Button
type="button"
variant="outline"
size="sm"
onClick={handleCopyJson}
>
{copiedJson ? ( {copiedJson ? (
<CheckCheck className="h-4 w-4 mr-2" /> <CheckCheck className="h-4 w-4 mr-2" />
) : ( ) : (