Skip to content

Commit 1a317c4

Browse files
committed
optimise state check
1 parent ab6d68d commit 1a317c4

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

apps/web/src/app/[id]/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export default function AppPage({ params }: { params: Promise<{ id: string }> })
2424
setError(undefined);
2525
try {
2626
const state = await checkToolState(token, TOOLS[id].ds);
27-
setToolState(state);
27+
console.log(id);
28+
console.log(state);
29+
console.log(state[id]);
30+
setToolState(state[id]);
2831
setIsValidToken(true);
2932
} catch (error) {
3033
if (error instanceof InvalidTokenError) {

apps/web/src/components/sidebar.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,10 @@ function SidebarContent({ activeAppId }: { activeAppId?: string }) {
8181
setIsLoading(true);
8282
setError(undefined);
8383
try {
84-
const states = await Promise.all(
85-
Object.values(TOOLS).map(async (app) => {
86-
const state = await checkToolState(token, app.ds);
87-
return [app.id, state] as const;
88-
})
89-
);
84+
const allStates = await checkToolState(token);
85+
const states = Object.values(TOOLS).map((app) => {
86+
return [app.id, allStates[app.ds]] as const;
87+
});
9088
setToolStates(Object.fromEntries(states));
9189
} catch (error) {
9290
if (error instanceof InvalidTokenError) {

apps/web/src/lib/tinybird.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,55 @@ export async function query(token: string, sql: string): Promise<QueryResult> {
6060
return data;
6161
}
6262

63-
export async function checkToolState(token: string, datasource: string): Promise<ToolState> {
63+
export async function checkToolState(token: string, datasource?: string): Promise<Record<string, ToolState>> {
6464
try {
65-
// First check if data source exists
65+
// Get all data sources
6666
const sources = await listDataSources(token);
67-
const exists = sources.some(source => source.name === datasource);
67+
const sourceNames = new Set(sources.map(source => source.name));
68+
69+
// If a specific datasource is requested, only check that one
70+
if (datasource) {
71+
let state: ToolState = 'available';
72+
if (sourceNames.has(datasource)) {
73+
state = 'installed';
74+
const rows = await query(token, `SELECT count(*) as count FROM ${datasource} FORMAT JSON`);
75+
if (rows.data[0]?.count > 0) {
76+
state = 'configured';
77+
}
78+
}
79+
return { [datasource]: state };
80+
}
6881

69-
if (!exists) {
70-
return 'available';
82+
// Build a query to check counts for all sources at once
83+
const sourcesArray = Array.from(sourceNames);
84+
if (sourcesArray.length === 0) {
85+
return {};
7186
}
7287

73-
// Then check if it has data
74-
const result = await query(token, `SELECT count(*) as count FROM ${datasource} FORMAT JSON`);
75-
const hasData = result.data[0]?.count > 0;
88+
const unionQuery = sourcesArray
89+
.map(name => `SELECT '${name}' as source, count(*) as count FROM ${name}`)
90+
.join(' UNION ALL ');
91+
92+
console.log(unionQuery);
93+
94+
const result = await query(token, `${unionQuery} FORMAT JSON`);
95+
console.log(result);
96+
97+
// Convert results to state map
98+
const stateMap: Record<string, ToolState> = {};
99+
result.data.forEach(row => {
100+
stateMap[row.source] = row.count > 0 ? 'configured' : 'installed';
101+
});
102+
console.log(stateMap);
76103

77-
return hasData ? 'configured' : 'installed';
104+
// Add available state for any datasource not in Tinybird
105+
return stateMap;
78106
} catch (error) {
79107
if (error instanceof InvalidTokenError) {
80108
throw error;
81109
}
82-
console.error('Error checking tool state:', error);
83-
return 'available';
110+
console.error('Error checking tool states:', error);
111+
return {};
84112
}
85113
}
86114

0 commit comments

Comments
 (0)