11import block from 'bem-cn-lite' ;
2- import { useCallback , useEffect , useMemo , useState } from 'react' ;
2+ import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
33import { useDispatch } from 'react-redux' ;
44import { escapeRegExp } from 'lodash/fp' ;
55
@@ -17,6 +17,7 @@ import {
1717 getConsumer ,
1818 selectPreparedPartitionsData ,
1919 setDataWasNotLoaded ,
20+ setSelectedConsumer ,
2021} from '../../../../store/reducers/consumer' ;
2122
2223import { TableSkeleton } from '../../../../components/TableSkeleton/TableSkeleton' ;
@@ -52,14 +53,17 @@ export const Partitions = ({path, type, nodes, consumers}: PartitionsProps) => {
5253
5354 const dispatch = useDispatch ( ) ;
5455
55- const [ selectedConsumer , setSelectedConsumer ] = useState < string [ ] > ( ) ;
56+ const isFirstRenderRef = useRef ( true ) ;
57+
5658 const [ generalSearchValue , setGeneralSearchValue ] = useState ( '' ) ;
5759 const [ partitionIdSearchValue , setPartitionIdSearchValue ] = useState ( '' ) ;
5860
5961 const [ componentCurrentPath , setComponentCurrentPath ] = useState ( path ) ;
6062
6163 const { autorefresh} = useTypedSelector ( ( state ) => state . schema ) ;
62- const { loading, wasLoaded, error} = useTypedSelector ( ( state ) => state . consumer ) ;
64+ const { loading, wasLoaded, error, selectedConsumer} = useTypedSelector (
65+ ( state ) => state . consumer ,
66+ ) ;
6367
6468 const partitions = useTypedSelector ( ( state ) => selectPreparedPartitionsData ( state ) ) ;
6569
@@ -70,16 +74,24 @@ export const Partitions = ({path, type, nodes, consumers}: PartitionsProps) => {
7074 useEffect ( ( ) => {
7175 // Manual path control to ensure it updates with other values so no request with wrong params will be sent
7276 setComponentCurrentPath ( path ) ;
73- } , [ path ] ) ;
77+
78+ // Do not reset selected consumer on first effect call
79+ // To enable navigating to specific consumer
80+ if ( isFirstRenderRef . current ) {
81+ isFirstRenderRef . current = false ;
82+ } else {
83+ dispatch ( setSelectedConsumer ( undefined ) ) ;
84+ }
85+ } , [ dispatch , path ] ) ;
7486
7587 const fetchConsumerData = useCallback (
7688 ( isBackground : boolean ) => {
7789 if ( ! isBackground ) {
7890 dispatch ( setDataWasNotLoaded ( ) ) ;
7991 }
8092
81- if ( selectedConsumer && selectedConsumer . length ) {
82- dispatch ( getConsumer ( componentCurrentPath , selectedConsumer [ 0 ] ) ) ;
93+ if ( selectedConsumer ) {
94+ dispatch ( getConsumer ( componentCurrentPath , selectedConsumer ) ) ;
8395 }
8496 } ,
8597 [ dispatch , selectedConsumer , componentCurrentPath ] ,
@@ -99,12 +111,10 @@ export const Partitions = ({path, type, nodes, consumers}: PartitionsProps) => {
99111 ) ;
100112
101113 useEffect ( ( ) => {
102- if ( consumersToSelect && consumersToSelect . length ) {
103- setSelectedConsumer ( [ consumersToSelect [ 0 ] . value ] ) ;
104- } else {
105- setSelectedConsumer ( undefined ) ;
114+ if ( consumersToSelect && consumersToSelect . length && ! selectedConsumer ) {
115+ dispatch ( setSelectedConsumer ( consumersToSelect [ 0 ] . value ) ) ;
106116 }
107- } , [ consumersToSelect ] ) ;
117+ } , [ dispatch , consumersToSelect , selectedConsumer ] ) ;
108118
109119 const selectedColumns : string [ ] = useMemo (
110120 ( ) =>
@@ -183,7 +193,7 @@ export const Partitions = ({path, type, nodes, consumers}: PartitionsProps) => {
183193 } ;
184194
185195 const handleConsumerSelectChange = ( value : string [ ] ) => {
186- setSelectedConsumer ( value ) ;
196+ dispatch ( setSelectedConsumer ( value [ 0 ] ) ) ;
187197 } ;
188198
189199 const handlePartitionIdSearchChange = ( value : string ) => {
@@ -210,7 +220,7 @@ export const Partitions = ({path, type, nodes, consumers}: PartitionsProps) => {
210220 placeholder = { i18n ( 'controls.consumerSelector.placeholder' ) }
211221 label = { i18n ( 'controls.consumerSelector' ) }
212222 options = { consumersToSelect }
213- value = { selectedConsumer }
223+ value = { [ selectedConsumer || '' ] }
214224 onUpdate = { handleConsumerSelectChange }
215225 />
216226 < Search
0 commit comments