From c87a581af1c759141f10e0bcc7c42da115d04f23 Mon Sep 17 00:00:00 2001 From: yuye-aws Date: Wed, 16 Aug 2023 13:18:00 +0800 Subject: [PATCH] refactor getFilteredWorkspaceList function Signed-off-by: yuye-aws --- .../ui/header/collapsible_nav_header.tsx | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/core/public/chrome/ui/header/collapsible_nav_header.tsx b/src/core/public/chrome/ui/header/collapsible_nav_header.tsx index 74166bf88359..536ce20f5477 100644 --- a/src/core/public/chrome/ui/header/collapsible_nav_header.tsx +++ b/src/core/public/chrome/ui/header/collapsible_nav_header.tsx @@ -39,21 +39,13 @@ function getFilteredWorkspaceList( workspaceList: WorkspaceAttribute[], currentWorkspace: WorkspaceAttribute | null ): WorkspaceAttribute[] { - // list top5 workspaces except management workspace - let filteredWorkspaceList = workspaceList - .filter((workspace) => workspace.id !== MANAGEMENT_WORKSPACE) - .slice(0, 5); - if (currentWorkspace) { - // current workspace located at the top of workspace list - filteredWorkspaceList = filteredWorkspaceList.filter( - (workspace) => workspace.id !== currentWorkspace.id - ); - filteredWorkspaceList.unshift(currentWorkspace); - if (filteredWorkspaceList.length > 5) { - filteredWorkspaceList.pop(); - } - } - return filteredWorkspaceList; + // list top5 workspaces except management workspace, place current workspace at the top + return [ + ...(currentWorkspace ? [currentWorkspace] : []), + ...workspaceList.filter( + (workspace) => workspace.id !== MANAGEMENT_WORKSPACE && workspace.id !== currentWorkspace?.id + ), + ].slice(0, 5); } export function CollapsibleNavHeader({ workspaces, getUrlForApp, basePath }: Props) {