Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ const mockHistoryItems = [
},
];

// Add mock data with more than 20 items for pagination testing
const mockManyHistoryItems = Array.from({ length: 25 }, (_, i) => ({
queryString: `FROM index_${i} | WHERE field = "value_${i}"`,
timeRan: '',
status: 'success' as const,
}));

describe('Starred and History queries components', () => {
const services = {
core: coreMock.createStart(),
Expand Down Expand Up @@ -500,5 +507,59 @@ describe('Starred and History queries components', () => {
expect(screen.queryByTestId('ESQLEditor-history-search')).not.toBeInTheDocument();
});
});

it('should show pagination controls when there are more than 20 items', async () => {
mockGetHistoryItems.mockReturnValue(mockManyHistoryItems);
mockGetStorageStats.mockReturnValue({
queryCount: 25,
storageSizeKB: 5,
maxStorageLimitKB: 50,
storageUsagePercent: 10,
});

render(
<KibanaContextProvider services={services}>
<HistoryAndStarredQueriesTabs
containerCSS={{}}
containerWidth={800}
onUpdateAndSubmit={jest.fn()}
height={400}
/>
</KibanaContextProvider>
);

// Wait for the component to render
await waitFor(() => {
expect(screen.getByTestId('ESQLEditor-queryHistory')).toBeInTheDocument();
});

// Check that pagination controls are present by looking for EUI pagination class
await waitFor(() => {
const paginationElement = document.querySelector('.euiPagination');
expect(paginationElement).toBeInTheDocument();
});
});

it('should not show pagination controls when there are 20 or fewer items', async () => {
render(
<KibanaContextProvider services={services}>
<HistoryAndStarredQueriesTabs
containerCSS={{}}
containerWidth={800}
onUpdateAndSubmit={jest.fn()}
height={400}
/>
</KibanaContextProvider>
);

// Wait for the component to render
await waitFor(() => {
expect(screen.getByTestId('ESQLEditor-queryHistory')).toBeInTheDocument();
});

// Check that pagination controls are not present
const paginationElement = document.querySelector('.euiPagination');
expect(paginationElement).not.toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import { cssFavoriteHoverWithinEuiTableRow } from '@kbn/content-management-favor
import { FAVORITES_LIMIT as ESQL_STARRED_QUERIES_LIMIT } from '@kbn/content-management-favorites-common';
import type { Interpolation, Theme } from '@emotion/react';
import { css } from '@emotion/react';
import { useEuiTablePersist } from '@kbn/shared-ux-table-persist';
import {
type QueryHistoryItem,
getHistoryItems,
Expand Down Expand Up @@ -261,13 +260,27 @@ export function QueryList({
const scrollBarStyles = euiScrollBarStyles(theme);
const [isDiscardQueryModalVisible, setIsDiscardQueryModalVisible] = useState(false);

const { sorting, onTableChange } = useEuiTablePersist<QueryHistoryItem>({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing bugs with pagination and I dont think it is important to persist the sorting here

tableId: 'esqlQueryHistory',
initialSort: {
field: 'timeRan',
direction: 'desc',
},
});
// Add simple sorting state that won't interfere with pagination
const sorting = useMemo(
() => ({
sort: {
field: 'timeRan' as keyof QueryHistoryItem,
direction: 'desc' as const,
},
}),
[]
);

// Only show pagination if more than 20 items
const pagination = useMemo(() => {
if (listItems.length > 20) {
return {
initialPageSize: 20,
showPerPageOptions: false,
};
}
return undefined;
}, [listItems.length]);

const actions: Array<CustomItemAction<QueryHistoryItem>> = useMemo(() => {
return [
Expand Down Expand Up @@ -383,7 +396,7 @@ export function QueryList({
items={listItems}
columns={columns}
sorting={sorting}
onChange={onTableChange}
pagination={pagination}
css={tableStyling}
tableLayout={containerWidth < 560 ? 'auto' : 'fixed'}
/>
Expand Down Expand Up @@ -544,6 +557,58 @@ export function HistoryAndStarredQueriesTabs({
);
}, [starredSearchQuery, starredQueries]);

// Create stable QueryList components outside of tabs array
const historyQueryList = useMemo(
() => (
<QueryList
containerCSS={containerCSS}
onUpdateAndSubmit={onUpdateAndSubmit}
containerWidth={containerWidth}
height={height}
listItems={filteredHistoryItems}
dataTestSubj="ESQLEditor-queryHistory"
tableCaption={i18n.translate('esqlEditor.query.querieshistoryTable', {
defaultMessage: 'Queries history table',
})}
starredQueriesService={starredQueriesService ?? undefined}
/>
),
[
containerCSS,
onUpdateAndSubmit,
containerWidth,
height,
filteredHistoryItems,
starredQueriesService,
]
);

const starredQueryList = useMemo(
() => (
<QueryList
containerCSS={containerCSS}
onUpdateAndSubmit={onUpdateAndSubmit}
containerWidth={containerWidth}
height={height}
listItems={filteredStarredQueries}
dataTestSubj="ESQLEditor-starredQueries"
tableCaption={i18n.translate('esqlEditor.query.starredQueriesTable', {
defaultMessage: 'Starred queries table',
})}
starredQueriesService={starredQueriesService ?? undefined}
isStarredTab={true}
/>
),
[
containerCSS,
onUpdateAndSubmit,
containerWidth,
height,
filteredStarredQueries,
starredQueriesService,
]
);

const { euiTheme } = useEuiTheme();
const tabs = useMemo(() => {
// use typed helper instead of .filter directly to remove falsy values from result type
Expand All @@ -557,20 +622,7 @@ export function HistoryAndStarredQueriesTabs({
defaultMessage: 'Recent',
}),
dataTestSubj: 'history-queries-tab',
content: (
<QueryList
containerCSS={containerCSS}
onUpdateAndSubmit={onUpdateAndSubmit}
containerWidth={containerWidth}
height={height}
listItems={filteredHistoryItems}
dataTestSubj="ESQLEditor-queryHistory"
tableCaption={i18n.translate('esqlEditor.query.querieshistoryTable', {
defaultMessage: 'Queries history table',
})}
starredQueriesService={starredQueriesService ?? undefined}
/>
),
content: historyQueryList,
},
starredQueriesService !== null && {
id: HistoryTabId.standardQueries,
Expand All @@ -583,33 +635,10 @@ export function HistoryAndStarredQueriesTabs({
{starredQueries?.length}
</EuiNotificationBadge>
),
content: (
<QueryList
containerCSS={containerCSS}
onUpdateAndSubmit={onUpdateAndSubmit}
containerWidth={containerWidth}
height={height}
listItems={filteredStarredQueries}
dataTestSubj="ESQLEditor-starredQueries"
tableCaption={i18n.translate('esqlEditor.query.starredQueriesTable', {
defaultMessage: 'Starred queries table',
})}
starredQueriesService={starredQueriesService ?? undefined}
isStarredTab={true}
/>
),
content: starredQueryList,
},
]);
}, [
containerCSS,
onUpdateAndSubmit,
containerWidth,
height,
filteredHistoryItems,
starredQueriesService,
starredQueries?.length,
filteredStarredQueries,
]);
}, [historyQueryList, starredQueriesService, starredQueries?.length, starredQueryList]);

const [selectedTabId, setSelectedTabId] = useRestorableState(
'historySelectedTabId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@kbn/content-management-favorites-common",
"@kbn/kibana-utils-plugin",
"@kbn/ui-actions-plugin",
"@kbn/shared-ux-table-persist",
"@kbn/esql-types",
"@kbn/field-types",
"@kbn/i18n-react",
Expand Down