import { useCallback, useEffect, useRef, useState } from "react"; import useWebSocket, { ReadyState } from "react-use-websocket"; import { ControlBar, Editor, Frame, Panel } from "./components"; import { useConfig, useEvent } from "./hooks"; import { toPng, toBlob } from "html-to-image"; import download from "downloadjs"; const CODE_EMPTY_PLACEHOLDER = `print "Hello, CodeSnap.nvim!"`; function App() { const [socketUrl] = useState(`ws://${window.location.host}/ws`); const { sendMessage, lastMessage, readyState } = useWebSocket(socketUrl); const event = useEvent(lastMessage); const config = useConfig(event?.config_setup); const frameRef = useRef(null); const [isCopyButtonDisabled, setIsCopyButtonDisabled] = useState(false); const handleCopyButtonClick = useCallback(async () => { if (!frameRef.current) { return; } setIsCopyButtonDisabled(true); try { const blob = await toBlob(frameRef.current); const clipboardItem = new ClipboardItem({ "image/png": blob! }); navigator.clipboard.write([clipboardItem]); } catch (e) { console.error(e); } finally { setIsCopyButtonDisabled(false); } }, []); const handleExportClick = useCallback(async () => { if (!frameRef.current) { return; } const dataURL = await toPng(frameRef.current); download(dataURL, "codesnap.png"); }, []); const notifyCopyCommand = useCallback(async () => { if (!frameRef.current) { return; } const dataURL = await toPng(frameRef.current); sendMessage(dataURL); }, [sendMessage]); useEffect(() => { if (readyState !== ReadyState.OPEN || !event?.copy) { return; } notifyCopyCommand(); }, [event, readyState, notifyCopyCommand]); return (

CodeSnap.nvim

{event?.code?.content ?? CODE_EMPTY_PLACEHOLDER}
GitHub
); } export default App;