|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License.AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { IDEOption, IDEOptions } from "@gitpod/gitpod-protocol/lib/ide-protocol"; |
| 8 | +import { useContext, useEffect, useState } from "react"; |
| 9 | +import { getGitpodService } from "../service/service"; |
| 10 | +import { UserContext } from "../user-context"; |
| 11 | +import CheckBox from "./CheckBox"; |
| 12 | +import DropDown2, { DropDown2Element } from "./DropDown2"; |
| 13 | + |
| 14 | +interface SelectIDEComponentProps { |
| 15 | + ideOptions?: IDEOptions; |
| 16 | + selectedIdeOption?: string; |
| 17 | + useLatest?: boolean; |
| 18 | + setUseLatest?: (useLatestVersion: boolean) => void; |
| 19 | + onSelectionChange: (ide: string) => void; |
| 20 | +} |
| 21 | + |
| 22 | +export default function SelectIDEComponent(props: SelectIDEComponentProps) { |
| 23 | + const { user } = useContext(UserContext); |
| 24 | + function options2Elements( |
| 25 | + ideOptions?: IDEOptions, |
| 26 | + useLatest?: boolean, |
| 27 | + setUseLatest?: (useLatest: boolean) => void, |
| 28 | + ): DropDown2Element[] { |
| 29 | + if (!ideOptions) { |
| 30 | + return []; |
| 31 | + } |
| 32 | + return [ |
| 33 | + ...IDEOptions.asArray(ideOptions).map((ide) => ({ |
| 34 | + id: ide.id, |
| 35 | + element: <IdeOptionElementSelected option={ide} useLatest={!!useLatest} />, |
| 36 | + elementInDropDown: <IdeOptionElementInDropDown option={ide} useLatest={!!useLatest} />, |
| 37 | + searchableString: `${ide.label}${ide.title}${ide.notes}${ide.id}`, |
| 38 | + isSelectable: true, |
| 39 | + })), |
| 40 | + { |
| 41 | + id: "non-selectable-checkbox", |
| 42 | + element: ( |
| 43 | + <div className="ml-3"> |
| 44 | + <CheckBox |
| 45 | + title="Use latest" |
| 46 | + desc="Use latest version of IDE" |
| 47 | + checked={!!useLatest} |
| 48 | + onChange={(e) => setUseLatest && setUseLatest(e.target.checked)} |
| 49 | + /> |
| 50 | + </div> |
| 51 | + ), |
| 52 | + isSelectable: false, |
| 53 | + }, |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + const [elements, setElements] = useState<DropDown2Element[]>( |
| 58 | + options2Elements(props.ideOptions, props.useLatest, props.setUseLatest), |
| 59 | + ); |
| 60 | + const [selectedIdeOption, setSelectedIdeOption] = useState<string | undefined>( |
| 61 | + props.selectedIdeOption || user?.additionalData?.ideSettings?.defaultIde, |
| 62 | + ); |
| 63 | + useEffect(() => { |
| 64 | + (async () => { |
| 65 | + let options = props.ideOptions; |
| 66 | + if (!options) { |
| 67 | + options = await getGitpodService().server.getIDEOptions(); |
| 68 | + } |
| 69 | + setElements(options2Elements(options, props.useLatest, props.setUseLatest)); |
| 70 | + if (!selectedIdeOption || !options.options[selectedIdeOption]) { |
| 71 | + setSelectedIdeOption(options.defaultIde); |
| 72 | + } |
| 73 | + })(); |
| 74 | + }, [props.ideOptions, selectedIdeOption, props.useLatest, props.setUseLatest]); |
| 75 | + return ( |
| 76 | + <DropDown2 |
| 77 | + elements={elements} |
| 78 | + onSelectionChange={props.onSelectionChange} |
| 79 | + searchable={true} |
| 80 | + selectedElement={selectedIdeOption} |
| 81 | + /> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +interface IdeOptionElementProps { |
| 86 | + option: IDEOption | undefined; |
| 87 | + useLatest: boolean; |
| 88 | +} |
| 89 | + |
| 90 | +function IdeOptionElementSelected(p: IdeOptionElementProps): JSX.Element { |
| 91 | + const { option, useLatest } = p; |
| 92 | + if (!option) { |
| 93 | + return <></>; |
| 94 | + } |
| 95 | + const version = useLatest ? option.latestImageVersion : option.imageVersion; |
| 96 | + const label = option.type === "desktop" ? "" : option.type; |
| 97 | + |
| 98 | + return ( |
| 99 | + <div className="flex" title={option.title}> |
| 100 | + <div className="mx-2 my-3"> |
| 101 | + <img className="w-8 filter-grayscale self-center" src={option.logo} alt="logo" /> |
| 102 | + </div> |
| 103 | + <div className="flex-col ml-1 mt-1 flex-grow"> |
| 104 | + <div className="flex">Editor</div> |
| 105 | + <div className="flex text-sm text-gray-100 dark:text-gray-600"> |
| 106 | + <div>{option.title}</div> |
| 107 | + <div className="ml-1">{version}</div> |
| 108 | + <div className="ml-1"> |
| 109 | + {label ? ( |
| 110 | + <span className={`font-semibold text-sm text-gray-600 dark:text-gray-500"}`}> |
| 111 | + {label[0].toLocaleUpperCase() + label.slice(1)} |
| 112 | + </span> |
| 113 | + ) : ( |
| 114 | + <></> |
| 115 | + )} |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +function IdeOptionElementInDropDown(p: IdeOptionElementProps): JSX.Element { |
| 124 | + const { option, useLatest } = p; |
| 125 | + if (!option) { |
| 126 | + return <></>; |
| 127 | + } |
| 128 | + const version = useLatest ? option.latestImageVersion : option.imageVersion; |
| 129 | + const label = option.type === "desktop" ? "" : option.type; |
| 130 | + |
| 131 | + return ( |
| 132 | + <div className="flex" title={option.title}> |
| 133 | + <div className="mx-2 my-3"> |
| 134 | + <img className="w-8 filter-grayscale self-center" src={option.logo} alt="logo" /> |
| 135 | + </div> |
| 136 | + <div className="flex-col ml-1 mt-1 flex-grow"> |
| 137 | + <div className="flex"> |
| 138 | + <div>{option.title}</div> |
| 139 | + <div className="ml-1 text-gray-100 dark:text-gray-600">{version}</div> |
| 140 | + </div> |
| 141 | + <div className=""> |
| 142 | + {label ? ( |
| 143 | + <span className={`font-semibold text-sm text-gray-600 dark:text-gray-500"}`}> |
| 144 | + {label[0].toLocaleUpperCase() + label.slice(1)} |
| 145 | + </span> |
| 146 | + ) : ( |
| 147 | + <></> |
| 148 | + )} |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + ); |
| 153 | +} |
0 commit comments