Skip to content

Commit 40e3305

Browse files
fix and optimize query usage details
1 parent b3bb957 commit 40e3305

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

client/packages/lowcoder/src/comps/queries/queryComp/queryPropertyView.tsx

+53-28
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
TriggerTypeStyled,
2222
} from "lowcoder-design";
2323
import { BottomTabs } from "pages/editor/bottom/BottomTabs";
24-
import { useContext, useMemo } from "react";
24+
import { useCallback, useContext, useDeferredValue, useEffect, useMemo, useState } from "react";
2525
import { useSelector } from "react-redux";
2626
import { getDataSource, getDataSourceTypes } from "redux/selectors/datasourceSelectors";
2727
import { BottomResTypeEnum } from "types/bottomRes";
@@ -552,7 +552,7 @@ function findQueryInNestedStructure(
552552
const regex = new RegExp(
553553
`{{\\s*[!?]?(\\s*${queryName}\\b(\\.[^}\\s]*)?\\s*)(\\?[^}:]*:[^}]*)?\\s*}}`
554554
);
555-
return regex.test(structure);
555+
return structure === queryName || regex.test(structure);
556556
}
557557

558558
if (typeof structure === "object" && structure !== null) {
@@ -565,13 +565,12 @@ function findQueryInNestedStructure(
565565
}
566566

567567
function collectComponentsUsingQuery(comps: any, queryName: string) {
568-
569568
// Select all active components
570569
const components = Object.values(comps);
571570

572571
// Filter components that reference the query by name
573572
const componentsUsingQuery = components.filter((component: any) => {
574-
return findQueryInNestedStructure(component.children, queryName);
573+
return findQueryInNestedStructure(component.toJsonValue(), queryName);
575574
});
576575

577576
return componentsUsingQuery;
@@ -596,7 +595,7 @@ function collectQueryUsageDetails(component: any, queryName: string): any[] {
596595
// Check if the string contains the query
597596
const regex = new RegExp(`{{\\s*[!?]?(\\s*${queryName}\\b(\\.[^}\\s]*)?\\s*)(\\?[^}:]*:[^}]*)?\\s*}}`);
598597
const entriesToRemove = ["children", "comp", "unevaledValue", "value"];
599-
if (regex.test(value)) {
598+
if (value === queryName || regex.test(value)) {
600599
console.log("tester",component.children);
601600
results.push({
602601
componentType: component.children.compType?.value || "Unknown Component",
@@ -724,39 +723,65 @@ export function ComponentUsagePanel(props: {
724723
}
725724

726725
// a usage display to show which components make use of this query
727-
export const QueryUsagePropertyView = (props: {
726+
export const QueryUsagePropertyView = React.memo((props: {
728727
comp: InstanceType<typeof QueryComp>;
729728
placement?: PageType;
730729
}) => {
731730
const { comp, placement = "editor" } = props;
731+
const [ loading, setLoading ] = useState(false);
732+
const [ usageObjects, setUsageObjects ] = useState<any[]>([]);
732733
const editorState = useContext(EditorContext);
733-
const queryName = comp.children.name.getView();
734-
const componentsUsingQuery = collectComponentsUsingQuery(editorState.getAllUICompMap(), queryName);
735-
736-
const usageObjects = buildQueryUsageDataset(componentsUsingQuery, queryName);
734+
const queryName = useMemo(() => comp.children.name.getView(), [comp.children.name]);
735+
const allUICompMap = useMemo(() => editorState.getAllUICompMap(), []);
736+
737+
738+
const buildUsageDataset = useCallback(async () => {
739+
return new Promise((resolve) => {
740+
setTimeout(() => {
741+
const componentsUsingQuery = collectComponentsUsingQuery(allUICompMap, queryName);
742+
const usageObjs = buildQueryUsageDataset(componentsUsingQuery, queryName);
743+
setUsageObjects(usageObjs);
744+
resolve(true);
745+
}, 2000);
746+
})
747+
}, [queryName, allUICompMap]);
748+
749+
const findQueryUsageObjects = useCallback(async () => {
750+
setLoading(true);
751+
try {
752+
await buildUsageDataset();
753+
} catch(e) {
754+
console.error(e);
755+
} finally {
756+
setLoading(false);
757+
}
758+
}, [buildQueryUsageDataset]);
737759

738-
const handleSelect = (componentType: string,componentName: string, path: string) => {
760+
useEffect(() => {
761+
findQueryUsageObjects();
762+
}, [findQueryUsageObjects]);
763+
764+
const handleSelect = useCallback((componentType: string,componentName: string, path: string) => {
739765
editorState.setSelectedCompNames(new Set([componentName]));
740766
// console.log(`Selected Component: ${componentName}, Path: ${path}`);
741-
};
767+
}, []);
742768

743-
if (usageObjects.length > 0) {
744-
return (
745-
<>
746-
<Divider />
747-
<QuerySectionWrapper>
748-
<QueryConfigWrapper>
749-
<QueryConfigLabel>{trans("query.componentsUsingQueryTitle")}</QueryConfigLabel>
750-
<ComponentUsagePanel components={usageObjects} onSelect={handleSelect} />
751-
</QueryConfigWrapper>
752-
</QuerySectionWrapper>
753-
</>
754-
);
755-
} else {
756-
return <div></div>;
769+
if (loading || !Boolean(usageObjects?.length)) {
770+
return <></>
757771
}
758-
759-
};
772+
773+
return (
774+
<>
775+
<Divider />
776+
<QuerySectionWrapper>
777+
<QueryConfigWrapper>
778+
<QueryConfigLabel>{trans("query.componentsUsingQueryTitle")}</QueryConfigLabel>
779+
<ComponentUsagePanel components={usageObjects} onSelect={handleSelect} />
780+
</QueryConfigWrapper>
781+
</QuerySectionWrapper>
782+
</>
783+
);
784+
});
760785

761786

762787
function useDatasourceStatus(datasourceId: string, datasourceType: ResourceType) {

0 commit comments

Comments
 (0)