1
0
mirror of https://github.com/Infisical/infisical.git synced 2025-03-29 22:02:57 +00:00

refactor(perf):go back to passing index and using it to mutate keypair

This commit is contained in:
lemmy mwaura
2022-12-11 22:25:16 +03:00
parent 864757e428
commit d456dcef28
3 changed files with 36 additions and 33 deletions
frontend
components
pages/dashboard

@ -7,8 +7,8 @@ import guidGenerator from "../utilities/randomId";
const REGEX = /([$]{.*?})/g; const REGEX = /([$]{.*?})/g;
interface DashboardInputFieldProps { interface DashboardInputFieldProps {
id: string; position: number;
onChangeHandler: (value: string, id: string) => void; onChangeHandler: (value: string, position: number) => void;
value: string; value: string;
type: "varName" | "value"; type: "varName" | "value";
blurred: boolean; blurred: boolean;
@ -18,7 +18,7 @@ interface DashboardInputFieldProps {
/** /**
* This component renders the input fields on the dashboard * This component renders the input fields on the dashboard
* @param {object} obj - the order number of a keyPair * @param {object} obj - the order number of a keyPair
* @param {string} obj.id - the order number of a keyPair * @param {number} obj.pos - the order number of a keyPair
* @param {function} obj.onChangeHandler - what happens when the input is modified * @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.type - whether the input field is for a Key Name or for a Key Value
* @param {string} obj.value - value of the InputField * @param {string} obj.value - value of the InputField
@ -28,7 +28,7 @@ interface DashboardInputFieldProps {
*/ */
const DashboardInputField = ({ const DashboardInputField = ({
id, position,
onChangeHandler, onChangeHandler,
type, type,
value, value,
@ -57,7 +57,7 @@ const DashboardInputField = ({
> >
<input <input
onChange={(e) => onChange={(e) =>
onChangeHandler(e.target.value.toUpperCase(), id) onChangeHandler(e.target.value.toUpperCase(), position)
} }
type={type} type={type}
value={value} value={value}
@ -87,7 +87,7 @@ const DashboardInputField = ({
> >
<input <input
value={value} value={value}
onChange={(e) => onChangeHandler(e.target.value, id)} onChange={(e) => onChangeHandler(e.target.value, position)}
onScroll={syncScroll} onScroll={syncScroll}
className={`${ className={`${
blurred blurred

@ -68,9 +68,10 @@ const getSecretsForProject = async ({
setFileState(tempFileState); setFileState(tempFileState);
setData( setData(
tempFileState.map((line) => { tempFileState.map((line, index) => {
return { return {
id: guidGenerator(), id: guidGenerator(),
pos: index,
key: line["key"], key: line["key"],
value: line["value"], value: line["value"],
type: line["type"] type: line["type"]

@ -73,7 +73,7 @@ const KeyPair = ({
<DashboardInputField <DashboardInputField
onChangeHandler={modifyKey} onChangeHandler={modifyKey}
type="varName" type="varName"
id={keyPair.id} position={keyPair.pos}
value={keyPair.key} value={keyPair.key}
duplicates={duplicates} duplicates={duplicates}
/> />
@ -84,7 +84,7 @@ const KeyPair = ({
<DashboardInputField <DashboardInputField
onChangeHandler={modifyValue} onChangeHandler={modifyValue}
type="value" type="value"
id={keyPair.id} position={keyPair.pos}
value={keyPair.value} value={keyPair.value}
blurred={isBlurred} blurred={isBlurred}
/> />
@ -115,7 +115,7 @@ const KeyPair = ({
onClick={() => onClick={() =>
modifyVisibility( modifyVisibility(
keyPair.type == "personal" ? "shared" : "personal", keyPair.type == "personal" ? "shared" : "personal",
keyPair.id keyPair.pos
) )
} }
className="relative flex justify-start items-center cursor-pointer select-none py-2 px-2 rounded-md text-gray-400 hover:bg-white/10 duration-200 hover:text-gray-200 w-full" className="relative flex justify-start items-center cursor-pointer select-none py-2 px-2 rounded-md text-gray-400 hover:bg-white/10 duration-200 hover:text-gray-200 w-full"
@ -139,7 +139,7 @@ const KeyPair = ({
[...Array(randomStringLength)] [...Array(randomStringLength)]
.map(() => Math.floor(Math.random() * 16).toString(16)) .map(() => Math.floor(Math.random() * 16).toString(16))
.join(""), .join(""),
keyPair.id keyPair.pos
); );
} }
}} }}
@ -319,9 +319,10 @@ export default function Dashboard() {
const addRow = () => { const addRow = () => {
setIsNew(false); setIsNew(false);
setData([...data, { setData([...data, {
id:guidGenerator(), id:guidGenerator(),
key:"", pos:data.length,
value:"", key:"",
value:"",
type:"shared" type:"shared"
}]); }]);
}; };
@ -331,41 +332,41 @@ export default function Dashboard() {
setData(data.filter((row) => row.id !== id)); setData(data.filter((row) => row.id !== id));
}; };
const modifyValue = (value, id) => { const modifyValue = (value, pos) => {
setData((oldData) => { setData((oldData) => {
oldData.find(data => data.id === id).value = value; oldData[pos].value = value;
return [...oldData]; return [...oldData];
}); });
setButtonReady(true); setButtonReady(true);
}; };
const modifyKey = (value, id) => { const modifyKey = (value, pos) => {
setData((oldData) => { setData((oldData) => {
oldData.find(data => data.id === id).key = value; oldData[pos].key = value;
return [...oldData]; return [...oldData];
}); });
setButtonReady(true); setButtonReady(true);
}; };
const modifyVisibility = (value, id) => { const modifyVisibility = (value, pos) => {
setData((oldData) => { setData((oldData) => {
oldData.find(data => data.id === id).type = value; oldData[pos].type = value;
return [...oldData]; return [...oldData];
}); });
setButtonReady(true); setButtonReady(true);
}; };
// For speed purposes and better perforamance, we are using useCallback // For speed purposes and better perforamance, we are using useCallback
const listenChangeValue = useCallback((value, id) => { const listenChangeValue = useCallback((value, pos) => {
modifyValue(value, id); modifyValue(value, pos);
}, []); }, []);
const listenChangeKey = useCallback((value, id) => { const listenChangeKey = useCallback((value, pos) => {
modifyKey(value, id); modifyKey(value, pos);
}, []); }, []);
const listenChangeVisibility = useCallback((value, id) => { const listenChangeVisibility = useCallback((value, pos) => {
modifyVisibility(value, id); modifyVisibility(value, pos);
}, []); }, []);
/** /**
@ -448,17 +449,18 @@ export default function Dashboard() {
}; };
const sortValuesHandler = () => { const sortValuesHandler = () => {
/** console.log(sortMethod)
* Since react's SetStateActionHandler optimises renders when values don't change
* we have to map and return a new sorted list to force a render.
* @returns {sorted list}
*/
let sortedData = data.sort((a, b) => const sortedData = data.sort((a, b) =>
sortMethod == "alphabetical" sortMethod == "alphabetical"
? a.key.localeCompare(b.key) ? a.key.localeCompare(b.key)
: b.key.localeCompare(a.key) : b.key.localeCompare(a.key)
).map((item) => item) ).map((item, index) => {
return {
...item, pos:index
}
})
setData(sortedData) setData(sortedData)
} }