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

16.66% Statements 4/24
0% Branches 0/37
0% Functions 0/4
17.39% Lines 4/23

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114          1x                                 1x             1x           1x                                                                                                                                                            
import React, { useState } from "react";
import { Loader2, Monitor, Smartphone, Tablet, Globe, MapPin } from "lucide-react";
import { formatLocalDate, parseServerDate, timeAgo } from "@/util/time";
import type { AccountSession } from "@/hooks/useSessions";
 
export const DeviceIcon: React.FC<{ type: string | null; className?: string }> = ({
  type,
  className,
}) => {
  switch (type) {
    case "mobile":
      return <Smartphone className={className} />;
    case "tablet":
      return <Tablet className={className} />;
    case "bot":
    case "other":
      return <Globe className={className} />;
    default:
      return <Monitor className={className} />;
  }
};
 
export const STATUS_STYLES: Record<AccountSession["status"], string> = {
  active:
    "bg-emerald-100 dark:bg-emerald-900/40 text-emerald-700 dark:text-emerald-400",
  expired: "bg-gray-100 dark:bg-gray-700/60 text-gray-500 dark:text-gray-400",
  ended: "bg-gray-100 dark:bg-gray-700/60 text-gray-500 dark:text-gray-400",
};
 
export const STATUS_LABELS: Record<AccountSession["status"], string> = {
  active: "Active",
  expired: "Expired",
  ended: "Signed out",
};
 
export const SessionRow: React.FC<{
  session: AccountSession;
  onRevoke?: (id: string) => Promise<void>;
}> = ({ session, onRevoke }) => {
  const [revoking, setRevoking] = useState(false);
 
  const browser = session.device.browser ?? "Unknown browser";
  const os = session.device.os;
  const place = [session.location.city, session.location.country]
    .filter(Boolean)
    .join(", ");
  const lastSeen = session.last_seen_at ? parseServerDate(session.last_seen_at) : null;
  const created = session.created_at ? parseServerDate(session.created_at) : null;
 
  const handleRevoke = async () => {
    if (!onRevoke) return;
    setRevoking(true);
    try {
      await onRevoke(session.id);
    } finally {
      setRevoking(false);
    }
  };
 
  return (
    <div className="flex items-start gap-3 py-3 border-b border-gray-100 dark:border-gray-700/60 last:border-b-0">
      <DeviceIcon
        type={session.device.type}
        className="size-5 text-gray-400 shrink-0 mt-0.5"
      />
      <div className="flex-1 min-w-0">
        <div className="flex items-center gap-2 flex-wrap">
          <p className="text-sm font-medium text-gray-800 dark:text-gray-200">
            {browser}
            {os ? ` · ${os}` : ""}
          </p>
          {session.current && (
            <span className="text-[10px] uppercase tracking-wide px-1.5 py-0.5 rounded bg-indigo-100 dark:bg-indigo-900/40 text-indigo-600 dark:text-indigo-400 font-semibold">
              This device
            </span>
          )}
        </div>
        <p className="text-xs text-gray-400 dark:text-gray-500 mt-0.5 flex items-center gap-1 flex-wrap">
          <MapPin className="size-3 shrink-0" />
          {place || "Unknown location"}
          {session.ip ? ` · ${session.ip}` : ""}
        </p>
        <p className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">
          {session.status === "active" && lastSeen
            ? `Last active ${timeAgo(lastSeen)}`
            : session.ended_at
              ? `Ended ${timeAgo(parseServerDate(session.ended_at))}`
              : lastSeen
                ? `Last active ${timeAgo(lastSeen)}`
                : ""}
          {created ? ` · Signed in ${formatLocalDate(created)}` : ""}
        </p>
      </div>
      <div className="flex flex-col items-end gap-1.5 shrink-0">
        <span
          className={`text-xs px-2 py-0.5 rounded-full font-medium ${STATUS_STYLES[session.status]}`}
        >
          {STATUS_LABELS[session.status]}
        </span>
        {onRevoke && !session.current && session.status === "active" && (
          <button
            onClick={() => void handleRevoke()}
            disabled={revoking}
            className="flex items-center gap-1 text-xs text-red-600 dark:text-red-400 hover:underline disabled:opacity-50"
          >
            {revoking && <Loader2 className="size-3 animate-spin" />}
            Revoke
          </button>
        )}
      </div>
    </div>
  );
};