chore: add language name to config in manifest as well
This commit is contained in:
parent
9291a377d3
commit
ae6fdfe726
2 changed files with 13 additions and 10 deletions
|
|
@ -21,9 +21,9 @@ describe('buildManifest', () => {
|
|||
const manifest = buildManifest(handlers, {});
|
||||
|
||||
expect(manifest.config).toStrictEqual([
|
||||
{ key: 'de', type: 'checkbox', title: '🇩🇪 (KinoKiste, MeineCloud)' },
|
||||
{ key: 'es', type: 'checkbox', title: '🇪🇸 (VerHdLink)' },
|
||||
{ key: 'mx', type: 'checkbox', title: '🇲🇽 (VerHdLink)' },
|
||||
{ key: 'de', type: 'checkbox', title: 'German 🇩🇪 (KinoKiste, MeineCloud)' },
|
||||
{ key: 'es', type: 'checkbox', title: 'Castilian Spanish 🇪🇸 (VerHdLink)' },
|
||||
{ key: 'mx', type: 'checkbox', title: 'Latin American Spanish 🇲🇽 (VerHdLink)' },
|
||||
{ key: 'excludeExternalUrls', type: 'checkbox', title: 'Exclude external URLs from results' },
|
||||
]);
|
||||
});
|
||||
|
|
@ -38,9 +38,9 @@ describe('buildManifest', () => {
|
|||
const manifest = buildManifest(handlers, { de: 'on' });
|
||||
|
||||
expect(manifest.config).toStrictEqual([
|
||||
{ key: 'de', type: 'checkbox', title: '🇩🇪 (KinoKiste, MeineCloud)', default: 'checked' },
|
||||
{ key: 'es', type: 'checkbox', title: '🇪🇸 (VerHdLink)' },
|
||||
{ key: 'mx', type: 'checkbox', title: '🇲🇽 (VerHdLink)' },
|
||||
{ key: 'de', type: 'checkbox', title: 'German 🇩🇪 (KinoKiste, MeineCloud)', default: 'checked' },
|
||||
{ key: 'es', type: 'checkbox', title: 'Castilian Spanish 🇪🇸 (VerHdLink)' },
|
||||
{ key: 'mx', type: 'checkbox', title: 'Latin American Spanish 🇲🇽 (VerHdLink)' },
|
||||
{ key: 'excludeExternalUrls', type: 'checkbox', title: 'Exclude external URLs from results' },
|
||||
]);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
import { flag } from 'country-emoji';
|
||||
import { Handler } from '../handler';
|
||||
import { Config, ManifestWithConfig } from '../types';
|
||||
import { Config, CountryCode, ManifestWithConfig } from '../types';
|
||||
import { languageFromCountryCode } from './languageFromCountryCode';
|
||||
|
||||
const typedEntries = <T extends object>(obj: T): [keyof T, T[keyof T]][] => (Object.entries(obj) as [keyof T, T[keyof T]][]);
|
||||
|
||||
export const buildManifest = (handlers: Handler[], config: Config): ManifestWithConfig => {
|
||||
const manifest: ManifestWithConfig = {
|
||||
|
|
@ -30,19 +33,19 @@ export const buildManifest = (handlers: Handler[], config: Config): ManifestWith
|
|||
},
|
||||
};
|
||||
|
||||
const countryCodeHandlers: Record<string, Handler[]> = {};
|
||||
const countryCodeHandlers: Partial<Record<CountryCode, Handler[]>> = {};
|
||||
handlers.forEach((handler) => {
|
||||
handler.countryCodes.forEach(countryCode => countryCodeHandlers[countryCode] = [...(countryCodeHandlers[countryCode] ?? []), handler]);
|
||||
});
|
||||
|
||||
const sortedLanguageHandlers = Object.entries(countryCodeHandlers)
|
||||
const sortedLanguageHandlers = typedEntries(countryCodeHandlers)
|
||||
.sort(([countryCodeA], [countryCodeB]) => countryCodeA.localeCompare(countryCodeB));
|
||||
|
||||
for (const [countryCode, handlers] of sortedLanguageHandlers) {
|
||||
manifest.config.push({
|
||||
key: countryCode,
|
||||
type: 'checkbox',
|
||||
title: `${flag(countryCode)} (${handlers.map(handler => handler.label).join(', ')})`,
|
||||
title: `${languageFromCountryCode(countryCode)} ${flag(countryCode)} (${(handlers as Handler[]).map(handler => handler.label).join(', ')})`,
|
||||
...(countryCode in config && { default: 'checked' }),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue