From ec6475379586cf3c54ce89d1c7b718477c4adab1 Mon Sep 17 00:00:00 2001 From: Salman Date: Fri, 15 Mar 2024 22:49:40 +0530 Subject: [PATCH] fix: refactor and onyl valid env --- .../components/v2/SecretInput/SecretInput.tsx | 59 +++++++++---------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/frontend/src/components/v2/SecretInput/SecretInput.tsx b/frontend/src/components/v2/SecretInput/SecretInput.tsx index f08706da0..b91025e89 100644 --- a/frontend/src/components/v2/SecretInput/SecretInput.tsx +++ b/frontend/src/components/v2/SecretInput/SecretInput.tsx @@ -11,7 +11,8 @@ import { useGetFoldersByEnv } from "@app/hooks/api/secretFolders/queries"; import { useGetProjectSecrets } from "@app/hooks/api/secrets/queries"; const REGEX_REFERENCE = /(\${([^}]*)})/g; -const REGEX_REFERENCE_INVALID = /(?:\/|\\|\n|\.$|^\.)/g +const REGEX_REFERENCE_INVALID = /(?:\/|\\|\n|\.$|^\.)/g; + const replaceContentWithDot = (str: string) => { let finalStr = ""; for (let i = 0; i < str.length; i += 1) { @@ -33,12 +34,16 @@ const syntaxHighlight = (content?: string | null, isVisible?: boolean) => { skipNext = true; return ( - ${{el.slice(2, -1)} + > + {el.slice(2, -1)} + } ); @@ -98,25 +103,21 @@ export const SecretInput = forwardRef( const [listReference, setListReference] = useState([]); const [showReferencePopup, setShowReferencePopup] = useState(false); const [referenceKey, setReferenceKey] = useState(); - const [lastSelectionIndex, setLastSelectionIndex] = useState(0); - const [secretPath, setSecretPath] = useState( - propSecretPath || currentWorkspace?.environments?.[0].slug! - ); - const [environment, setEnvironment] = useState( - propEnvironment || currentWorkspace?.environments?.[0].slug! - ); + const [lastCaretPos, setLastCaretPos] = useState(0); + const [secretPath, setSecretPath] = useState(propSecretPath || "/"); + const [environment, setEnvironment] = useState(propEnvironment); const workspaceId = currentWorkspace?.id || ""; const { data: decryptFileKey } = useGetUserWsKey(workspaceId); const { data: secrets } = useGetProjectSecrets({ decryptFileKey: decryptFileKey!, - environment, + environment: environment || currentWorkspace?.environments?.[0].slug!, secretPath, workspaceId }); const { folderNames: folders } = useGetFoldersByEnv({ path: secretPath, - environments: [environment], + environments: [environment || currentWorkspace?.environments?.[0].slug!], projectId: workspaceId }); @@ -131,34 +132,28 @@ export const SecretInput = forwardRef( } const isNested = referenceKey.includes("."); - const currentListReference: ReferenceType[] = []; if (isNested) { const [envSlug, ...folderPaths] = referenceKey.split("."); - currentEnvironment = envSlug; + const isValidEnvSlug = currentWorkspace?.environments.find((e) => e.slug === envSlug); + currentEnvironment = isValidEnvSlug ? envSlug : undefined; currentSecretPath = `/${folderPaths?.join("/")}` || "/"; } - if ( - !currentEnvironment || - !decryptFileKey || - !currentSecretPath || - !currentWorkspace || - !referenceKey - ) { - // this need to clean up? - setListReference(currentListReference); - return; - } setSecretPath(currentSecretPath); setEnvironment(currentEnvironment); - setShowReferencePopup(true); }, [referenceKey]); useEffect(() => { const currentListReference: ReferenceType[] = []; const isNested = referenceKey?.includes("."); + if (!environment) { + setListReference(currentListReference); + setShowReferencePopup(true); + return; + } + if (isNested) { folders?.forEach((folder) => { currentListReference.unshift({ name: folder, type: "folder" }); @@ -170,15 +165,15 @@ export const SecretInput = forwardRef( }); setListReference(currentListReference); - }, [secrets, referenceKey]); + setShowReferencePopup(true); + }, [secrets, environment, referenceKey]); function findMatch(str: string, start: number) { const matches = [...str.matchAll(REGEX_REFERENCE)]; for (let i = 0; i < matches.length; i += 1) { const match = matches[i]; if ( - match && - typeof match.index !== "undefined" && + typeof match?.index !== "undefined" && match.index <= start && start < match.index + match[0].length ) { @@ -202,7 +197,7 @@ export const SecretInput = forwardRef( function referencePopup(text: string, pos: number) { const match = findMatch(text, pos); if (match && typeof match.index !== "undefined") { - setLastSelectionIndex(pos); + setLastCaretPos(pos); setReferenceKey(match?.[2]); } setShowReferencePopup(!!match); @@ -275,7 +270,7 @@ export const SecretInput = forwardRef( } let newValue = value || ""; - const match = findMatch(newValue, lastSelectionIndex); + const match = findMatch(newValue, lastCaretPos); const referenceStartIndex = match?.index || 0; const referenceEndIndex = referenceStartIndex + (match?.[0]?.length || 0); const [start, oldReference, end] = [