|
| 1 | +import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown"; |
| 2 | +import ButtonGroup from "@mui/material/ButtonGroup"; |
| 3 | +import Menu from "@mui/material/Menu"; |
| 4 | +import MenuItem from "@mui/material/MenuItem"; |
| 5 | +import { API } from "api/api"; |
| 6 | +import type { DisplayApp } from "api/typesGenerated"; |
| 7 | +import { VSCodeIcon } from "components/Icons/VSCodeIcon"; |
| 8 | +import { VSCodeInsidersIcon } from "components/Icons/VSCodeInsidersIcon"; |
| 9 | +import { type FC, useRef, useState } from "react"; |
| 10 | +import { AgentButton } from "../AgentButton"; |
| 11 | +import { DisplayAppNameMap } from "../AppLink/AppLink"; |
| 12 | + |
| 13 | +export interface VSCodeDevContainerButtonProps { |
| 14 | + userName: string; |
| 15 | + workspaceName: string; |
| 16 | + agentName?: string; |
| 17 | + devContainerName: string; |
| 18 | + devContainerFolder: string; |
| 19 | + displayApps: readonly DisplayApp[]; |
| 20 | +} |
| 21 | + |
| 22 | +type VSCodeVariant = "vscode" | "vscode-insiders"; |
| 23 | + |
| 24 | +const VARIANT_KEY = "vscode-variant"; |
| 25 | + |
| 26 | +export const VSCodeDevContainerButton: FC<VSCodeDevContainerButtonProps> = ( |
| 27 | + props, |
| 28 | +) => { |
| 29 | + const [isVariantMenuOpen, setIsVariantMenuOpen] = useState(false); |
| 30 | + const previousVariant = localStorage.getItem(VARIANT_KEY); |
| 31 | + const [variant, setVariant] = useState<VSCodeVariant>(() => { |
| 32 | + if (!previousVariant) { |
| 33 | + return "vscode"; |
| 34 | + } |
| 35 | + return previousVariant as VSCodeVariant; |
| 36 | + }); |
| 37 | + const menuAnchorRef = useRef<HTMLDivElement>(null); |
| 38 | + |
| 39 | + const selectVariant = (variant: VSCodeVariant) => { |
| 40 | + localStorage.setItem(VARIANT_KEY, variant); |
| 41 | + setVariant(variant); |
| 42 | + setIsVariantMenuOpen(false); |
| 43 | + }; |
| 44 | + |
| 45 | + const includesVSCodeDesktop = props.displayApps.includes("vscode"); |
| 46 | + const includesVSCodeInsiders = props.displayApps.includes("vscode_insiders"); |
| 47 | + |
| 48 | + return includesVSCodeDesktop && includesVSCodeInsiders ? ( |
| 49 | + <div> |
| 50 | + <ButtonGroup ref={menuAnchorRef} variant="outlined"> |
| 51 | + {variant === "vscode" ? ( |
| 52 | + <VSCodeButton {...props} /> |
| 53 | + ) : ( |
| 54 | + <VSCodeInsidersButton {...props} /> |
| 55 | + )} |
| 56 | + |
| 57 | + <AgentButton |
| 58 | + aria-controls={ |
| 59 | + isVariantMenuOpen ? "vscode-variant-button-menu" : undefined |
| 60 | + } |
| 61 | + aria-expanded={isVariantMenuOpen ? "true" : undefined} |
| 62 | + aria-label="select VSCode variant" |
| 63 | + aria-haspopup="menu" |
| 64 | + disableRipple |
| 65 | + onClick={() => { |
| 66 | + setIsVariantMenuOpen(true); |
| 67 | + }} |
| 68 | + css={{ paddingLeft: 0, paddingRight: 0 }} |
| 69 | + > |
| 70 | + <KeyboardArrowDownIcon css={{ fontSize: 16 }} /> |
| 71 | + </AgentButton> |
| 72 | + </ButtonGroup> |
| 73 | + |
| 74 | + <Menu |
| 75 | + open={isVariantMenuOpen} |
| 76 | + anchorEl={menuAnchorRef.current} |
| 77 | + onClose={() => setIsVariantMenuOpen(false)} |
| 78 | + css={{ |
| 79 | + "& .MuiMenu-paper": { |
| 80 | + width: menuAnchorRef.current?.clientWidth, |
| 81 | + }, |
| 82 | + }} |
| 83 | + > |
| 84 | + <MenuItem |
| 85 | + css={{ fontSize: 14 }} |
| 86 | + onClick={() => { |
| 87 | + selectVariant("vscode"); |
| 88 | + }} |
| 89 | + > |
| 90 | + <VSCodeIcon css={{ width: 12, height: 12 }} /> |
| 91 | + {DisplayAppNameMap.vscode} |
| 92 | + </MenuItem> |
| 93 | + <MenuItem |
| 94 | + css={{ fontSize: 14 }} |
| 95 | + onClick={() => { |
| 96 | + selectVariant("vscode-insiders"); |
| 97 | + }} |
| 98 | + > |
| 99 | + <VSCodeInsidersIcon css={{ width: 12, height: 12 }} /> |
| 100 | + {DisplayAppNameMap.vscode_insiders} |
| 101 | + </MenuItem> |
| 102 | + </Menu> |
| 103 | + </div> |
| 104 | + ) : includesVSCodeDesktop ? ( |
| 105 | + <VSCodeButton {...props} /> |
| 106 | + ) : ( |
| 107 | + <VSCodeInsidersButton {...props} /> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +const VSCodeButton: FC<VSCodeDevContainerButtonProps> = ({ |
| 112 | + userName, |
| 113 | + workspaceName, |
| 114 | + agentName, |
| 115 | + devContainerName, |
| 116 | + devContainerFolder, |
| 117 | +}) => { |
| 118 | + const [loading, setLoading] = useState(false); |
| 119 | + |
| 120 | + return ( |
| 121 | + <AgentButton |
| 122 | + startIcon={<VSCodeIcon />} |
| 123 | + disabled={loading} |
| 124 | + onClick={() => { |
| 125 | + setLoading(true); |
| 126 | + API.getApiKey() |
| 127 | + .then(({ key }) => { |
| 128 | + const query = new URLSearchParams({ |
| 129 | + owner: userName, |
| 130 | + workspace: workspaceName, |
| 131 | + url: location.origin, |
| 132 | + token: key, |
| 133 | + devContainerName, |
| 134 | + devContainerFolder, |
| 135 | + }); |
| 136 | + if (agentName) { |
| 137 | + query.set("agent", agentName); |
| 138 | + } |
| 139 | + |
| 140 | + location.href = `vscode://coder.coder-remote/openDevContainer?${query.toString()}`; |
| 141 | + }) |
| 142 | + .catch((ex) => { |
| 143 | + console.error(ex); |
| 144 | + }) |
| 145 | + .finally(() => { |
| 146 | + setLoading(false); |
| 147 | + }); |
| 148 | + }} |
| 149 | + > |
| 150 | + {DisplayAppNameMap.vscode} |
| 151 | + </AgentButton> |
| 152 | + ); |
| 153 | +}; |
| 154 | + |
| 155 | +const VSCodeInsidersButton: FC<VSCodeDevContainerButtonProps> = ({ |
| 156 | + userName, |
| 157 | + workspaceName, |
| 158 | + agentName, |
| 159 | + devContainerName, |
| 160 | + devContainerFolder, |
| 161 | +}) => { |
| 162 | + const [loading, setLoading] = useState(false); |
| 163 | + |
| 164 | + return ( |
| 165 | + <AgentButton |
| 166 | + startIcon={<VSCodeInsidersIcon />} |
| 167 | + disabled={loading} |
| 168 | + onClick={() => { |
| 169 | + setLoading(true); |
| 170 | + API.getApiKey() |
| 171 | + .then(({ key }) => { |
| 172 | + const query = new URLSearchParams({ |
| 173 | + owner: userName, |
| 174 | + workspace: workspaceName, |
| 175 | + url: location.origin, |
| 176 | + token: key, |
| 177 | + devContainerName, |
| 178 | + devContainerFolder, |
| 179 | + }); |
| 180 | + if (agentName) { |
| 181 | + query.set("agent", agentName); |
| 182 | + } |
| 183 | + |
| 184 | + location.href = `vscode-insiders://coder.coder-remote/openDevContainer?${query.toString()}`; |
| 185 | + }) |
| 186 | + .catch((ex) => { |
| 187 | + console.error(ex); |
| 188 | + }) |
| 189 | + .finally(() => { |
| 190 | + setLoading(false); |
| 191 | + }); |
| 192 | + }} |
| 193 | + > |
| 194 | + {DisplayAppNameMap.vscode_insiders} |
| 195 | + </AgentButton> |
| 196 | + ); |
| 197 | +}; |
0 commit comments