@@ -164,19 +164,28 @@ export function toggleFolder(id: string, schema: string, folder: string) {
164164 } )
165165}
166166
167- export function addTableToGroup ( id : string , schema : string , folder : string , table : string ) {
167+ export function addTableToFolder ( id : string , schema : string , folder : string , table : string ) {
168168 const store = databaseStore ( id )
169169 store . setState ( ( state ) => {
170- const existingGroup = state . tableGroups . find ( g => g . schema === schema && g . folder === folder )
170+ // First, remove the table from any other folder in the same schema
171+ const updatedFolders = state . tableFolders
172+ . map ( g =>
173+ g . schema === schema && g . tables . includes ( table )
174+ ? { ...g , tables : g . tables . filter ( t => t !== table ) }
175+ : g ,
176+ )
177+ . filter ( g => g . tables . length > 0 )
178+
179+ const existingFolder = updatedFolders . find ( g => g . schema === schema && g . folder === folder )
171180
172- if ( existingGroup ) {
173- if ( existingGroup . tables . includes ( table ) ) {
181+ if ( existingFolder ) {
182+ if ( existingFolder . tables . includes ( table ) ) {
174183 return state
175184 }
176185
177186 return {
178187 ...state ,
179- tableGroups : state . tableGroups . map ( g =>
188+ tableFolders : updatedFolders . map ( g =>
180189 g . schema === schema && g . folder === folder
181190 ? { ...g , tables : [ ...g . tables , table ] }
182191 : g ,
@@ -186,16 +195,16 @@ export function addTableToGroup(id: string, schema: string, folder: string, tabl
186195
187196 return {
188197 ...state ,
189- tableGroups : [ ...state . tableGroups , { schema, folder, tables : [ table ] } ] ,
198+ tableFolders : [ ...updatedFolders , { schema, folder, tables : [ table ] } ] ,
190199 } satisfies typeof state
191200 } )
192201}
193202
194- export function removeTableFromGroup ( id : string , schema : string , folder : string , table : string ) {
203+ export function removeTableFromFolder ( id : string , schema : string , folder : string , table : string ) {
195204 const store = databaseStore ( id )
196205 store . setState ( state => ( {
197206 ...state ,
198- tableGroups : state . tableGroups
207+ tableFolders : state . tableFolders
199208 . map ( g =>
200209 g . schema === schema && g . folder === folder
201210 ? { ...g , tables : g . tables . filter ( t => t !== table ) }
@@ -205,11 +214,22 @@ export function removeTableFromGroup(id: string, schema: string, folder: string,
205214 } satisfies typeof state ) )
206215}
207216
208- export function renameGroup ( id : string , schema : string , oldFolder : string , newFolder : string ) {
217+ export function folderExists ( id : string , schema : string , folder : string ) : boolean {
209218 const store = databaseStore ( id )
219+ return store . state . tableFolders . some ( g => g . schema === schema && g . folder === folder )
220+ }
221+
222+ export function renameFolder ( id : string , schema : string , oldFolder : string , newFolder : string ) : boolean {
223+ const store = databaseStore ( id )
224+
225+ // Check if a folder with the new name already exists
226+ if ( folderExists ( id , schema , newFolder ) ) {
227+ return false
228+ }
229+
210230 store . setState ( state => ( {
211231 ...state ,
212- tableGroups : state . tableGroups . map ( g =>
232+ tableFolders : state . tableFolders . map ( g =>
213233 g . schema === schema && g . folder === oldFolder
214234 ? { ...g , folder : newFolder }
215235 : g ,
@@ -220,13 +240,15 @@ export function renameGroup(id: string, schema: string, oldFolder: string, newFo
220240 : f ,
221241 ) ,
222242 } satisfies typeof state ) )
243+
244+ return true
223245}
224246
225- export function deleteGroup ( id : string , schema : string , folder : string ) {
247+ export function deleteFolder ( id : string , schema : string , folder : string ) {
226248 const store = databaseStore ( id )
227249 store . setState ( state => ( {
228250 ...state ,
229- tableGroups : state . tableGroups . filter ( g => ! ( g . schema === schema && g . folder === folder ) ) ,
251+ tableFolders : state . tableFolders . filter ( g => ! ( g . schema === schema && g . folder === folder ) ) ,
230252 tablesTreeOpenedFolders : state . tablesTreeOpenedFolders . filter ( f => ! ( f . schema === schema && f . folder === folder ) ) ,
231253 } satisfies typeof state ) )
232254}
@@ -262,20 +284,32 @@ export function selectMultipleTables(id: string, tables: { schema: string, table
262284 } satisfies typeof state ) )
263285}
264286
265- export function addMultipleTablesToGroup ( id : string , schema : string , folder : string , tables : string [ ] ) {
287+ export function addMultipleTablesToFolder ( id : string , schema : string , folder : string , tables : string [ ] ) {
266288 const store = databaseStore ( id )
267289 store . setState ( ( state ) => {
268- const existingGroup = state . tableGroups . find ( g => g . schema === schema && g . folder === folder )
290+ // First, remove all these tables from any other folder in the same schema
291+ const updatedFolders = state . tableFolders
292+ . map ( g =>
293+ g . schema === schema
294+ ? { ...g , tables : g . tables . filter ( t => ! tables . includes ( t ) ) }
295+ : g ,
296+ )
297+ . filter ( g => g . tables . length > 0 )
269298
270- if ( existingGroup ) {
271- const newTables = tables . filter ( t => ! existingGroup . tables . includes ( t ) )
299+ const existingFolder = updatedFolders . find ( g => g . schema === schema && g . folder === folder )
300+
301+ if ( existingFolder ) {
302+ const newTables = tables . filter ( t => ! existingFolder . tables . includes ( t ) )
272303 if ( newTables . length === 0 ) {
273- return state
304+ return {
305+ ...state ,
306+ tableFolders : updatedFolders ,
307+ } satisfies typeof state
274308 }
275309
276310 return {
277311 ...state ,
278- tableGroups : state . tableGroups . map ( g =>
312+ tableFolders : updatedFolders . map ( g =>
279313 g . schema === schema && g . folder === folder
280314 ? { ...g , tables : [ ...g . tables , ...newTables ] }
281315 : g ,
@@ -285,7 +319,7 @@ export function addMultipleTablesToGroup(id: string, schema: string, folder: str
285319
286320 return {
287321 ...state ,
288- tableGroups : [ ...state . tableGroups , { schema, folder, tables } ] ,
322+ tableFolders : [ ...updatedFolders , { schema, folder, tables } ] ,
289323 } satisfies typeof state
290324 } )
291325}
0 commit comments