upend/webui/src/lib/util/labels.ts

32 lines
1022 B
TypeScript

import api from '$lib/api';
import { i18n } from '$lib/i18n';
import { derived, readable, type Readable } from 'svelte/store';
import type { AttributeListingResult } from '@upnd/upend/types';
import debug from 'debug';
const dbg = debug('kestrel:labels');
const databaseAttributeLabels: Readable<{ [key: string]: string }> = readable({}, (set) => {
const result: Record<string, string> = {};
dbg('Fetching all attributes');
api.fetchAllAttributes().then((attributes: AttributeListingResult) => {
dbg('Fetched all attributes: %o', attributes);
attributes.forEach((attribute) => {
if (attribute.labels.length) {
result[attribute.name] = attribute.labels.sort()[0];
}
});
set(result);
});
});
export const attributeLabels: Readable<{ [key: string]: string }> = derived(
[i18n, databaseAttributeLabels],
([i18n, attributeLabels]) => {
const result = {};
Object.assign(result, i18n.getResourceBundle(i18n.language, 'attributes'));
Object.assign(result, attributeLabels);
return result;
}
);