|
| 1 | +import TextField from "@mui/material/TextField"; |
| 2 | +import type { ProvisionerDaemon } from "api/typesGenerated"; |
| 3 | +import { Button } from "components/Button/Button"; |
| 4 | +import { Input } from "components/Input/Input"; |
| 5 | +import { PlusIcon } from "lucide-react"; |
| 6 | +import { ProvisionerTag } from "modules/provisioners/ProvisionerTag"; |
| 7 | +import { type FC, useRef, useState } from "react"; |
| 8 | +import * as Yup from "yup"; |
| 9 | + |
| 10 | +// Users can't delete these tags |
| 11 | +const REQUIRED_TAGS = ["scope", "organization", "user"]; |
| 12 | + |
| 13 | +// Users can't override these tags |
| 14 | +const IMMUTABLE_TAGS = ["owner"]; |
| 15 | + |
| 16 | +type ProvisionerTagsFieldProps = { |
| 17 | + value: ProvisionerDaemon["tags"]; |
| 18 | + onChange: (value: ProvisionerDaemon["tags"]) => void; |
| 19 | +}; |
| 20 | + |
| 21 | +export const ProvisionerTagsField: FC<ProvisionerTagsFieldProps> = ({ |
| 22 | + value: fieldValue, |
| 23 | + onChange, |
| 24 | +}) => { |
| 25 | + return ( |
| 26 | + <div className="flex flex-col gap-3"> |
| 27 | + <div className="flex items-center gap-2 flex-wrap"> |
| 28 | + {Object.entries(fieldValue) |
| 29 | + // Filter out since users cannot override it |
| 30 | + .filter(([key]) => !IMMUTABLE_TAGS.includes(key)) |
| 31 | + .map(([key, value]) => { |
| 32 | + const onDelete = (key: string) => { |
| 33 | + const { [key]: _, ...newFieldValue } = fieldValue; |
| 34 | + onChange(newFieldValue); |
| 35 | + }; |
| 36 | + |
| 37 | + return ( |
| 38 | + <ProvisionerTag |
| 39 | + key={key} |
| 40 | + tagName={key} |
| 41 | + tagValue={value} |
| 42 | + // Required tags can't be deleted |
| 43 | + onDelete={REQUIRED_TAGS.includes(key) ? undefined : onDelete} |
| 44 | + /> |
| 45 | + ); |
| 46 | + })} |
| 47 | + </div> |
| 48 | + |
| 49 | + <NewTagControl |
| 50 | + onAdd={(tag) => { |
| 51 | + onChange({ ...fieldValue, [tag.key]: tag.value }); |
| 52 | + }} |
| 53 | + /> |
| 54 | + </div> |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +const newTagSchema = Yup.object({ |
| 59 | + key: Yup.string() |
| 60 | + .required("Key is required") |
| 61 | + .notOneOf(["owner"], "Cannot override owner tag"), |
| 62 | + value: Yup.string() |
| 63 | + .required("Value is required") |
| 64 | + .when("key", ([key], schema) => { |
| 65 | + if (key === "scope") { |
| 66 | + return schema.oneOf( |
| 67 | + ["organization", "scope"], |
| 68 | + "Scope value must be 'organization' or 'user'", |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + return schema; |
| 73 | + }), |
| 74 | +}); |
| 75 | + |
| 76 | +type Tag = { key: string; value: string }; |
| 77 | + |
| 78 | +type NewTagControlProps = { |
| 79 | + onAdd: (tag: Tag) => void; |
| 80 | +}; |
| 81 | + |
| 82 | +const NewTagControl: FC<NewTagControlProps> = ({ onAdd }) => { |
| 83 | + const keyInputRef = useRef<HTMLInputElement>(null); |
| 84 | + const [error, setError] = useState<string>(); |
| 85 | + const [newTag, setNewTag] = useState<Tag>({ |
| 86 | + key: "", |
| 87 | + value: "", |
| 88 | + }); |
| 89 | + |
| 90 | + const addNewTag = async () => { |
| 91 | + try { |
| 92 | + await newTagSchema.validate(newTag); |
| 93 | + onAdd(newTag); |
| 94 | + setNewTag({ key: "", value: "" }); |
| 95 | + keyInputRef.current?.focus(); |
| 96 | + } catch (e) { |
| 97 | + const isValidationError = e instanceof Yup.ValidationError; |
| 98 | + |
| 99 | + if (!isValidationError) { |
| 100 | + throw e; |
| 101 | + } |
| 102 | + |
| 103 | + if (e instanceof Yup.ValidationError) { |
| 104 | + setError(e.errors[0]); |
| 105 | + } |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + const addNewTagOnEnter = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 110 | + if (e.key === "Enter") { |
| 111 | + e.preventDefault(); |
| 112 | + e.stopPropagation(); |
| 113 | + addNewTag(); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + return ( |
| 118 | + <div className="flex flex-col gap-1 max-w-72"> |
| 119 | + <div className="flex items-center gap-2"> |
| 120 | + <label className="sr-only" htmlFor="tag-key-input"> |
| 121 | + Tag key |
| 122 | + </label> |
| 123 | + <TextField |
| 124 | + inputRef={keyInputRef} |
| 125 | + size="small" |
| 126 | + id="tag-key-input" |
| 127 | + name="key" |
| 128 | + placeholder="Key" |
| 129 | + value={newTag.key} |
| 130 | + onChange={(e) => setNewTag({ ...newTag, key: e.target.value.trim() })} |
| 131 | + onKeyDown={addNewTagOnEnter} |
| 132 | + /> |
| 133 | + |
| 134 | + <label className="sr-only" htmlFor="tag-value-input"> |
| 135 | + Tag value |
| 136 | + </label> |
| 137 | + <TextField |
| 138 | + size="small" |
| 139 | + id="tag-value-input" |
| 140 | + name="value" |
| 141 | + placeholder="Value" |
| 142 | + value={newTag.value} |
| 143 | + onChange={(e) => |
| 144 | + setNewTag({ ...newTag, value: e.target.value.trim() }) |
| 145 | + } |
| 146 | + onKeyDown={addNewTagOnEnter} |
| 147 | + /> |
| 148 | + |
| 149 | + <Button |
| 150 | + className="flex-shrink-0" |
| 151 | + size="icon" |
| 152 | + type="button" |
| 153 | + onClick={addNewTag} |
| 154 | + > |
| 155 | + <PlusIcon /> |
| 156 | + <span className="sr-only">Add tag</span> |
| 157 | + </Button> |
| 158 | + </div> |
| 159 | + {error && ( |
| 160 | + <span className="text-xs text-content-destructive">{error}</span> |
| 161 | + )} |
| 162 | + </div> |
| 163 | + ); |
| 164 | +}; |
0 commit comments