11import { atom } from "jotai" ;
2- import { atomWithStorage } from "jotai/utils" ;
32import axios from "axios" ;
43import { QueryProcessor } from "@/lib/query-processor" ;
54import { toast } from "sonner" ;
@@ -13,20 +12,26 @@ export type { QueryHistoryItem };
1312// Import tab atoms
1413import {
1514 currentTabQueryAtom ,
16- currentTabDatabaseAtom ,
17- currentTabTableAtom ,
18- currentTabTimeFieldAtom ,
19- currentTabTimeRangeAtom ,
2015 currentTabQueryHistoryAtom ,
21- addToTabQueryHistoryAtom ,
2216 clearTabQueryHistoryAtom ,
2317 currentTabQueryResultsAtom ,
2418 currentTabQueryErrorAtom ,
2519 currentTabQueryLoadingAtom ,
2620 currentTabQueryExecutionTimeAtom ,
2721 currentTabQueryMetricsAtom ,
2822 currentTabRawQueryResponseAtom ,
29- currentTabProcessedQueryAtom
23+ currentTabProcessedQueryAtom ,
24+ activeTabIdAtom ,
25+ addRunningQueryAtom ,
26+ removeRunningQueryAtom ,
27+ updateTabQueryResultsByIdAtom ,
28+ updateTabQueryErrorByIdAtom ,
29+ updateTabQueryLoadingByIdAtom ,
30+ updateTabQueryMetricsByIdAtom ,
31+ updateTabRawResponseByIdAtom ,
32+ updateTabProcessedQueryByIdAtom ,
33+ addToTabQueryHistoryByIdAtom ,
34+ getTabByIdAtom
3035} from "./tab-atoms" ;
3136
3237// Alias the tab-aware atoms for backward compatibility
@@ -48,11 +53,27 @@ export const setQueryAtom = atom(null, (_get, set, query: string) => {
4853} ) ;
4954
5055export const executeQueryAtom = atom ( null , async ( get , set ) => {
51- const query = get ( queryAtom ) ;
52- const selectedDb = get ( currentTabDatabaseAtom ) ;
53- const selectedTable = get ( currentTabTableAtom ) ;
54- const selectedTimeField = get ( currentTabTimeFieldAtom ) ;
55- const timeRange = get ( currentTabTimeRangeAtom ) ;
56+ // Capture the tab ID at the start of execution
57+ const executeTabId = get ( activeTabIdAtom ) ;
58+ if ( ! executeTabId ) {
59+ toast . error ( "No active tab" ) ;
60+ return ;
61+ }
62+
63+ // Get the tab data at the time of execution
64+ const getTabById = get ( getTabByIdAtom ) ;
65+ const executeTab = getTabById ( executeTabId ) ;
66+ if ( ! executeTab ) {
67+ toast . error ( "Tab not found" ) ;
68+ return ;
69+ }
70+
71+ // Use the tab's data at execution time
72+ const query = executeTab . query ;
73+ const selectedDb = executeTab . database ;
74+ const selectedTable = executeTab . table ;
75+ const selectedTimeField = executeTab . timeField ;
76+ const timeRange = executeTab . timeRange ;
5677 const tableSchema = get ( tableSchemaAtom ) ;
5778 const apiUrl = get ( apiUrlAtom ) ;
5879
@@ -66,9 +87,13 @@ export const executeQueryAtom = atom(null, async (get, set) => {
6687 return ;
6788 }
6889
69- set ( queryLoadingAtom , true ) ;
70- set ( queryErrorAtom , null ) ;
71- set ( queryResultsAtom , null ) ; // Clear previous results
90+ // Update loading state for the specific tab
91+ set ( updateTabQueryLoadingByIdAtom , { tabId : executeTabId , loading : true } ) ;
92+ set ( updateTabQueryErrorByIdAtom , { tabId : executeTabId , error : null } ) ;
93+ set ( updateTabQueryResultsByIdAtom , { tabId : executeTabId , results : null } ) ; // Clear previous results
94+
95+ // Mark this tab as having a running query
96+ set ( addRunningQueryAtom , executeTabId ) ;
7297
7398 const startTime = Date . now ( ) ;
7499
@@ -112,13 +137,13 @@ export const executeQueryAtom = atom(null, async (get, set) => {
112137 } ) ;
113138
114139 if ( processed . errors . length > 0 ) {
115- set ( queryErrorAtom , processed . errors . join ( "; " ) ) ;
140+ set ( updateTabQueryErrorByIdAtom , { tabId : executeTabId , error : processed . errors . join ( "; " ) } ) ;
116141 toast . error ( `Query processing error: ${ processed . errors [ 0 ] } ` ) ;
117142 return ;
118143 }
119144
120145 processedQuery = processed . query ;
121- set ( processedQueryAtom , processedQuery ) ;
146+ set ( updateTabProcessedQueryByIdAtom , { tabId : executeTabId , query : processedQuery } ) ;
122147 }
123148
124149 // Execute the processed query
@@ -176,23 +201,25 @@ export const executeQueryAtom = atom(null, async (get, set) => {
176201 }
177202
178203 // Store raw response for Raw tab
179- set ( rawQueryResponseAtom , rawResponse ) ;
204+ set ( updateTabRawResponseByIdAtom , { tabId : executeTabId , response : rawResponse } ) ;
180205
181206 const executionTime = Date . now ( ) - startTime ;
182- set ( queryResultsAtom , parsedResults ) ;
183- set ( queryExecutionTimeAtom , executionTime ) ;
207+ set ( updateTabQueryResultsByIdAtom , { tabId : executeTabId , results : parsedResults } ) ;
184208
185209 // Calculate size properly based on the raw response
186210 const responseSize =
187211 typeof result === "string"
188212 ? result . length
189213 : JSON . stringify ( result ) . length ;
190214
191- set ( queryMetricsAtom , {
192- executionTime,
193- rowCount : parsedResults . length ,
194- size : responseSize ,
195- processedRows : parsedResults . length ,
215+ set ( updateTabQueryMetricsByIdAtom , {
216+ tabId : executeTabId ,
217+ metrics : {
218+ executionTime,
219+ rowCount : parsedResults . length ,
220+ size : responseSize ,
221+ processedRows : parsedResults . length ,
222+ }
196223 } ) ;
197224
198225 // Add to history with full context
@@ -211,8 +238,8 @@ export const executeQueryAtom = atom(null, async (get, set) => {
211238 rowCount : parsedResults . length ,
212239 } ;
213240
214- // Add to current tab's history
215- set ( addToTabQueryHistoryAtom , historyItem ) ;
241+ // Add to the specific tab's history
242+ set ( addToTabQueryHistoryByIdAtom , { tabId : executeTabId , historyItem } ) ;
216243 } catch ( error : any ) {
217244 let errorMessage = "Query failed" ;
218245
@@ -245,8 +272,8 @@ export const executeQueryAtom = atom(null, async (get, set) => {
245272 errorMessage = error . message ;
246273 }
247274
248- set ( queryErrorAtom , errorMessage ) ;
249- set ( queryResultsAtom , null ) ; // Clear results on error
275+ set ( updateTabQueryErrorByIdAtom , { tabId : executeTabId , error : errorMessage } ) ;
276+ set ( updateTabQueryResultsByIdAtom , { tabId : executeTabId , results : null } ) ; // Clear results on error
250277
251278 // Add failed query to history with full context
252279 const historyItem : QueryHistoryItem = {
@@ -262,10 +289,14 @@ export const executeQueryAtom = atom(null, async (get, set) => {
262289 error : errorMessage ,
263290 } ;
264291
265- // Add to current tab's history
266- set ( addToTabQueryHistoryAtom , historyItem ) ;
292+ // Add to the specific tab's history
293+ set ( addToTabQueryHistoryByIdAtom , { tabId : executeTabId , historyItem } ) ;
267294 } finally {
268- set ( queryLoadingAtom , false ) ;
295+ // Update loading state for the specific tab
296+ set ( updateTabQueryLoadingByIdAtom , { tabId : executeTabId , loading : false } ) ;
297+
298+ // Remove this tab from running queries
299+ set ( removeRunningQueryAtom , executeTabId ) ;
269300 }
270301} ) ;
271302
0 commit comments