import { Action, ActionPanel, Color, Detail, Icon, List, showToast, Toast, useNavigation, } from "@raycast/api"; import { useCallback, useEffect, useState } from "react"; import { getErrorMessage, listCommands, RaycastCommandSummary, runCommand, stringifyResult, } from "./kody-client"; function resolveIcon(icon: string): Icon | { source: string } { if (!icon) return Icon.Terminal; if ([...icon].length <= 2) return { source: icon }; const key = icon as keyof typeof Icon; if (Icon[key]) return Icon[key]; return Icon.Terminal; } export default function Command() { const { push } = useNavigation(); const [commands, setCommands] = useState([]); const [isLoading, setIsLoading] = useState(true); const [runningId, setRunningId] = useState(null); const load = useCallback(async (forceRefresh = false) => { setIsLoading(true); try { setCommands(await listCommands({ forceRefresh })); } catch (error) { await showToast({ style: Toast.Style.Failure, title: "Failed to load Kody commands", message: getErrorMessage(error), }); } finally { setIsLoading(false); } }, []); useEffect(() => { void load(false); }, [load]); async function run(command: RaycastCommandSummary) { try { setRunningId(command.id); const result = await runCommand({ commandId: command.id }); await showToast({ style: Toast.Style.Success, title: "Kody finished", message: command.title, }); push( , ); } catch (error) { await showToast({ style: Toast.Style.Failure, title: "Kody command failed", message: getErrorMessage(error), }); } finally { setRunningId(null); } } return ( {commands.map((command) => ( void run(command)} /> void load(true)} /> } /> ))} ); }