All files / src/apps/user/pages/settings SessionLocationView.tsx

69.23% Statements 9/13
71.42% Branches 5/7
50% Functions 3/6
69.23% Lines 9/13

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                  1x   1x 1x               1x 4x                     1x         2x   2x         2x                                                                                                        
// core/src/apps/user/pages/settings/SessionLocationView.tsx
import React, { useState } from "react";
import { Globe as GlobeIcon, Map as MapIcon } from "lucide-react";
import type { AccountSession } from "@/hooks/useSessions";
import { SessionGlobe } from "./SessionGlobe";
import { SessionFlatMap } from "./SessionFlatMap";
 
type LocationViewMode = "globe" | "map";
 
const VIEW_MODE_KEY = "settings:security:session_location_view";
 
const readStoredViewMode = (): LocationViewMode =>
  localStorage.getItem(VIEW_MODE_KEY) === "map" ? "map" : "globe";
 
interface SessionLocationViewProps {
  sessions: AccountSession[];
  loading?: boolean;
  onSelectSessions?: (sessions: AccountSession[]) => void;
}
 
const segmentClasses = (active: boolean): string =>
  `flex items-center gap-1.5 px-2.5 py-1 rounded-md text-xs font-medium transition-colors ${
    active
      ? "bg-white dark:bg-gray-700 text-indigo-600 dark:text-indigo-400 shadow-sm"
      : "text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200"
  }`;
 
/**
 * Session location panel: a rotating globe by default, with a flat,
 * zoomable/pannable map as an alternative for pinpointing exact session
 * locations. The chosen view persists across visits to this page.
 */
export const SessionLocationView: React.FC<SessionLocationViewProps> = ({
  sessions,
  loading = false,
  onSelectSessions,
}) => {
  const [mode, setMode] = useState<LocationViewMode>(readStoredViewMode);
 
  const selectMode = (next: LocationViewMode) => {
    setMode(next);
    localStorage.setItem(VIEW_MODE_KEY, next);
  };
 
  return (
    <div>
      <div className="flex items-center justify-between mb-3">
        <p className="text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wide">
          Session Locations
        </p>
        <div
          role="tablist"
          aria-label="Location view"
          className="inline-flex items-center gap-0.5 rounded-lg border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/40 p-0.5"
        >
          <button
            type="button"
            role="tab"
            aria-selected={mode === "globe"}
            onClick={() => selectMode("globe")}
            className={segmentClasses(mode === "globe")}
          >
            <GlobeIcon className="size-3.5" />
            Globe
          </button>
          <button
            type="button"
            role="tab"
            aria-selected={mode === "map"}
            onClick={() => selectMode("map")}
            className={segmentClasses(mode === "map")}
          >
            <MapIcon className="size-3.5" />
            Map
          </button>
        </div>
      </div>
 
      {mode === "globe" ? (
        <SessionGlobe
          sessions={sessions}
          loading={loading}
          onSelectSessions={onSelectSessions}
        />
      ) : (
        <SessionFlatMap
          sessions={sessions}
          loading={loading}
          onSelectSessions={onSelectSessions}
        />
      )}
    </div>
  );
};
 
export default SessionLocationView;