| 1 | import { |
| 2 | ArrowTopRightOnSquareIcon, |
| 3 | Cog6ToothIcon, |
| 4 | ExclamationTriangleIcon, |
| 5 | } from "@heroicons/react/24/outline"; |
| 6 | import { ProfileDescription } from "core/config/ProfileLifecycleManager"; |
| 7 | import { useContext, useMemo } from "react"; |
| 8 | import { useNavigate } from "react-router-dom"; |
| 9 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 10 | import { useAppDispatch } from "../../redux/hooks"; |
| 11 | import { setSelectedProfile } from "../../redux/slices/profilesSlice"; |
| 12 | import { isLocalProfile } from "../../util"; |
| 13 | import { ROUTES } from "../../util/navigation"; |
| 14 | import { useLump } from "../mainInput/Lump/LumpContext"; |
| 15 | import { ListboxOption, useFontSize } from "../ui"; |
| 16 | import { AssistantIcon } from "./AssistantIcon"; |
| 17 | |
| 18 | interface AssistantOptionProps { |
| 19 | profile: ProfileDescription; |
| 20 | selected: boolean; |
| 21 | onClick: () => void; |
| 22 | } |
| 23 | |
| 24 | export function AssistantOption({ |
| 25 | profile, |
| 26 | selected, |
| 27 | onClick, |
| 28 | }: AssistantOptionProps) { |
| 29 | const tinyFont = useFontSize(-4); |
| 30 | const navigate = useNavigate(); |
| 31 | const { setSelectedSection } = useLump(); |
| 32 | const dispatch = useAppDispatch(); |
| 33 | const ideMessenger = useContext(IdeMessengerContext); |
| 34 | |
| 35 | const hasFatalErrors = useMemo(() => { |
| 36 | return !!profile.errors?.find((error) => error.fatal); |
| 37 | }, [profile.errors]); |
| 38 | |
| 39 | function handleOptionClick() { |
| 40 | // optimistic update |
| 41 | dispatch(setSelectedProfile(profile.id)); |
| 42 | // notify core which will handle actual update |
| 43 | ideMessenger.post("didChangeSelectedProfile", { |
| 44 | id: profile.id, |
| 45 | }); |
| 46 | onClick(); |
| 47 | } |
| 48 | |
| 49 | function handleConfigure() { |
| 50 | ideMessenger.post("config/openProfile", { profileId: profile.id }); |
| 51 | onClick(); |
| 52 | } |
| 53 | |
| 54 | function handleClickError() { |
| 55 | if (profile.id === "local") { |
| 56 | navigate(ROUTES.HOME); |
| 57 | setSelectedSection("error"); |
| 58 | } else { |
| 59 | ideMessenger.post("config/openProfile", { profileId: profile.id }); |
| 60 | } |
| 61 | onClick(); |
| 62 | } |
| 63 | |
| 64 | return ( |
| 65 | <ListboxOption |
| 66 | value={profile.id} |
| 67 | disabled={hasFatalErrors} |
| 68 | onClick={!hasFatalErrors ? handleOptionClick : undefined} |
| 69 | fontSizeModifier={-2} |
| 70 | className={selected ? "bg-list-active text-list-active-foreground" : ""} |
| 71 | > |
| 72 | <div className="flex w-full items-center justify-between gap-2"> |
| 73 | <div className="flex w-full items-center gap-2"> |
| 74 | <div className="flex h-4 w-4 flex-shrink-0"> |
| 75 | <AssistantIcon size={3.5} assistant={profile} /> |
| 76 | </div> |
| 77 | <span |
| 78 | className={`line-clamp-1 flex-1 ${selected ? "font-semibold" : ""}`} |
| 79 | > |
| 80 | {profile.title} |
| 81 | </span> |
| 82 | </div> |
| 83 | <div className="flex flex-row items-center gap-1"> |
| 84 | {profile.errors && profile.errors?.length > 0 && ( |
| 85 | <ExclamationTriangleIcon |
| 86 | data-tooltip-id={`${profile.id}-errors-tooltip`} |
| 87 | className="text-error h-3 w-3 flex-shrink-0 cursor-pointer" |
| 88 | onClick={(e) => { |
| 89 | e.preventDefault(); |
| 90 | e.stopPropagation(); |
| 91 | handleClickError(); |
| 92 | }} |
| 93 | /> |
| 94 | )} |
| 95 | {isLocalProfile(profile) ? ( |
| 96 | <Cog6ToothIcon |
| 97 | className="text-description h-3 w-3 flex-shrink-0 cursor-pointer" |
| 98 | onClick={(e) => { |
| 99 | e.stopPropagation(); |
| 100 | e.preventDefault(); |
| 101 | handleConfigure(); |
| 102 | }} |
| 103 | /> |
| 104 | ) : ( |
| 105 | <ArrowTopRightOnSquareIcon |
| 106 | className="text-description h-3 w-3 flex-shrink-0 cursor-pointer" |
| 107 | onClick={(e) => { |
| 108 | e.stopPropagation(); |
| 109 | e.preventDefault(); |
| 110 | handleConfigure(); |
| 111 | }} |
| 112 | /> |
| 113 | )} |
| 114 | </div> |
| 115 | </div> |
| 116 | </ListboxOption> |
| 117 | ); |
| 118 | } |