2025-01-23 18:19:04 -05:00
|
|
|
import React, { useState } from 'react';
|
2025-11-12 13:32:47 -05:00
|
|
|
import { Download, CircleCheckBig } from 'lucide-react';
|
2025-01-23 18:19:04 -05:00
|
|
|
import type { Artifact } from '~/common';
|
2025-11-12 13:32:47 -05:00
|
|
|
import { Button } from '@librechat/client';
|
2025-01-23 18:19:04 -05:00
|
|
|
import useArtifactProps from '~/hooks/Artifacts/useArtifactProps';
|
2025-11-12 13:32:47 -05:00
|
|
|
import { useCodeState } from '~/Providers/EditorContext';
|
2025-01-23 18:19:04 -05:00
|
|
|
import { useLocalize } from '~/hooks';
|
|
|
|
|
|
2025-11-12 13:32:47 -05:00
|
|
|
const DownloadArtifact = ({ artifact }: { artifact: Artifact }) => {
|
2025-01-23 18:19:04 -05:00
|
|
|
const localize = useLocalize();
|
2025-11-12 13:32:47 -05:00
|
|
|
const { currentCode } = useCodeState();
|
2025-01-23 18:19:04 -05:00
|
|
|
const [isDownloaded, setIsDownloaded] = useState(false);
|
|
|
|
|
const { fileKey: fileName } = useArtifactProps({ artifact });
|
|
|
|
|
|
|
|
|
|
const handleDownload = () => {
|
|
|
|
|
try {
|
|
|
|
|
const content = currentCode ?? artifact.content ?? '';
|
|
|
|
|
if (!content) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const blob = new Blob([content], { type: 'text/plain' });
|
|
|
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
|
const link = document.createElement('a');
|
|
|
|
|
link.href = url;
|
|
|
|
|
link.download = fileName;
|
|
|
|
|
document.body.appendChild(link);
|
|
|
|
|
link.click();
|
|
|
|
|
document.body.removeChild(link);
|
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
|
setIsDownloaded(true);
|
|
|
|
|
setTimeout(() => setIsDownloaded(false), 3000);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Download failed:', error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-11-12 13:32:47 -05:00
|
|
|
<Button
|
|
|
|
|
size="icon"
|
|
|
|
|
variant="ghost"
|
2025-01-23 18:19:04 -05:00
|
|
|
onClick={handleDownload}
|
|
|
|
|
aria-label={localize('com_ui_download_artifact')}
|
|
|
|
|
>
|
2025-11-12 13:32:47 -05:00
|
|
|
{isDownloaded ? <CircleCheckBig size={16} /> : <Download size={16} />}
|
|
|
|
|
</Button>
|
2025-01-23 18:19:04 -05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DownloadArtifact;
|