1
1
import { MuxMatcherType , MuxRule } from "@/api/generated" ;
2
2
import { useFormState } from "@/hooks/useFormState" ;
3
- import { useCallback , useEffect } from "react" ;
3
+ import { isEqual } from "lodash" ;
4
+ import { useCallback , useEffect , useRef } from "react" ;
4
5
import { v4 as uuidv4 } from "uuid" ;
5
6
6
7
export type PreferredMuxRule = MuxRule & { id : string } ;
@@ -22,14 +23,18 @@ export const useMuxingRulesFormState = (initialValues: MuxRule[]) => {
22
23
rules : [ { ...DEFAULT_STATE , id : uuidv4 ( ) } ] ,
23
24
} ) ;
24
25
const { values, updateFormValues, setInitialValues } = formState ;
26
+ const lastValuesRef = useRef ( values . rules ) ;
25
27
26
28
useEffect ( ( ) => {
27
- setInitialValues ( {
28
- rules :
29
- initialValues . length == 0
30
- ? [ DEFAULT_STATE ]
31
- : initialValues . map ( ( item ) => ( { ...item , id : uuidv4 ( ) } ) ) ,
32
- } ) ;
29
+ const newValues =
30
+ initialValues . length === 0
31
+ ? [ DEFAULT_STATE ]
32
+ : initialValues . map ( ( item ) => ( { ...item , id : uuidv4 ( ) } ) ) ;
33
+
34
+ if ( ! isEqual ( lastValuesRef . current , newValues ) ) {
35
+ lastValuesRef . current = newValues ;
36
+ setInitialValues ( { rules : newValues } ) ;
37
+ }
33
38
} , [ initialValues , setInitialValues ] ) ;
34
39
35
40
const addRule = useCallback ( ( ) => {
0 commit comments