From b3b04269b2742f9feb108bb4fb53ce17d8bbab5f Mon Sep 17 00:00:00 2001 From: Evangelos Skopelitis Date: Fri, 13 Dec 2024 14:45:45 -0500 Subject: [PATCH] frontend: k8s: Sort namespaces in useKubeObjectList This change alphabetically sorts namespaces natively in useKubeObjectList, addressing the case where namespace lists from k3d/k3s clusters are not sorted by default. Fixes: #2656 Signed-off-by: Evangelos Skopelitis --- frontend/src/lib/k8s/api/v2/useKubeObjectList.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/frontend/src/lib/k8s/api/v2/useKubeObjectList.ts b/frontend/src/lib/k8s/api/v2/useKubeObjectList.ts index 4e11f8033f..d4b383b068 100644 --- a/frontend/src/lib/k8s/api/v2/useKubeObjectList.ts +++ b/frontend/src/lib/k8s/api/v2/useKubeObjectList.ts @@ -313,9 +313,21 @@ export function useKubeObjectList({ queryParams: cleanedUpQueryParams, }); + const sortedItems = useMemo(() => { + if (!query.items) { + return query.items; + } + + if (kubeObjectClass.kind === 'Namespace') { + return query.items.slice().sort((a, b) => a.metadata.name.localeCompare(b.metadata.name)); + } + + return query.items; + }, [query.items, kubeObjectClass]); + // @ts-ignore - TS compiler gets confused with iterators return { - items: query.items, + items: sortedItems, clusterResults: query.clusterResults, clusterErrors: query.clusterErrors, isError: query.isError, @@ -323,7 +335,7 @@ export function useKubeObjectList({ isFetching: query.isFetching, isSuccess: query.isSuccess, *[Symbol.iterator](): ArrayIterator { - yield query.items; + yield sortedItems; yield query.errors[0] as any as ApiError; }, };