← Files ItalicARCHIVED FILE

ui/device-names.js

3.85 KB · Nov 25, 2026 · 00:20 UTC

↓ Download file

/**
 * What the Library shows for the device a recording came from: its saved
 * device's current name, removed or not, else the product name, or Granola
 * for a meeting imported from there. iPhone, the Mac and the web
 * name the same cases (test-fixtures/recording-device-names.json). A
 * recording names its device by the importing iPhone's own ID for it, which
 * the device lists in recordingDeviceIds; by `cloud:` and its account ID; or,
 * for a device saved without a hardware address, by the ID whose SHA-256 is
 * its account ID. `hashes` maps deviceHashKey()s to those hashes as far as
 * they are known (deviceIdHash).
 */
/** Product names by device type: devices/catalog.json's, which the build writes in. */
export const deviceProducts = /* @italic-device-products */ {};

/** The device types a recording's source names (ring, glyph, dex), by every name the source has had. Other types are named by recordingDevice alone. */
export function recordingDeviceKind(source) {
  if (source === 'sudo' || source === 'ring') return 'ring';
  if (source === 'glyph' || source === 'pocket' || source === 'caption') return 'glyph';
  return source === 'dex' ? 'dex' : undefined;
}

export function deviceProductName(source) {
  if (source === 'phone') return 'iPhone';
  const type = recordingDeviceKind(source) ?? source;
  return Object.hasOwn(deviceProducts, type) ? deviceProducts[type] : undefined;
}

/**
 * The type of the saved device a recording came from. A source names its own
 * (ring, glyph, dex); iPhone and the web have none. With no source, the
 * recording's device type names it, except the three above, which a source
 * must confirm: a custom device and the types of devices added later need none.
 */
export function recordingDeviceType(content) {
  if (content?.recordingSource) return recordingDeviceKind(content.recordingSource);
  const typeId = content?.recordingDevice?.typeId;
  return typeof typeId === 'string' && typeId && !recordingDeviceKind(typeId) ? typeId : undefined;
}

export function deviceHashKey(content) {
  const type = recordingDeviceType(content), deviceId = content?.recordingDevice?.deviceId;
  return type && typeof deviceId === 'string' ? `${type}|${deviceId}` : undefined;
}

export async function deviceIdHash(key) {
  const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(key));
  return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('');
}

export function recordingDeviceName(content, devices = [], hashes = new Map()) {
  // A meeting imported from Granola carries its origin beside its content (MCP for Claude and Italic for Mac).
  if (content?.origin?.app === 'granola') return 'Granola';
  const type = recordingDeviceType(content);
  // iPhone, or a browser: a source but no saved device.
  if (!type) return deviceProductName(content?.recordingSource);
  const reference = content.recordingDevice;
  let device;
  if (reference && (recordingDeviceKind(reference.typeId) ?? reference.typeId) === type && typeof reference.deviceId === 'string') {
    const sameType = devices.filter(item => item.typeId === type), deviceId = reference.deviceId;
    const hash = hashes.get(`${type}|${deviceId}`);
    device = deviceId.startsWith('cloud:') ? sameType.find(item => item.id === deviceId.slice(6))
      : sameType.find(item => item.recordingDeviceIds?.includes(deviceId)) ?? (hash ? sameType.find(item => item.id === hash) : undefined);
  }
  // A removed device keeps its name; only the apps that link to its settings tell them apart. A type without a product (a custom device) goes by the name it was saved with.
  const own = device ? String(device.defaultName ?? '').trim() : '';
  const named = own && own !== {ring: 'Ring', glyph: 'Pocket'}[device.typeId] ? own : '';
  return device ? String(device.customName ?? '').trim() || named || deviceProductName(device.typeId) || undefined : deviceProductName(type);
}

SHA-256: 1799d0a93d32b96bd755a6f95359310e8ce5fdf724a920380d0e57251005a4a7