| 1 | import { |
| 2 | ArrowPathIcon, |
| 3 | ArrowTopRightOnSquareIcon, |
| 4 | } from "@heroicons/react/24/outline"; |
| 5 | import { useContext, useEffect, useState } from "react"; |
| 6 | import { StyledActionButton } from "../.."; |
| 7 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 8 | import { providers } from "../../../pages/AddNewModel/configs/providers"; |
| 9 | import { OllamaConnectionStatuses } from "../utils"; |
| 10 | import OllamaCompletedStep from "./OllamaCompletedStep"; |
| 11 | |
| 12 | interface OllamaStatusProps { |
| 13 | isOllamaConnected: boolean; |
| 14 | } |
| 15 | |
| 16 | const downloadUrl = providers.ollama!.downloadUrl!; |
| 17 | |
| 18 | export function OllamaStatus({ isOllamaConnected }: OllamaStatusProps) { |
| 19 | const ideMessenger = useContext(IdeMessengerContext); |
| 20 | |
| 21 | const [status, setStatus] = useState<OllamaConnectionStatuses>( |
| 22 | OllamaConnectionStatuses.WaitingToDownload, |
| 23 | ); |
| 24 | useEffect(() => { |
| 25 | if (isOllamaConnected) { |
| 26 | setStatus(OllamaConnectionStatuses.Connected); |
| 27 | } else if (status !== OllamaConnectionStatuses.Downloading) { |
| 28 | setStatus(OllamaConnectionStatuses.WaitingToDownload); |
| 29 | } |
| 30 | }, [status, isOllamaConnected]); |
| 31 | |
| 32 | function onClickDownload() { |
| 33 | ideMessenger.post("openUrl", downloadUrl); |
| 34 | setStatus(OllamaConnectionStatuses.Downloading); |
| 35 | } |
| 36 | |
| 37 | switch (status) { |
| 38 | case OllamaConnectionStatuses.WaitingToDownload: |
| 39 | return ( |
| 40 | <StyledActionButton onClick={onClickDownload}> |
| 41 | <p className="mr-1 line-clamp-1 text-sm underline">{downloadUrl}</p> |
| 42 | <ArrowTopRightOnSquareIcon width={16} height={16} /> |
| 43 | </StyledActionButton> |
| 44 | ); |
| 45 | case OllamaConnectionStatuses.Downloading: |
| 46 | return ( |
| 47 | <div className="flex items-center justify-between"> |
| 48 | <p className="lines mr-1 w-3/4 font-mono text-sm"> |
| 49 | Checking for connection to Ollama at http://localhost:11434 |
| 50 | </p> |
| 51 | <ArrowPathIcon className="animate-spin-slow mr-1 h-3 w-3" /> |
| 52 | </div> |
| 53 | ); |
| 54 | case OllamaConnectionStatuses.Connected: |
| 55 | return ( |
| 56 | <OllamaCompletedStep text="Ollama is running at http://localhost:11434" /> |
| 57 | ); |
| 58 | } |
| 59 | } |