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
@@ -8,7 +8,7 @@ import {
} from "@app/helpers/secret-reference"; } from "@app/helpers/secret-reference";
import { useToggle } from "@app/hooks"; import { useToggle } from "@app/hooks";
import SecretReferenceSelect from "./SecretReferenceSelect"; import SecretReferenceSelect, { ReferenceType } from "./SecretReferenceSelect";
const replaceContentWithDot = (str: string) => { const replaceContentWithDot = (str: string) => {
let finalStr = ""; let finalStr = "";
@@ -194,7 +194,7 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
slug slug
}: { }: {
name: string; name: string;
type: "folder" | "secret" | "environment"; type: ReferenceType;
slug?: string; slug?: string;
}) => { }) => {
setShowReferencePopup(false); setShowReferencePopup(false);
@@ -218,22 +218,22 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
]; ];
let oldReferenceStr = oldReference.slice(2, -1); let oldReferenceStr = oldReference.slice(2, -1);
let currentPath = type === "environment" ? slug! : name; let currentPath = type === ReferenceType.ENVIRONMENT ? slug! : name;
currentPath = currentPath.replace(/\./g, "\\."); currentPath = currentPath.replace(/\./g, "\\.");
let replaceReference = ""; let replaceReference = "";
let offset = 3; let offset = 3;
switch (type) { switch (type) {
case "folder": case ReferenceType.FOLDER:
replaceReference = `${oldReferenceStr}${currentPath}.`; replaceReference = `${oldReferenceStr}${currentPath}.`;
offset -= 1; offset -= 1;
break; break;
case "secret": { case ReferenceType.SECRET: {
if (oldReferenceStr.indexOf(".") === -1) oldReferenceStr = ""; if (oldReferenceStr.indexOf(".") === -1) oldReferenceStr = "";
replaceReference = `${oldReferenceStr}${currentPath}`; replaceReference = `${oldReferenceStr}${currentPath}`;
break; break;
} }
case "environment": case ReferenceType.ENVIRONMENT:
replaceReference = `${currentPath}.`; replaceReference = `${currentPath}.`;
offset -= 1; offset -= 1;
break; break;
@@ -244,10 +244,10 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
setValue(newValue); setValue(newValue);
// TODO: there should be a better way to do // TODO: there should be a better way to do
onChange?.({ target: { value: newValue } } as any); onChange?.({ target: { value: newValue } } as any);
setShowReferencePopup(type !== "secret"); setShowReferencePopup(type !== ReferenceType.SECRET);
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
setIsSecretFocused.on(); setIsSecretFocused.on();
if (type !== "secret") setReferenceKey(replaceReference); if (type !== ReferenceType.SECRET) setReferenceKey(replaceReference);
const caretPos = start.length + replaceReference.length + offset; const caretPos = start.length + replaceReference.length + offset;
setCaretPos(caretPos); setCaretPos(caretPos);
clearTimeout(timeout); clearTimeout(timeout);
@@ -16,7 +16,11 @@ import { useGetUserWsKey } from "@app/hooks/api";
import { useGetFoldersByEnv } from "@app/hooks/api/secretFolders/queries"; import { useGetFoldersByEnv } from "@app/hooks/api/secretFolders/queries";
import { useGetProjectSecrets } from "@app/hooks/api/secrets/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 = { type Props = {
open: boolean; open: boolean;
@@ -30,7 +34,7 @@ type Props = {
type ReferenceItem = { type ReferenceItem = {
name: string; name: string;
type: "folder" | "secret"; type: ReferenceType.FOLDER | ReferenceType.SECRET;
slug?: string; slug?: string;
}; };
@@ -95,12 +99,12 @@ export default function SecretReferenceSelect({
if (isNested) { if (isNested) {
folders?.forEach((folder) => { folders?.forEach((folder) => {
currentListReference.unshift({ name: folder, type: "folder" }); currentListReference.unshift({ name: folder, type: ReferenceType.FOLDER });
}); });
} }
secrets?.forEach((secret) => { secrets?.forEach((secret) => {
currentListReference.unshift({ name: secret.key, type: "secret" }); currentListReference.unshift({ name: secret.key, type: ReferenceType.SECRET });
}); });
setListReference(currentListReference); setListReference(currentListReference);
@@ -179,7 +183,7 @@ export default function SecretReferenceSelect({
<SelectPrimitive.Item <SelectPrimitive.Item
className="flex items-center justify-between border-b border-mineshaft-600 px-2 text-left last:border-b-0" 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}`} key={`secret-reference-env-${i + 1}`}
value={JSON.stringify({ ...env, type: "environment" })} value={JSON.stringify({ ...env, type: ReferenceType.ENVIRONMENT })}
asChild asChild
> >
<SelectPrimitive.ItemText asChild> <SelectPrimitive.ItemText asChild>