mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-22 07:53:34 +01:00
feat: ux improvements in environment settings
This commit is contained in:
parent
b14f867811
commit
a425b42615
@ -1,5 +1,6 @@
|
|||||||
import React, { useEffect, useState, forwardRef, useRef } from 'react';
|
import React, { useEffect, useState, forwardRef, useRef } from 'react';
|
||||||
import { findEnvironmentInCollection } from 'utils/collections';
|
import { findEnvironmentInCollection } from 'utils/collections';
|
||||||
|
import usePrevious from 'hooks/usePrevious';
|
||||||
import EnvironmentDetails from './EnvironmentDetails';
|
import EnvironmentDetails from './EnvironmentDetails';
|
||||||
import CreateEnvironment from '../CreateEnvironment/index';
|
import CreateEnvironment from '../CreateEnvironment/index';
|
||||||
import StyledWrapper from './StyledWrapper';
|
import StyledWrapper from './StyledWrapper';
|
||||||
@ -9,14 +10,36 @@ const EnvironmentList = ({ collection }) => {
|
|||||||
const [selectedEnvironment, setSelectedEnvironment] = useState(null);
|
const [selectedEnvironment, setSelectedEnvironment] = useState(null);
|
||||||
const [openCreateModal, setOpenCreateModal] = useState(false);
|
const [openCreateModal, setOpenCreateModal] = useState(false);
|
||||||
|
|
||||||
|
const envUids = environments ? environments.map((env) => env.uid) : [];
|
||||||
|
const prevEnvUids = usePrevious(envUids);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if(selectedEnvironment) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const environment = findEnvironmentInCollection(collection, collection.activeEnvironmentUid);
|
const environment = findEnvironmentInCollection(collection, collection.activeEnvironmentUid);
|
||||||
if(environment) {
|
if(environment) {
|
||||||
setSelectedEnvironment(environment);
|
setSelectedEnvironment(environment);
|
||||||
} else {
|
} else {
|
||||||
setSelectedEnvironment(environments && environments.length ? environments[0] : null);
|
setSelectedEnvironment(environments && environments.length ? environments[0] : null);
|
||||||
}
|
}
|
||||||
}, [collection, environments]);
|
}, [collection, environments, selectedEnvironment]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// check env add
|
||||||
|
if (prevEnvUids && prevEnvUids.length && envUids.length > prevEnvUids.length) {
|
||||||
|
const newEnv = environments.find((env) => !prevEnvUids.includes(env.uid));
|
||||||
|
if(newEnv){
|
||||||
|
setSelectedEnvironment(newEnv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check env delete
|
||||||
|
if (prevEnvUids && prevEnvUids.length && envUids.length < prevEnvUids.length) {
|
||||||
|
setSelectedEnvironment(environments && environments.length ? environments[0] : null);
|
||||||
|
}
|
||||||
|
}, [envUids, environments, prevEnvUids]);
|
||||||
|
|
||||||
if (!selectedEnvironment) {
|
if (!selectedEnvironment) {
|
||||||
return null;
|
return null;
|
||||||
@ -31,7 +54,11 @@ const EnvironmentList = ({ collection }) => {
|
|||||||
{environments &&
|
{environments &&
|
||||||
environments.length &&
|
environments.length &&
|
||||||
environments.map((env) => (
|
environments.map((env) => (
|
||||||
<div key={env.uid} className={selectedEnvironment.uid === env.uid ? 'environment-item active' : 'environment-item'} onClick={() => setSelectedEnvironment(env)}>
|
<div
|
||||||
|
key={env.uid}
|
||||||
|
className={selectedEnvironment.uid === env.uid ? 'environment-item active' : 'environment-item'}
|
||||||
|
onClick={() => setSelectedEnvironment(env)}
|
||||||
|
>
|
||||||
<span>{env.name}</span>
|
<span>{env.name}</span>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
13
packages/bruno-app/src/hooks/usePrevious/index.js
Normal file
13
packages/bruno-app/src/hooks/usePrevious/index.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { useRef, useEffect } from 'react';
|
||||||
|
|
||||||
|
function usePrevious(value) {
|
||||||
|
const ref = useRef();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
ref.current = value; //assign the value of ref to the argument
|
||||||
|
},[value]); //this code will run when the value of 'value' changes
|
||||||
|
|
||||||
|
return ref.current; //in the end, return the current ref value.
|
||||||
|
}
|
||||||
|
|
||||||
|
export default usePrevious;
|
Loading…
Reference in New Issue
Block a user