import React, { SyntheticEvent, useRef } from 'react'; import { faCircle } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import guidGenerator from '../utilities/randomId'; const REGEX = /([$]{.*?})/g; interface DashboardInputFieldProps { index: number; onChangeHandler: (value: string, index: number) => void; value: string; type: 'varName' | 'value'; blurred: boolean; duplicates: string[]; } /** * This component renders the input fields on the dashboard * @param {object} obj - the order number of a keyPair * @param {number} obj.index - the order number of a keyPair * @param {function} obj.onChangeHandler - what happens when the input is modified * @param {string} obj.type - whether the input field is for a Key Name or for a Key Value * @param {string} obj.value - value of the InputField * @param {boolean} obj.blurred - whether the input field should be blurred (behind the gray dots) or not; this can be turned on/off in the dashboard * @param {string[]} obj.duplicates - list of all the duplicated key names on the dashboard * @returns */ const DashboardInputField = ({ index, onChangeHandler, type, value, blurred, duplicates }: DashboardInputFieldProps) => { const ref = useRef(null); const syncScroll = (e: SyntheticEvent) => { if (ref.current == null) return; ref.current.scrollTop = e.currentTarget.scrollTop; ref.current.scrollLeft = e.currentTarget.scrollLeft; }; if (type === 'varName') { const startsWithNumber = !isNaN(Number(value.charAt(0))) && value != ''; const hasDuplicates = duplicates?.includes(value); const error = startsWithNumber || hasDuplicates; return (
onChangeHandler(e.target.value.toUpperCase(), index) } type={type} value={value} className={`z-10 peer font-mono ph-no-capture bg-bunker-800 rounded-md caret-white text-gray-400 text-md px-2 py-1.5 w-full min-w-16 outline-none focus:ring-2 ${ error ? 'focus:ring-red/50' : 'focus:ring-primary/50' } duration-200`} spellCheck="false" />
{startsWithNumber && (

Should not start with a number

)} {hasDuplicates && !startsWithNumber && (

Secret names should be unique

)}
); } else if (type === 'value') { return (
onChangeHandler(e.target.value, index)} onScroll={syncScroll} className={`${ blurred ? 'text-transparent group-hover:text-transparent focus:text-transparent active:text-transparent' : '' } z-10 peer font-mono ph-no-capture bg-transparent rounded-md caret-white text-transparent text-md px-2 py-1.5 w-full min-w-16 outline-none focus:ring-2 focus:ring-primary/50 duration-200 no-scrollbar no-scrollbar::-webkit-scrollbar`} spellCheck="false" />
{value.split(REGEX).map((word, id) => { if (word.match(REGEX) !== null) { return ( {word.slice(0, 2)} {word.slice(2, word.length - 1)} {word.slice(word.length - 1, word.length) == '}' ? ( {word.slice(word.length - 1, word.length)} ) : ( {word.slice(word.length - 1, word.length)} )} ); } else { return ( {word} ); } })}
{blurred && (
{value.split('').map(() => ( ))}
)}
); } return <>Something Wrong; }; export default React.memo(DashboardInputField);