All files / src/apps/user/pages/settings sessionLocations.ts

26.66% Statements 4/15
0% Branches 0/8
100% Functions 1/1
28.57% Lines 4/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                              2x 2x   2x                               2x    
// core/src/apps/user/pages/settings/sessionLocations.ts
import type { AccountSession } from "@/hooks/useSessions";
 
export interface SessionLocationGroup {
  key: string;
  lat: number;
  lng: number;
  /** True if any session at this location is the caller's current one. */
  current: boolean;
  sessions: AccountSession[];
}
 
/**
 * Groups sessions into one entry per distinct (rounded) location, so a
 * single pin on the globe or map represents every session there rather than
 * overlapping 1:1 markers. Sessions without lat/lng are excluded from the
 * groups and counted via `unknownCount` instead of being silently dropped.
 * Shared by SessionGlobe and SessionFlatMap so both views cluster identically.
 */
export function groupSessionsByLocation(sessions: AccountSession[]): {
  groups: SessionLocationGroup[];
  unknownCount: number;
} {
  const byKey = new Map<string, SessionLocationGroup>();
  let unknownCount = 0;
 
  for (const session of sessions) {
    const { lat, lng } = session.location;
    if (lat == null || lng == null) {
      unknownCount += 1;
      continue;
    }
    const key = `${lat.toFixed(1)},${lng.toFixed(1)}`;
    const existing = byKey.get(key);
    if (existing) {
      existing.sessions.push(session);
      if (session.current) existing.current = true;
    } else {
      byKey.set(key, { key, lat, lng, current: session.current, sessions: [session] });
    }
  }
 
  return { groups: Array.from(byKey.values()), unknownCount };
}