|
| 1 | +import { fetchAPI } from "@/lib/pulse-editor-website/backend"; |
| 2 | +import { addToast, Button, Input } from "@heroui/react"; |
| 3 | +import { useState } from "react"; |
| 4 | +import Icon from "./icon"; |
| 5 | + |
| 6 | +export default function EnvInput({ |
| 7 | + appId, |
| 8 | + env, |
| 9 | + onUpdated, |
| 10 | +}: { |
| 11 | + appId: string; |
| 12 | + env: { key: string; value: string; isSecret: boolean }; |
| 13 | + onUpdated: () => void; |
| 14 | +}) { |
| 15 | + const [isEditing, setIsEditing] = useState<boolean>(false); |
| 16 | + const [editedValue, setEditedValue] = useState<string>(env.value); |
| 17 | + const [isSecret, setIsSecret] = useState<boolean>(env.isSecret); |
| 18 | + |
| 19 | + return ( |
| 20 | + <div className="flex items-center gap-x-1" key={env.key}> |
| 21 | + <div className="w-1/3 font-mono text-sm break-all">{env.key}</div> |
| 22 | + <div className="w-2/3 font-mono text-sm break-all"> |
| 23 | + {isEditing ? ( |
| 24 | + <Input |
| 25 | + size="sm" |
| 26 | + value={editedValue} |
| 27 | + onValueChange={setEditedValue} |
| 28 | + type={isSecret ? "password" : "text"} |
| 29 | + /> |
| 30 | + ) : ( |
| 31 | + <p>{env.value}</p> |
| 32 | + )} |
| 33 | + </div> |
| 34 | + |
| 35 | + {isEditing ? ( |
| 36 | + <Button |
| 37 | + isIconOnly |
| 38 | + variant="light" |
| 39 | + onPress={async () => { |
| 40 | + addToast({ |
| 41 | + title: "Updating Variable", |
| 42 | + description: `Updating environment variable ${env.key}.`, |
| 43 | + }); |
| 44 | + |
| 45 | + await fetchAPI("/api/app/settings/env/set", { |
| 46 | + method: "POST", |
| 47 | + body: JSON.stringify({ |
| 48 | + appId: appId, |
| 49 | + key: env.key, |
| 50 | + value: editedValue, |
| 51 | + isSecret: isSecret, |
| 52 | + }), |
| 53 | + headers: { |
| 54 | + "Content-Type": "application/json", |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + onUpdated(); |
| 59 | + setIsEditing((prev) => false); |
| 60 | + |
| 61 | + addToast({ |
| 62 | + title: "Variable Updated", |
| 63 | + description: `Environment variable ${env.key} updated successfully.`, |
| 64 | + color: "success", |
| 65 | + }); |
| 66 | + }} |
| 67 | + > |
| 68 | + <Icon name="check" /> |
| 69 | + </Button> |
| 70 | + ) : ( |
| 71 | + <Button |
| 72 | + isIconOnly |
| 73 | + variant="light" |
| 74 | + onPress={() => setIsEditing((prev) => true)} |
| 75 | + > |
| 76 | + <Icon name="edit" /> |
| 77 | + </Button> |
| 78 | + )} |
| 79 | + |
| 80 | + {isEditing ? ( |
| 81 | + <Button |
| 82 | + isIconOnly |
| 83 | + variant="light" |
| 84 | + onPress={() => setIsSecret((prev) => !prev)} |
| 85 | + > |
| 86 | + {isSecret ? <Icon name="lock" /> : <Icon name="lock_open" />} |
| 87 | + </Button> |
| 88 | + ) : ( |
| 89 | + <Button |
| 90 | + isIconOnly |
| 91 | + variant="light" |
| 92 | + onPress={async () => { |
| 93 | + addToast({ |
| 94 | + title: "Deleting Variable", |
| 95 | + description: `Deleting environment variable ${env.key}.`, |
| 96 | + }); |
| 97 | + await fetchAPI("/api/app/settings/env/delete", { |
| 98 | + method: "DELETE", |
| 99 | + body: JSON.stringify({ |
| 100 | + appId: appId, |
| 101 | + key: env.key, |
| 102 | + }), |
| 103 | + headers: { |
| 104 | + "Content-Type": "application/json", |
| 105 | + }, |
| 106 | + }); |
| 107 | + onUpdated(); |
| 108 | + |
| 109 | + addToast({ |
| 110 | + title: "Variable Deleted", |
| 111 | + description: `Environment variable ${env.key} deleted successfully.`, |
| 112 | + color: "success", |
| 113 | + }); |
| 114 | + }} |
| 115 | + > |
| 116 | + <Icon name="delete" className="text-danger!" /> |
| 117 | + </Button> |
| 118 | + )} |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
0 commit comments