From 918734b26bd28ee679745f879bad6b78f732260c Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Tue, 23 Apr 2024 10:43:10 +0800 Subject: [PATCH] adjustment: used enum for reference type --- .../InfisicalSecretInput.tsx | 16 ++++++++-------- .../SecretReferenceSelect.tsx | 14 +++++++++----- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/frontend/src/components/v2/InfisicalSecretInput/InfisicalSecretInput.tsx b/frontend/src/components/v2/InfisicalSecretInput/InfisicalSecretInput.tsx index 5987e9278..09f42ff7d 100644 --- a/frontend/src/components/v2/InfisicalSecretInput/InfisicalSecretInput.tsx +++ b/frontend/src/components/v2/InfisicalSecretInput/InfisicalSecretInput.tsx @@ -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( slug }: { name: string; - type: "folder" | "secret" | "environment"; + type: ReferenceType; slug?: string; }) => { setShowReferencePopup(false); @@ -218,22 +218,22 @@ export const InfisicalSecretInput = forwardRef( ]; 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( 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); diff --git a/frontend/src/components/v2/InfisicalSecretInput/SecretReferenceSelect.tsx b/frontend/src/components/v2/InfisicalSecretInput/SecretReferenceSelect.tsx index 454e3600c..6d2157af3 100644 --- a/frontend/src/components/v2/InfisicalSecretInput/SecretReferenceSelect.tsx +++ b/frontend/src/components/v2/InfisicalSecretInput/SecretReferenceSelect.tsx @@ -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({