adjustment: used enum for reference type

This commit is contained in:
Sheen Capadngan
2024-04-23 10:43:10 +08:00
parent 729c75112b
commit 918734b26b
2 changed files with 17 additions and 13 deletions

View File

@@ -8,7 +8,7 @@ import {
} from "@app/helpers/secret-reference";
import { useToggle } from "@app/hooks";
import SecretReferenceSelect from "./SecretReferenceSelect";
import SecretReferenceSelect, { ReferenceType } from "./SecretReferenceSelect";
const replaceContentWithDot = (str: string) => {
let finalStr = "";
@@ -194,7 +194,7 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
slug
}: {
name: string;
type: "folder" | "secret" | "environment";
type: ReferenceType;
slug?: string;
}) => {
setShowReferencePopup(false);
@@ -218,22 +218,22 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
];
let oldReferenceStr = oldReference.slice(2, -1);
let currentPath = type === "environment" ? slug! : name;
let currentPath = type === ReferenceType.ENVIRONMENT ? slug! : name;
currentPath = currentPath.replace(/\./g, "\\.");
let replaceReference = "";
let offset = 3;
switch (type) {
case "folder":
case ReferenceType.FOLDER:
replaceReference = `${oldReferenceStr}${currentPath}.`;
offset -= 1;
break;
case "secret": {
case ReferenceType.SECRET: {
if (oldReferenceStr.indexOf(".") === -1) oldReferenceStr = "";
replaceReference = `${oldReferenceStr}${currentPath}`;
break;
}
case "environment":
case ReferenceType.ENVIRONMENT:
replaceReference = `${currentPath}.`;
offset -= 1;
break;
@@ -244,10 +244,10 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
setValue(newValue);
// TODO: there should be a better way to do
onChange?.({ target: { value: newValue } } as any);
setShowReferencePopup(type !== "secret");
setShowReferencePopup(type !== ReferenceType.SECRET);
const timeout = setTimeout(() => {
setIsSecretFocused.on();
if (type !== "secret") setReferenceKey(replaceReference);
if (type !== ReferenceType.SECRET) setReferenceKey(replaceReference);
const caretPos = start.length + replaceReference.length + offset;
setCaretPos(caretPos);
clearTimeout(timeout);

View File

@@ -16,7 +16,11 @@ import { useGetUserWsKey } from "@app/hooks/api";
import { useGetFoldersByEnv } from "@app/hooks/api/secretFolders/queries";
import { useGetProjectSecrets } from "@app/hooks/api/secrets/queries";
type ReferenceType = "environment" | "folder" | "secret";
export enum ReferenceType {
ENVIRONMENT = "environment",
FOLDER = "folder",
SECRET = "secret"
}
type Props = {
open: boolean;
@@ -30,7 +34,7 @@ type Props = {
type ReferenceItem = {
name: string;
type: "folder" | "secret";
type: ReferenceType.FOLDER | ReferenceType.SECRET;
slug?: string;
};
@@ -95,12 +99,12 @@ export default function SecretReferenceSelect({
if (isNested) {
folders?.forEach((folder) => {
currentListReference.unshift({ name: folder, type: "folder" });
currentListReference.unshift({ name: folder, type: ReferenceType.FOLDER });
});
}
secrets?.forEach((secret) => {
currentListReference.unshift({ name: secret.key, type: "secret" });
currentListReference.unshift({ name: secret.key, type: ReferenceType.SECRET });
});
setListReference(currentListReference);
@@ -179,7 +183,7 @@ export default function SecretReferenceSelect({
<SelectPrimitive.Item
className="flex items-center justify-between border-b border-mineshaft-600 px-2 text-left last:border-b-0"
key={`secret-reference-env-${i + 1}`}
value={JSON.stringify({ ...env, type: "environment" })}
value={JSON.stringify({ ...env, type: ReferenceType.ENVIRONMENT })}
asChild
>
<SelectPrimitive.ItemText asChild>