Files
auto-trade/features/auth/components/session-timer.tsx

80 lines
2.7 KiB
TypeScript
Raw Normal View History

/**
* @file features/auth/components/session-timer.tsx
* @description
* @remarks
* - [] Components/UI
* - [ ] ->
* - [ ] Zustand Store -> Calculation -> UI
* - [ ] stores/session-store.ts, features/layout/header.tsx
*/
"use client";
import { useEffect, useState } from "react";
import { useSessionStore } from "@/stores/session-store";
import { SESSION_TIMEOUT_MS } from "@/features/auth/constants";
2026-02-10 11:16:39 +09:00
import { cn } from "@/lib/utils";
/**
*
* mm:ss (10 )
* @returns ( )
* @remarks 1
* @see header.tsx -
*/
2026-02-10 11:16:39 +09:00
interface SessionTimerProps {
/** 셰이더 배경 위에서 가독성을 높이는 투명 모드 */
blendWithBackground?: boolean;
}
export function SessionTimer({ blendWithBackground = false }: SessionTimerProps) {
const lastActive = useSessionStore((state) => state.lastActive);
// [State] 남은 시간 (밀리초)
const [timeLeft, setTimeLeft] = useState<number>(SESSION_TIMEOUT_MS);
useEffect(() => {
const calculateTimeLeft = () => {
const now = Date.now();
const passed = now - lastActive;
// [Step 1] 남은 시간 계산 (음수 방지)
const remaining = Math.max(0, SESSION_TIMEOUT_MS - passed);
setTimeLeft(remaining);
};
calculateTimeLeft(); // 초기 실행
// [Step 2] 1초마다 남은 시간 갱신
const interval = setInterval(calculateTimeLeft, 1000);
return () => clearInterval(interval);
}, [lastActive]);
// [Step 3] 시간 포맷팅 (mm:ss)
const minutes = Math.floor(timeLeft / 60000);
const seconds = Math.floor((timeLeft % 60000) / 1000);
// [Step 4] 10분 미만일 때 긴급 스타일 적용
const isUrgent = timeLeft < 10 * 60 * 1000;
return (
<div
2026-02-10 11:16:39 +09:00
className={cn(
"hidden rounded-full border px-3 py-1.5 text-sm font-medium tabular-nums backdrop-blur-md transition-colors md:block",
isUrgent
2026-02-10 11:16:39 +09:00
? "border-red-200 bg-red-50/50 text-red-500 dark:border-red-800 dark:bg-red-900/20"
: blendWithBackground
? "border-white/30 bg-black/45 text-white shadow-sm shadow-black/40"
: "border-border/40 bg-background/50 text-muted-foreground",
)}
>
{/* ========== 라벨 ========== */}
<span className="mr-2"> </span>
{/* ========== 시간 표시 ========== */}
{minutes.toString().padStart(2, "0")}:
{seconds.toString().padStart(2, "0")}
</div>
);
}