From 38023eedf5bd0be8789395ac0062002fdabd543c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Thu, 14 May 2026 17:06:42 +0200 Subject: [PATCH] refactor(source-priority): reuse getDeviceTypeLadder in canonical-row picker Drops the inline override-then-default-then-constant lookup ladder inside pickCanonicalSourceRows and routes the resolution back through getDeviceTypeLadder, the single helper that already encodes that order. The picker keeps its per-bucket cache; only the resolution itself moves out. Future changes to the fallback rule land in one place instead of two. --- src/lib/analytics/source-priority.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/lib/analytics/source-priority.ts b/src/lib/analytics/source-priority.ts index dd4613659..93bd364f7 100644 --- a/src/lib/analytics/source-priority.ts +++ b/src/lib/analytics/source-priority.ts @@ -34,8 +34,8 @@ import type { } from "@/generated/prisma/client"; import { - DEFAULT_DEVICE_TYPE_PRIORITY, type DeviceType, + getDeviceTypeLadder, getSourceLadder, normalizeDeviceType, parseSourcePriority, @@ -134,22 +134,14 @@ export function pickCanonicalSourceRows( // Cache device-type ladders by (MeasurementType | "__default__") so a // bucket with mixed-type rows resolves the ladder once per type - // rather than once per row. + // rather than once per row. Resolution itself lives in + // `getDeviceTypeLadder` — the cache is the only extra layer here. const ladderCache = new Map(); function resolveLadder(rowType: MeasurementType | null | undefined): readonly DeviceType[] { const cacheKey = rowType ?? "__default__"; let ladder = ladderCache.get(cacheKey); if (!ladder) { - const override = rowType - ? resolved.deviceTypePriority[rowType] - : undefined; - ladder = - override && override.length > 0 - ? override - : resolved.deviceTypePriority.default && - resolved.deviceTypePriority.default.length > 0 - ? resolved.deviceTypePriority.default - : DEFAULT_DEVICE_TYPE_PRIORITY; + ladder = getDeviceTypeLadder(resolved, rowType ?? "default"); ladderCache.set(cacheKey, ladder); } return ladder;