anotation-frontend/src/layouts/components/Sidebar/UserInfo.tsx

95 lines
3.7 KiB
TypeScript

import React from 'react'
import { useNavigate } from 'react-router-dom'
import { useAuthStore } from '@/stores/authStore'
import { showToast } from '../../../components/feedback/Toast'
const ROLE_LABELS: Record<string, string> = {
admin: 'Quản trị viên',
manager: 'Quản lý dự án',
reviewer: 'Người kiểm duyệt',
annotator: 'Người gán nhãn',
}
function initialsOf(name: string): string {
const parts = name.trim().split(/\s+/)
if (parts.length >= 2) {
return `${parts[0]?.[0] ?? ''}${parts[parts.length - 1]?.[0] ?? ''}`.toUpperCase()
}
return (name.trim().slice(0, 2) || '??').toUpperCase()
}
export const UserInfo: React.FC<{ isProjectWorkspace?: boolean }> = ({ isProjectWorkspace = false }) => {
const navigate = useNavigate()
const user = useAuthStore((s) => s.user)
const storeLogout = useAuthStore((s) => s.logout)
const displayName = user?.name ?? 'Người dùng'
const displayRole = (user?.role && ROLE_LABELS[user.role]) || 'Thành viên'
const handleLogout = () => {
storeLogout()
showToast.success('Đã đăng xuất khỏi phiên làm việc')
navigate('/login')
}
return (
<div className="space-y-3">
<button
type="button"
onClick={() => showToast.info('Tài liệu hướng dẫn sử dụng danable v1.0')}
className={`flex w-full items-center gap-2 text-xs font-medium text-slate-600 transition-colors hover:text-slate-900 ${isProjectWorkspace ? 'px-2.5 py-2 text-[11px]' : 'px-2 py-1.5'}`}
>
{isProjectWorkspace ? <img className="h-4 w-4" src="/assets/projects-ui/circle-help.svg" alt="" /> : <svg
className={`h-4 w-4 ${isProjectWorkspace ? 'text-[#697386]' : 'text-slate-400'}`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={1.8}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M8.228 9c.549-1.165 2.03-2 3.772-2 2.21 0 4 1.343 4 3 0 1.4-1.278 2.575-3.006 2.907-.542.104-.994.54-.994 1.093m0 3h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>}
<span>Tài liệu & hỗ trợ</span>
</button>
<div className={`flex items-center justify-between ${isProjectWorkspace ? 'border-t border-[#2d374d] pt-[18px]' : 'border-t border-slate-200 pt-3'}`}>
<div className="flex items-center gap-2.5 min-w-0">
<div className={`flex shrink-0 items-center justify-center rounded-full font-semibold ${isProjectWorkspace ? 'h-[34px] w-[34px] bg-[#f5ded6] text-[11px] text-[#332926]' : 'h-8 w-8 bg-orange-100 text-xs text-slate-800'}`}>
{initialsOf(displayName)}
</div>
<div className="min-w-0">
<div className={`truncate font-semibold ${isProjectWorkspace ? 'text-[12px] text-[#332926]' : 'text-xs text-slate-900'}`}>
{displayName}
</div>
<div className={`truncate ${isProjectWorkspace ? 'text-[10px] text-[#332926]' : 'text-[11px] text-slate-500'}`}>
{displayRole}
</div>
</div>
</div>
{!isProjectWorkspace && <button
type="button"
onClick={handleLogout}
title="Đăng xuất"
className="rounded-lg p-1.5 text-slate-400 hover:bg-slate-100 hover:text-slate-700 transition-colors"
>
<svg
className="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={1.8}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"
/>
</svg>
</button>}
</div>
</div>
)
}