adjustment: added click handler to suggestions and finalized env icon

This commit is contained in:
Sheen Capadngan
2024-04-25 21:43:25 +08:00
parent 4bae65cc55
commit 20b1cdf909
@@ -1,5 +1,5 @@
import { TextareaHTMLAttributes, useEffect, useRef, useState } from "react"; import { TextareaHTMLAttributes, useEffect, useRef, useState } from "react";
import { faFolder, faKey } from "@fortawesome/free-solid-svg-icons"; import { faFolder, faFolderOpen, 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 { twMerge } from "tailwind-merge";
@@ -109,6 +109,11 @@ export const InfisicalSecretInput = ({
const currentListReference: ReferenceItem[] = []; const currentListReference: ReferenceItem[] = [];
const isNested = currentReference?.includes("."); const isNested = currentReference?.includes(".");
if (!currentReference) {
setListReference(currentListReference);
return;
}
if (!environment) { if (!environment) {
currentWorkspace?.environments.forEach((env) => { currentWorkspace?.environments.forEach((env) => {
currentListReference.unshift({ currentListReference.unshift({
@@ -116,18 +121,11 @@ export const InfisicalSecretInput = ({
type: ReferenceType.ENVIRONMENT type: ReferenceType.ENVIRONMENT
}); });
}); });
} } else if (isNested) {
if (!environment || !currentReference) {
setListReference(currentListReference);
return;
}
if (isNested) {
folders?.forEach((folder) => { folders?.forEach((folder) => {
currentListReference.unshift({ name: folder, type: ReferenceType.FOLDER }); currentListReference.unshift({ name: folder, type: ReferenceType.FOLDER });
}); });
} else { } else if (environment) {
currentWorkspace?.environments.forEach((env) => { currentWorkspace?.environments.forEach((env) => {
currentListReference.unshift({ currentListReference.unshift({
name: env.slug, name: env.slug,
@@ -182,8 +180,9 @@ export const InfisicalSecretInput = ({
} }
}; };
const handleSuggestionSelect = () => { const handleSuggestionSelect = (selectedIndex?: number) => {
const selectedSuggestion = listReference[highlightedIndex]; const selectedSuggestion = listReference[selectedIndex ?? highlightedIndex];
// update current reference based on selection // update current reference based on selection
const indexIter = getIndexOfUnclosedRefToTheLeft(currentCursorPosition); const indexIter = getIndexOfUnclosedRefToTheLeft(currentCursorPosition);
if (indexIter === -1) { if (indexIter === -1) {
@@ -290,26 +289,44 @@ export const InfisicalSecretInput = ({
}} }}
> >
<div className="max-w-60 h-full w-full flex-col items-center justify-center rounded-md p-1 py-2 text-white"> <div className="max-w-60 h-full w-full flex-col items-center justify-center rounded-md p-1 py-2 text-white">
{listReference.map((item, i) => ( {listReference.map((item, i) => {
<div let entryIcon;
style={{ pointerEvents: "auto" }} if (item.type === ReferenceType.SECRET) {
className="flex items-center justify-between border-b border-mineshaft-600 px-2 text-left last:border-b-0" entryIcon = faKey;
key={`secret-reference-secret-${i + 1}`} } else if (item.type === ReferenceType.ENVIRONMENT) {
> entryIcon = faFolderOpen;
} else {
entryIcon = faFolder;
}
return (
<div <div
className={`${ tabIndex={0}
highlightedIndex === i ? "bg-gray-600" : "" role="button"
} text-md relative mb-0.5 flex w-full cursor-pointer select-none items-center justify-between rounded-md px-2 outline-none transition-all hover:bg-mineshaft-500 data-[highlighted]:bg-mineshaft-500`} onMouseDown={(e) => {
e.preventDefault();
setHighlightedIndex(i);
handleSuggestionSelect(i);
}}
style={{ pointerEvents: "auto" }}
className="flex items-center justify-between border-b border-mineshaft-600 px-2 text-left last:border-b-0"
key={`secret-reference-secret-${i + 1}`}
> >
<div className="flex gap-2"> <div
<div className="flex items-center text-yellow-700"> className={`${
<FontAwesomeIcon icon={item.type === "secret" ? faKey : faFolder} /> 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 outline-none transition-all hover:bg-mineshaft-500 data-[highlighted]:bg-mineshaft-500`}
>
<div className="flex gap-2">
<div className="flex items-center text-yellow-700">
<FontAwesomeIcon icon={entryIcon} />
</div>
<div className="text-md w-48 truncate text-left">{item.name}</div>
</div> </div>
<div className="text-md w-48 truncate text-left">{item.name}</div>
</div> </div>
</div> </div>
</div> );
))} })}
</div> </div>
</Popover.Content> </Popover.Content>
</Popover.Root> </Popover.Root>