Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
import { useRef, useState, useCallback, useEffect } from 'react';
import { getCodeBlockFilename, triggerDownload } from '~/utils';
export default function useDownloadCode(
codeRef: React.RefObject<HTMLElement | null>,
lang?: string,
) {
const [isDownloaded, setIsDownloaded] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
const timerRef = useRef<ReturnType<typeof setTimeout>>();
useEffect(() => {
return () => clearTimeout(timerRef.current);
}, []);
const handleDownload = useCallback(() => {
const codeString = codeRef.current?.textContent;
if (codeString == null || codeString === '') {
return;
}
const wasFocused = document.activeElement === buttonRef.current;
setIsDownloaded(true);
const blob = new Blob([codeString], { type: 'text/plain' });
triggerDownload(URL.createObjectURL(blob), getCodeBlockFilename(lang));
if (wasFocused) {
requestAnimationFrame(() => buttonRef.current?.focus());
}
clearTimeout(timerRef.current);
timerRef.current = setTimeout(() => {
setIsDownloaded(false);
}, 3000);
}, [codeRef, lang]);
return { isDownloaded, buttonRef, handleDownload };
}