Mirror of https://github.com/roostorg/osprey
github.com/roostorg/osprey
1import * as React from 'react';
2
3import Feature from '../components/common/Feature';
4import EntityWithPopover from '../components/entities/EntityWithPopover';
5import useApplicationConfigStore, { LabelInfoMapping } from '../stores/ApplicationConfigStore';
6import { SortedLabels, Label, LabelConnotation } from '../types/LabelTypes';
7import { FeatureLocation } from '../types/ConfigTypes';
8import EntityNameWithPopover from '../components/entities/EntityNameWithPopover';
9import FeatureName from '../components/common/FeatureName';
10
11export function wrapEntityKeysWithFeatureLocationsMenu(
12 featureName: string,
13 featureLocations: Array<FeatureLocation> | undefined
14) {
15 const location = featureLocations?.find((location) => location.name === featureName);
16
17 return <EntityNameWithPopover name={featureName} location={location} />;
18}
19
20export function wrapEntityValuesWithLabelMenu(value: unknown, featureName: string) {
21 if (value == null) {
22 return <Feature featureName={featureName} value={null} />;
23 }
24
25 const { featureNameToEntityTypeMapping } = useApplicationConfigStore.getState();
26 const entityType = featureNameToEntityTypeMapping.get(featureName);
27
28 return entityType != null ? (
29 <EntityWithPopover featureName={featureName} entityId={String(value)} entityType={entityType} />
30 ) : (
31 <Feature featureName={featureName} value={value} />
32 );
33}
34
35export function sortLabels(labels: Label[], labelInfoMapping: LabelInfoMapping): SortedLabels {
36 const sortedLabels: SortedLabels = { positive: [], negative: [], neutral: [] };
37
38 for (const label of labels) {
39 const connotation: LabelConnotation = labelInfoMapping.get(label.name)?.connotation ?? LabelConnotation.NEUTRAL;
40 sortedLabels[connotation].push(label);
41 }
42
43 return sortedLabels;
44}