Skip to content

Commit c71d54d

Browse files
Check and update db list if not present (#320)
* check and update db list if not present * Use trigger state update to trigger db load --------- Co-authored-by: Sreejith <1743700+ppsreejith@users.noreply.github.com>
1 parent aaf100b commit c71d54d

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

apps/src/base/appState.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export abstract class AppState<T extends InternalState, K> {
3535
public async getQuerySelectorMap() {
3636
return this.useStore().getState().querySelectorMap;
3737
}
38+
39+
public async triggerStateUpdate() {
40+
}
3841
}
3942

4043
export abstract class DefaultAppState<T> extends AppState<InternalState, T> {

apps/src/metabase/appState.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ function minifyDbs(allDBs: any) {
137137
return Object.values(allDBs || {}).map((db: any) => ({ id: db.id, name: db.name }))
138138
}
139139

140+
async function fetchAllDBsIfEmpty() {
141+
let minifiedDBs = minifyDbs(await RPCs.getMetabaseState('entities.databases'))
142+
let _tries = 0
143+
while (isEmpty(minifiedDBs)) {
144+
await new Promise(resolve => setTimeout(resolve, 500))
145+
minifiedDBs = minifyDbs(await RPCs.getMetabaseState('entities.databases'))
146+
if (_tries++ > 50) {
147+
break
148+
}
149+
}
150+
return minifiedDBs
151+
}
152+
140153
export class MetabaseState extends DefaultAppState<MetabaseAppState> {
141154
initialInternalState = metabaseInternalState;
142155
actionController = new MetabaseController(this);
@@ -150,13 +163,8 @@ export class MetabaseState extends DefaultAppState<MetabaseAppState> {
150163

151164
const getState = this.useStore().getState
152165
let minifiedDBs = minifyDbs(allDBs)
153-
let _tries = 0
154-
while (isEmpty(minifiedDBs)) {
155-
await new Promise(resolve => setTimeout(resolve, 500))
156-
minifiedDBs = minifyDbs(await RPCs.getMetabaseState('entities.databases'));
157-
if (_tries++ > 50) {
158-
break
159-
}
166+
if (isEmpty(minifiedDBs)) {
167+
minifiedDBs = await fetchAllDBsIfEmpty()
160168
}
161169

162170
let toolEnabledNew = shouldEnable(elements, url);

web/src/components/common/TaskUI.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,19 @@ const TaskUI = forwardRef<HTMLTextAreaElement>((_props, ref) => {
764764
<Text fontSize={"sm"} color={"gray.500"} mb={3}>We're unable to auto-select your Database. Pick one to get started</Text>
765765
<Box display="flex" justifyContent="center">
766766
<Menu placement="bottom">
767-
<MenuButton as={Button} rightIcon={<BiChevronDown />} size="xs" variant="solid" colorScheme='minusxGreen'>
767+
<MenuButton
768+
as={Button}
769+
rightIcon={<BiChevronDown />}
770+
size="xs"
771+
variant="solid"
772+
colorScheme='minusxGreen'
773+
onClick={() => {
774+
const allDBs = get(toolContext, 'allDBs', [])
775+
if (isEmpty(allDBs)) {
776+
app.triggerStateUpdate()
777+
}
778+
}}
779+
>
768780
Choose a Database
769781
</MenuButton>
770782
<MenuList maxH="200px" overflowY="auto">

0 commit comments

Comments
 (0)