@@ -24,13 +24,20 @@ import {
2424 KBarSearch ,
2525 useMatches ,
2626 useRegisterActions ,
27+ useKBar ,
28+ KBarState ,
2729} from "kbar" ;
2830import { Action } from "kbar/lib/types" ;
2931import { Theme } from "@mui/material/styles" ;
3032import makeStyles from "@mui/styles/makeStyles" ;
3133import { routesAsKbarActions } from "./kbar-actions" ;
3234import { Box } from "@mui/material" ;
3335import { MenuExpandedIcon } from "../../icons/SidebarMenus" ;
36+ import { AppState } from "../../store" ;
37+ import { connect } from "react-redux" ;
38+ import useApi from "./Common/Hooks/useApi" ;
39+ import { useCallback , useEffect , useState } from "react" ;
40+ import { Bucket , BucketList } from "./Buckets/types" ;
3441
3542const useStyles = makeStyles ( ( theme : Theme ) => ( {
3643 resultItem : {
@@ -81,19 +88,81 @@ const groupNameStyle = {
8188 borderBottom : "1px solid #eaeaea" ,
8289} ;
8390
91+ const KBarStateChangeMonitor = ( {
92+ onShow,
93+ onHide,
94+ } : {
95+ onShow ?: ( ) => void ;
96+ onHide ?: ( ) => void ;
97+ } ) => {
98+ const [ isOpen , setIsOpen ] = useState ( false ) ;
99+ const { visualState } = useKBar ( ( state : KBarState ) => {
100+ return {
101+ visualState : state . visualState ,
102+ } ;
103+ } ) ;
104+
105+ useEffect ( ( ) => {
106+ if ( visualState === "showing" ) {
107+ setIsOpen ( true ) ;
108+ } else {
109+ setIsOpen ( false ) ;
110+ }
111+ // eslint-disable-next-line react-hooks/exhaustive-deps
112+ } , [ visualState ] ) ;
113+
114+ useEffect ( ( ) => {
115+ if ( isOpen ) {
116+ onShow ?.( ) ;
117+ } else {
118+ onHide ?.( ) ;
119+ }
120+ // eslint-disable-next-line react-hooks/exhaustive-deps
121+ } , [ isOpen ] ) ;
122+
123+ //just to hook into the internal state of KBar. !
124+ return null ;
125+ } ;
126+
84127const CommandBar = ( {
85- features,
86- operatorMode,
128+ features = [ ] ,
129+ operatorMode = false ,
87130} : {
88- operatorMode : boolean ;
89- features : string [ ] | null ;
131+ operatorMode ? : boolean ;
132+ features ? : string [ ] | null ;
90133} ) => {
91- const initialActions : Action [ ] = routesAsKbarActions ( features , operatorMode ) ;
134+ const [ buckets , setBuckets ] = useState < Bucket [ ] > ( [ ] ) ;
92135
93- useRegisterActions ( initialActions , [ operatorMode ] ) ;
136+ const [ , invokeListBucketsApi ] = useApi (
137+ ( res : BucketList ) => {
138+ setBuckets ( res . buckets ) ;
139+ } ,
140+ ( ) => { }
141+ ) ;
142+
143+ const fetchBuckets = useCallback ( ( ) => {
144+ invokeListBucketsApi ( "GET" , `/api/v1/buckets` ) ;
145+ // eslint-disable-next-line react-hooks/exhaustive-deps
146+ } , [ ] ) ;
147+
148+ const initialActions : Action [ ] = routesAsKbarActions (
149+ features ,
150+ operatorMode ,
151+ buckets
152+ ) ;
153+
154+ useRegisterActions ( initialActions , [ operatorMode , buckets , features ] ) ;
155+
156+ //fetch buckets everytime the kbar is shown so that new buckets created elsewhere , within first page is also shown
94157
95158 return (
96159 < KBarPortal >
160+ < KBarStateChangeMonitor
161+ onShow = { fetchBuckets }
162+ onHide = { ( ) => {
163+ setBuckets ( [ ] ) ;
164+ } }
165+ />
97166 < KBarPositioner
98167 style = { {
99168 zIndex : 9999 ,
@@ -250,4 +319,11 @@ const ResultItem = React.forwardRef(
250319 }
251320) ;
252321
253- export default CommandBar ;
322+ const mapState = ( state : AppState ) => ( {
323+ operatorMode : state . system . operatorMode ,
324+ features : state . console . session . features ,
325+ } ) ;
326+
327+ const connector = connect ( mapState , null ) ;
328+
329+ export default connector ( CommandBar ) ;
0 commit comments