Skip to content

Commit

Permalink
PR feedback: simplify onclick callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
awahab07 committed Oct 9, 2024
1 parent a9f2b50 commit 545446d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ describe('LogsExplorerTabs', () => {
},
} as unknown as typeof discoverServiceMock;

render(<LogsExplorerTabs services={services} selectedTab={selectedTab} />);
const { unmount } = render(<LogsExplorerTabs services={services} selectedTab={selectedTab} />);

return {
mockDiscoverLocator,
mockLogsExplorerLocator,
unmount,
};
};

Expand Down Expand Up @@ -99,12 +100,14 @@ describe('LogsExplorerTabs', () => {
});

it('should update the last used viewer in local storage', async () => {
renderTabs();
const { unmount } = renderTabs('discover');

mockSetLastUsedViewer.mockClear();
await userEvent.click(getLogsExplorerTab());
expect(mockSetLastUsedViewer).toHaveBeenCalledWith(OBSERVABILITY_LOGS_EXPLORER_APP_ID);

unmount();
renderTabs('logs-explorer');
mockSetLastUsedViewer.mockClear();
await userEvent.click(getDiscoverTab());
expect(mockSetLastUsedViewer).toHaveBeenCalledWith(DISCOVER_APP_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,19 @@ export const LogsExplorerTabs = ({ services, selectedTab }: LogsExplorerTabsProp
typeof DISCOVER_APP_ID | typeof OBSERVABILITY_LOGS_EXPLORER_APP_ID
>(OBS_LOGS_EXPLORER_LOGS_VIEWER_KEY, OBSERVABILITY_LOGS_EXPLORER_APP_ID);

const navigateToDiscover = createNavigateHandler(
() => {
if (selectedTab !== 'discover') {
discoverLocator?.navigate(emptyParams);
}
},
() => {
const navigateToDiscover = createNavigateHandler(() => {
if (selectedTab !== 'discover') {
setLastUsedViewer(DISCOVER_APP_ID);
discoverLocator?.navigate(emptyParams);
}
);
});

const navigateToLogsExplorer = createNavigateHandler(
() => {
if (selectedTab !== 'logs-explorer') {
logsExplorerLocator?.navigate(emptyParams);
}
},
() => {
const navigateToLogsExplorer = createNavigateHandler(() => {
if (selectedTab !== 'logs-explorer') {
setLastUsedViewer(OBSERVABILITY_LOGS_EXPLORER_APP_ID);
logsExplorerLocator?.navigate(emptyParams);
}
);
});

return (
<EuiTabs bottomBorder={false} data-test-subj="logsExplorerTabs">
Expand Down Expand Up @@ -98,12 +90,11 @@ const isModifiedEvent = (event: MouseEvent) =>

const isLeftClickEvent = (event: MouseEvent) => event.button === 0;

const createNavigateHandler = (onClick: () => void, updateState: () => void) => (e: MouseEvent) => {
const createNavigateHandler = (onClick: () => void) => (e: MouseEvent) => {
if (isModifiedEvent(e) || !isLeftClickEvent(e)) {
return;
}

e.preventDefault();
updateState(); // Update local storage
onClick();
};

0 comments on commit 545446d

Please sign in to comment.