Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/repositories/detail/variables/ConfigurationAddVariableModal.tsx
2502 views
1
/**
2
* Copyright (c) 2023 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 { useCallback, useState } from "react";
8
import { LoadingButton } from "@podkit/buttons/LoadingButton";
9
import Modal, { ModalHeader, ModalBody, ModalFooter, ModalFooterAlert } from "../../../components/Modal";
10
import { CheckboxInputField } from "../../../components/forms/CheckboxInputField";
11
import { Button } from "@podkit/buttons/Button";
12
import { EnvironmentVariableAdmission } from "@gitpod/public-api/lib/gitpod/v1/envvar_pb";
13
import { useCreateConfigurationVariable } from "../../../data/configurations/configuration-queries";
14
import { useToast } from "../../../components/toasts/Toasts";
15
import { TextInputField } from "../../../components/forms/TextInputField";
16
17
type Props = {
18
configurationId: string;
19
onClose: () => void;
20
};
21
export const AddVariableModal = ({ configurationId, onClose }: Props) => {
22
const { toast } = useToast();
23
24
const [name, setName] = useState("");
25
const [value, setValue] = useState("");
26
const [admission, setAdmission] = useState<EnvironmentVariableAdmission>(EnvironmentVariableAdmission.EVERYWHERE);
27
const createVariable = useCreateConfigurationVariable();
28
29
const addVariable = useCallback(() => {
30
createVariable.mutateAsync(
31
{
32
configurationId,
33
name,
34
value,
35
admission,
36
},
37
{
38
onSuccess: () => {
39
toast("Variable added");
40
onClose();
41
},
42
},
43
);
44
}, [createVariable, configurationId, name, value, admission, onClose, toast]);
45
46
return (
47
<Modal visible onClose={onClose} onSubmit={addVariable}>
48
<ModalHeader>Add a variable</ModalHeader>
49
<ModalBody>
50
<div className="mt-8">
51
<TextInputField
52
label="Name"
53
autoComplete={"off"}
54
className="w-full"
55
value={name}
56
placeholder="Variable name"
57
onChange={(name) => setName(name)}
58
autoFocus
59
required
60
/>
61
</div>
62
<div className="mt-4">
63
<TextInputField
64
label="Value"
65
autoComplete={"off"}
66
className="w-full"
67
value={value}
68
placeholder="Variable value"
69
onChange={(value) => setValue(value)}
70
required
71
/>
72
</div>
73
<CheckboxInputField
74
label="Only use this variable in Prebuilds"
75
hint="This will hide the variable in the workspace, however, it may still appear in system logs."
76
checked={admission === EnvironmentVariableAdmission.PREBUILD}
77
onChange={(checked) =>
78
setAdmission(
79
checked ? EnvironmentVariableAdmission.PREBUILD : EnvironmentVariableAdmission.EVERYWHERE,
80
)
81
}
82
/>
83
</ModalBody>
84
<ModalFooter
85
alert={
86
createVariable.isError ? (
87
<ModalFooterAlert type="danger">
88
{String(createVariable.error).replace(/Error: Request \w+ failed with message: /, "")}
89
</ModalFooterAlert>
90
) : null
91
}
92
>
93
<Button variant="secondary" onClick={onClose}>
94
Cancel
95
</Button>
96
<LoadingButton type="submit" loading={createVariable.isLoading}>
97
Add Variable
98
</LoadingButton>
99
</ModalFooter>
100
</Modal>
101
);
102
};
103
104