Skip to content

Commit 5ea54a0

Browse files
authored
Merge pull request #673 from KelvinTegelaar/main
[pull] main from KelvinTegelaar:main
2 parents fb484b9 + edcad60 commit 5ea54a0

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/components/CippComponents/CippTenantSelector.jsx

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export const CippTenantSelector = (props) => {
184184
// Cancel all in-flight queries before changing tenant
185185
queryClient.cancelQueries();
186186

187-
// Update router and settings
187+
// Update router only - let the URL watcher handle settings
188188
query.tenantFilter = currentTenant.value;
189189
router.replace(
190190
{
@@ -194,53 +194,52 @@ export const CippTenantSelector = (props) => {
194194
undefined,
195195
{ shallow: true }
196196
);
197-
198-
settings.handleUpdate({
199-
currentTenant: currentTenant.value,
200-
});
201197
}
202-
//if we have a tenantfilter, we add the tenantfilter to the title of the tab/page so its "Tenant - original title".
203198
}
204199
}, [currentTenant?.value]);
205200

206-
// This effect handles when the URL parameter changes externally
201+
// This effect handles when the URL parameter changes (from deep link or user selection)
202+
// This is the single source of truth for tenant changes
207203
useEffect(() => {
208-
if (!router.isReady || !tenantList.isSuccess || !settings.isInitialized) return;
204+
if (!router.isReady || !tenantList.isSuccess) return;
209205

210-
// Get the current tenant from URL or settings
211-
const urlTenant = router.query.tenantFilter || settings.currentTenant;
206+
const urlTenant = router.query.tenantFilter;
212207

213-
// Only update if there's a URL tenant and it's different from our current state
214-
if (urlTenant && (!currentTenant || urlTenant !== currentTenant.value)) {
208+
// Only process if we have a URL tenant
209+
if (urlTenant) {
215210
// Find the tenant in our list
216211
const matchingTenant = tenantList.data.find(
217212
({ defaultDomainName }) => defaultDomainName === urlTenant
218213
);
219214

220215
if (matchingTenant) {
221-
setSelectedTenant({
222-
value: urlTenant,
223-
label: `${matchingTenant.displayName} (${urlTenant})`,
224-
addedFields: {
225-
defaultDomainName: matchingTenant.defaultDomainName,
226-
displayName: matchingTenant.displayName,
227-
customerId: matchingTenant.customerId,
228-
initialDomainName: matchingTenant.initialDomainName,
229-
},
230-
});
216+
// Update local state if different
217+
if (!currentTenant || urlTenant !== currentTenant.value) {
218+
setSelectedTenant({
219+
value: urlTenant,
220+
label: `${matchingTenant.displayName} (${urlTenant})`,
221+
addedFields: {
222+
defaultDomainName: matchingTenant.defaultDomainName,
223+
displayName: matchingTenant.displayName,
224+
customerId: matchingTenant.customerId,
225+
initialDomainName: matchingTenant.initialDomainName,
226+
},
227+
});
228+
}
229+
230+
// Update settings if different (null filter in settings-context prevents saving null)
231+
if (settings.currentTenant !== urlTenant) {
232+
settings.handleUpdate({
233+
currentTenant: urlTenant,
234+
});
235+
}
231236
}
232237
}
233-
}, [
234-
router.isReady,
235-
router.query.tenantFilter,
236-
tenantList.isSuccess,
237-
settings.currentTenant,
238-
settings.isInitialized,
239-
]);
238+
}, [router.isReady, router.query.tenantFilter, tenantList.isSuccess]);
240239

241240
// This effect ensures the tenant filter parameter is included in the URL when missing
242241
useEffect(() => {
243-
if (!router.isReady || !settings.isInitialized || !settings.currentTenant) return;
242+
if (!router.isReady || !settings.currentTenant) return;
244243

245244
// If the tenant parameter is missing from the URL but we have it in settings
246245
if (!router.query.tenantFilter && settings.currentTenant) {
@@ -254,7 +253,7 @@ export const CippTenantSelector = (props) => {
254253
{ shallow: true }
255254
);
256255
}
257-
}, [router.isReady, router.query, settings.currentTenant, settings.isInitialized]);
256+
}, [router.isReady, router.query.tenantFilter, settings.currentTenant]);
258257

259258
useEffect(() => {
260259
if (tenant && currentTenant?.value && currentTenant?.value !== "AllTenants") {

0 commit comments

Comments
 (0)