255 lines
11 KiB
TypeScript
255 lines
11 KiB
TypeScript
import { MoreHorizontal } from 'lucide-react';
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
import { DataTableCard, type DataTableColumn, type DataTableFilter } from '../../components/data-table';
|
|
import { Button, Modal } from '../../components/ui';
|
|
import { PageTitle } from '../../layout/AppLayout';
|
|
import { cn } from '../../lib/utils';
|
|
import { loadUserSessions, lockUser, terminateUserSession, unlockUser, type UserSession } from './operations-api';
|
|
import { formatDateTime, LoadingModal, StatusBadge, useMinimumLoading } from './operation-utils';
|
|
|
|
export function UserSessionsPage() {
|
|
const [sessions, setSessions] = useState<UserSession[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [failed, setFailed] = useState(false);
|
|
const [menuSession, setMenuSession] = useState<{ id: string; x: number; y: number } | null>(null);
|
|
const [detailSession, setDetailSession] = useState<UserSession | null>(null);
|
|
const loadingVisible = useMinimumLoading(loading);
|
|
|
|
const refresh = () => {
|
|
setLoading(true);
|
|
loadUserSessions()
|
|
.then((nextSessions) => {
|
|
setSessions(nextSessions);
|
|
setFailed(false);
|
|
})
|
|
.catch(() => {
|
|
setSessions([]);
|
|
setFailed(true);
|
|
})
|
|
.finally(() => setLoading(false));
|
|
};
|
|
|
|
useEffect(() => {
|
|
refresh();
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!menuSession) return;
|
|
|
|
const close = () => setMenuSession(null);
|
|
const closeOnEscape = (event: KeyboardEvent) => {
|
|
if (event.key === 'Escape') setMenuSession(null);
|
|
};
|
|
|
|
window.addEventListener('click', close);
|
|
window.addEventListener('keydown', closeOnEscape);
|
|
return () => {
|
|
window.removeEventListener('click', close);
|
|
window.removeEventListener('keydown', closeOnEscape);
|
|
};
|
|
}, [menuSession]);
|
|
|
|
const rows = useMemo(() => sessions.map((session, index) => ({ ...session, no: sessions.length - index })), [sessions]);
|
|
|
|
const columns = useMemo<Array<DataTableColumn<UserSession>>>(
|
|
() => [
|
|
{ key: 'no', header: 'No.', className: 'w-20 px-5 py-3 font-medium', cell: (row) => <span className="font-semibold">{row.no}</span> },
|
|
{
|
|
key: 'user',
|
|
header: '사용자',
|
|
className: 'w-[280px] px-5 py-3 font-medium',
|
|
cell: (row) => (
|
|
<div className="flex min-w-0 items-center gap-3">
|
|
<UserAvatar session={row} />
|
|
<div className="min-w-0">
|
|
<p className="truncate font-semibold">{row.user}</p>
|
|
{row.email ? <p className="mt-1 truncate text-xs font-medium text-muted-foreground">{row.email}</p> : null}
|
|
</div>
|
|
</div>
|
|
)
|
|
},
|
|
{ key: 'session', header: '세션', className: 'w-28 px-5 py-3', cell: (row) => <span className="font-mono text-xs text-muted-foreground">{row.session}</span> },
|
|
{ key: 'expiresAt', header: '만료', className: 'w-40 px-5 py-3', cell: (row) => <span className="whitespace-nowrap text-muted-foreground">{formatDateTime(row.expiresAt)}</span> },
|
|
{ key: 'lastSeen', header: '최근 접속', className: 'w-40 px-5 py-3', cell: (row) => <span className="whitespace-nowrap text-muted-foreground">{formatDateTime(row.lastSeen)}</span> },
|
|
{ key: 'state', header: '상태', className: 'w-24 px-5 py-3', cell: (row) => <StatusBadge status={row.state} /> },
|
|
{
|
|
key: 'actions',
|
|
header: '',
|
|
className: 'w-20 px-5 py-3 text-right font-medium',
|
|
cell: (row) => (
|
|
<div className="relative flex justify-end">
|
|
<Button
|
|
variant="ghost"
|
|
className="size-8 px-0"
|
|
title="메뉴"
|
|
aria-label="메뉴"
|
|
onClick={(event) => {
|
|
event.stopPropagation();
|
|
const rect = event.currentTarget.getBoundingClientRect();
|
|
setMenuSession((prev) => (prev?.id === row.id ? null : { id: row.id, x: rect.right, y: rect.bottom + 6 }));
|
|
}}
|
|
>
|
|
<MoreHorizontal className="size-4" />
|
|
</Button>
|
|
</div>
|
|
)
|
|
}
|
|
],
|
|
[menuSession?.id]
|
|
);
|
|
|
|
const filters = useMemo<Array<DataTableFilter<UserSession>>>(
|
|
() => [
|
|
{ key: 'state', label: '상태', options: [...new Set(sessions.map((row) => row.state))].map((state) => ({ label: state, value: state })), getValue: (row) => row.state }
|
|
],
|
|
[sessions]
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<PageTitle eyebrow="운영" title="사용자/세션" description="사용자의 유효토큰, 만료, 종료 상태를 확인하고 필요한 제어 작업을 실행합니다." />
|
|
{failed ? (
|
|
<div className="mb-4 rounded-md border border-rose-500/30 bg-rose-500/10 px-4 py-3 text-sm text-rose-200">
|
|
사용자/세션 데이터를 불러오지 못했습니다.
|
|
</div>
|
|
) : null}
|
|
<DataTableCard
|
|
title="목록"
|
|
rows={rows}
|
|
columns={columns}
|
|
filters={filters}
|
|
searchPlaceholder="사용자, 이메일, 세션 검색"
|
|
getSearchText={(row) => `${row.user} ${row.email ?? ''} ${row.session} ${row.state}`}
|
|
getRowKey={(row) => row.id}
|
|
pageSize={6}
|
|
minWidth={900}
|
|
loading={loading}
|
|
emptyMessage="표시할 사용자/세션 데이터가 없습니다."
|
|
/>
|
|
{menuSession ? (() => {
|
|
const row = rows.find((item) => item.id === menuSession.id);
|
|
if (!row) return null;
|
|
const locked = row.state === '잠김';
|
|
return (
|
|
<div onClick={(event) => event.stopPropagation()} className="fixed z-[70] min-w-32 overflow-hidden rounded-md border border-border bg-card py-1 text-left shadow-xl" style={{ left: menuSession.x, top: menuSession.y, transform: 'translateX(-100%)' }}>
|
|
<button
|
|
type="button"
|
|
className="block w-full px-3 py-2 text-left text-xs text-foreground hover:bg-muted"
|
|
onClick={() => {
|
|
setMenuSession(null);
|
|
setDetailSession(row);
|
|
}}
|
|
>
|
|
상세보기
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="block w-full px-3 py-2 text-left text-xs text-foreground hover:bg-muted"
|
|
onClick={() => {
|
|
setMenuSession(null);
|
|
const request = locked ? unlockUser(row.userId) : lockUser(row.userId);
|
|
request.then(refresh).catch(() => setFailed(true));
|
|
}}
|
|
>
|
|
{locked ? '잠금 해제' : '잠금'}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="block w-full px-3 py-2 text-left text-xs text-foreground hover:bg-muted disabled:cursor-not-allowed disabled:text-muted-foreground disabled:hover:bg-transparent"
|
|
disabled={!row.sessionId || row.state === '종료' || row.state === '만료'}
|
|
onClick={() => {
|
|
if (!row.sessionId) return;
|
|
setMenuSession(null);
|
|
terminateUserSession(row.sessionId).then(refresh).catch(() => setFailed(true));
|
|
}}
|
|
>
|
|
유효토큰 종료
|
|
</button>
|
|
</div>
|
|
);
|
|
})() : null}
|
|
<Modal
|
|
open={Boolean(detailSession)}
|
|
title="사용자 상세 정보"
|
|
onClose={() => setDetailSession(null)}
|
|
className="!max-w-2xl"
|
|
footer={<Button variant="outline" onClick={() => setDetailSession(null)}>닫기</Button>}
|
|
>
|
|
{detailSession ? (
|
|
<div className="space-y-5">
|
|
<div className="flex min-w-0 items-start justify-between gap-4 rounded-md border border-border bg-muted/20 px-4 py-3">
|
|
<div className="flex min-w-0 items-center gap-3">
|
|
<UserAvatar session={detailSession} size="lg" />
|
|
<div className="min-w-0">
|
|
<p className="truncate text-sm font-semibold text-foreground">{detailSession.user}</p>
|
|
<p className="mt-1 truncate text-xs text-muted-foreground">{detailSession.email ?? '-'}</p>
|
|
</div>
|
|
</div>
|
|
<StatusBadge status={detailSession.state} />
|
|
</div>
|
|
<div className="grid gap-3 text-sm">
|
|
<DetailItem label="사용자 ID" value={detailSession.userId} mono />
|
|
<DetailItem label="조직" value={detailSession.primaryAffiliation?.organizationName ?? '-'} />
|
|
<DetailItem label="조직 유형" value={organizationTypeLabel(detailSession.primaryAffiliation?.organizationType)} />
|
|
<DetailItem label="부서" value={detailSession.primaryAffiliation?.department ?? '-'} />
|
|
<DetailItem label="직책" value={detailSession.primaryAffiliation?.position ?? '-'} />
|
|
<DetailItem label="세션 ID" value={detailSession.sessionId ?? '-'} mono />
|
|
<DetailItem label="세션" value={detailSession.session} mono />
|
|
<DetailItem label="상태" value={detailSession.state} />
|
|
<DetailItem label="만료" value={formatDateTime(detailSession.expiresAt)} />
|
|
<DetailItem label="최근 접속" value={formatDateTime(detailSession.lastSeen)} />
|
|
</div>
|
|
</div>
|
|
) : null}
|
|
</Modal>
|
|
<LoadingModal open={loadingVisible} />
|
|
</>
|
|
);
|
|
}
|
|
|
|
function organizationTypeLabel(type?: string | null) {
|
|
const labels: Record<string, string> = {
|
|
COMPANY: '회사',
|
|
SCHOOL: '학교',
|
|
GOVERNMENT: '공공기관',
|
|
LAB: '연구소',
|
|
OTHER: '기타'
|
|
};
|
|
|
|
return type ? labels[type] ?? type : '-';
|
|
}
|
|
|
|
function UserAvatar({ session, size = 'sm' }: { session: UserSession; size?: 'sm' | 'lg' }) {
|
|
const [failed, setFailed] = useState(false);
|
|
const initial = (session.user || session.email || '?').trim().charAt(0).toUpperCase() || '?';
|
|
const sizeClassName = size === 'lg' ? 'size-14 text-lg' : 'size-9 text-sm';
|
|
|
|
if (session.avatarUrl && !failed) {
|
|
return (
|
|
<img
|
|
src={session.avatarUrl}
|
|
alt=""
|
|
className={cn(sizeClassName, 'shrink-0 rounded-full border border-border bg-muted object-cover')}
|
|
onError={() => setFailed(true)}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<span className={cn(sizeClassName, 'inline-flex shrink-0 items-center justify-center rounded-full border border-border bg-muted font-semibold text-muted-foreground')}>
|
|
{initial}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
function DetailItem({ label, value, mono = false }: { label: string; value: string; mono?: boolean }) {
|
|
return (
|
|
<label className="block min-w-0">
|
|
<span className="text-xs font-medium text-muted-foreground">{label}</span>
|
|
<div className={cn('mt-2 min-h-9 w-full rounded-md border border-input bg-background px-3 py-2 text-sm font-medium text-foreground', mono && 'break-all font-mono text-xs font-normal')}>
|
|
{value || '-'}
|
|
</div>
|
|
</label>
|
|
);
|
|
}
|