Skip to content

Commit 09b0ea9

Browse files
authored
KBar buckets search (#1980)
1 parent f409049 commit 09b0ea9

File tree

3 files changed

+102
-17
lines changed

3 files changed

+102
-17
lines changed

portal-ui/src/screens/Console/CommandBar.tsx

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@ import {
2424
KBarSearch,
2525
useMatches,
2626
useRegisterActions,
27+
useKBar,
28+
KBarState,
2729
} from "kbar";
2830
import { Action } from "kbar/lib/types";
2931
import { Theme } from "@mui/material/styles";
3032
import makeStyles from "@mui/styles/makeStyles";
3133
import { routesAsKbarActions } from "./kbar-actions";
3234
import { Box } from "@mui/material";
3335
import { 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

3542
const 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+
84127
const 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);

portal-ui/src/screens/Console/ConsoleKBar.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ import { AppState } from "../../store";
2020
import { connect } from "react-redux";
2121
import CommandBar from "./CommandBar";
2222

23-
const ConsoleKBar = ({
24-
features,
25-
operatorMode,
26-
}: {
27-
operatorMode: boolean;
28-
features: string[] | null;
29-
}) => {
23+
const ConsoleKBar = ({ features }: { features: string[] | null }) => {
3024
// if we are hiding the menu also disable the k-bar so just return console
3125
if (features?.includes("hide-menu")) {
3226
return <Console />;
@@ -38,14 +32,13 @@ const ConsoleKBar = ({
3832
enableHistory: true,
3933
}}
4034
>
41-
<CommandBar operatorMode={operatorMode} features={features} />
35+
<CommandBar />
4236
<Console />
4337
</KBarProvider>
4438
);
4539
};
4640

4741
const mapState = (state: AppState) => ({
48-
operatorMode: state.system.operatorMode,
4942
features: state.console.session.features,
5043
});
5144

portal-ui/src/screens/Console/kbar-actions.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import { Action } from "kbar/lib/types";
1818
import history from "../../history";
1919
import { BucketsIcon } from "../../icons";
2020
import { validRoutes } from "./valid-routes";
21+
import { Bucket } from "./Buckets/types";
2122

2223
export const routesAsKbarActions = (
2324
features: string[] | null,
24-
operatorMode: boolean
25+
operatorMode: boolean,
26+
buckets: Bucket[]
2527
) => {
2628
const initialActions: Action[] = [];
2729
const allowedMenuItems = validRoutes(features, operatorMode);
@@ -58,6 +60,20 @@ export const routesAsKbarActions = (
5860
icon: <BucketsIcon />,
5961
};
6062
initialActions.push(a);
63+
64+
if (buckets) {
65+
buckets.map((buck) => [
66+
initialActions.push({
67+
id: buck.name,
68+
name: buck.name,
69+
section: "List of Buckets",
70+
perform: () => {
71+
history.push(`/buckets/${buck.name}/browse`);
72+
},
73+
icon: <BucketsIcon />,
74+
}),
75+
]);
76+
}
6177
}
6278
return initialActions;
6379
};

0 commit comments

Comments
 (0)