Skip to content
Open
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 @@ -38,9 +38,13 @@ describe('Download Chart > Bar chart', () => {
cy.visitChartByParams(formData);
cy.get('.header-with-actions .ant-dropdown-trigger').click();
cy.get(':nth-child(3) > .ant-dropdown-menu-submenu-title').click();
cy.get(
'.ant-dropdown-menu-submenu > .ant-dropdown-menu li:nth-child(1) > .ant-dropdown-menu-submenu-title',
).click();
cy.get(
'.ant-dropdown-menu-submenu > .ant-dropdown-menu li:nth-child(3)',
).click();

cy.verifyDownload('.jpg', {
contains: true,
timeout: 25000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface DataTableProps<D extends object> extends TableOptions<D> {
onSearchColChange: (searchCol: string) => void;
searchOptions: SearchOption[];
onFilteredDataChange?: (rows: Row<D>[], filterValue?: string) => void;
onFilteredRowsChange?: (rows: D[]) => void;
}

export interface RenderHTMLCellProps extends HTMLProps<HTMLTableCellElement> {
Expand Down Expand Up @@ -133,6 +134,7 @@ export default typedMemo(function DataTable<D extends object>({
onSearchColChange,
searchOptions,
onFilteredDataChange,
onFilteredRowsChange,
...moreUseTableOptions
}: DataTableProps<D>): JSX.Element {
const tableHooks: PluginHook<D>[] = [
Expand Down Expand Up @@ -204,6 +206,7 @@ export default typedMemo(function DataTable<D extends object>({
);

const {
rows, // filtered/sorted rows before pagination
getTableProps,
getTableBodyProps,
prepareRow,
Expand All @@ -218,7 +221,6 @@ export default typedMemo(function DataTable<D extends object>({
wrapStickyTable,
setColumnOrder,
allColumns,
rows,
state: {
pageIndex,
pageSize,
Expand Down Expand Up @@ -452,6 +454,83 @@ export default typedMemo(function DataTable<D extends object>({
onServerPaginationChange(pageNumber, serverPageSize);
}

// Emit filtered rows to parent in client-side mode (debounced via RAF)
const isMountedRef = useRef(true);
useEffect(() => {
isMountedRef.current = true;
return () => {
isMountedRef.current = false;
};
}, []);

const rafRef = useRef<number | null>(null);
const lastSigRef = useRef<string>('');

// Prefer a stable identifier from original row data; otherwise use a deterministic
// concatenation of visible values (keys sorted so column order changes are detected).
function stableRowKey<D extends object>(r: Row<D>): string {
const orig = r.original as Record<string, unknown> | undefined;
if (orig) {
const idLike =
(orig as any).id ??
(orig as any).ID ??
(orig as any).key ??
(orig as any).uuid;
if (idLike != null) return String(idLike);
}

// Fallback: derive from row.values, but make it stable against column order changes.
const v = r.values as Record<string, unknown>;
const keys = Object.keys(v).sort(); // detect column order changes
return keys.map(k => String(v[k] ?? '')).join('|');
}

// Very small, fast hash for strings (no crypto dependency).
function hashString(s: string): string {
let h = 0;
for (let i = 0; i < s.length; i += 1) {
h = (h * 31 + s.charCodeAt(i)) | 0;
}
return String(h);
}

function signatureOfRows<D extends object>(rs: Row<D>[]): string {
const keys = rs.map(stableRowKey);
const len = keys.length;
const first = keys[0] ?? '';
const last = keys[len - 1] ?? '';
const digest = hashString(keys.join('\u0001')); // non-printable separator to avoid collisions
return `${len}|${first}|${last}|${digest}`;
}

useEffect(() => {
if (serverPagination || typeof onFilteredRowsChange !== 'function') {
return;
}

const sig = signatureOfRows(rows);

if (sig !== lastSigRef.current) {
lastSigRef.current = sig;
if (rafRef.current != null) {
cancelAnimationFrame(rafRef.current);
}
rafRef.current = requestAnimationFrame(() => {
if (isMountedRef.current) {
// Only emit originals when the signature truly changed
onFilteredRowsChange(rows.map(r => r.original as D));
}
});
}

return () => {
if (rafRef.current != null) {
cancelAnimationFrame(rafRef.current);
rafRef.current = null;
}
};
}, [rows, serverPagination, onFilteredRowsChange]);

return (
<div
ref={wrapperRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,6 @@ interface TableOwnState {
sortColumn?: string;
sortOrder?: 'asc' | 'desc';
searchText?: string;

clientView?: ClientViewSnapshot;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@
* under the License.
*/

import { SetDataMaskHook } from '@superset-ui/core';
import { TableOwnState } from '../types/react-table';
import type { SetDataMaskHook } from '@superset-ui/core';
import type { TableOwnState } from '../types/react-table';

export const updateExternalFormData = (
setDataMask: SetDataMaskHook = () => {},
pageNumber: number,
pageSize: number,
) =>
) => {
setDataMask({
ownState: {
currentPage: pageNumber,
pageSize,
},
});
};

export const updateTableOwnState = (
setDataMask: SetDataMaskHook = () => {},
modifiedOwnState: TableOwnState,
) =>
) => {
setDataMask({
ownState: modifiedOwnState,
});
};
46 changes: 46 additions & 0 deletions superset-frontend/plugins/plugin-chart-table/src/TableChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
MouseEvent,
KeyboardEvent as ReactKeyboardEvent,
useEffect,
useRef,
} from 'react';

import {
Expand Down Expand Up @@ -1358,6 +1359,50 @@ export default function TableChart<D extends DataRecord = DataRecord>(
}
};

// collect client-side filtered rows for export & push snapshot to ownState (guarded)
const [clientViewRows, setClientViewRows] = useState<DataRecord[]>([]);

const exportColumns = useMemo(
() =>
visibleColumnsMeta.map(col => ({
key: col.key,
label: col.config?.customColumnName || col.originalLabel || col.key,
})),
[visibleColumnsMeta],
);

// Use a ref to store previous clientViewRows and exportColumns for robust change detection
const prevClientViewRef = useRef<{
rows: DataRecord[];
columns: typeof exportColumns;
} | null>(null);
useEffect(() => {
if (serverPagination) return; // only for client-side mode
const prev = prevClientViewRef.current;
const rowsChanged = !prev || !isEqual(prev.rows, clientViewRows);
const columnsChanged = !prev || !isEqual(prev.columns, exportColumns);
if (rowsChanged || columnsChanged) {
prevClientViewRef.current = {
rows: clientViewRows,
columns: exportColumns,
};
updateTableOwnState(setDataMask, {
...serverPaginationData,
clientView: {
rows: clientViewRows,
columns: exportColumns,
count: clientViewRows.length,
},
});
}
}, [
clientViewRows,
exportColumns,
serverPagination,
setDataMask,
serverPaginationData,
]);

return (
<Styles>
<DataTable<D>
Expand Down Expand Up @@ -1395,6 +1440,7 @@ export default function TableChart<D extends DataRecord = DataRecord>(
onSearchChange={debouncedSearch}
searchOptions={searchOptions}
onFilteredDataChange={handleFilteredDataChange}
onFilteredRowsChange={setClientViewRows}
/>
</Styles>
);
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/plugins/plugin-chart-table/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const metadata = new ChartMetadata({
Behavior.InteractiveChart,
Behavior.DrillToDetail,
Behavior.DrillBy,
'EXPORT_CURRENT_VIEW' as any,
],
Comment on lines +42 to 43
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

[nitpick] Using as any to add a custom behavior bypasses TypeScript's type safety. Consider either:

  1. Adding EXPORT_CURRENT_VIEW to the Behavior enum in @superset-ui/core if this is intended to be a standard behavior, or
  2. Extending the behaviors array type to support string literals for custom behaviors.

This would make the implementation more type-safe and maintainable.

Suggested change
'EXPORT_CURRENT_VIEW' as any,
],
'EXPORT_CURRENT_VIEW',
] as (Behavior | 'EXPORT_CURRENT_VIEW')[],

Copilot uses AI. Check for mistakes.
category: t('Table'),
canBeAnnotationTypes: ['EVENT', 'INTERVAL'],
Expand Down
Loading