Skip to content

Commit 03c29fd

Browse files
committed
frontend: TopBar: Implement individual cluster logout functionality
Add logic to handle logout from specific clusters in a multi-cluster context. Update the URL to remove the logged-out cluster while persisting the view for remaining clusters. Signed-off-by: alokdangre <alokdangre@gmail.com>
1 parent 11a4e19 commit 03c29fd

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

frontend/src/components/App/TopBar.tsx

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,41 @@ export function processAppBarActions(
7878
return appBarActionsProcessed;
7979
}
8080

81+
/**
82+
* Handles the logic for updating the URL after a user logs out from a cluster.
83+
* If the user logs out from a specific cluster in a multi-cluster context,
84+
* it removes that cluster from the URL. Otherwise, it redirects to the home page.
85+
*
86+
* @param clusterToLogout - The name of the cluster the user is logging out from.
87+
* @param currentPath - The current URL path (e.g., from history.location.pathname).
88+
* @param historyPush - Function to navigate to a new path (e.g., history.push).
89+
*/
90+
function handleLogoutPathUpdate(
91+
clusterToLogout: string | undefined,
92+
currentPath: string,
93+
historyPush: (path: string) => void
94+
) {
95+
if (clusterToLogout) {
96+
const clusterSegmentMatch = currentPath.match(/\/c\/([^/]+)(\/|$)/);
97+
if (clusterSegmentMatch) {
98+
const currentClusterParam = clusterSegmentMatch[1];
99+
const clustersInPath = currentClusterParam.split('+');
100+
const remainingClustersInPath = clustersInPath.filter(c => c !== clusterToLogout);
101+
if (remainingClustersInPath.length > 0) {
102+
const newClusterParam = remainingClustersInPath.join('+');
103+
const newPath = currentPath.replace(`/c/${currentClusterParam}`, `/c/${newClusterParam}`);
104+
historyPush(newPath);
105+
} else {
106+
historyPush('/');
107+
}
108+
} else {
109+
historyPush('/');
110+
}
111+
} else {
112+
historyPush('/');
113+
}
114+
}
115+
81116
/**
82117
* Gets the display name for a user in a cluster.
83118
*
@@ -168,7 +203,9 @@ export default function TopBar({}: TopBarProps) {
168203
}
169204
}
170205

171-
history.push('/');
206+
handleLogoutPathUpdate(clusterToLogout, history.location.pathname, (path: string) =>
207+
history.push(path)
208+
);
172209
},
173210
[cluster, selectedClusters, history]
174211
);
@@ -472,21 +509,19 @@ export const PureTopBar = memo(
472509
{
473510
id: DefaultAppBarAction.USER,
474511
action: showUserMenu && (
475-
<MenuItem>
476-
<IconButton
477-
aria-label={t('Account of current user')}
478-
aria-controls={userMenuId}
479-
aria-haspopup="true"
480-
color="inherit"
481-
onClick={event => {
482-
handleMenuClose();
483-
handleProfileMenuOpen(event);
484-
}}
485-
size="medium"
486-
>
487-
<Icon icon="mdi:account" />
488-
</IconButton>
489-
</MenuItem>
512+
<IconButton
513+
aria-label={t('Account of current user')}
514+
aria-controls={userMenuId}
515+
aria-haspopup="true"
516+
color="inherit"
517+
onClick={event => {
518+
handleMenuClose();
519+
handleProfileMenuOpen(event);
520+
}}
521+
size="medium"
522+
>
523+
<Icon icon="mdi:account" />
524+
</IconButton>
490525
),
491526
},
492527
];

0 commit comments

Comments
 (0)