Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[front] - fix(TablePicker): infinite scroll #11446

Merged
merged 10 commits into from
Mar 19, 2025
Prev Previous commit
Next Next commit
[front] - refactor: switch TablePicker allTables to use Map for bette…
…r efficiency

 - Changed the state structure from an array to a Map to optimize table lookups and avoid duplicates
 - Updated the useEffect to handle pagination logic using the new Map, improving performance with large table lists
 - Modified rendering logic to convert the Map to an array for display and filter operations
JulesBelveze committed Mar 19, 2025
commit f255cb7108ba57dc63cef96d1941384ff3f6d487
33 changes: 19 additions & 14 deletions front/components/tables/TablePicker.tsx
Original file line number Diff line number Diff line change
@@ -52,7 +52,9 @@ export default function TablePicker({
}: TablePickerProps) {
void dataSource;
const [debouncedSearch, setDebouncedSearch] = useState<string>("");
const [allTables, setAllTables] = useState<DataSourceViewContentNode[]>([]);
const [allTablesMap, setallTablesMap] = useState<
Map<string, DataSourceViewContentNode>
>(new Map());
const [currentTable, setCurrentTable] = useState<CoreAPITable>();
const {
cursorPagination,
@@ -89,17 +91,20 @@ export default function TablePicker({

useEffect(() => {
if (tables && !isTablesLoading) {
setAllTables((prevTables) => {
setallTablesMap((prevTablesMap) => {
if (pageIndex === 0) {
return tables;
return new Map(tables.map((table) => [table.internalId, table]));
} else {
const newTables = tables.filter(
(table) =>
!prevTables.some(
(prevTable) => prevTable.internalId === table.internalId
)
);
return [...prevTables, ...newTables];
// Create a new Map to avoid mutating the previous state
const newTablesMap = new Map(prevTablesMap);

tables.forEach((table) => {
if (!prevTablesMap.has(table.internalId)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do you really need the test?

Copy link
Contributor Author

@JulesBelveze JulesBelveze Mar 19, 2025

Choose a reason for hiding this comment

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

Fair ✅

newTablesMap.set(table.internalId, table);
}
});

return newTablesMap;
}
});
}
@@ -153,7 +158,7 @@ export default function TablePicker({
</div>
<ChevronDownIcon className="mt-0.5 h-4 w-4 hover:text-gray-700" />
</div>
) : allTables.length > 0 ? (
) : allTablesMap.size > 0 ? (
<Button
variant="outline"
label="Select Table"
@@ -181,7 +186,7 @@ export default function TablePicker({
/>
<ScrollArea hideScrollBar className="flex max-h-[300px] flex-col">
<div className="w-full space-y-1">
{allTables
{Array.from(allTablesMap.values())
.filter(
(t) =>
!excludeTables?.some(
@@ -205,7 +210,7 @@ export default function TablePicker({
</div>
</div>
))}
{allTables.length === 0 && (
{allTablesMap.size === 0 && (
<span className="block px-4 pt-2 text-sm text-gray-700">
No tables found
</span>
@@ -219,7 +224,7 @@ export default function TablePicker({
isValidating={isTablesLoading}
isLoading={isTablesLoading}
>
{isTablesLoading && !allTables.length && (
{isTablesLoading && !allTablesMap.size && (
<div className="py-2 text-center text-sm text-element-700">
Loading tables...
</div>