Skip to content

Commit

Permalink
[sc-2510] Sort entities alphabetically (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
operramon authored Sep 30, 2022
1 parent 91aeb32 commit f8a15a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
13 changes: 8 additions & 5 deletions libs/search-widget/src/core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import type {
TokenAnnotation,
} from '@nuclia/core';
import { Nuclia, Resource, ResourceProperties, Search, WritableKnowledgeBox } from '@nuclia/core';
import { filter, forkJoin, map, merge, Observable, of } from 'rxjs';
import { filter, forkJoin, map, merge, Observable, of, take } from 'rxjs';
import { nucliaStore } from './stores/main.store';
import { loadModel } from './tensor';
import type { EntityGroup, WidgetOptions } from './models';
import { generatedEntitiesColor } from './utils';
import { _ } from './i18n';
import type { Annotation } from './stores/annotation.store';

let nucliaApi: Nuclia | null;
Expand Down Expand Up @@ -92,17 +93,19 @@ export const loadEntities = (): Observable<EntityGroup[]> => {
if (!nucliaApi) {
throw new Error('Nuclia API not initialized');
}
return nucliaApi.knowledgeBox.getEntities().pipe(
map((entityMap: Entities) =>
return forkJoin([nucliaApi.knowledgeBox.getEntities(), _.pipe(take(1))]).pipe(
map(([entityMap, translate]) =>
Object.entries(entityMap)
.map(([groupId, group]) => ({
id: groupId,
title: group.title || `entities.${groupId.toLowerCase()}`,
color: group.color || generatedEntitiesColor[groupId],
entities: Object.entries(group.entities).map(([entityId, entity]) => entity.value),
entities: Object.entries(group.entities)
.map(([entityId, entity]) => entity.value)
.sort((a, b) => a.localeCompare(b)),
custom: group.custom,
}))
.sort((a, b) => a.id.localeCompare(b.id)),
.sort((a, b) => translate(a.title).localeCompare(translate(b.title))),
),
);
};
Expand Down
20 changes: 10 additions & 10 deletions libs/search-widget/src/core/stores/resource.store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, take } from 'rxjs';
import type { Resource } from '@nuclia/core';
import type { EntityGroup } from '../models';
import { generatedEntitiesColor } from '../utils';
import { _ } from '../../core/i18n';

export type ResourceStore = {
resource: BehaviorSubject<Resource | null>;
Expand All @@ -26,17 +27,16 @@ function initResourceStore() {

function setResource(resource: Resource) {
resourceStore.resource.next(resource);
const entityGroups: EntityGroup[] = Object.entries(resource.getNamedEntities())
.sort((a, b) => a[0].localeCompare(b[0]))
.reduce((groups, [groupId, entities]) => {
groups.push({
_.pipe(take(1)).subscribe((translate) => {
const entityGroups: EntityGroup[] = Object.entries(resource.getNamedEntities())
.map(([groupId, entities]) => ({
id: groupId,
title: `entities.${groupId.toLowerCase()}`,
color: generatedEntitiesColor[groupId],
entities: entities.filter((value) => !!value).sort((a, b) => a.localeCompare(b)),
});
return groups;
}, [] as EntityGroup[]);
resourceStore.resourceEntities.next(entityGroups);
resourceStore.hasEntities.next(entityGroups.length > 0);
}))
.sort((a, b) => translate(a.title).localeCompare(translate(b.title)));
resourceStore.resourceEntities.next(entityGroups);
resourceStore.hasEntities.next(entityGroups.length > 0);
});
}

0 comments on commit f8a15a0

Please sign in to comment.