Skip to content

Commit eddc1c3

Browse files
committed
fix: restoring selected stream in dashboard
1. Restored selected stream in dashboard panel 2. Added loader for stream and org selectors
1 parent e8005aa commit eddc1c3

File tree

1 file changed

+65
-34
lines changed

1 file changed

+65
-34
lines changed

src/components/QueryEditor.tsx

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
1616
const [streamOptions, setStreamOptions]: any = useState([]);
1717
const [orgOptions, setOrgOptions]: any = useState([]);
1818
const [isMounted, setIsMounted]: any = useState(false);
19+
const [isLoading, setIsLoading]: any = useState([]);
1920

2021
const isInDashboard = useMemo(() => app === 'panel-editor', [app]);
2122

2223
const getTimeStampColumnName = () => {
2324
return datasource.instanceSettings?.jsonData?.timestamp_column || '_timestamp';
2425
};
2526

27+
const startLoading = () => {
28+
setIsLoading([...isLoading, true]);
29+
};
30+
31+
const stopLoading = () => {
32+
setIsLoading(isLoading.slice(1));
33+
};
34+
2635
useEffect(() => {
36+
startLoading();
2737
getOrganizations({ url: datasource.url, page_num: 0, page_size: 1000, sort_by: 'id' })
2838
.then((orgs: any) => {
2939
setOrgOptions([
@@ -32,26 +42,42 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
3242
value: org.name,
3343
})),
3444
]);
35-
setupStreams(orgs.data[0].name).then((streams: any) => {
36-
datasource.updateStreamFields(streams[0].schema);
37-
setStreamOptions([
38-
...Object.values(streams).map((stream: any) => ({
39-
label: stream.name,
40-
value: stream.name,
41-
})),
42-
]);
43-
if (!(query.organization && query.stream && query.hasOwnProperty('sqlMode'))) {
44-
onChange({
45-
...query,
46-
stream: streams[0].name,
47-
organization: orgs.data[0].name,
48-
sqlMode: isInDashboard ? true : false,
49-
});
50-
}
51-
setIsMounted(true);
52-
});
45+
46+
let seletedOrg: string = orgs.data[0].name;
47+
48+
if (isInDashboard && query.organization) {
49+
seletedOrg = query.organization;
50+
}
51+
52+
startLoading();
53+
setupStreams(seletedOrg)
54+
.then((streams: any) => {
55+
datasource.updateStreamFields(streams[0].schema);
56+
setStreamOptions([
57+
...Object.values(streams).map((stream: any) => ({
58+
label: stream.name,
59+
value: stream.name,
60+
})),
61+
]);
62+
63+
if (!(query.organization && query.stream && query.hasOwnProperty('sqlMode'))) {
64+
onChange({
65+
...query,
66+
stream: streams[0].name,
67+
organization: orgs.data[0].name,
68+
sqlMode: isInDashboard ? true : false,
69+
});
70+
} else if (isInDashboard && query.organization && query.stream && query.query) {
71+
updateQuery();
72+
onRunQuery();
73+
}
74+
75+
setIsMounted(true);
76+
})
77+
.finally(() => stopLoading());
5378
})
54-
.catch((err) => console.log(err));
79+
.catch((err) => console.log(err))
80+
.finally(() => stopLoading());
5581
// eslint-disable-next-line react-hooks/exhaustive-deps
5682
}, []);
5783

@@ -125,21 +151,24 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
125151
};
126152

127153
const orgUpdated = (organization: any) => {
128-
setupStreams(organization.value).then((streams: any) => {
129-
onChange({
130-
...query,
131-
query: '',
132-
stream: streams[0].name,
133-
organization: organization.value,
134-
});
135-
datasource.updateStreamFields(cloneDeep(streams[0].schema));
136-
setStreamOptions([
137-
...streams.map((stream: any) => ({
138-
label: stream.name,
139-
value: stream.name,
140-
})),
141-
]);
142-
});
154+
startLoading();
155+
setupStreams(organization.value)
156+
.then((streams: any) => {
157+
onChange({
158+
...query,
159+
query: '',
160+
stream: streams[0].name,
161+
organization: organization.value,
162+
});
163+
datasource.updateStreamFields(cloneDeep(streams[0].schema));
164+
setStreamOptions([
165+
...streams.map((stream: any) => ({
166+
label: stream.name,
167+
value: stream.name,
168+
})),
169+
]);
170+
})
171+
.finally(() => stopLoading());
143172
};
144173

145174
const toggleSqlMode = () => {
@@ -186,6 +215,7 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
186215
options={orgOptions}
187216
value={query.organization}
188217
onChange={orgUpdated}
218+
isLoading={isLoading.length > 0}
189219
/>
190220
</div>
191221
<div
@@ -212,6 +242,7 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
212242
options={streamOptions}
213243
value={query.stream}
214244
onChange={streamUpdated}
245+
isLoading={isLoading.length > 0}
215246
/>
216247
</div>
217248
</div>

0 commit comments

Comments
 (0)