| 1 | import { |
| 2 | Cog6ToothIcon, |
| 3 | ExclamationTriangleIcon, |
| 4 | } from "@heroicons/react/24/outline"; |
| 5 | import { ProfileDescription } from "core/config/ProfileLifecycleManager"; |
| 6 | import { useContext, useMemo } from "react"; |
| 7 | import { useNavigate } from "react-router-dom"; |
| 8 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 9 | import { useAppDispatch } from "../../redux/hooks"; |
| 10 | import { setSelectedProfile } from "../../redux/slices/profilesSlice"; |
| 11 | import { CONFIG_ROUTES } from "../../util/navigation"; |
| 12 | import { ToolTip } from "../gui/Tooltip"; |
| 13 | import { Button, ListboxOption, useFontSize } from "../ui"; |
| 14 | import { AssistantIcon } from "./AssistantIcon"; |
| 15 | |
| 16 | interface AssistantOptionProps { |
| 17 | profile: ProfileDescription; |
| 18 | selected: boolean; |
| 19 | onClick: () => void; |
| 20 | } |
| 21 | |
| 22 | export function AssistantOption({ |
| 23 | profile, |
| 24 | selected, |
| 25 | onClick, |
| 26 | }: AssistantOptionProps) { |
| 27 | const tinyFont = useFontSize(-4); |
| 28 | const navigate = useNavigate(); |
| 29 | const dispatch = useAppDispatch(); |
| 30 | const ideMessenger = useContext(IdeMessengerContext); |
| 31 | |
| 32 | const hasFatalErrors = useMemo(() => { |
| 33 | return !!profile.errors?.find((error) => error.fatal); |
| 34 | }, [profile.errors]); |
| 35 | |
| 36 | function handleOptionClick() { |
| 37 | // Always select the agent first (even if it has errors) |
| 38 | // optimistic update |
| 39 | dispatch(setSelectedProfile(profile.id)); |
| 40 | // notify core which will handle actual update |
| 41 | ideMessenger.post("didChangeSelectedProfile", { |
| 42 | id: profile.id, |
| 43 | }); |
| 44 | |
| 45 | onClick(); |
| 46 | } |
| 47 | |
| 48 | return ( |
| 49 | <ListboxOption |
| 50 | value={profile.id} |
| 51 | disabled={hasFatalErrors} |
| 52 | onClick={!hasFatalErrors ? handleOptionClick : undefined} |
| 53 | fontSizeModifier={-2} |
| 54 | className={`group ${selected ? "bg-list-active text-list-active-foreground" : ""}`} |
| 55 | > |
| 56 | <div className="flex w-full items-center justify-between gap-10 py-0.5"> |
| 57 | <div className="flex w-full items-center gap-3"> |
| 58 | <div className="flex h-4 w-4 flex-shrink-0 items-center justify-center"> |
| 59 | <AssistantIcon assistant={profile} /> |
| 60 | </div> |
| 61 | <span |
| 62 | className={`line-clamp-1 flex-1 ${selected ? "font-semibold" : ""} ${profile.errors && profile.errors.length > 0 ? "text-error" : ""}`} |
| 63 | > |
| 64 | {profile.title} |
| 65 | </span> |
| 66 | </div> |
| 67 | <div className="flex flex-row items-center gap-1.5"> |
| 68 | <Button |
| 69 | variant="ghost" |
| 70 | size="sm" |
| 71 | className="text-description-muted hover:enabled:text-foreground my-0 h-4 w-4 p-0 opacity-0 transition-opacity group-hover:opacity-100" |
| 72 | onClick={(e) => { |
| 73 | e.stopPropagation(); |
| 74 | ideMessenger.post("config/openProfile", { |
| 75 | profileId: profile.id, |
| 76 | }); |
| 77 | onClick(); // Close the listbox |
| 78 | }} |
| 79 | > |
| 80 | <Cog6ToothIcon className="h-3.5 w-3.5" /> |
| 81 | </Button> |
| 82 | {profile.errors && profile.errors?.length > 0 && ( |
| 83 | <ToolTip content="View errors"> |
| 84 | <Button |
| 85 | variant="ghost" |
| 86 | size="sm" |
| 87 | className="text-error hover:enabled:text-error my-0 h-4 w-4 p-0" |
| 88 | onClick={(e) => { |
| 89 | e.stopPropagation(); |
| 90 | navigate(CONFIG_ROUTES.AGENTS); |
| 91 | }} |
| 92 | > |
| 93 | <ExclamationTriangleIcon className="h-3.5 w-3.5" /> |
| 94 | </Button> |
| 95 | </ToolTip> |
| 96 | )} |
| 97 | </div> |
| 98 | </div> |
| 99 | </ListboxOption> |
| 100 | ); |
| 101 | } |