38 lines
1.8 KiB
JavaScript
38 lines
1.8 KiB
JavaScript
import { t } from '../i18n.js';
|
|
// Turns MediaDeviceInfo entries of one kind into select options. DOM-free for unit tests.
|
|
//
|
|
// Chromium adds aliases ("default", "communications") next to the physical endpoints and, without media
|
|
// permission, returns one anonymous entry per kind (empty deviceId and label). Aliases are not offered as
|
|
// separate choices; the system-default option names the device the alias currently points to.
|
|
|
|
const ALIASES = new Set(['default', 'communications']);
|
|
|
|
/** Strips Chromium's localized "<Default> - " prefix from the alias label to get the device name. */
|
|
function aliasTarget(label) {
|
|
const m = /^[^-]+ - (.+)$/.exec(label || '');
|
|
return m ? m[1] : '';
|
|
}
|
|
|
|
/**
|
|
* @param {{deviceId:string,label:string}[]} list entries of one kind
|
|
* @param {string} selected saved deviceId ('' = system default)
|
|
* @returns {{options:{value:string,label:string}[], physical:number, redacted:boolean}}
|
|
*/
|
|
export function buildDeviceOptions(list, selected) {
|
|
const defaultEntry = list.find((d) => d.deviceId === 'default');
|
|
const defaultName = aliasTarget(defaultEntry?.label);
|
|
const options = [{ value: '', label: defaultName ? t('message.system.default.p0', { p0: defaultName }) : t('ui.system.default.device') }];
|
|
let physical = 0;
|
|
for (const d of list) {
|
|
if (!d.deviceId || ALIASES.has(d.deviceId)) continue;
|
|
physical += 1;
|
|
options.push({ value: d.deviceId, label: d.label || t('message.unnamed.device.p0', { p0: d.deviceId.slice(0, 8) }) });
|
|
}
|
|
if (selected && !options.some((o) => o.value === selected)) {
|
|
options.push({ value: selected, label: t('ui.previously.selected.device.currently.unavailable') });
|
|
}
|
|
// Entries exist but none carries an id: the engine withheld the list (no media permission).
|
|
const redacted = physical === 0 && list.some((d) => !d.deviceId);
|
|
return { options, physical, redacted };
|
|
}
|