Skip to content

Commit

Permalink
frontend: k8s: Sort namespaces in useKubeObjectList
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
skoeva committed Dec 13, 2024
1 parent d94fde1 commit b3b0426
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions frontend/src/lib/k8s/api/v2/useKubeObjectList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,29 @@ export function useKubeObjectList<K extends KubeObject>({
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,
isLoading: query.isLoading,
isFetching: query.isFetching,
isSuccess: query.isSuccess,
*[Symbol.iterator](): ArrayIterator<ApiError | K[] | null> {
yield query.items;
yield sortedItems;
yield query.errors[0] as any as ApiError;
},
};
Expand Down

0 comments on commit b3b0426

Please sign in to comment.