@@ -122,6 +122,7 @@ function getTabHash(values: multitabEditorLogicType['values']): Record<string, a
122122 return hash
123123}
124124
125+ // Misnomer now: this logic is responsible for the state of one sql editor tab
125126export const multitabEditorLogic = kea < multitabEditorLogicType > ( [
126127 path ( [ 'data-warehouse' , 'editor' , 'multitabEditorLogic' ] ) ,
127128 props ( { } as MultitabEditorLogicProps ) ,
@@ -166,7 +167,6 @@ export const multitabEditorLogic = kea<multitabEditorLogicType>([
166167 } ) ,
167168 setActiveQuery : ( query : string ) => ( { query } ) ,
168169
169- setTabs : ( tabs : QueryTab [ ] ) => ( { tabs } ) ,
170170 createTab : (
171171 query ?: string ,
172172 view ?: DataWarehouseSavedQuery ,
@@ -290,11 +290,10 @@ export const multitabEditorLogic = kea<multitabEditorLogicType>([
290290 updateTab : ( _ , { tab } ) => tab . insight ?? null ,
291291 } ,
292292 ] ,
293- allTabs : [
294- [ ] as QueryTab [ ] ,
293+ activeTab : [
294+ null as QueryTab | null ,
295295 {
296- updateTab : ( _ , { tab } ) => [ tab ] ,
297- setTabs : ( _ , { tabs } ) => tabs ,
296+ updateTab : ( _ , { tab } ) => tab ,
298297 } ,
299298 ] ,
300299 error : [
@@ -625,17 +624,15 @@ export const multitabEditorLogic = kea<multitabEditorLogicType>([
625624 }
626625 }
627626 } ,
628- saveDraft : async ( { activeTab, queryInput, viewId } ) => {
629- const latestActiveTab = values . allTabs . find ( ( tab ) => tab . uri . toString ( ) === activeTab . uri . toString ( ) )
630-
631- if ( latestActiveTab ) {
627+ saveDraft : async ( { queryInput, viewId } ) => {
628+ if ( values . activeTab ) {
632629 actions . saveAsDraft (
633630 {
634631 kind : NodeKind . HogQLQuery ,
635632 query : queryInput ,
636633 } ,
637634 viewId ,
638- latestActiveTab
635+ values . activeTab
639636 )
640637 }
641638 } ,
@@ -814,34 +811,30 @@ export const multitabEditorLogic = kea<multitabEditorLogicType>([
814811 router . actions . push ( urls . insightView ( savedInsight . short_id ) )
815812 } ,
816813 loadDataWarehouseSavedQueriesSuccess : ( { dataWarehouseSavedQueries } ) => {
817- // keep tab views up to date
818- const tab = values . activeTab
819- const view = dataWarehouseSavedQueries . find ( ( v ) => v . id === tab . view ?. id )
820- if ( tab && view ) {
821- actions . setTabs ( [ { ... tab , view } ] )
822- actions . setQueryInput ( view . query . query || '' )
814+ if ( values . activeTab ?. view ) {
815+ const view = dataWarehouseSavedQueries . find ( ( v ) => v . id === values . activeTab ?. view ?. id )
816+ if ( view && values . activeTab ) {
817+ actions . updateTab ( { ... values . activeTab , view } )
818+ actions . setQueryInput ( view . query . query || '' )
819+ }
823820 }
824821 } ,
825822 deleteDataWarehouseSavedQuerySuccess : ( { payload : viewId } ) => {
826- const mustRemoveTab = values . allTabs . find ( ( tab ) => tab . view ?. id === viewId && ! tab . draft )
827- if ( mustRemoveTab ) {
828- actions . setTabs ( [ ] )
823+ if ( values . activeTab ?. view ?. id === viewId && ! values . activeTab ?. draft ) {
829824 actions . createTab ( )
830825 }
831- lemonToast . success ( 'View deleted' )
832826 } ,
833827 createDataWarehouseSavedQuerySuccess : ( { dataWarehouseSavedQueries, payload : view } ) => {
834828 const newView = view && dataWarehouseSavedQueries . find ( ( v ) => v . name === view . name )
835829 if ( newView ) {
836830 const oldTab = values . activeTab
837- if ( oldTab ) {
831+ // Only update the tab if it doesn't have a view (new query being saved)
832+ // or if it's the same view being recreated (edge case)
833+ if ( oldTab && ( ! oldTab . view || oldTab . view . id === newView . id ) ) {
838834 actions . updateTab ( { ...oldTab , view : newView } )
839835 }
840836 }
841837 } ,
842- updateDataWarehouseSavedQuerySuccess : ( ) => {
843- lemonToast . success ( 'View updated' )
844- } ,
845838 updateView : async ( { view, draftId } ) => {
846839 const latestView = await api . dataWarehouseSavedQueries . get ( view . id )
847840 // Only check for conflicts if there's an activity log (latest_history_id exists)
@@ -880,13 +873,13 @@ export const multitabEditorLogic = kea<multitabEditorLogicType>([
880873 }
881874 } ,
882875 deleteDraftSuccess : ( { draftId, viewName } ) => {
883- // remove draft from all tabs
884- const newTabs = values . allTabs . map ( ( tab ) => ( {
885- ...tab ,
886- draft : tab . draft ?. id === draftId ? undefined : tab . draft ,
887- name : tab . draft ?. id === draftId && viewName ? viewName : tab . name ,
888- } ) )
889- actions . setTabs ( newTabs )
876+ if ( values . activeTab && values . activeTab . draft ?. id === draftId ) {
877+ actions . updateTab ( {
878+ ...values . activeTab ,
879+ draft : undefined ,
880+ name : viewName ?? values . activeTab . name ,
881+ } )
882+ }
890883 } ,
891884 } ) ) ,
892885 subscriptions ( ( { actions, values } ) => ( {
@@ -921,20 +914,21 @@ export const multitabEditorLogic = kea<multitabEditorLogicType>([
921914 }
922915 } ,
923916 drafts : ( drafts ) => {
924- // update all drafts in all tabs
925- const newTabs = values . allTabs . map ( ( tab ) => ( {
926- ...tab ,
927- draft : drafts . find ( ( d : DataWarehouseSavedQueryDraft ) => d . id === tab . draft ?. id ) ,
928- name :
929- drafts . find ( ( d : DataWarehouseSavedQueryDraft ) => d . id === tab . draft ?. id ) ?. name ??
930- tab . view ?. name ??
931- tab . name ,
932- } ) )
933- actions . setTabs ( newTabs )
917+ if ( values . activeTab && values . activeTab . draft ) {
918+ const updatedDraft = drafts . find (
919+ ( d : DataWarehouseSavedQueryDraft ) => d . id === values . activeTab ?. draft ?. id
920+ )
921+ if ( updatedDraft ) {
922+ actions . updateTab ( {
923+ ...values . activeTab ,
924+ draft : updatedDraft ,
925+ name : updatedDraft . name ?? values . activeTab . view ?. name ?? values . activeTab . name ,
926+ } )
927+ }
928+ }
934929 } ,
935930 } ) ) ,
936931 selectors ( {
937- activeTab : [ ( s ) => [ s . allTabs ] , ( allTabs : QueryTab [ ] ) => allTabs ?. [ 0 ] ?? null ] ,
938932 suggestedSource : [
939933 ( s ) => [ s . suggestionPayload ] ,
940934 ( suggestionPayload ) => {
@@ -1130,9 +1124,7 @@ export const multitabEditorLogic = kea<multitabEditorLogicType>([
11301124 return
11311125 }
11321126
1133- const existingTab = values . allTabs . find ( ( tab ) => {
1134- return tab . draft ?. id === draft . id
1135- } )
1127+ const existingTab = values . activeTab ?. draft ?. id === draft . id ? values . activeTab : null
11361128
11371129 if ( ! existingTab ) {
11381130 const associatedView = draft . saved_query_id
@@ -1141,9 +1133,8 @@ export const multitabEditorLogic = kea<multitabEditorLogicType>([
11411133
11421134 actions . createTab ( draft . query . query , associatedView , undefined , draft )
11431135
1144- const newTab = values . allTabs [ values . allTabs . length - 1 ]
1145- if ( newTab ) {
1146- actions . setTabDraftId ( newTab . uri . toString ( ) , draft . id )
1136+ if ( values . activeTab ) {
1137+ actions . setTabDraftId ( values . activeTab . uri . toString ( ) , draft . id )
11471138 }
11481139 }
11491140 return
0 commit comments