Skip to content

Commit 57e9d05

Browse files
committed
Refactor FilterTablesButton to simplify table loading and integrate event bus for updates
1 parent dde5e16 commit 57e9d05

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/reactviews/pages/SchemaDesigner/toolbar/filterTablesButton.tsx

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import * as FluentIcons from "@fluentui/react-icons";
1616
import { useContext, useEffect, useState } from "react";
1717
import { SchemaDesignerContext } from "../schemaDesignerStateProvider";
1818
import { locConstants } from "../../../common/locConstants";
19-
import { Edge, Node, useReactFlow } from "@xyflow/react";
20-
import { SchemaDesigner } from "../../../../sharedInterfaces/schemaDesigner";
21-
import { flowUtils } from "../schemaDesignerUtils";
19+
import { useReactFlow } from "@xyflow/react";
20+
import eventBus from "../schemaDesignerEvents";
2221

2322
export function FilterTablesButton() {
2423
const context = useContext(SchemaDesignerContext);
@@ -33,27 +32,17 @@ export function FilterTablesButton() {
3332
const [isFilterMenuOpen, setIsFilterMenuOpen] = useState(false);
3433

3534
function loadTables() {
36-
const schema = flowUtils.extractSchemaModel(
37-
reactFlow.getNodes() as Node<SchemaDesigner.Table>[],
38-
reactFlow.getEdges() as Edge<SchemaDesigner.ForeignKey>[],
39-
);
40-
41-
if (!schema) {
42-
return;
35+
// Update the selected tables based on the current nodes
36+
const nodes = reactFlow.getNodes();
37+
const tableNames = nodes
38+
.filter((node) => node.hidden !== true)
39+
.map((node) => `${node.data.schema}.${node.data.name}`);
40+
if (nodes.length === tableNames.length) {
41+
setSelectedTables([]);
42+
} else {
43+
setSelectedTables(tableNames);
4344
}
44-
const tableNames = schema.tables.map((table) => `${table.schema}.${table.name}`);
45-
// bring selected tables to the top
46-
tableNames.sort((a, b) => {
47-
const aSelected = selectedTables.includes(a);
48-
const bSelected = selectedTables.includes(b);
49-
if (aSelected && !bSelected) {
50-
return -1;
51-
}
52-
if (!aSelected && bSelected) {
53-
return 1;
54-
}
55-
return a.localeCompare(b);
56-
});
45+
setFilterText("");
5746
}
5847

5948
useEffect(() => {
@@ -83,6 +72,14 @@ export function FilterTablesButton() {
8372
});
8473
}, [selectedTables]);
8574

75+
useEffect(() => {
76+
eventBus.on("getScript", () =>
77+
requestAnimationFrame(() => {
78+
loadTables();
79+
}),
80+
);
81+
}, []);
82+
8683
// Function to highlight text based on search
8784
const highlightText = (text: string, searchText: string) => {
8885
if (!searchText || searchText.trim() === "") {
@@ -212,6 +209,13 @@ export function FilterTablesButton() {
212209
style={{}}
213210
onClick={() => {
214211
setSelectedTables([]);
212+
const nodes = reactFlow.getNodes();
213+
nodes.forEach((node) => {
214+
reactFlow.updateNode(node.id, {
215+
...node,
216+
hidden: false,
217+
});
218+
});
215219
}}
216220
appearance="subtle"
217221
icon={<FluentIcons.DismissRegular />}>

0 commit comments

Comments
 (0)