import type React from "react"; import type { ReactNode } from "react"; import { useState } from "react"; import { Button } from "../ui/button"; import { IpcClient } from "../../ipc/ipc_client"; import { useAtom, useAtomValue } from "jotai"; import { chatMessagesAtom, selectedChatIdAtom } from "../../atoms/chatAtoms"; import { useStreamChat } from "@/hooks/useStreamChat"; import { Package, ChevronsUpDown, ChevronsDownUp, Loader, ExternalLink, Download, } from "lucide-react"; import { CodeHighlight } from "./CodeHighlight"; interface DyadAddDependencyProps { children?: ReactNode; node?: any; packages?: string; } export const DyadAddDependency: React.FC = ({ children, node, }) => { // Extract package attribute from the node if available const packages = node?.properties?.packages?.split(" ") || ""; console.log("packages", packages); const [isInstalling, setIsInstalling] = useState(false); const [error, setError] = useState(null); const selectedChatId = useAtomValue(selectedChatIdAtom); const [messages, setMessages] = useAtom(chatMessagesAtom); const { streamMessage, isStreaming } = useStreamChat(); const [isContentVisible, setIsContentVisible] = useState(false); const hasChildren = !!children; const handleInstall = async () => { if (!packages || !selectedChatId) return; setIsInstalling(true); setError(null); try { const ipcClient = IpcClient.getInstance(); await ipcClient.addDependency({ chatId: selectedChatId, packages, }); // Refresh the chat messages const chat = await IpcClient.getInstance().getChat(selectedChatId); setMessages(chat.messages); await streamMessage({ prompt: `I've installed ${packages.join(", ")}. Keep going.`, chatId: selectedChatId, }); } catch (err) { setError("There was an error installing this package."); const chat = await IpcClient.getInstance().getChat(selectedChatId); setMessages(chat.messages); } finally { setIsInstalling(false); } }; return (
setIsContentVisible(!isContentVisible) : undefined } >
{packages.length > 0 && (
Do you want to install these packages?
{" "}
{packages.map((p: string) => ( { IpcClient.getInstance().openExternalUrl( `https://www.npmjs.com/package/${p}` ); }} > {p} ))}
)} {isInstalling && (
Installing...
)}
{hasChildren && (
{isContentVisible ? ( ) : ( )}
)}
{packages.length > 0 && (
Make sure these packages are what you want.{" "}
)} {/* Show content if it's visible and has children */} {isContentVisible && hasChildren && (
{children}
)} {/* Always show install button if there are no children */} {packages.length > 0 && !hasChildren && (
{error &&
{error}
}
)}
); };