11import { type LineageColumn } from '@api/client'
22import { type TableColumn , type TableRow } from '@components/table/help'
3- import { uid } from '@utils/index'
3+ import { isFalse , isNil , isNotNil , uid } from '@utils/index'
44import { create } from 'zustand'
55import useLocalStorage from '~/hooks/useLocalStorage'
66import { type ErrorKey , type ErrorIDE } from '~/library/pages/ide/context'
@@ -17,9 +17,14 @@ export interface Lineage {
1717 columns ?: Record < string , LineageColumn >
1818}
1919
20+ export interface StoredTab {
21+ id ?: ID
22+ content ?: string
23+ }
24+
2025interface EditorStore {
21- storedTabsId ?: ID
22- storedTabsIds : ID [ ]
26+ storedTabId ?: ID
27+ storedTabs : StoredTab [ ]
2328 tabs : Map < ModelFile , EditorTab >
2429 tab ?: EditorTab
2530 engine : Worker
@@ -34,6 +39,7 @@ interface EditorStore {
3439 replaceTab : ( from : EditorTab , to : EditorTab ) => void
3540 updateStoredTabsIds : ( ) => void
3641 addTab : ( tab : EditorTab ) => void
42+ addTabs : ( tabs : EditorTab [ ] ) => void
3743 closeTab : ( file : ModelFile ) => void
3844 createTab : ( file ?: ModelFile ) => EditorTab
3945 setDialects : ( dialects : Dialect [ ] ) => void
@@ -65,17 +71,23 @@ export interface EditorTab {
6571 el ?: HTMLElement
6672}
6773
68- const [ getStoredTabs , setStoredTabs ] = useLocalStorage < { ids : ID [ ] ; id : ID } > (
69- 'tabs' ,
70- )
74+ const [ getStoredTabs , setStoredTabs ] = useLocalStorage < {
75+ tabs : StoredTab [ ]
76+ id ?: ID
77+ } > ( 'tabs' )
7178
79+ const { tabs : storedTabs = [ ] , id : storedTabId } = getStoredTabs ( ) ?? { }
7280const initialFile = createLocalFile ( )
7381const initialTab : EditorTab = createTab ( initialFile )
74- const initialTabs = new Map ( [ [ initialFile , initialTab ] ] )
82+ const initialTabs = new Map (
83+ storedTabs . length > 0 && isNotNil ( storedTabId )
84+ ? [ ]
85+ : [ [ initialFile , initialTab ] ] ,
86+ )
7587
7688export const useStoreEditor = create < EditorStore > ( ( set , get ) => ( {
77- storedTabsIds : getStoredTabs ( ) ?. ids ?? [ ] ,
78- storedTabsId : getStoredTabs ( ) ?. id ,
89+ storedTabs ,
90+ storedTabId ,
7991 tab : initialTab ,
8092 tabs : initialTabs ,
8193 engine : sqlglotWorker ,
@@ -101,19 +113,43 @@ export const useStoreEditor = create<EditorStore>((set, get) => ({
101113 } ,
102114 updateStoredTabsIds ( ) {
103115 const s = get ( )
104- const id = s . tab ?. file . id
105- const ids = Array . from ( get ( ) . tabs . values ( ) )
106- . filter ( tab => tab . file . isRemote )
107- . map ( tab => tab . file . id )
116+
117+ if ( isNil ( s . tab ) ) {
118+ setStoredTabs ( {
119+ id : undefined ,
120+ tabs : [ ] ,
121+ } )
122+
123+ set ( ( ) => ( {
124+ storedTabId : undefined ,
125+ storedTabs : [ ] ,
126+ } ) )
127+
128+ return
129+ }
130+
131+ const tabs : StoredTab [ ] = [ ]
132+
133+ for ( const tab of s . tabs . values ( ) ) {
134+ if ( isFalse ( tab . file . isChanged ) && tab . file . isLocal ) continue
135+
136+ tabs . push ( {
137+ id : tab . file . id ,
138+ content : tab . file . isChanged ? tab . file . content : undefined ,
139+ } )
140+ }
141+
142+ const id =
143+ s . tab . file . isChanged || s . tab . file . isRemote ? s . tab . file . id : undefined
108144
109145 setStoredTabs ( {
110146 id,
111- ids ,
147+ tabs ,
112148 } )
113149
114150 set ( ( ) => ( {
115- storedTabsId : id ,
116- storedTabsIds : ids ,
151+ storedTabId : id ,
152+ storedTabs : tabs ,
117153 } ) )
118154 } ,
119155 refreshTab ( ) {
@@ -129,7 +165,22 @@ export const useStoreEditor = create<EditorStore>((set, get) => ({
129165 } ) )
130166 } ,
131167 selectTab ( tab ) {
168+ const s = get ( )
169+
132170 set ( ( ) => ( { tab } ) )
171+
172+ s . updateStoredTabsIds ( )
173+ } ,
174+ addTabs ( tabs ) {
175+ const s = get ( )
176+
177+ for ( const tab of tabs ) {
178+ s . tabs . set ( tab . file , tab )
179+ }
180+
181+ set ( ( ) => ( {
182+ tabs : new Map ( s . tabs ) ,
183+ } ) )
133184 } ,
134185 addTab ( tab ) {
135186 const s = get ( )
@@ -204,8 +255,9 @@ function createTab(file: ModelFile = createLocalFile()): EditorTab {
204255 }
205256}
206257
207- function createLocalFile ( ) : ModelFile {
258+ export function createLocalFile ( id ?: ID ) : ModelFile {
208259 return new ModelFile ( {
260+ id,
209261 name : '' ,
210262 path : '' ,
211263 content :
0 commit comments