11<script setup lang="ts">
2- import { computed , h , nextTick , ref , resolveComponent , watch } from ' vue' ;
2+ import { computed , h , nextTick , ref , resolveComponent , watch , watchEffect } from ' vue' ;
33import { useMutation } from ' @vue/apollo-composable' ;
44
55import BaseTreeTable from ' @/components/Common/BaseTreeTable.vue' ;
@@ -260,8 +260,7 @@ const { visibleFolders, expandedFolders, toggleExpandFolder, setExpandedFolders
260260});
261261const busyRowIds = ref <Set <string >>(new Set ());
262262
263- const { loadColumnVisibility, mergeServerPreferences, saveColumnVisibility } =
264- useDockerViewPreferences ();
263+ const { mergeServerPreferences, saveColumnVisibility, columnVisibilityRef } = useDockerViewPreferences ();
265264
266265function setRowsBusy(ids : string [], busy : boolean ) {
267266 const next = new Set (busyRowIds .value );
@@ -467,7 +466,7 @@ const columns = computed<TableColumn<TreeRow<DockerContainer>>[]>(() => {
467466 return cols ;
468467});
469468
470- const hasAppliedPreferences = ref (false );
469+ const isApplyingColumnVisibility = ref (false );
471470
472471function getDefaultColumnVisibility(isCompact : boolean ): Record <string , boolean > {
473472 if (isCompact ) {
@@ -498,38 +497,53 @@ function getDefaultColumnVisibility(isCompact: boolean): Record<string, boolean>
498497 }
499498}
500499
501- function applyColumnVisibilityPreferences() {
502- if (! baseTableRef .value ?.columnVisibility ) return ;
503- if (hasAppliedPreferences .value ) return ;
500+ const defaultColumnVisibility = computed (() => getDefaultColumnVisibility (props .compact ));
504501
505- const savedPrefs = loadColumnVisibility ();
506- const defaultPrefs = getDefaultColumnVisibility (props .compact );
502+ const resolvedColumnVisibility = computed <Record <string , boolean >>(() => {
503+ const saved = columnVisibilityRef .value ;
504+ return {
505+ ... defaultColumnVisibility .value ,
506+ ... (saved ?? {}),
507+ };
508+ });
507509
508- baseTableRef .value .columnVisibility .value = savedPrefs || defaultPrefs ;
509- hasAppliedPreferences .value = true ;
510+ function visibilityStatesEqual(a ? : Record <string , boolean >, b ? : Record <string , boolean >): boolean {
511+ if (a === b ) return true ;
512+ if (! a || ! b ) return false ;
513+ const aKeys = Object .keys (a );
514+ const bKeys = Object .keys (b );
515+ if (aKeys .length !== bKeys .length ) return false ;
516+ for (const key of aKeys ) {
517+ if (a [key ] !== b [key ]) {
518+ return false ;
519+ }
520+ }
521+ return true ;
510522}
511523
512- watch (
513- baseTableRef ,
514- (ref ) => {
515- if (ref ?.columnVisibility && ! hasAppliedPreferences .value ) {
516- applyColumnVisibilityPreferences ();
517- }
518- },
519- { immediate: true , flush: ' post' }
520- );
524+ watchEffect (() => {
525+ const tableVisibility = baseTableRef .value ?.columnVisibility ;
526+ if (! tableVisibility ) return ;
527+
528+ const target = resolvedColumnVisibility .value ;
529+ const current = tableVisibility .value || {};
530+
531+ if (visibilityStatesEqual (current , target )) {
532+ return ;
533+ }
534+
535+ isApplyingColumnVisibility .value = true ;
536+ tableVisibility .value = { ... target };
537+ nextTick (() => {
538+ isApplyingColumnVisibility .value = false ;
539+ });
540+ });
521541
522542watch (
523543 () => props .viewPrefs ,
524544 (prefs ) => {
525- if (prefs && hasAppliedPreferences . value ) {
545+ if (prefs ) {
526546 mergeServerPreferences (prefs );
527- if (baseTableRef .value ?.columnVisibility ) {
528- const savedPrefs = loadColumnVisibility ();
529- if (savedPrefs ) {
530- baseTableRef .value .columnVisibility .value = savedPrefs ;
531- }
532- }
533547 }
534548 },
535549 { immediate: true }
@@ -538,9 +552,10 @@ watch(
538552watch (
539553 () => baseTableRef .value ?.columnVisibility ?.value ,
540554 (columnVisibility ) => {
541- if (columnVisibility && hasAppliedPreferences . value && ! props . compact ) {
542- saveColumnVisibility ( columnVisibility ) ;
555+ if (! columnVisibility || props . compact || isApplyingColumnVisibility . value ) {
556+ return ;
543557 }
558+ saveColumnVisibility ({ ... columnVisibility });
544559 },
545560 { deep: true }
546561);
0 commit comments