@@ -36,6 +36,7 @@ import { getTableTypes } from 'src/app/lib/setup-table-row-structure';
3636import {
3737 CustomAction ,
3838 TableForeignKey ,
39+ TableOrdering ,
3940 TablePermissions ,
4041 TableProperties ,
4142 TableRow ,
@@ -44,7 +45,6 @@ import {
4445import { NotificationsService } from 'src/app/services/notifications.service' ;
4546import { TableRowService } from 'src/app/services/table-row.service' ;
4647import { TableStateService } from 'src/app/services/table-state.service' ;
47- import { UiSettingsService } from 'src/app/services/ui-settings.service' ;
4848import { tableDisplayTypes , UIwidgets } from '../../../consts/table-display-types' ;
4949import { normalizeTableName } from '../../../lib/normalize' ;
5050import { PlaceholderTableDataComponent } from '../../skeletons/placeholder-table-data/placeholder-table-data.component' ;
@@ -164,7 +164,6 @@ export class DbTableViewComponent implements OnInit {
164164 private _notifications : NotificationsService ,
165165 private _tableRow : TableRowService ,
166166 private _connections : ConnectionsService ,
167- private _uiSettings : UiSettingsService ,
168167 private _tables : TablesService ,
169168 private route : ActivatedRoute ,
170169 public router : Router ,
@@ -175,16 +174,42 @@ export class DbTableViewComponent implements OnInit {
175174 ngAfterViewInit ( ) {
176175 this . tableData . paginator = this . paginator ;
177176
178- this . tableData . sort = this . sort ;
177+ // Check if sort params exist in URL
178+ const urlSortActive = this . route . snapshot . queryParams . sort_active ;
179+ const urlSortDirection = this . route . snapshot . queryParams . sort_direction ;
179180
180- // Load default sort from settings
181- this . loadDefaultSort ( ) ;
181+ let sortInitialized = false ;
182182
183- // Initialize sort - default sort takes priority
184- if ( this . defaultSort ) {
185- this . sort . active = this . defaultSort . column ;
186- this . sort . direction = this . defaultSort . direction ;
187- }
183+ // Subscribe to loading state to initialize default sort after data is loaded
184+ this . tableData . loading$ . subscribe ( ( loading : boolean ) => {
185+ console . log ( 'loading$ changed:' , loading , 'sort.active:' , this . sort ?. active , 'sort.direction:' , this . sort ?. direction ) ;
186+
187+ if ( ! loading && this . tableData . defaultSort !== undefined ) {
188+ // Update defaultSort reference whenever data loads
189+ this . defaultSort = this . tableData . defaultSort ;
190+
191+ console . log ( 'DbTableViewComponent tableData loaded:' , this . tableData ) ;
192+ console . log ( 'DbTableViewComponent tableData.defaultSort loaded:' , this . tableData . defaultSort ) ;
193+
194+ // Only initialize sort on first load
195+ if ( ! sortInitialized ) {
196+ sortInitialized = true ;
197+
198+ // Initialize sort based on priority: URL params > default sort
199+ if ( urlSortActive && urlSortDirection ) {
200+ // Use sort from URL
201+ this . sort . active = urlSortActive ;
202+ this . sort . direction = urlSortDirection . toLowerCase ( ) as 'asc' | 'desc' ;
203+ } else if ( this . defaultSort ) {
204+ // Use default sort if no URL params
205+ this . sort . active = this . defaultSort . column ;
206+ this . sort . direction = this . defaultSort . direction ;
207+ }
208+ }
209+
210+ console . log ( 'After loading complete - sort.active:' , this . sort ?. active , 'sort.direction:' , this . sort ?. direction ) ;
211+ }
212+ } ) ;
188213
189214 merge ( this . sort . sortChange , this . paginator . page )
190215 . pipe (
@@ -272,7 +297,6 @@ export class DbTableViewComponent implements OnInit {
272297 // If this column was the default, remove the default too
273298 if ( this . defaultSort ?. column === column ) {
274299 this . defaultSort = null ;
275- this . _uiSettings . updateTableSetting ( this . connectionID , this . name , 'defaultSort' , null ) ;
276300 }
277301 // Clear sort
278302 this . sort . active = '' ;
@@ -285,23 +309,36 @@ export class DbTableViewComponent implements OnInit {
285309 }
286310 }
287311
288- loadDefaultSort ( ) {
289- const tableSettings = this . _uiSettings . settings ?. connections ?. [ this . connectionID ] ?. tables ?. [ this . name ] ;
290- if ( tableSettings ?. defaultSort ) {
291- this . defaultSort = tableSettings . defaultSort ;
292- }
293- }
294-
295312 toggleDefaultSort ( column : string ) {
296313 if ( this . isDefaultSort ( column ) ) {
297314 // Remove default sort
298315 this . defaultSort = null ;
299- this . _uiSettings . updateTableSetting ( this . connectionID , this . name , 'defaultSort' , null ) ;
316+ this . _tables . updatePersonalTableViewSettings ( this . connectionID , this . name , {
317+ ordering : null ,
318+ ordering_field : null ,
319+ } ) . subscribe ( {
320+ next : ( ) => {
321+ console . log ( 'Personal table view settings updated - default sort removed' ) ;
322+ } ,
323+ error : ( error ) => {
324+ console . error ( 'Error updating personal table view settings:' , error ) ;
325+ }
326+ } ) ;
300327 } else {
301328 // Set current sort as default
302329 const direction = this . sort . active === column ? this . sort . direction : 'asc' ;
303330 this . defaultSort = { column, direction : direction as 'asc' | 'desc' } ;
304- this . _uiSettings . updateTableSetting ( this . connectionID , this . name , 'defaultSort' , this . defaultSort ) ;
331+ this . _tables . updatePersonalTableViewSettings ( this . connectionID , this . name , {
332+ ordering : this . sort . direction === 'asc' ? TableOrdering . Ascending : TableOrdering . Descending ,
333+ ordering_field : column ,
334+ } ) . subscribe ( {
335+ next : ( ) => {
336+ console . log ( 'Personal table view settings updated - default sort removed' ) ;
337+ } ,
338+ error : ( error ) => {
339+ console . error ( 'Error updating personal table view settings:' , error ) ;
340+ }
341+ } ) ;
305342 }
306343 }
307344
0 commit comments