@@ -21,7 +21,7 @@ import {
21
21
TriggerTypeStyled,
22
22
} from "lowcoder-design";
23
23
import { BottomTabs } from "pages/editor/bottom/BottomTabs";
24
- import { useContext, useMemo } from "react";
24
+ import { useCallback, useContext, useDeferredValue, useEffect, useMemo, useState } from "react";
25
25
import { useSelector } from "react-redux";
26
26
import { getDataSource, getDataSourceTypes } from "redux/selectors/datasourceSelectors";
27
27
import { BottomResTypeEnum } from "types/bottomRes";
@@ -552,7 +552,7 @@ function findQueryInNestedStructure(
552
552
const regex = new RegExp(
553
553
`{{\\s*[!?]?(\\s*${queryName}\\b(\\.[^}\\s]*)?\\s*)(\\?[^}:]*:[^}]*)?\\s*}}`
554
554
);
555
- return regex.test(structure);
555
+ return structure === queryName || regex.test(structure);
556
556
}
557
557
558
558
if (typeof structure === "object" && structure !== null) {
@@ -565,13 +565,12 @@ function findQueryInNestedStructure(
565
565
}
566
566
567
567
function collectComponentsUsingQuery(comps: any, queryName: string) {
568
-
569
568
// Select all active components
570
569
const components = Object.values(comps);
571
570
572
571
// Filter components that reference the query by name
573
572
const componentsUsingQuery = components.filter((component: any) => {
574
- return findQueryInNestedStructure(component.children , queryName);
573
+ return findQueryInNestedStructure(component.toJsonValue() , queryName);
575
574
});
576
575
577
576
return componentsUsingQuery;
@@ -596,7 +595,7 @@ function collectQueryUsageDetails(component: any, queryName: string): any[] {
596
595
// Check if the string contains the query
597
596
const regex = new RegExp(`{{\\s*[!?]?(\\s*${queryName}\\b(\\.[^}\\s]*)?\\s*)(\\?[^}:]*:[^}]*)?\\s*}}`);
598
597
const entriesToRemove = ["children", "comp", "unevaledValue", "value"];
599
- if (regex.test(value)) {
598
+ if (value === queryName || regex.test(value)) {
600
599
console.log("tester",component.children);
601
600
results.push({
602
601
componentType: component.children.compType?.value || "Unknown Component",
@@ -724,39 +723,65 @@ export function ComponentUsagePanel(props: {
724
723
}
725
724
726
725
// a usage display to show which components make use of this query
727
- export const QueryUsagePropertyView = (props: {
726
+ export const QueryUsagePropertyView = React.memo( (props: {
728
727
comp: InstanceType<typeof QueryComp>;
729
728
placement?: PageType;
730
729
}) => {
731
730
const { comp, placement = "editor" } = props;
731
+ const [ loading, setLoading ] = useState(false);
732
+ const [ usageObjects, setUsageObjects ] = useState<any[]>([]);
732
733
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]);
737
759
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) => {
739
765
editorState.setSelectedCompNames(new Set([componentName]));
740
766
// console.log(`Selected Component: ${componentName}, Path: ${path}`);
741
- };
767
+ }, []) ;
742
768
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 <></>
757
771
}
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
+ });
760
785
761
786
762
787
function useDatasourceStatus(datasourceId: string, datasourceType: ResourceType) {
0 commit comments