fix(app/stack): virtual grouping in EnvSelector for non admins (#2001)

This commit is contained in:
LP B
2026-03-06 15:00:01 +01:00
committed by GitHub
parent 7a76d749e3
commit 20b971dc1f
2 changed files with 18 additions and 21 deletions

View File

@@ -173,24 +173,19 @@ describe('getEnvironmentOptions', () => {
});
});
it('should create "Unassigned" group for GroupId = 1', () => {
it('should auto create an Others group if group is missing', () => {
const environments: Environment[] = [
{ Id: 1, Name: 'Env 1', GroupId: 1 } as Environment,
{ Id: 2, Name: 'Env 2', GroupId: 2 } as Environment,
];
const groups: EnvironmentGroup[] = [];
const result = getEnvironmentOptions([], environments);
expect(result[0].label).toBe('Unassigned');
expect(result[0].options[0]).toEqual({ label: 'Env 1', value: 1 });
});
it('should throw error if group is missing for non-unassigned GroupId', () => {
const environments: Environment[] = [
{ Id: 1, Name: 'Env 1', GroupId: 2 } as Environment,
];
expect(() => getEnvironmentOptions([], environments)).toThrow(
'Missing group with id 2'
);
const result = getEnvironmentOptions(groups, environments);
expect(result.length).toBe(1);
expect(result[0].label).toBe('Others');
expect(result[0].options).toEqual([
{ label: 'Env 1', value: 1 },
{ label: 'Env 2', value: 2 },
]);
});
});

View File

@@ -1,4 +1,5 @@
import { useMemo } from 'react';
import { sortBy } from 'lodash';
import { useEnvironmentList } from '@/react/portainer/environments/queries';
import { useGroups } from '@/react/portainer/environments/environment-groups/queries';
@@ -73,7 +74,11 @@ export function getEnvironmentOptions(
return acc;
}
const groupId = environment.GroupId;
let groupId = environment.GroupId;
if (!groups.some((g) => g.Id === groupId)) {
groupId = -1;
}
if (!acc[groupId]) {
acc[groupId] = [];
}
@@ -87,13 +92,10 @@ export function getEnvironmentOptions(
return Object.entries(groupedEnvironments).map(([groupId, envOptions]) => {
const parsedGroupId = parseInt(groupId, 10);
const group = groups.find((g) => g.Id === parsedGroupId);
if (!group && parsedGroupId !== 1) {
throw new Error(`Missing group with id ${groupId}`);
}
return {
label: group?.Name || 'Unassigned',
options: envOptions,
label: group?.Name || 'Others',
options: sortBy(envOptions, 'label'),
};
});
}