@@ -3,7 +3,7 @@ import { Button } from "@/components/ui/button";
33import { Checkbox } from "@/components/ui/checkbox" ;
44import { Input } from "@/components/ui/input" ;
55import { Trash2 } from "lucide-react" ;
6- import { Fragment , useEffect , useState } from "react" ;
6+ import { Fragment } from "react" ;
77
88type EnvVars = { name : string ; value : string | null ; isSensitive : boolean } [ ] ;
99
@@ -18,29 +18,7 @@ export const EnvVarGrid = ({
1818 fixedSensitiveVars : Record < string , number > ;
1919 disabled : boolean ;
2020} ) => {
21- const [ error , setError ] = useState ( "" ) ;
22- useEffect ( ( ) => {
23- const names = new Set < string > ( ) ;
24- const duplicates = new Set < string > ( ) ;
25-
26- envVars . forEach ( ( env ) => {
27- if ( env . name === "" ) return ;
28-
29- if ( names . has ( env . name ) ) {
30- duplicates . add ( env . name ) ;
31- } else {
32- names . add ( env . name ) ;
33- }
34- } ) ;
35-
36- if ( duplicates . size !== 0 ) {
37- setError (
38- `Duplicate environment variable(s): ${ [ ...duplicates . values ( ) ] . join ( ", " ) } ` ,
39- ) ;
40- } else {
41- setError ( "" ) ;
42- }
43- } , [ envVars , setEnvironmentVariables ] ) ;
21+ const error = getEnvError ( envVars ) ;
4422
4523 return (
4624 < div className = "grid grid-cols-[1fr_min-content_1fr_min-content_min-content] items-center gap-2" >
@@ -165,3 +143,28 @@ export const getCorrectEnvBlanks = (envVars: EnvVars): EnvVars => {
165143 }
166144 return cleanedVars ;
167145} ;
146+
147+ const getDuplicates = ( values : string [ ] ) => {
148+ const unique = new Set < string > ( ) ;
149+ const duplicates = new Set < string > ( ) ;
150+
151+ values . forEach ( ( value ) => {
152+ if ( unique . has ( value ) ) {
153+ duplicates . add ( value ) ;
154+ } else {
155+ unique . add ( value ) ;
156+ }
157+ } ) ;
158+
159+ return duplicates ;
160+ } ;
161+
162+ export const getEnvError = ( env : EnvVars ) => {
163+ const duplicates = getDuplicates ( env . map ( ( ev ) => ev . name ) . filter ( Boolean ) ) ;
164+
165+ if ( duplicates . size !== 0 ) {
166+ return `Duplicate environment variable(s): ${ [ ...duplicates . values ( ) ] . join ( ", " ) } ` ;
167+ }
168+
169+ return "" ;
170+ } ;
0 commit comments