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