|
| 1 | +import { createDockerDesktopClient } from "@docker/extension-api-client"; |
| 2 | +import { ExecResult } from "@docker/extension-api-client-types/dist/v1"; |
| 3 | +import { |
| 4 | + Box, |
| 5 | + Button, |
| 6 | + CircularProgress, |
| 7 | + Dialog, |
| 8 | + DialogActions, |
| 9 | + DialogContent, |
| 10 | + DialogProps, |
| 11 | + DialogTitle, |
| 12 | + MenuItem, |
| 13 | + Stack, |
| 14 | + TextField, |
| 15 | + useMediaQuery, |
| 16 | +} from "@mui/material"; |
| 17 | +import { Add } from "@mui/icons-material"; |
| 18 | +import React from "react"; |
| 19 | +import { useLocalStorage } from "../hooks/useLocalStorage"; |
| 20 | +import { getConnectionString, officialDBs, setToLocalStorage } from "../utils"; |
| 21 | +import { IConnection, IDBConnection } from "../utils/types"; |
| 22 | + |
| 23 | +const ddClient = createDockerDesktopClient(); |
| 24 | + |
| 25 | +interface EditDatabaseDialogProps extends DialogProps { |
| 26 | + database: IDBConnection; |
| 27 | +} |
| 28 | + |
| 29 | +export const EditDatabaseDialog = (props: EditDatabaseDialogProps) => { |
| 30 | + // TODO: remove this as soon as the icon button colors are fixed in the design system |
| 31 | + const useDarkTheme = useMediaQuery("(prefers-color-scheme: dark)"); |
| 32 | + |
| 33 | + const { onClose, ...other } = props; |
| 34 | + |
| 35 | + const [username, setUsername] = React.useState(props.database.connection.username); |
| 36 | + const [password, setPassword] = React.useState(props.database.connection.password); |
| 37 | + const [port, setPort] = React.useState(props.database.connection.port); |
| 38 | + const [database, setDatabase] = React.useState(props.database.connection.database); |
| 39 | + const [creatingContainer, setCreatingContainer] = React.useState(false); |
| 40 | + const [isValid, setIsValid] = React.useState(false); |
| 41 | + const [error, setError] = React.useState(""); |
| 42 | + |
| 43 | + const resetForm = () => { |
| 44 | + setUsername(""); |
| 45 | + setPassword(""); |
| 46 | + setPort(""); |
| 47 | + setDatabase(""); |
| 48 | + }; |
| 49 | + |
| 50 | + const close = () => { |
| 51 | + resetForm(); |
| 52 | + onClose?.({}, "escapeKeyDown"); |
| 53 | + }; |
| 54 | + |
| 55 | + const handleChangeUsername = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 56 | + setUsername(event.target.value as string); |
| 57 | + setIsValid(Boolean(username && port && database)); |
| 58 | + }; |
| 59 | + |
| 60 | + const handleChangePassword = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 61 | + setPassword(event.target.value as string); |
| 62 | + }; |
| 63 | + |
| 64 | + const handleChangePort = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 65 | + // TODO: check port availability |
| 66 | + setPort(event.target.value as string); |
| 67 | + setIsValid(Boolean(username && port && database)); |
| 68 | + }; |
| 69 | + |
| 70 | + const handleChangeDatabase = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 71 | + setDatabase(event.target.value as string); |
| 72 | + setIsValid(Boolean(username && port && database)); |
| 73 | + }; |
| 74 | + |
| 75 | + const handleCreateDatabase = async () => { |
| 76 | + const foundDB = officialDBs.find( |
| 77 | + (db) => db.id === props.database.containerId |
| 78 | + ); |
| 79 | + if (!foundDB) return; |
| 80 | + |
| 81 | + const { image, defaults } = foundDB; |
| 82 | + |
| 83 | + let envs = []; |
| 84 | + const values = { password, username, database }; |
| 85 | + if (defaults.envs) { |
| 86 | + for (const key in defaults.envs) { |
| 87 | + const value = defaults.envs[key]; |
| 88 | + const variableName = value.replaceAll("%", ""); |
| 89 | + // @ts-expect-error type this |
| 90 | + const resolvedValue = values[variableName]; |
| 91 | + envs.push("-e", `${key}=${resolvedValue}`); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + setCreatingContainer(true); |
| 96 | + let containerID = ""; |
| 97 | + try { |
| 98 | + const result = await ddClient.docker.cli.exec("run", [ |
| 99 | + "-d", |
| 100 | + "-p", |
| 101 | + port, |
| 102 | + ...envs, |
| 103 | + image, |
| 104 | + ]); |
| 105 | + containerID = result.stdout.trim(); |
| 106 | + } catch (e) { |
| 107 | + console.log(e); |
| 108 | + setCreatingContainer(false); |
| 109 | + setError((e as ExecResult).stderr); |
| 110 | + |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const connectionString = getConnectionString(props.database.image, { |
| 115 | + password, |
| 116 | + username, |
| 117 | + database, |
| 118 | + port: port.split(":")[0], |
| 119 | + }); |
| 120 | + |
| 121 | + if (!connectionString) return; |
| 122 | + |
| 123 | + let name = ""; |
| 124 | + try { |
| 125 | + const result = await ddClient.docker.cli.exec("inspect", [ |
| 126 | + "-f", |
| 127 | + "{{.Name}}", |
| 128 | + containerID, |
| 129 | + ]); |
| 130 | + name = result.stdout.trim().replaceAll("/", ""); |
| 131 | + } catch (e) { |
| 132 | + console.log(e); |
| 133 | + } |
| 134 | + |
| 135 | + const dbConnection: IDBConnection = { |
| 136 | + containerId: containerID, |
| 137 | + containerName: name, |
| 138 | + connection: { port, username, password, database }, |
| 139 | + image, |
| 140 | + }; |
| 141 | + |
| 142 | + setToLocalStorage(containerID, dbConnection.connection); |
| 143 | + |
| 144 | + setCreatingContainer(false); |
| 145 | + // setCurrentDatabase(connection); |
| 146 | + close(); |
| 147 | + }; |
| 148 | + |
| 149 | + const { fullWidth = true, ...rest } = props; |
| 150 | + return ( |
| 151 | + <Dialog fullWidth={fullWidth} onClose={close} {...other}> |
| 152 | + <DialogTitle>Create a new database</DialogTitle> |
| 153 | + <DialogContent> |
| 154 | + <Stack gap={1.5} mt={2}> |
| 155 | + <TextField |
| 156 | + id="port" |
| 157 | + label="Port" |
| 158 | + value={port} |
| 159 | + onChange={handleChangePort} |
| 160 | + variant="outlined" |
| 161 | + /> |
| 162 | + <TextField |
| 163 | + id="database" |
| 164 | + label="Database name" |
| 165 | + value={database} |
| 166 | + onChange={handleChangeDatabase} |
| 167 | + variant="outlined" |
| 168 | + /> |
| 169 | + <TextField |
| 170 | + id="username" |
| 171 | + label="Username" |
| 172 | + value={username} |
| 173 | + onChange={handleChangeUsername} |
| 174 | + variant="outlined" |
| 175 | + /> |
| 176 | + <TextField |
| 177 | + id="password" |
| 178 | + type="password" |
| 179 | + label="Password" |
| 180 | + value={password} |
| 181 | + onChange={handleChangePassword} |
| 182 | + variant="outlined" |
| 183 | + /> |
| 184 | + {error && ( |
| 185 | + <Box |
| 186 | + sx={{ |
| 187 | + padding: 1, |
| 188 | + borderRadius: 1, |
| 189 | + backgroundColor: (theme) => |
| 190 | + useDarkTheme |
| 191 | + ? theme.palette.docker.red[200] |
| 192 | + : theme.palette.docker.red[100], // red-100 should be "#FDEAEA" but it's not 🤷♂️ |
| 193 | + }} |
| 194 | + > |
| 195 | + {error} |
| 196 | + </Box> |
| 197 | + )} |
| 198 | + </Stack> |
| 199 | + </DialogContent> |
| 200 | + <DialogActions> |
| 201 | + <Button autoFocus onClick={close} disabled={creatingContainer}> |
| 202 | + Cancel |
| 203 | + </Button> |
| 204 | + <Button |
| 205 | + onClick={handleCreateDatabase} |
| 206 | + disabled={creatingContainer || !isValid} |
| 207 | + startIcon={ |
| 208 | + creatingContainer ? <CircularProgress size={20} /> : <Add /> |
| 209 | + } |
| 210 | + > |
| 211 | + Create |
| 212 | + </Button> |
| 213 | + </DialogActions> |
| 214 | + </Dialog> |
| 215 | + ); |
| 216 | +}; |
0 commit comments