import { useState, useEffect } from "react"; import { Plus } from "lucide-react"; import cocoLogoImg from "@/assets/app-icon.png"; import { tauriFetch } from "@/api/tauriFetchClient"; import { useConnectStore } from "@/stores/connectStore"; import { useAppStore } from "@/stores/appStore"; interface SidebarProps { addService: () => void; } type StringBooleanMap = { [key: string]: boolean; }; export function Sidebar({ addService }: SidebarProps) { const defaultService = useConnectStore((state) => state.defaultService); const currentService = useConnectStore((state) => state.currentService); const otherServices = useConnectStore((state) => state.otherServices); const setCurrentService = useConnectStore((state) => state.setCurrentService); const setEndpoint = useAppStore((state) => state.setEndpoint); const [defaultHealth, setDefaultHealth] = useState(false); const [otherHealth, setOtherHealth] = useState({}); const addServiceClick = () => { addService(); }; useEffect(() => { getDefaultHealth(); }, []); useEffect(() => { getOtherHealth(currentService); setEndpoint(currentService.endpoint); }, [currentService.endpoint]); const getDefaultHealth = () => { let baseURL = defaultService.endpoint if (baseURL.endsWith("/")) { baseURL = baseURL.slice(0, -1); } tauriFetch({ url: `${baseURL}/health`, method: "GET", }) .then((res) => { // "services": { // "system_cluster": "yellow" // }, // "status": "yellow" setDefaultHealth(res.data?.status !== "red"); }) .catch((err) => { console.error(err); }); }; const getOtherHealth = (item: any) => { if (!item.endpoint) return; // let baseURL = item.endpoint if (baseURL.endsWith("/")) { baseURL = baseURL.slice(0, -1); } tauriFetch({ url: `${baseURL}/health`, method: "GET", }) .then((res) => { let obj = { ...otherHealth, [item.endpoint]: res.data?.status !== "red", }; setOtherHealth(obj); }) .catch((err) => { console.error(err); }); }; return (
{ setCurrentService(defaultService); setEndpoint(defaultService.endpoint); getDefaultHealth(); }} > cocoLogoImg {defaultService.name}
Third-party services
{otherServices?.map((item, index) => (
{ setEndpoint(item.endpoint); setCurrentService(item); getOtherHealth(item); }} > LogoImg {item.name}
))}
); }