Skip to content

Commit

Permalink
flow Explorer everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
analogrelay committed Aug 13, 2024
1 parent d074114 commit cc7ff45
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/Explorer/Menus/CommandBar/CommandBarComponentAdapter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export const CommandBar: React.FC<Props> = ({ container }: Props) => {
const isHidden = useCommandBar((state) => state.isHidden);
const backgroundColor = StyleConstants.BaseLight;
const setKeyboardHandlers = useKeyboardActionGroup(KeyboardActionGroup.COMMAND_BAR);

const staticButtons = createStaticCommandBarButtons(selectedNodeState);
const platformButtons = createPlatformButtons();

const uiFabricStaticButtons = CommandBarUtil.convertButton(staticButtons, backgroundColor, container);
if (contextButtons?.length > 0) {
uiFabricStaticButtons.forEach((btn: ICommandBarItemProps) => (btn.iconOnly = true));
Expand All @@ -39,7 +40,6 @@ export const CommandBar: React.FC<Props> = ({ container }: Props) => {
uiFabricStaticButtons.push(CommandBarUtil.createDivider("commandBarDivider"));
}

const platformButtons = createPlatformButtons();
const uiFabricPlatformButtons = CommandBarUtil.convertButton(platformButtons || [], backgroundColor, container);
uiFabricPlatformButtons.forEach((btn: ICommandBarItemProps) => (btn.iconOnly = true));

Expand Down Expand Up @@ -69,7 +69,7 @@ export const CommandBar: React.FC<Props> = ({ container }: Props) => {
};

const allButtons = staticButtons.concat(contextButtons).concat(platformButtons);
const keyboardHandlers = CommandBarUtil.createKeyboardHandlers(allButtons);
const keyboardHandlers = CommandBarUtil.createKeyboardHandlers(allButtons, container);
setKeyboardHandlers(keyboardHandlers);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ import { AuthType } from "../../../AuthType";
import { DatabaseAccount } from "../../../Contracts/DataModels";
import { CollectionBase } from "../../../Contracts/ViewModels";
import { updateUserContext } from "../../../UserContext";
import Explorer from "../../Explorer";
import { useNotebook } from "../../Notebook/useNotebook";
import { useDatabases } from "../../useDatabases";
import { useSelectedNode } from "../../useSelectedNode";
import * as CommandBarComponentButtonFactory from "./CommandBarComponentButtonFactory";

describe("CommandBarComponentButtonFactory tests", () => {
let mockExplorer: Explorer;

afterEach(() => useSelectedNode.getState().setSelectedNode(undefined));

describe("Enable Azure Synapse Link Button", () => {
const enableAzureSynapseLinkBtnLabel = "Enable Azure Synapse Link";
const selectedNodeState = useSelectedNode.getState();

beforeAll(() => {
mockExplorer = {} as Explorer;
updateUserContext({
databaseAccount: {
properties: {
Expand All @@ -30,7 +26,7 @@ describe("CommandBarComponentButtonFactory tests", () => {
});

it("Button should be visible", () => {
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(mockExplorer, selectedNodeState);
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(selectedNodeState);
const enableAzureSynapseLinkBtn = buttons.find(
(button) => button.commandButtonLabel === enableAzureSynapseLinkBtnLabel,
);
Expand All @@ -46,7 +42,7 @@ describe("CommandBarComponentButtonFactory tests", () => {
} as DatabaseAccount,
});

const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(mockExplorer, selectedNodeState);
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(selectedNodeState);
const enableAzureSynapseLinkBtn = buttons.find(
(button) => button.commandButtonLabel === enableAzureSynapseLinkBtnLabel,
);
Expand All @@ -62,7 +58,7 @@ describe("CommandBarComponentButtonFactory tests", () => {
} as DatabaseAccount,
});

const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(mockExplorer, selectedNodeState);
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(selectedNodeState);
const enableAzureSynapseLinkBtn = buttons.find(
(button) => button.commandButtonLabel === enableAzureSynapseLinkBtnLabel,
);
Expand All @@ -75,7 +71,6 @@ describe("CommandBarComponentButtonFactory tests", () => {
const selectedNodeState = useSelectedNode.getState();

beforeAll(() => {
mockExplorer = {} as Explorer;
updateUserContext({
databaseAccount: {
properties: {
Expand Down Expand Up @@ -108,7 +103,7 @@ describe("CommandBarComponentButtonFactory tests", () => {
},
} as DatabaseAccount,
});
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(mockExplorer, selectedNodeState);
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(selectedNodeState);
const openCassandraShellBtn = buttons.find((button) => button.commandButtonLabel === openCassandraShellBtnLabel);
expect(openCassandraShellBtn).toBeUndefined();
});
Expand All @@ -118,13 +113,13 @@ describe("CommandBarComponentButtonFactory tests", () => {
portalEnv: "mooncake",
});

const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(mockExplorer, selectedNodeState);
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(selectedNodeState);
const openCassandraShellBtn = buttons.find((button) => button.commandButtonLabel === openCassandraShellBtnLabel);
expect(openCassandraShellBtn).toBeUndefined();
});

it("Notebooks is not enabled and is unavailable - button should be shown and disabled", () => {
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(mockExplorer, selectedNodeState);
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(selectedNodeState);
const openCassandraShellBtn = buttons.find((button) => button.commandButtonLabel === openCassandraShellBtnLabel);
expect(openCassandraShellBtn).toBeUndefined();
});
Expand All @@ -134,20 +129,16 @@ describe("CommandBarComponentButtonFactory tests", () => {
const openPostgresShellButtonLabel = "Open PSQL shell";
const openVCoreMongoShellButtonLabel = "Open MongoDB (vCore) shell";

beforeAll(() => {
mockExplorer = {} as Explorer;
});

it("creates Postgres shell button", () => {
const buttons = CommandBarComponentButtonFactory.createPostgreButtons(mockExplorer);
const buttons = CommandBarComponentButtonFactory.createPostgreButtons();
const openPostgresShellButton = buttons.find(
(button) => button.commandButtonLabel === openPostgresShellButtonLabel,
);
expect(openPostgresShellButton).toBeDefined();
});

it("creates vCore Mongo shell button", () => {
const buttons = CommandBarComponentButtonFactory.createVCoreMongoButtons(mockExplorer);
const buttons = CommandBarComponentButtonFactory.createVCoreMongoButtons();
const openVCoreMongoShellButton = buttons.find(
(button) => button.commandButtonLabel === openVCoreMongoShellButtonLabel,
);
Expand All @@ -162,8 +153,6 @@ describe("CommandBarComponentButtonFactory tests", () => {
const selectedNodeState = useSelectedNode.getState();

beforeAll(() => {
mockExplorer = {} as Explorer;

updateUserContext({
authType: AuthType.ResourceToken,
});
Expand All @@ -175,7 +164,7 @@ describe("CommandBarComponentButtonFactory tests", () => {
kind: "DocumentDB",
} as DatabaseAccount,
});
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(mockExplorer, selectedNodeState);
const buttons = CommandBarComponentButtonFactory.createStaticCommandBarButtons(selectedNodeState);
expect(buttons.length).toBe(2);
expect(buttons[0].commandButtonLabel).toBe("New SQL Query");
expect(buttons[0].disabled).toBe(false);
Expand Down
12 changes: 7 additions & 5 deletions src/Explorer/Menus/CommandBar/CommandBarUtil.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ICommandBarItemProps } from "@fluentui/react";
import Explorer from "Explorer/Explorer";
import { CommandButtonComponentProps } from "../../Controls/CommandButton/CommandButtonComponent";
import * as CommandBarUtil from "./CommandBarUtil";

describe("CommandBarUtil tests", () => {
const mockExplorer = {} as Explorer;
const createButton = (): CommandButtonComponentProps => {
return {
iconSrc: "icon",
Expand All @@ -22,7 +24,7 @@ describe("CommandBarUtil tests", () => {
const btn = createButton();
const backgroundColor = "backgroundColor";

const converteds = CommandBarUtil.convertButton([btn], backgroundColor);
const converteds = CommandBarUtil.convertButton([btn], backgroundColor, mockExplorer);
expect(converteds.length).toBe(1);
const converted = converteds[0];
expect(converted.split).toBe(undefined);
Expand All @@ -46,7 +48,7 @@ describe("CommandBarUtil tests", () => {
btn.children.push(child);
}

const converteds = CommandBarUtil.convertButton([btn], "backgroundColor");
const converteds = CommandBarUtil.convertButton([btn], "backgroundColor", mockExplorer);
expect(converteds.length).toBe(1);
const converted = converteds[0];
expect(converted.split).toBe(true);
Expand All @@ -62,7 +64,7 @@ describe("CommandBarUtil tests", () => {
btns.push(createButton());
}

const converteds = CommandBarUtil.convertButton(btns, "backgroundColor");
const converteds = CommandBarUtil.convertButton(btns, "backgroundColor", mockExplorer);
const uniqueKeys = converteds
.map((btn: ICommandBarItemProps) => btn.key)
.filter((value: string, index: number, self: string[]) => self.indexOf(value) === index);
Expand All @@ -74,10 +76,10 @@ describe("CommandBarUtil tests", () => {
const backgroundColor = "backgroundColor";

btn.commandButtonLabel = undefined;
let converted = CommandBarUtil.convertButton([btn], backgroundColor)[0];
let converted = CommandBarUtil.convertButton([btn], backgroundColor, mockExplorer)[0];
expect(converted.text).toEqual(btn.tooltipText);

converted = CommandBarUtil.convertButton([btn], backgroundColor)[0];
converted = CommandBarUtil.convertButton([btn], backgroundColor, mockExplorer)[0];
delete btn.commandButtonLabel;
expect(converted.text).toEqual(btn.tooltipText);
});
Expand Down
8 changes: 4 additions & 4 deletions src/Explorer/Menus/CommandBar/CommandBarUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const convertButton = (btns: CommandButtonComponentProps[], backgroundCol
result.split = true;

result.subMenuProps = {
items: convertButton(btn.children, backgroundColor),
items: convertButton(btn.children, backgroundColor, container),
styles: {
list: {
// TODO Figure out how to do it the proper way with subComponentStyles.
Expand Down Expand Up @@ -185,7 +185,7 @@ export const convertButton = (btns: CommandButtonComponentProps[], backgroundCol
option?: IDropdownOption,
index?: number,
): void => {
btn.children[index].onCommandClick(event);
btn.children[index].onCommandClick(event, container);
TelemetryProcessor.trace(Action.ClickCommandBarButton, ActionModifiers.Mark, { label: option.text });
};

Expand Down Expand Up @@ -236,14 +236,14 @@ export const createConnectionStatus = (container: Explorer, poolId: PoolIdType,
};
};

export function createKeyboardHandlers(allButtons: CommandButtonComponentProps[]): KeyboardHandlerMap {
export function createKeyboardHandlers(allButtons: CommandButtonComponentProps[], container: Explorer): KeyboardHandlerMap {
const handlers: KeyboardHandlerMap = {};

function createHandlers(buttons: CommandButtonComponentProps[]) {
buttons.forEach((button) => {
if (!button.disabled && button.keyboardAction) {
handlers[button.keyboardAction] = (e) => {
button.onCommandClick(e);
button.onCommandClick(e, container);

// If the handler is bound, it means the button is visible and enabled, so we should prevent the default action
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ describe("Documents tab (noSql API)", () => {
useCommandBar
.getState()
.contextButtons.find((button) => button.id === NEW_DOCUMENT_BUTTON_ID)
.onCommandClick(undefined);
.onCommandClick(undefined, undefined);
});
expect(wrapper.findWhere((node) => node.text().includes("replace_with_new_document_id")).exists()).toBeTruthy();
});
Expand All @@ -452,7 +452,7 @@ describe("Documents tab (noSql API)", () => {
useCommandBar
.getState()
.contextButtons.find((button) => button.id === NEW_DOCUMENT_BUTTON_ID)
.onCommandClick(undefined);
.onCommandClick(undefined, undefined);
});

expect(useCommandBar.getState().contextButtons.find((button) => button.id === SAVE_BUTTON_ID)).toBeDefined();
Expand All @@ -467,7 +467,7 @@ describe("Documents tab (noSql API)", () => {
useCommandBar
.getState()
.contextButtons.find((button) => button.id === DELETE_BUTTON_ID)
.onCommandClick(undefined);
.onCommandClick(undefined, undefined);
});

expect(mockDeleteDocuments).toHaveBeenCalled();
Expand Down
6 changes: 3 additions & 3 deletions src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2Mongo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe("Documents tab (Mongo API)", () => {
useCommandBar
.getState()
.contextButtons.find((button) => button.id === NEW_DOCUMENT_BUTTON_ID)
.onCommandClick(undefined);
.onCommandClick(undefined, undefined);
});
expect(wrapper.findWhere((node) => node.text().includes("replace_with_new_document_id")).exists()).toBeTruthy();
});
Expand All @@ -171,7 +171,7 @@ describe("Documents tab (Mongo API)", () => {
useCommandBar
.getState()
.contextButtons.find((button) => button.id === NEW_DOCUMENT_BUTTON_ID)
.onCommandClick(undefined);
.onCommandClick(undefined, undefined);
});

expect(useCommandBar.getState().contextButtons.find((button) => button.id === SAVE_BUTTON_ID)).toBeDefined();
Expand All @@ -186,7 +186,7 @@ describe("Documents tab (Mongo API)", () => {
useCommandBar
.getState()
.contextButtons.find((button) => button.id === DELETE_BUTTON_ID)
.onCommandClick(undefined);
.onCommandClick(undefined, undefined);
});

expect(mockDeleteDocument).toHaveBeenCalled();
Expand Down

0 comments on commit cc7ff45

Please sign in to comment.