@@ -33,7 +33,8 @@ export default class CustomFieldsManagerComponent extends Component {
3333 super ( ...arguments ) ;
3434 this . subjects = subjects ?? [ ] ;
3535 next ( ( ) => {
36- if ( ! this . subjects ) return ;
36+ if ( ! this . subjects || this . subjects . length === 0 ) return ;
37+ // Load the first subject immediately
3738 this . loadCustomFields . perform ( this . subjects [ 0 ] ) ;
3839 } ) ;
3940 }
@@ -51,7 +52,44 @@ export default class CustomFieldsManagerComponent extends Component {
5152 }
5253 }
5354
55+ /**
56+ * Restore custom fields from cache for subjects that haven't been loaded yet
57+ * This method checks the CustomFieldsRegistry service cache and restores
58+ * previously loaded data to avoid losing it during navigation
59+ */
60+ async restoreFromCache ( ) {
61+ if ( ! this . subjects || this . subjects . length <= 1 ) return ;
62+
63+ // Skip the first subject as it's loaded in constructor
64+ const subjectsToRestore = this . subjects . slice ( 1 ) ;
65+
66+ for ( const subject of subjectsToRestore ) {
67+ try {
68+ const company = await this . currentUser . loadCompany ( ) ;
69+ const loadOptions = {
70+ groupedFor : `${ underscore ( subject . model ) } _custom_field_group` ,
71+ fieldFor : subject . type ,
72+ } ;
73+
74+ // Check if we have a cached manager for this subject
75+ const cachedManager = this . customFieldsRegistry . forSubject ( company , { loadOptions } ) ;
76+
77+ // Only restore if we have cached groups data
78+ if ( cachedManager && cachedManager . groups && cachedManager . groups . length > 0 ) {
79+ this . #updateSubject( subject , ( s ) => {
80+ return { ...s , groups : cachedManager . groups } ;
81+ } ) ;
82+ }
83+ } catch ( err ) {
84+ // Silently continue if cache restore fails for a subject
85+ console . warn ( `Failed to restore cache for subject ${ subject . model } :` , err ) ;
86+ }
87+ }
88+ }
89+
5490 @task * loadCustomFields ( subject ) {
91+ if ( ! subject ) return ;
92+
5593 try {
5694 const company = yield this . currentUser . loadCompany ( ) ;
5795 const customFieldsManager = yield this . customFieldsRegistry . loadSubjectCustomFields . perform ( company , {
@@ -60,12 +98,14 @@ export default class CustomFieldsManagerComponent extends Component {
6098 fieldFor : subject . type ,
6199 } ,
62100 } ) ;
101+
63102 this . #updateSubject( subject , ( s ) => {
64103 return { ...s , groups : customFieldsManager . customFieldGroups } ;
65104 } ) ;
66105
67106 return customFieldsManager ;
68107 } catch ( err ) {
108+ console . error ( `❌ Failed to load custom fields for ${ subject . model } :` , err ) ;
69109 this . notifications . serverError ( err ) ;
70110 }
71111 }
@@ -127,7 +167,7 @@ export default class CustomFieldsManagerComponent extends Component {
127167
128168 try {
129169 await group . destroyRecord ( ) ;
130- await this . loadCustomFields . perform ( subject ) ;
170+ await this . loadCustomFields . perform ( subject , true ) ; // Force reload after deletion
131171 modal . done ( ) ;
132172 } catch ( error ) {
133173 this . notifications . serverError ( error ) ;
@@ -151,7 +191,7 @@ export default class CustomFieldsManagerComponent extends Component {
151191
152192 try {
153193 await customField . destroyRecord ( ) ;
154- await this . loadCustomFields . perform ( subject ) ;
194+ await this . loadCustomFields . perform ( subject , true ) ; // Force reload after deletion
155195 modal . done ( ) ;
156196 } catch ( error ) {
157197 this . notifications . serverError ( error ) ;
@@ -161,6 +201,13 @@ export default class CustomFieldsManagerComponent extends Component {
161201 } ) ;
162202 }
163203
204+ @action onTabChange ( subject ) {
205+ // Ensure custom fields are loaded when switching tabs
206+ if ( subject && ( ! subject . groups || subject . groups . length === 0 ) ) {
207+ this . loadCustomFields . perform ( subject ) ;
208+ }
209+ }
210+
164211 #addCustomFieldToGroup( subject , customField , group ) {
165212 this . #updateGroupOnSubject( subject , group . id , ( g ) => {
166213 const current = isArray ( g . customFields ) ? g . customFields : [ ] ;
@@ -207,12 +254,8 @@ export default class CustomFieldsManagerComponent extends Component {
207254 #updateSubject( subject , updater ) {
208255 if ( ! subject ) return ;
209256
210- // Try to locate by object identity first; fallback to stable keys
211- const idKey = 'id' in subject ? 'id' : 'model' in subject ? 'model' : null ;
212- const idx = this . subjects . findIndex ( ( s ) => {
213- if ( s === subject ) return true ;
214- return idKey ? s ?. [ idKey ] === subject ?. [ idKey ] : false ;
215- } ) ;
257+ // Find by model property since tab objects have extra properties
258+ const idx = this . subjects . findIndex ( ( s ) => s ?. model === subject ?. model ) ;
216259
217260 if ( idx === - 1 ) return ;
218261
0 commit comments