display the custom labels by in case-insensitive alphabetical ascending order (#21041)

relates to #20194

The UI displayed the custom labels by creation date, but we now display
custom labels in the UI by name in case-insensitive alphabetical
ascending order.


![image](https://github.com/user-attachments/assets/43ce642b-8335-4542-ac38-4b5a4ab569e4)

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files)
for more information.
- [x] Manual QA for all new/changed functionality
This commit is contained in:
Gabriel Hernandez 2024-08-06 10:52:23 +01:00 committed by GitHub
parent 9325bca9c1
commit 9200f18229
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 28 additions and 6 deletions

View file

@ -0,0 +1 @@
- display the label names case-insensitive alphabetical order in the fleet UI

View file

@ -7,7 +7,7 @@ import { NotificationContext } from "context/notification";
import { IApiError } from "interfaces/errors";
import { ILabelSummary } from "interfaces/label";
import labelsAPI from "services/entities/labels";
import labelsAPI, { getCustomLabels } from "services/entities/labels";
import mdmAPI from "services/entities/mdm";
// @ts-ignore
@ -250,10 +250,7 @@ const AddProfileModal = ({
isError: isErrorLabels,
} = useQuery<ILabelSummary[], Error>(
["custom_labels"],
() =>
labelsAPI
.summary()
.then((res) => res.labels.filter((l) => l.label_type !== "builtin")),
() => labelsAPI.summary().then((res) => getCustomLabels(res.labels)),
{
enabled: isPremiumTier,

View file

@ -1,4 +1,6 @@
import { ILabel } from "interfaces/label";
import { getCustomLabels } from "services/entities/labels";
import { EMPTY_OPTION, FILTERED_LINUX, NO_LABELS_OPTION } from "./constants";
export interface IEmptyOption {
@ -27,7 +29,7 @@ const createOptionGroup = (
/** Will create the custom label group options and handles when no labels have been created yet or
* will filter by the desired search query */
const createCustomLabelOptions = (labels: ILabel[], query: string) => {
const customLabels = labels.filter((label) => label.label_type === "regular");
const customLabels = getCustomLabels(labels);
let customLabelGroupOptions: ILabel[] | IEmptyOption[];
if (customLabels.length === 0) {

View file

@ -49,6 +49,28 @@ const generateCreateLabelBody = (
const generateUpdateLabelBody = generateCreateLabelBody;
/** gets the custom label and returns them in case-insensitive alphabetical
* ascending order by label name. (e.g. [A, B, C, a, b, c] => [A, a, B, b, C, c])
*/
export const getCustomLabels = <T extends { label_type: string; name: string }>(
labels: T[]
) => {
if (labels.length === 0) {
return [];
}
return labels
.filter((label) => label.label_type === "regular")
.sort((a, b) => {
// Found this technique here
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
// This is a case insensitive sort
return a.name.localeCompare(b.name, undefined, {
sensitivity: "base",
});
});
};
export default {
create: (
formData: IDynamicLabelFormData | IManualLabelFormData