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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | 1x 4x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x | import React, { useMemo, useState } from "react";
import SubPage from "@/components/page/SubPage/SubPage";
import { useAuth } from "@/hooks/useAuth";
import {
useSessions,
useSessionHistory,
type AccountSession,
} from "@/hooks/useSessions";
import {
ShieldCheck,
ShieldAlert,
LogOut,
KeyRound,
Loader2,
History,
TriangleAlert,
} from "lucide-react";
import { SettingsTabs } from "./SettingsTabs";
import { SessionLocationView } from "./SessionLocationView";
import { SessionMarkerModal } from "./SessionMarkerModal";
import { SessionRow } from "./SessionUI";
import {
SectionCard,
} from "@/components/detail";
const TabButton: React.FC<{
active: boolean;
onClick: () => void;
children: React.ReactNode;
}> = ({ active, onClick, children }) => (
<button
onClick={onClick}
className={`px-3 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${
active
? "border-indigo-600 text-indigo-600 dark:border-indigo-400 dark:text-indigo-400"
: "border-transparent text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-300"
}`}
>
{children}
</button>
);
const SessionsSection: React.FC = () => {
const { sessions, loading, error, actionError, revokeSession, revokeOthers } =
useSessions();
const history = useSessionHistory();
const [tab, setTab] = useState<"active" | "history">("active");
const [confirmRevokeAll, setConfirmRevokeAll] = useState(false);
const [revokingAll, setRevokingAll] = useState(false);
const [selected, setSelected] = useState<AccountSession[] | null>(null);
// Stable reference: SessionGlobe tears down and recreates its WebGL context
// whenever this array's identity changes, so it must not be a fresh literal
// on every render (e.g. when the marker modal opens/closes).
const allSessions = useMemo(
() => [...sessions, ...history.sessions],
[sessions, history.sessions],
);
const otherActiveCount = sessions.filter(
(s) => !s.current && s.status === "active",
).length;
// Revoking a session ends it, so refresh history to surface the moved row.
const handleRevoke = async (id: string) => {
await revokeSession(id);
history.refresh();
};
const handleRevokeAll = async () => {
setRevokingAll(true);
try {
await revokeOthers();
setConfirmRevokeAll(false);
history.refresh();
} finally {
setRevokingAll(false);
}
};
return (
<SectionCard title="Sessions">
<SessionLocationView
sessions={allSessions}
loading={loading || history.loading}
onSelectSessions={setSelected}
/>
<div className="mt-5 flex gap-1 border-b border-gray-100 dark:border-gray-700/60">
<TabButton active={tab === "active"} onClick={() => setTab("active")}>
Active{sessions.length > 0 ? ` (${sessions.length})` : ""}
</TabButton>
<TabButton active={tab === "history"} onClick={() => setTab("history")}>
History
</TabButton>
</div>
<div className="pt-3">
{tab === "active" ? (
loading ? (
<div className="flex items-center gap-2 text-sm text-gray-400 dark:text-gray-500 py-2">
<Loader2 className="size-4 animate-spin" />
Loading sessions…
</div>
) : error ? (
<p className="text-sm text-red-500 py-2">{error}</p>
) : sessions.length === 0 ? (
<p className="text-sm text-gray-400 dark:text-gray-500 py-2">
No active sessions.
</p>
) : (
<>
{actionError && (
<p className="text-sm text-red-500 mb-2">{actionError}</p>
)}
<div>
{sessions.map((session) => (
<SessionRow
key={session.id}
session={session}
onRevoke={handleRevoke}
/>
))}
</div>
{otherActiveCount > 0 && (
<div className="mt-4 pt-3 border-t border-gray-100 dark:border-gray-700/60 flex items-center justify-between">
<p className="text-xs text-gray-400 dark:text-gray-500">
{otherActiveCount} other active{" "}
{otherActiveCount === 1 ? "session" : "sessions"}
</p>
{confirmRevokeAll ? (
<div className="flex gap-2">
<button
onClick={() => void handleRevokeAll()}
disabled={revokingAll}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-red-600 text-white text-sm font-medium hover:bg-red-700 transition-colors disabled:opacity-50"
>
{revokingAll && (
<Loader2 className="size-3.5 animate-spin" />
)}
Confirm
</button>
<button
onClick={() => setConfirmRevokeAll(false)}
className="px-3 py-1.5 rounded-lg border border-gray-300 dark:border-gray-600 text-sm text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors"
>
Cancel
</button>
</div>
) : (
<button
onClick={() => setConfirmRevokeAll(true)}
className="flex items-center gap-2 px-3 py-1.5 rounded-lg border border-red-300 dark:border-red-800 text-red-600 dark:text-red-400 text-sm font-medium hover:bg-red-50 dark:hover:bg-red-900/30 transition-colors"
>
<LogOut className="size-3.5" />
Sign out all other sessions
</button>
)}
</div>
)}
</>
)
) : history.loading ? (
<div className="flex items-center gap-2 text-sm text-gray-400 dark:text-gray-500 py-2">
<Loader2 className="size-4 animate-spin" />
Loading history…
</div>
) : history.error ? (
<p className="text-sm text-red-500 py-2">{history.error}</p>
) : history.sessions.length === 0 ? (
<div className="flex items-center gap-2 text-sm text-gray-400 dark:text-gray-500 py-2">
<History className="size-4 shrink-0" />
No past sessions yet.
</div>
) : (
<>
<div>
{history.sessions.map((session) => (
<SessionRow key={session.id} session={session} />
))}
</div>
<div className="mt-4 pt-3 border-t border-gray-100 dark:border-gray-700/60 flex items-center justify-between">
<p className="text-xs text-gray-400 dark:text-gray-500">
Showing {history.sessions.length} of {history.total}
</p>
{history.hasMore && (
<button
onClick={history.loadMore}
disabled={history.loadingMore}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-gray-300 dark:border-gray-600 text-sm text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors disabled:opacity-50"
>
{history.loadingMore && (
<Loader2 className="size-3.5 animate-spin" />
)}
Load more
</button>
)}
</div>
</>
)}
</div>
{selected && (
<SessionMarkerModal
sessions={selected}
onClose={() => setSelected(null)}
onRevoke={handleRevoke}
/>
)}
</SectionCard>
);
};
const SettingsSecurity: React.FC = () => {
const { user, logout } = useAuth();
const [confirmLogout, setConfirmLogout] = useState(false);
return (
<SubPage title="Security" links={[<SettingsTabs key="tabs" active="security" />]}>
<div className="max-w-xl space-y-6">
{/* Verification status */}
<SectionCard title="Email Verification">
<div className="flex items-center gap-3">
{user?.email_verified ? (
<>
<ShieldCheck className="size-8 text-emerald-500 shrink-0" />
<div>
<p className="text-sm font-medium text-gray-800 dark:text-gray-200">
Email verified
</p>
<p className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">
{user.email} is confirmed and active.
</p>
</div>
</>
) : (
<>
<ShieldAlert className="size-8 text-amber-500 shrink-0" />
<div>
<p className="text-sm font-medium text-gray-800 dark:text-gray-200">
Email not verified
</p>
<p className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">
Check your inbox for a verification link.
</p>
</div>
</>
)}
</div>
</SectionCard>
{/* Password */}
<SectionCard title="Password">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<KeyRound className="size-5 text-gray-400 shrink-0" />
<div>
<p className="text-sm text-gray-700 dark:text-gray-300">
Password managed by your identity provider
</p>
<p className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">
Contact your administrator to reset your password.
</p>
</div>
</div>
</div>
</SectionCard>
{/* Sessions: globe + active/history tabs */}
<SessionsSection />
{/* Danger zone */}
<div className="rounded-xl border border-red-200 dark:border-red-900/60 bg-red-50 dark:bg-red-950/20 shadow-sm overflow-hidden">
<div className="px-5 py-3.5 border-b border-red-100 dark:border-red-900/40 flex items-center gap-2">
<TriangleAlert className="size-4 text-red-500 shrink-0" />
<h3 className="text-sm font-semibold text-red-700 dark:text-red-400">
Danger Zone
</h3>
</div>
<div className="px-5 py-4">
<div className="flex items-center justify-between">
<div>
<p className="text-sm font-medium text-gray-800 dark:text-gray-200">
Sign out
</p>
<p className="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
End your current session and return to the login page.
</p>
</div>
{confirmLogout ? (
<div className="flex gap-2 shrink-0">
<button
onClick={logout}
className="px-3 py-1.5 rounded-lg bg-red-600 text-white text-sm font-medium hover:bg-red-700 transition-colors"
>
Confirm
</button>
<button
onClick={() => setConfirmLogout(false)}
className="px-3 py-1.5 rounded-lg border border-gray-300 dark:border-gray-600 text-sm text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors"
>
Cancel
</button>
</div>
) : (
<button
onClick={() => setConfirmLogout(true)}
className="shrink-0 flex items-center gap-2 px-3 py-1.5 rounded-lg border border-red-300 dark:border-red-800 text-red-600 dark:text-red-400 text-sm font-medium hover:bg-red-50 dark:hover:bg-red-900/30 transition-colors"
>
<LogOut className="size-3.5" />
Sign out
</button>
)}
</div>
</div>
</div>
</div>
</SubPage>
);
};
export default SettingsSecurity;
|