feat: simplified secret input with auto completion

This commit is contained in:
=
2024-05-13 13:40:59 +05:30
parent 8ef727b4ec
commit ecbdae110d
2 changed files with 304 additions and 311 deletions
@@ -1,17 +1,42 @@
import { TextareaHTMLAttributes, useEffect, useRef, useState } from "react"; import { forwardRef, TextareaHTMLAttributes, useCallback, useMemo, useRef, useState } from "react";
import { faCircle, faFolder, faKey } from "@fortawesome/free-solid-svg-icons"; import { faCircle, faFolder, faKey } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Popover from "@radix-ui/react-popover"; import * as Popover from "@radix-ui/react-popover";
import { twMerge } from "tailwind-merge";
import { useWorkspace } from "@app/context"; import { useWorkspace } from "@app/context";
import { useDebounce } from "@app/hooks"; import { useDebounce, useToggle } from "@app/hooks";
import { useGetFoldersByEnv, useGetProjectSecrets, useGetUserWsKey } from "@app/hooks/api"; import { useGetProjectFolders, useGetProjectSecrets, useGetUserWsKey } from "@app/hooks/api";
import { SecretInput } from "../SecretInput"; import { SecretInput } from "../SecretInput";
const REGEX_UNCLOSED_SECRET_REFERENCE = /\${(?![^{}]*\})/g; const getIndexOfUnclosedRefToTheLeft = (value: string, pos: number) => {
const REGEX_OPEN_SECRET_REFERENCE = /\${/g; // take substring up to pos in order to consider edits for closed references
for (let i = pos; i >= 1; i -= 1) {
if (value[i] === "}") return -1;
if (value[i - 1] === "$" && value[i] === "{") {
return i;
}
}
return -1;
};
const getIndexOfUnclosedRefToTheRight = (value: string, pos: number) => {
// use it with above to identify an open ${
for (let i = pos; i < value.length; i += 1) {
if (value[i] === "}") return i - 1;
}
return -1;
};
const getClosingSymbol = (isSelectedSecret: boolean, isClosed: boolean) => {
if (!isClosed) {
return isSelectedSecret ? "}" : ".";
}
if (!isSelectedSecret) return ".";
return "";
};
const mod = (n: number, m: number) => ((n % m) + m) % m;
export enum ReferenceType { export enum ReferenceType {
ENVIRONMENT = "environment", ENVIRONMENT = "environment",
@@ -19,8 +44,9 @@ export enum ReferenceType {
SECRET = "secret" SECRET = "secret"
} }
type Props = TextareaHTMLAttributes<HTMLTextAreaElement> & { type Props = Omit<TextareaHTMLAttributes<HTMLTextAreaElement>, "onChange" | "value"> & {
value?: string | null; value?: string;
onChange: (val: string) => void;
isImport?: boolean; isImport?: boolean;
isVisible?: boolean; isVisible?: boolean;
isReadOnly?: boolean; isReadOnly?: boolean;
@@ -31,295 +57,248 @@ type Props = TextareaHTMLAttributes<HTMLTextAreaElement> & {
}; };
type ReferenceItem = { type ReferenceItem = {
name: string; label: string;
type: ReferenceType; type: ReferenceType;
slug?: string; slug: string;
}; };
export const InfisicalSecretInput = ({ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
value: propValue, (
{
value = "",
onChange,
containerClassName, containerClassName,
secretPath: propSecretPath, secretPath: propSecretPath,
environment: propEnvironment, environment: propEnvironment,
onChange,
...props ...props
}: Props) => { },
const [inputValue, setInputValue] = useState(propValue ?? ""); ref
const [isSuggestionsOpen, setIsSuggestionsOpen] = useState(false); ) => {
const [currentCursorPosition, setCurrentCursorPosition] = useState(0);
const [currentReference, setCurrentReference] = useState<string>("");
const [secretPath, setSecretPath] = useState<string>(propSecretPath || "/");
const [environment, setEnvironment] = useState<string | undefined>(propEnvironment);
const { currentWorkspace } = useWorkspace(); const { currentWorkspace } = useWorkspace();
const workspaceId = currentWorkspace?.id || ""; const workspaceId = currentWorkspace?.id || "";
const { data: decryptFileKey } = useGetUserWsKey(workspaceId); const { data: decryptFileKey } = useGetUserWsKey(workspaceId);
const debouncedValue = useDebounce(value, 500);
const [highlightedIndex, setHighlightedIndex] = useState(-1);
const inputRef = useRef<HTMLTextAreaElement>(null);
const popoverContentRef = useRef<HTMLDivElement>(null);
const [isFocused, setIsFocused] = useToggle(false);
const currentCursorPosition = inputRef.current?.selectionStart || 0;
const suggestionSource = useMemo(() => {
const left = getIndexOfUnclosedRefToTheLeft(debouncedValue, currentCursorPosition - 1);
if (left === -1) return { left, value: "", predicate: "", isDeep: false };
const suggestionSourceValue = debouncedValue.slice(left + 1, currentCursorPosition);
let suggestionSourceEnv: string | undefined = propEnvironment;
let suggestionSourceSecretPath: string | undefined = propSecretPath || "/";
// means its like <environment>.<folder1>.<...more folder>.secret
const isDeep = suggestionSourceValue.includes(".");
let predicate = suggestionSourceValue;
if (isDeep) {
const [envSlug, ...folderPaths] = suggestionSourceValue.split(".");
const isValidEnvSlug = currentWorkspace?.environments.find((e) => e.slug === envSlug);
suggestionSourceEnv = isValidEnvSlug ? envSlug : undefined;
suggestionSourceSecretPath = `/${folderPaths.slice(0, -1)?.join("/")}`;
predicate = folderPaths[folderPaths.length - 1];
}
return {
left: left + 1,
// the full value inside a ${<value>}
value: suggestionSourceValue,
// the final part after staging.dev.<folder1>.<predicate>
predicate,
isOpen: left !== -1,
isDeep,
environment: suggestionSourceEnv,
secretPath: suggestionSourceSecretPath
};
}, [debouncedValue]);
const isPopupOpen = Boolean(suggestionSource.isOpen) && isFocused;
const { data: secrets } = useGetProjectSecrets({ const { data: secrets } = useGetProjectSecrets({
decryptFileKey: decryptFileKey!, decryptFileKey: decryptFileKey!,
environment: environment || currentWorkspace?.environments?.[0].slug!, environment: suggestionSource.environment || "",
secretPath, secretPath: suggestionSource.secretPath || "",
workspaceId workspaceId,
options: {
enabled: isPopupOpen
}
}); });
const { folderNames: folders } = useGetFoldersByEnv({ const { data: folders } = useGetProjectFolders({
path: secretPath, environment: suggestionSource.environment || "",
environments: [environment || currentWorkspace?.environments?.[0].slug!], path: suggestionSource.secretPath || "",
projectId: workspaceId projectId: workspaceId,
options: {
enabled: isPopupOpen
}
}); });
const debouncedCurrentReference = useDebounce(currentReference, 100); const suggestions = useMemo(() => {
if (!isPopupOpen) return [];
// reset highlight whenever recomputation happens
setHighlightedIndex(-1);
const suggestionsArr: ReferenceItem[] = [];
const predicate = suggestionSource.predicate.toLowerCase();
const [listReference, setListReference] = useState<ReferenceItem[]>([]); if (!suggestionSource.isDeep) {
const [highlightedIndex, setHighlightedIndex] = useState(-1); // At first level only environments and secrets
const inputRef = useRef<HTMLTextAreaElement>(null); (currentWorkspace?.environments || []).forEach(({ name, slug }) => {
const isPopupOpen = isSuggestionsOpen && listReference.length > 0 && currentReference.length > 0; if (name.toLowerCase().startsWith(predicate))
suggestionsArr.push({
useEffect(() => { label: name,
setInputValue(propValue ?? ""); slug,
}, [propValue]);
useEffect(() => {
let currentEnvironment = propEnvironment;
let currentSecretPath = propSecretPath || "/";
if (!currentReference) {
setSecretPath(currentSecretPath);
setEnvironment(currentEnvironment);
return;
}
const isNested = currentReference.includes(".");
if (isNested) {
const [envSlug, ...folderPaths] = currentReference.split(".");
const isValidEnvSlug = currentWorkspace?.environments.find((e) => e.slug === envSlug);
currentEnvironment = isValidEnvSlug ? envSlug : undefined;
// should be based on the last valid section (with .)
folderPaths.pop();
currentSecretPath = `/${folderPaths?.join("/")}`;
}
setSecretPath(currentSecretPath);
setEnvironment(currentEnvironment);
}, [debouncedCurrentReference]);
useEffect(() => {
const currentListReference: ReferenceItem[] = [];
const isNested = currentReference?.includes(".");
if (!currentReference) {
setListReference(currentListReference);
return;
}
if (!environment) {
currentWorkspace?.environments.forEach((env) => {
currentListReference.unshift({
name: env.slug,
type: ReferenceType.ENVIRONMENT type: ReferenceType.ENVIRONMENT
}); });
}); });
} else if (isNested) { } else {
folders?.forEach((folder) => { // one deeper levels its based on an environment folders and secrets
currentListReference.unshift({ name: folder, type: ReferenceType.FOLDER }); (folders || []).forEach(({ name }) => {
}); if (name.toLowerCase().startsWith(predicate))
} else if (environment) { suggestionsArr.push({
currentWorkspace?.environments.forEach((env) => { label: name,
currentListReference.unshift({ slug: name,
name: env.slug, type: ReferenceType.FOLDER
type: ReferenceType.ENVIRONMENT
}); });
}); });
} }
(secrets || []).forEach(({ key }) => {
secrets?.forEach((secret) => { if (key.toLowerCase().startsWith(predicate))
currentListReference.unshift({ name: secret.key, type: ReferenceType.SECRET }); suggestionsArr.push({
label: key,
slug: key,
type: ReferenceType.SECRET
}); });
// Get fragment inside currentReference
const searchFragment = isNested ? currentReference.split(".").pop() || "" : currentReference;
const filteredListRef = currentListReference
.filter((suggestionEntry) =>
suggestionEntry.name.toUpperCase().startsWith(searchFragment.toUpperCase())
)
.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
setListReference(filteredListRef);
}, [secrets, environment, debouncedCurrentReference]);
const getIndexOfUnclosedRefToTheLeft = (pos: number) => {
// take substring up to pos in order to consider edits for closed references
const unclosedReferenceIndexMatches = [
...inputValue.substring(0, pos).matchAll(REGEX_UNCLOSED_SECRET_REFERENCE)
].map((match) => match.index);
// find unclosed reference index less than the current cursor position
let indexIter = -1;
unclosedReferenceIndexMatches.forEach((index) => {
if (index !== undefined && index > indexIter && index < pos) {
indexIter = index;
}
}); });
return suggestionsArr;
}, [secrets, folders, currentWorkspace?.environments, isPopupOpen, suggestionSource.value]);
return indexIter; const handleSuggestionSelect = (selectIndex?: number) => {
}; const selectedSuggestion =
suggestions[typeof selectIndex !== "undefined" ? selectIndex : highlightedIndex];
const getIndexOfUnclosedRefToTheRight = (pos: number) => {
const unclosedReferenceIndexMatches = [...inputValue.matchAll(REGEX_OPEN_SECRET_REFERENCE)].map(
(match) => match.index
);
// find the next unclosed reference index to the right of the current cursor position
// this is so that we know the limitation for slicing references
let indexIter = Infinity;
unclosedReferenceIndexMatches.forEach((index) => {
if (index !== undefined && index > pos && index < indexIter) {
indexIter = index;
}
});
return indexIter;
};
const handleKeyUp = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
// open suggestions if current position is to the right of an unclosed secret reference
const indexIter = getIndexOfUnclosedRefToTheLeft(currentCursorPosition);
if (indexIter === -1) {
return;
}
setIsSuggestionsOpen(true);
if (e.key !== "Enter") {
// current reference is then going to be based on the text from the closest ${ to the right
// until the current cursor position
const openReferenceValue = inputValue.slice(indexIter + 2, currentCursorPosition);
setCurrentReference(openReferenceValue);
}
};
const handleSuggestionSelect = (selectedIndex?: number) => {
const selectedSuggestion = listReference[selectedIndex ?? highlightedIndex];
if (!selectedSuggestion) { if (!selectedSuggestion) {
return; return;
} }
const leftIndexIter = getIndexOfUnclosedRefToTheLeft(currentCursorPosition); const rightBracketIndex = getIndexOfUnclosedRefToTheRight(value, suggestionSource.left);
const rightIndexLimit = getIndexOfUnclosedRefToTheRight(currentCursorPosition); const isEnclosed = rightBracketIndex !== -1;
// <lhsValue>${}<rhsvalue>
if (leftIndexIter === -1) { const lhsValue = value.slice(0, suggestionSource.left);
return; const rhsValue = value.slice(
} rightBracketIndex !== -1 ? rightBracketIndex + 1 : currentCursorPosition
);
let newValue = ""; // mid will be computed value inside the interpolation
const currentOpenRef = inputValue.slice(leftIndexIter + 2, currentCursorPosition); const mid = suggestionSource.isDeep
if (currentOpenRef.includes(".")) { ? `${suggestionSource.value.slice(0, -suggestionSource.predicate.length || undefined)}${selectedSuggestion.slug
// append suggestion after last DOT (.) }`
const lastDotIndex = currentReference.lastIndexOf("."); : selectedSuggestion.slug;
const existingPath = currentReference.slice(0, lastDotIndex); // whether we should append . or closing bracket on selecting suggestion
const refEndAfterAppending = Math.min( const closingSymbol = getClosingSymbol(
leftIndexIter + selectedSuggestion.type === ReferenceType.SECRET,
3 + isEnclosed
existingPath.length +
selectedSuggestion.name.length +
Number(selectedSuggestion.type !== ReferenceType.SECRET),
rightIndexLimit - 1
); );
newValue = `${inputValue.slice(0, leftIndexIter + 2)}${existingPath}.${ const newValue = `${lhsValue}${mid}${closingSymbol}${rhsValue}`;
selectedSuggestion.name onChange?.(newValue);
}${selectedSuggestion.type !== ReferenceType.SECRET ? "." : "}"}${inputValue.slice( // this delay is for cursor adjustment
refEndAfterAppending // cannot do this without a delay because what happens in onChange gets propogated after the cursor change
)}`; // Thus the cursor goes last to avoid that we put a slight delay on cursor change to make it happen later
const openReferenceValue = newValue.slice(leftIndexIter + 2, refEndAfterAppending); const delay = setTimeout(() => {
setCurrentReference(openReferenceValue); clearTimeout(delay);
if (inputRef.current)
// add 1 in order to prevent referenceOpen from being triggered by handleKeyUp inputRef.current.selectionEnd =
setCurrentCursorPosition(refEndAfterAppending + 1); lhsValue.length +
} else { mid.length +
// append selectedSuggestion at position after unclosed ${ closingSymbol.length +
const refEndAfterAppending = Math.min( (isEnclosed && selectedSuggestion.type === ReferenceType.SECRET ? 1 : 0); // if secret is selected the cursor should move after the closing bracket -> }
selectedSuggestion.name.length + }, 10);
leftIndexIter + setHighlightedIndex(-1); // reset highlight
2 +
Number(selectedSuggestion.type !== ReferenceType.SECRET),
rightIndexLimit - 1
);
newValue = `${inputValue.slice(0, leftIndexIter + 2)}${selectedSuggestion.name}${
selectedSuggestion.type !== ReferenceType.SECRET ? "." : "}"
}${inputValue.slice(refEndAfterAppending)}`;
const openReferenceValue = newValue.slice(leftIndexIter + 2, refEndAfterAppending);
setCurrentReference(openReferenceValue);
setCurrentCursorPosition(refEndAfterAppending);
}
onChange?.({ target: { value: newValue } } as any);
setInputValue(newValue);
setHighlightedIndex(-1);
setIsSuggestionsOpen(false);
}; };
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => { const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
const mod = (n: number, m: number) => ((n % m) + m) % m; // key operation should trigger only when popup is open
if (e.key === "ArrowDown") { if (isPopupOpen) {
setHighlightedIndex((prevIndex) => mod(prevIndex + 1, listReference.length)); if (e.key === "ArrowDown" || (e.key === "Tab" && !e.shiftKey)) {
} else if (e.key === "ArrowUp") { setHighlightedIndex((prevIndex) => {
setHighlightedIndex((prevIndex) => mod(prevIndex - 1, listReference.length)); const pos = mod(prevIndex + 1, suggestions.length);
popoverContentRef.current?.children?.[pos]?.scrollIntoView({
block: "nearest",
behavior: "smooth"
});
return pos;
});
} else if (e.key === "ArrowUp" || (e.key === "Tab" && e.shiftKey)) {
setHighlightedIndex((prevIndex) => {
const pos = mod(prevIndex - 1, suggestions.length);
popoverContentRef.current?.children?.[pos]?.scrollIntoView({
block: "nearest",
behavior: "smooth"
});
return pos;
});
} else if (e.key === "Enter" && highlightedIndex >= 0) { } else if (e.key === "Enter" && highlightedIndex >= 0) {
e.preventDefault();
handleSuggestionSelect(); handleSuggestionSelect();
} }
if (["ArrowDown", "ArrowUp", "Tab"].includes(e.key)) {
if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key) && isPopupOpen) {
e.preventDefault(); e.preventDefault();
} }
}
}; };
const setIsOpen = (isOpen: boolean) => { const handlePopUpOpen = () => {
setHighlightedIndex(-1); setHighlightedIndex(-1);
if (isSuggestionsOpen) {
setIsSuggestionsOpen(isOpen);
}
}; };
const handleSecretChange = (e: any) => { // to handle multiple ref for single component
// propagate event to react-hook-form onChange const handleRef = useCallback((el: HTMLTextAreaElement) => {
if (onChange) { // @ts-expect-error this is for multiple ref single component
onChange(e); inputRef.current = el;
if (ref) {
if (typeof ref === "function") {
ref(el);
} else {
// eslint-disable-next-line
ref.current = el;
} }
}
setCurrentCursorPosition(inputRef.current?.selectionStart || 0); }, []);
setInputValue(e.target.value);
};
return ( return (
<Popover.Root open={isPopupOpen} onOpenChange={setIsOpen}> <Popover.Root open={isPopupOpen} onOpenChange={handlePopUpOpen}>
<Popover.Trigger asChild> <Popover.Trigger asChild>
<SecretInput <SecretInput
{...props} {...props}
ref={inputRef} ref={handleRef}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp} value={value}
value={inputValue} onFocus={() => setIsFocused.on()}
onChange={handleSecretChange} onBlur={(evt) => {
// should not on blur when its mouse down selecting a item from suggestion
if (!(evt.relatedTarget?.getAttribute("aria-label") === "suggestion-item"))
setIsFocused.off();
}}
onChange={(e) => onChange?.(e.target.value)}
containerClassName={containerClassName} containerClassName={containerClassName}
/> />
</Popover.Trigger> </Popover.Trigger>
<Popover.Content <Popover.Content
align="start" align="start"
onOpenAutoFocus={(e) => e.preventDefault()} onOpenAutoFocus={(e) => e.preventDefault()}
className={twMerge( className="relative top-2 z-[100] max-h-64 overflow-auto rounded-md border border-mineshaft-600 bg-mineshaft-900 font-inter text-bunker-100 shadow-md"
"relative top-2 z-[100] overflow-hidden rounded-md border border-mineshaft-600 bg-mineshaft-900 font-inter text-bunker-100 shadow-md"
)}
style={{ style={{
width: "var(--radix-popover-trigger-width)", width: "var(--radix-popover-trigger-width)"
maxHeight: "var(--radix-select-content-available-height)"
}} }}
> >
<div className="max-w-60 h-full w-full flex-col items-center justify-center rounded-md text-white"> <div
{listReference.map((item, i) => { className="max-w-60 h-full w-full flex-col items-center justify-center rounded-md text-white"
ref={popoverContentRef}
>
{suggestions.map((item, i) => {
let entryIcon; let entryIcon;
if (item.type === ReferenceType.SECRET) { if (item.type === ReferenceType.SECRET) {
entryIcon = faKey; entryIcon = faKey;
@@ -333,18 +312,23 @@ export const InfisicalSecretInput = ({
<div <div
tabIndex={0} tabIndex={0}
role="button" role="button"
onMouseDown={(e) => { onKeyDown={(e) => {
if (e.key === "Enter") handleSuggestionSelect(i);
}}
aria-label="suggestion-item"
onClick={(e) => {
inputRef.current?.focus();
e.preventDefault(); e.preventDefault();
setHighlightedIndex(i); e.stopPropagation();
handleSuggestionSelect(i); handleSuggestionSelect(i);
}} }}
onMouseEnter={() => setHighlightedIndex(i)}
style={{ pointerEvents: "auto" }} style={{ pointerEvents: "auto" }}
className="flex items-center justify-between border-mineshaft-600 text-left" className="flex items-center justify-between border-mineshaft-600 text-left"
key={`secret-reference-secret-${i + 1}`} key={`secret-reference-secret-${i + 1}`}
> >
<div <div
className={`${ className={`${highlightedIndex === i ? "bg-gray-600" : ""
highlightedIndex === i ? "bg-gray-600" : ""
} text-md relative mb-0.5 flex w-full cursor-pointer select-none items-center justify-between rounded-md px-2 py-2 outline-none transition-all hover:bg-mineshaft-500 data-[highlighted]:bg-mineshaft-500`} } text-md relative mb-0.5 flex w-full cursor-pointer select-none items-center justify-between rounded-md px-2 py-2 outline-none transition-all hover:bg-mineshaft-500 data-[highlighted]:bg-mineshaft-500`}
> >
<div className="flex w-full gap-2"> <div className="flex w-full gap-2">
@@ -354,7 +338,7 @@ export const InfisicalSecretInput = ({
size={item.type === ReferenceType.ENVIRONMENT ? "xs" : "1x"} size={item.type === ReferenceType.ENVIRONMENT ? "xs" : "1x"}
/> />
</div> </div>
<div className="text-md w-10/12 truncate text-left">{item.name}</div> <div className="text-md w-10/12 truncate text-left">{item.label}</div>
</div> </div>
</div> </div>
</div> </div>
@@ -364,6 +348,7 @@ export const InfisicalSecretInput = ({
</Popover.Content> </Popover.Content>
</Popover.Root> </Popover.Root>
); );
}; }
);
InfisicalSecretInput.displayName = "InfisicalSecretInput"; InfisicalSecretInput.displayName = "InfisicalSecretInput";
+8
View File
@@ -45,6 +45,14 @@ html {
width: 1%; width: 1%;
white-space: nowrap; white-space: nowrap;
} }
.w-inherit {
width: inherit;
}
.h-inherit {
height: inherit;
}
} }
@layer components { @layer components {