feat: scaffold complete danable frontend architecture with all pages, layouts, stores, and 3 annotator modes
4
.env
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
VITE_API_URL=http://localhost:8000/api/v1
|
||||||
|
VITE_APP_NAME=danable
|
||||||
|
VITE_USE_MOCK_DATA=true
|
||||||
|
|
||||||
4
.env.example
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
VITE_API_URL=http://localhost:8000/api/v1
|
||||||
|
VITE_APP_NAME=danable
|
||||||
|
VITE_USE_MOCK_DATA=true
|
||||||
|
|
||||||
7
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
*.local
|
||||||
|
.env.local
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
50
README.md
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
# danable · Nền tảng dữ liệu gán nhãn cho AI (Frontend)
|
||||||
|
|
||||||
|
Dự án frontend cho nền tảng gán nhãn dữ liệu hình ảnh **danable** (hỗ trợ 3 bài toán: **Classification**, **Object Detection**, và **Segmentation**), được thiết kế theo kiến trúc phân lớp rõ ràng để sẵn sàng tích hợp với Backend / Orchestrator.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Khởi chạy dự án
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Cài đặt dependencies
|
||||||
|
npm install
|
||||||
|
|
||||||
|
# Chạy môi trường phát triển (http://localhost:3000)
|
||||||
|
npm run dev
|
||||||
|
|
||||||
|
# Kiểm tra TypeScript & build production
|
||||||
|
npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Cấu trúc thư mục
|
||||||
|
|
||||||
|
- `public/` — Tài nguyên tĩnh (`favicon.svg`, ảnh minh họa...)
|
||||||
|
- `src/app/` — Cấu hình cấp ứng dụng (`router.tsx`, `providers.tsx`, `constants.ts`)
|
||||||
|
- `src/layouts/` — 3 Layout chính (`AuthLayout`, `DashboardLayout`, `AnnotatorLayout`) cùng `Sidebar` & `Header`
|
||||||
|
- `src/pages/` — Mỗi trang được tách thành folder riêng kèm `components/` nội bộ:
|
||||||
|
- `auth/` — Đăng nhập (`/login`), Quên mật khẩu (`/forgot-password`)
|
||||||
|
- `projects/` — Danh sách dự án (`/projects`), Modal tạo dự án, Tổng quan dự án (`/projects/:id`)
|
||||||
|
- `users/` — Quản lý người dùng & phân quyền (`/users`)
|
||||||
|
- `annotator/` — Giao diện gán nhãn 3-panel (`/projects/:id/annotate`) tự động chuyển đổi theo bài toán (`Classification`, `Object Detection`, `Segmentation`)
|
||||||
|
- `src/components/` — Thư viện UI dùng chung (`ui/`, `feedback/`, `data-display/`)
|
||||||
|
- `src/services/` — Tầng giao tiếp API duy nhất (`api.ts`, `projectService.ts`, `annotationService.ts`...)
|
||||||
|
- `src/stores/` — Quản lý state với Zustand (`authStore`, `projectStore`, `annotationStore`, `canvasStore`, `uiStore`)
|
||||||
|
- `src/hooks/` — Custom hooks tách biệt business logic khỏi giao diện
|
||||||
|
- `src/types/` — Định nghĩa TypeScript interfaces/types dùng chung
|
||||||
|
- `src/utils/` — Hàm tiện ích (chuyển đổi tọa độ canvas, xuất LabelMe JSON, xuất mask PNG, định dạng số...)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Hướng dẫn nối vào Backend / Orchestrator
|
||||||
|
|
||||||
|
1. Mở file `.env` và cập nhật địa chỉ API Gateway / Orchestrator:
|
||||||
|
```env
|
||||||
|
VITE_API_URL=https://orchestrator.your-domain.com/api/v1
|
||||||
|
VITE_USE_MOCK_DATA=false
|
||||||
|
```
|
||||||
|
2. Toàn bộ HTTP client được cấu hình tập trung tại `src/services/api.ts` (đã gắn sẵn Bearer Token interceptor).
|
||||||
|
3. Các hàm trong `src/services/*Service.ts` đã định nghĩa đầy đủ chữ ký hàm và kiểu trả về chuẩn theo `src/types/`.
|
||||||
|
|
||||||
BIN
img-plan/MVP · Annotator Classification.png
Normal file
|
After Width: | Height: | Size: 684 KiB |
BIN
img-plan/MVP · Annotator Object Detection.png
Normal file
|
After Width: | Height: | Size: 744 KiB |
BIN
img-plan/MVP · Annotator Segmentation.png
Normal file
|
After Width: | Height: | Size: 747 KiB |
BIN
img-plan/MVP · Project dashboard.png
Normal file
|
After Width: | Height: | Size: 129 KiB |
BIN
img-plan/MVP · Quản lý dự án.png
Normal file
|
After Width: | Height: | Size: 476 KiB |
BIN
img-plan/MVP · Quản lý người dùng.png
Normal file
|
After Width: | Height: | Size: 116 KiB |
BIN
img-plan/MVP · Tạo dự án.png
Normal file
|
After Width: | Height: | Size: 42 KiB |
BIN
img-plan/MVP · Wireframe flow tổng thể.png
Normal file
|
After Width: | Height: | Size: 90 KiB |
BIN
img-plan/MVP · Đăng nhập.png
Normal file
|
After Width: | Height: | Size: 365 KiB |
17
index.html
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="vi">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>danable · Nền tảng dữ liệu cho AI</title>
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body class="bg-slate-50 text-slate-900 antialiased selection:bg-brand-500 selection:text-white">
|
||||||
|
<div id="root"></div>
|
||||||
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
4364
package-lock.json
generated
Normal file
35
package.json
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
{
|
||||||
|
"name": "danable",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.1.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "tsc -b && vite build",
|
||||||
|
"lint": "eslint .",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"react": "^18.3.1",
|
||||||
|
"react-dom": "^18.3.1",
|
||||||
|
"react-router-dom": "^6.28.0",
|
||||||
|
"zustand": "^5.0.0",
|
||||||
|
"@tanstack/react-query": "^5.60.0",
|
||||||
|
"axios": "^1.7.0",
|
||||||
|
"react-konva": "^18.2.10",
|
||||||
|
"konva": "^9.3.0",
|
||||||
|
"clsx": "^2.1.1",
|
||||||
|
"react-hot-toast": "^2.4.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/react": "^18.3.12",
|
||||||
|
"@types/react-dom": "^18.3.1",
|
||||||
|
"@vitejs/plugin-react": "^4.3.4",
|
||||||
|
"autoprefixer": "^10.4.20",
|
||||||
|
"postcss": "^8.4.49",
|
||||||
|
"tailwindcss": "^3.4.15",
|
||||||
|
"typescript": "~5.6.2",
|
||||||
|
"vite": "^6.0.0",
|
||||||
|
"eslint": "^9.13.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
7
postcss.config.js
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
export default {
|
||||||
|
plugins: {
|
||||||
|
tailwindcss: {},
|
||||||
|
autoprefixer: {},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
8
public/favicon.svg
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" fill="none">
|
||||||
|
<rect width="36" height="36" rx="8" fill="#C95C3C"/>
|
||||||
|
<path d="M10 14V11C10 10.4477 10.4477 10 11 10H14" stroke="white" stroke-width="2.5" stroke-linecap="round"/>
|
||||||
|
<path d="M22 10H25C25.5523 10 26 10.4477 26 11V14" stroke="white" stroke-width="2.5" stroke-linecap="round"/>
|
||||||
|
<path d="M26 22V25C26 25.5523 25.5523 26 25 26H22" stroke="white" stroke-width="2.5" stroke-linecap="round"/>
|
||||||
|
<path d="M14 26H11C10.4477 26 10 25.5523 10 25V22" stroke="white" stroke-width="2.5" stroke-linecap="round"/>
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
After Width: | Height: | Size: 584 B |
14
src/App.tsx
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { AppProviders } from '@/app/providers'
|
||||||
|
import { AppRouter } from '@/app/router'
|
||||||
|
|
||||||
|
export const App: React.FC = () => {
|
||||||
|
return (
|
||||||
|
<AppProviders>
|
||||||
|
<AppRouter />
|
||||||
|
</AppProviders>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App
|
||||||
|
|
||||||
61
src/app/constants.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
import { ImageAnnotationStatus, ProjectStatus, TaskType, UserRole, UserStatus } from '@/types'
|
||||||
|
|
||||||
|
export const APP_NAME = 'danable'
|
||||||
|
export const APP_TAGLINE = 'Nền tảng gán nhãn dữ liệu thị giác máy tính'
|
||||||
|
|
||||||
|
export const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000/api/v1'
|
||||||
|
export const USE_MOCK_DATA = import.meta.env.VITE_USE_MOCK_DATA !== 'false'
|
||||||
|
|
||||||
|
export const STORAGE_KEYS = {
|
||||||
|
ACCESS_TOKEN: 'danable_access_token',
|
||||||
|
REFRESH_TOKEN: 'danable_refresh_token',
|
||||||
|
CURRENT_USER: 'danable_current_user',
|
||||||
|
MOCK_DB: 'danable_mock_db_v1',
|
||||||
|
THEME: 'danable_theme',
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const ROLE_LABELS: Record<UserRole, string> = {
|
||||||
|
admin: 'Admin',
|
||||||
|
manager: 'Manager',
|
||||||
|
annotator: 'Annotator',
|
||||||
|
reviewer: 'Reviewer',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ROLE_VI_LABELS: Record<UserRole, string> = {
|
||||||
|
admin: 'Quản trị viên (Admin)',
|
||||||
|
manager: 'Quản lý dự án (Manager)',
|
||||||
|
annotator: 'Người gán nhãn (Annotator)',
|
||||||
|
reviewer: 'Người kiểm duyệt (Reviewer)',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const USER_STATUS_LABELS: Record<UserStatus, string> = {
|
||||||
|
active: 'Đang hoạt động',
|
||||||
|
invited: 'Đã gửi lời mời',
|
||||||
|
suspended: 'Tạm khóa',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TASK_TYPE_LABELS: Record<TaskType, string> = {
|
||||||
|
classification: 'Classification',
|
||||||
|
object_detection: 'Object Detection',
|
||||||
|
segmentation: 'Segmentation',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TASK_TYPE_VI_LABELS: Record<TaskType, string> = {
|
||||||
|
classification: 'Phân loại ảnh (Classification)',
|
||||||
|
object_detection: 'Phát hiện vật thể (Object Detection)',
|
||||||
|
segmentation: 'Phân vùng ảnh (Segmentation)',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const PROJECT_STATUS_LABELS: Record<ProjectStatus, string> = {
|
||||||
|
annotating: 'Đang gán nhãn',
|
||||||
|
pending_review: 'Chờ duyệt',
|
||||||
|
completed: 'Hoàn thành',
|
||||||
|
draft: 'Bản nháp',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const IMAGE_STATUS_LABELS: Record<ImageAnnotationStatus, string> = {
|
||||||
|
unassigned: 'Chưa gán',
|
||||||
|
in_progress: 'Đang làm',
|
||||||
|
pending_review: 'Chờ duyệt',
|
||||||
|
completed: 'Hoàn thành',
|
||||||
|
}
|
||||||
39
src/app/providers.tsx
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
|
import { Toaster } from 'react-hot-toast'
|
||||||
|
|
||||||
|
const queryClient = new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
staleTime: 1000 * 60 * 5,
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
|
retry: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
interface AppProvidersProps {
|
||||||
|
children: React.ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AppProviders: React.FC<AppProvidersProps> = ({ children }) => {
|
||||||
|
return (
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
{children}
|
||||||
|
<Toaster
|
||||||
|
position="bottom-right"
|
||||||
|
toastOptions={{
|
||||||
|
duration: 2800,
|
||||||
|
style: {
|
||||||
|
background: '#1e293b',
|
||||||
|
color: '#f8fafc',
|
||||||
|
fontSize: '13px',
|
||||||
|
borderRadius: '10px',
|
||||||
|
padding: '10px 14px',
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</QueryClientProvider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
112
src/app/router.tsx
Normal file
|
|
@ -0,0 +1,112 @@
|
||||||
|
import React, { Suspense, lazy } from 'react'
|
||||||
|
import { createBrowserRouter, Navigate, RouterProvider } from 'react-router-dom'
|
||||||
|
import { AuthLayout } from '@/layouts/AuthLayout'
|
||||||
|
import { DashboardLayout } from '@/layouts/DashboardLayout'
|
||||||
|
import { AnnotatorLayout } from '@/layouts/AnnotatorLayout'
|
||||||
|
|
||||||
|
const LoginPage = lazy(() =>
|
||||||
|
import('@/pages/auth/LoginPage').then((m) => ({ default: m.LoginPage }))
|
||||||
|
)
|
||||||
|
const ForgotPasswordPage = lazy(() =>
|
||||||
|
import('@/pages/auth/ForgotPasswordPage').then((m) => ({ default: m.ForgotPasswordPage }))
|
||||||
|
)
|
||||||
|
const ProjectListPage = lazy(() =>
|
||||||
|
import('@/pages/projects/ProjectListPage').then((m) => ({ default: m.ProjectListPage }))
|
||||||
|
)
|
||||||
|
const ProjectDetailPage = lazy(() =>
|
||||||
|
import('@/pages/projects/ProjectDetailPage').then((m) => ({ default: m.ProjectDetailPage }))
|
||||||
|
)
|
||||||
|
const UserManagementPage = lazy(() =>
|
||||||
|
import('@/pages/users/UserManagementPage').then((m) => ({ default: m.UserManagementPage }))
|
||||||
|
)
|
||||||
|
const AnnotatorPage = lazy(() =>
|
||||||
|
import('@/pages/annotator/AnnotatorPage').then((m) => ({ default: m.AnnotatorPage }))
|
||||||
|
)
|
||||||
|
|
||||||
|
const PageLoader: React.FC = () => (
|
||||||
|
<div className="flex h-full min-h-[360px] w-full items-center justify-center">
|
||||||
|
<div className="flex flex-col items-center gap-3">
|
||||||
|
<div className="h-8 w-8 animate-spin rounded-full border-2 border-slate-200 border-t-[#C95C3C]" />
|
||||||
|
<span className="text-xs font-medium text-slate-500">Đang tải giao diện...</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const router = createBrowserRouter([
|
||||||
|
{
|
||||||
|
path: '/',
|
||||||
|
element: <Navigate to="/projects" replace />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
element: <AuthLayout />,
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
path: '/login',
|
||||||
|
element: (
|
||||||
|
<Suspense fallback={<PageLoader />}>
|
||||||
|
<LoginPage />
|
||||||
|
</Suspense>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/forgot-password',
|
||||||
|
element: (
|
||||||
|
<Suspense fallback={<PageLoader />}>
|
||||||
|
<ForgotPasswordPage />
|
||||||
|
</Suspense>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
element: <DashboardLayout />,
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
path: '/projects',
|
||||||
|
element: (
|
||||||
|
<Suspense fallback={<PageLoader />}>
|
||||||
|
<ProjectListPage />
|
||||||
|
</Suspense>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/projects/:id',
|
||||||
|
element: (
|
||||||
|
<Suspense fallback={<PageLoader />}>
|
||||||
|
<ProjectDetailPage />
|
||||||
|
</Suspense>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/users',
|
||||||
|
element: (
|
||||||
|
<Suspense fallback={<PageLoader />}>
|
||||||
|
<UserManagementPage />
|
||||||
|
</Suspense>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
element: <AnnotatorLayout />,
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
path: '/projects/:id/annotate',
|
||||||
|
element: (
|
||||||
|
<Suspense fallback={<PageLoader />}>
|
||||||
|
<AnnotatorPage />
|
||||||
|
</Suspense>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '*',
|
||||||
|
element: <Navigate to="/projects" replace />,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
export const AppRouter: React.FC = () => {
|
||||||
|
return <RouterProvider router={router} />
|
||||||
|
}
|
||||||
|
|
||||||
72
src/components/data-display/DataTable.tsx
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
import React from 'react'
|
||||||
|
import {
|
||||||
|
TableContainer,
|
||||||
|
TableHead,
|
||||||
|
TableBody,
|
||||||
|
TableRow,
|
||||||
|
TableHeaderCell,
|
||||||
|
TableCell,
|
||||||
|
} from '../ui/Table'
|
||||||
|
import { EmptyState } from '../feedback/EmptyState'
|
||||||
|
|
||||||
|
export interface DataTableColumn<T> {
|
||||||
|
key: string
|
||||||
|
header: string
|
||||||
|
className?: string
|
||||||
|
render: (item: T, index: number) => React.ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DataTableProps<T> {
|
||||||
|
columns: DataTableColumn<T>[]
|
||||||
|
data: T[]
|
||||||
|
keyExtractor: (item: T, index: number) => string
|
||||||
|
emptyTitle?: string
|
||||||
|
emptyDescription?: string
|
||||||
|
onRowClick?: (item: T) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DataTable<T>({
|
||||||
|
columns,
|
||||||
|
data,
|
||||||
|
keyExtractor,
|
||||||
|
emptyTitle = 'Không có dữ liệu',
|
||||||
|
emptyDescription = 'Chưa có bản ghi nào phù hợp với bộ lọc hiện tại.',
|
||||||
|
onRowClick,
|
||||||
|
}: DataTableProps<T>) {
|
||||||
|
if (data.length === 0) {
|
||||||
|
return (
|
||||||
|
<div className="p-6">
|
||||||
|
<EmptyState title={emptyTitle} description={emptyDescription} />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TableContainer>
|
||||||
|
<TableHead>
|
||||||
|
<tr>
|
||||||
|
{columns.map((col) => (
|
||||||
|
<TableHeaderCell key={col.key} className={col.className}>
|
||||||
|
{col.header}
|
||||||
|
</TableHeaderCell>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</TableHead>
|
||||||
|
<TableBody>
|
||||||
|
{data.map((item, idx) => (
|
||||||
|
<TableRow
|
||||||
|
key={keyExtractor(item, idx)}
|
||||||
|
onClick={onRowClick ? () => onRowClick(item) : undefined}
|
||||||
|
className={onRowClick ? 'cursor-pointer' : undefined}
|
||||||
|
>
|
||||||
|
{columns.map((col) => (
|
||||||
|
<TableCell key={col.key} className={col.className}>
|
||||||
|
{col.render(item, idx)}
|
||||||
|
</TableCell>
|
||||||
|
))}
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</TableContainer>
|
||||||
|
)
|
||||||
|
}
|
||||||
87
src/components/data-display/Pagination.tsx
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface PaginationProps {
|
||||||
|
currentPage: number
|
||||||
|
totalPages: number
|
||||||
|
totalItems?: number
|
||||||
|
pageSize?: number
|
||||||
|
onPageChange: (page: number) => void
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Pagination: React.FC<PaginationProps> = ({
|
||||||
|
currentPage,
|
||||||
|
totalPages,
|
||||||
|
totalItems,
|
||||||
|
pageSize = 10,
|
||||||
|
onPageChange,
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
if (totalPages <= 1 && !totalItems) return null
|
||||||
|
|
||||||
|
const startItem = totalItems ? (currentPage - 1) * pageSize + 1 : 0
|
||||||
|
const endItem = totalItems ? Math.min(currentPage * pageSize, totalItems) : 0
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'flex flex-col sm:flex-row items-center justify-between gap-3 px-5 py-3.5 border-t border-slate-100 text-xs text-slate-500',
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
{totalItems !== undefined ? (
|
||||||
|
<span>
|
||||||
|
Hiển thị <strong className="font-semibold text-slate-700">{startItem}</strong> -{' '}
|
||||||
|
<strong className="font-semibold text-slate-700">{endItem}</strong> trong tổng số{' '}
|
||||||
|
<strong className="font-semibold text-slate-700">{totalItems}</strong> mục
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span>
|
||||||
|
Trang <strong className="font-semibold text-slate-700">{currentPage}</strong> /{' '}
|
||||||
|
{totalPages}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-1.5">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={currentPage <= 1}
|
||||||
|
onClick={() => onPageChange(currentPage - 1)}
|
||||||
|
className="px-3 py-1.5 rounded-lg border border-slate-200 bg-white text-slate-700 font-medium hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"
|
||||||
|
>
|
||||||
|
Trước
|
||||||
|
</button>
|
||||||
|
{Array.from({ length: totalPages }).map((_, i) => {
|
||||||
|
const page = i + 1
|
||||||
|
const active = page === currentPage
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={page}
|
||||||
|
type="button"
|
||||||
|
onClick={() => onPageChange(page)}
|
||||||
|
className={clsx(
|
||||||
|
'h-7 w-7 rounded-lg text-xs font-semibold transition-colors',
|
||||||
|
active
|
||||||
|
? 'bg-[#C95C3C] text-white'
|
||||||
|
: 'text-slate-600 hover:bg-slate-100'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{page}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={currentPage >= totalPages}
|
||||||
|
onClick={() => onPageChange(currentPage + 1)}
|
||||||
|
className="px-3 py-1.5 rounded-lg border border-slate-200 bg-white text-slate-700 font-medium hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"
|
||||||
|
>
|
||||||
|
Sau
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
55
src/components/data-display/StatCard.tsx
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface StatCardProps {
|
||||||
|
label: string
|
||||||
|
value: string | number
|
||||||
|
subtitle?: string
|
||||||
|
icon?: React.ReactNode
|
||||||
|
trend?: {
|
||||||
|
value: string
|
||||||
|
positive?: boolean
|
||||||
|
}
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const StatCard: React.FC<StatCardProps> = ({
|
||||||
|
label,
|
||||||
|
value,
|
||||||
|
subtitle,
|
||||||
|
icon,
|
||||||
|
trend,
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'rounded-2xl bg-white border border-slate-200/80 p-5 shadow-sm flex flex-col justify-between',
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-xs font-medium text-slate-500">{label}</span>
|
||||||
|
{icon && <div className="text-slate-400">{icon}</div>}
|
||||||
|
</div>
|
||||||
|
<div className="mt-2.5 text-3xl font-bold tracking-tight text-slate-900">
|
||||||
|
{value}
|
||||||
|
</div>
|
||||||
|
{(subtitle || trend) && (
|
||||||
|
<div className="mt-2.5 flex items-center gap-1.5 text-xs text-slate-500">
|
||||||
|
{trend && (
|
||||||
|
<span
|
||||||
|
className={clsx(
|
||||||
|
'font-semibold',
|
||||||
|
trend.positive ? 'text-emerald-600' : 'text-amber-600'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{trend.value}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{subtitle && <span>{subtitle}</span>}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
59
src/components/feedback/EmptyState.tsx
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { Button } from '../ui/Button'
|
||||||
|
|
||||||
|
export interface EmptyStateProps {
|
||||||
|
title: string
|
||||||
|
description?: string
|
||||||
|
actionLabel?: string
|
||||||
|
onAction?: () => void
|
||||||
|
icon?: React.ReactNode
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const EmptyState: React.FC<EmptyStateProps> = ({
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
actionLabel,
|
||||||
|
onAction,
|
||||||
|
icon,
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'flex flex-col items-center justify-center rounded-2xl border border-dashed border-slate-200 bg-slate-50/40 px-6 py-12 text-center',
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="mb-3 flex h-12 w-12 items-center justify-center rounded-full bg-orange-50 text-[#C95C3C]">
|
||||||
|
{icon || (
|
||||||
|
<svg
|
||||||
|
className="h-6 w-6"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={1.8}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<h4 className="text-base font-semibold text-slate-900">{title}</h4>
|
||||||
|
{description && (
|
||||||
|
<p className="mt-1 max-w-sm text-sm text-slate-500">{description}</p>
|
||||||
|
)}
|
||||||
|
{actionLabel && onAction && (
|
||||||
|
<div className="mt-4">
|
||||||
|
<Button variant="primary" size="sm" onClick={onAction}>
|
||||||
|
{actionLabel}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
70
src/components/feedback/ErrorBoundary.tsx
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
import React, { Component, ErrorInfo, ReactNode } from 'react'
|
||||||
|
import { Button } from '../ui/Button'
|
||||||
|
|
||||||
|
export interface ErrorBoundaryProps {
|
||||||
|
children: ReactNode
|
||||||
|
fallback?: ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ErrorBoundaryState {
|
||||||
|
hasError: boolean
|
||||||
|
error: Error | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
||||||
|
public state: ErrorBoundaryState = {
|
||||||
|
hasError: false,
|
||||||
|
error: null,
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getDerivedStateFromError(error: Error): ErrorBoundaryState {
|
||||||
|
return { hasError: true, error }
|
||||||
|
}
|
||||||
|
|
||||||
|
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
||||||
|
console.error('ErrorBoundary caught error:', error, errorInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleReset = () => {
|
||||||
|
this.setState({ hasError: false, error: null })
|
||||||
|
}
|
||||||
|
|
||||||
|
public render() {
|
||||||
|
if (this.state.hasError) {
|
||||||
|
if (this.props.fallback) {
|
||||||
|
return this.props.fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex min-h-[320px] flex-col items-center justify-center rounded-2xl border border-rose-200 bg-rose-50/40 p-8 text-center">
|
||||||
|
<div className="mb-3 flex h-12 w-12 items-center justify-center rounded-full bg-rose-100 text-rose-600">
|
||||||
|
<svg
|
||||||
|
className="h-6 w-6"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={2}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M12 9v2m0 4h.01M5.07 19h13.86c1.54 0 2.5-1.67 1.73-3L13.73 4c-.77-1.33-2.69-1.33-3.46 0L3.34 16c-.77 1.33.19 3 1.73 3z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<h3 className="text-lg font-semibold text-slate-900">Đã xảy ra lỗi hiển thị</h3>
|
||||||
|
<p className="mt-1 max-w-md text-sm text-slate-600">
|
||||||
|
{this.state.error?.message || 'Một lỗi không mong muốn đã xảy ra. Vui lòng thử tải lại.'}
|
||||||
|
</p>
|
||||||
|
<div className="mt-4">
|
||||||
|
<Button variant="primary" size="sm" onClick={this.handleReset}>
|
||||||
|
Thử lại
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.props.children
|
||||||
|
}
|
||||||
|
}
|
||||||
39
src/components/feedback/LoadingSkeleton.tsx
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface LoadingSkeletonProps {
|
||||||
|
rows?: number
|
||||||
|
className?: string
|
||||||
|
cardCount?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export const LoadingSkeleton: React.FC<LoadingSkeletonProps> = ({
|
||||||
|
rows = 3,
|
||||||
|
cardCount,
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
if (cardCount && cardCount > 0) {
|
||||||
|
return (
|
||||||
|
<div className={clsx('grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4', className)}>
|
||||||
|
{Array.from({ length: cardCount }).map((_, idx) => (
|
||||||
|
<div
|
||||||
|
key={idx}
|
||||||
|
className="animate-pulse rounded-2xl border border-slate-200 bg-white p-5 space-y-3"
|
||||||
|
>
|
||||||
|
<div className="h-3.5 w-24 rounded bg-slate-200" />
|
||||||
|
<div className="h-8 w-20 rounded bg-slate-200" />
|
||||||
|
<div className="h-3 w-32 rounded bg-slate-100" />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={clsx('animate-pulse space-y-3', className)}>
|
||||||
|
{Array.from({ length: rows }).map((_, idx) => (
|
||||||
|
<div key={idx} className="h-12 w-full rounded-xl bg-slate-200/70" />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
37
src/components/feedback/Toast.tsx
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import React from 'react'
|
||||||
|
import toast, { Toaster } from 'react-hot-toast'
|
||||||
|
|
||||||
|
export const showToast = {
|
||||||
|
success: (message: string) =>
|
||||||
|
toast.success(message, {
|
||||||
|
style: {
|
||||||
|
borderRadius: '12px',
|
||||||
|
background: '#1E293B',
|
||||||
|
color: '#fff',
|
||||||
|
fontSize: '14px',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
error: (message: string) =>
|
||||||
|
toast.error(message, {
|
||||||
|
style: {
|
||||||
|
borderRadius: '12px',
|
||||||
|
background: '#1E293B',
|
||||||
|
color: '#fff',
|
||||||
|
fontSize: '14px',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
info: (message: string) =>
|
||||||
|
toast(message, {
|
||||||
|
icon: 'ℹ️',
|
||||||
|
style: {
|
||||||
|
borderRadius: '12px',
|
||||||
|
background: '#1E293B',
|
||||||
|
color: '#fff',
|
||||||
|
fontSize: '14px',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ToasterProvider: React.FC = () => {
|
||||||
|
return <Toaster position="top-right" toastOptions={{ duration: 3000 }} />
|
||||||
|
}
|
||||||
43
src/components/ui/Avatar.tsx
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface AvatarProps {
|
||||||
|
initials: string
|
||||||
|
color?: string
|
||||||
|
size?: 'xs' | 'sm' | 'md' | 'lg'
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Avatar: React.FC<AvatarProps> = ({
|
||||||
|
initials,
|
||||||
|
color = 'bg-orange-100 text-[#C95C3C]',
|
||||||
|
size = 'md',
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
const sizeClasses: Record<NonNullable<AvatarProps['size']>, string> = {
|
||||||
|
xs: 'h-6 w-6 text-[10px]',
|
||||||
|
sm: 'h-7 w-7 text-xs',
|
||||||
|
md: 'h-8 w-8 text-xs',
|
||||||
|
lg: 'h-10 w-10 text-sm',
|
||||||
|
}
|
||||||
|
|
||||||
|
const isHexOrRgb = color.startsWith('#') || color.startsWith('rgb')
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={
|
||||||
|
isHexOrRgb
|
||||||
|
? { backgroundColor: `${color}22`, color: color }
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
className={clsx(
|
||||||
|
'inline-flex items-center justify-center rounded-full font-semibold select-none shrink-0',
|
||||||
|
sizeClasses[size],
|
||||||
|
!isHexOrRgb && color,
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{initials.slice(0, 2).toUpperCase()}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
60
src/components/ui/Badge.tsx
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export type BadgeVariant = 'success' | 'warning' | 'danger' | 'info' | 'neutral' | 'purple'
|
||||||
|
|
||||||
|
export interface BadgeProps {
|
||||||
|
children: React.ReactNode
|
||||||
|
variant?: BadgeVariant
|
||||||
|
dot?: boolean
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Badge: React.FC<BadgeProps> = ({
|
||||||
|
children,
|
||||||
|
variant = 'neutral',
|
||||||
|
dot = false,
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
const variantStyles: Record<BadgeVariant, { container: string; dot: string }> = {
|
||||||
|
success: {
|
||||||
|
container: 'bg-emerald-50 text-emerald-700 border-emerald-200/60',
|
||||||
|
dot: 'bg-emerald-500',
|
||||||
|
},
|
||||||
|
warning: {
|
||||||
|
container: 'bg-amber-50 text-amber-700 border-amber-200/60',
|
||||||
|
dot: 'bg-amber-500',
|
||||||
|
},
|
||||||
|
danger: {
|
||||||
|
container: 'bg-rose-50 text-rose-700 border-rose-200/60',
|
||||||
|
dot: 'bg-rose-500',
|
||||||
|
},
|
||||||
|
info: {
|
||||||
|
container: 'bg-blue-50 text-blue-700 border-blue-200/60',
|
||||||
|
dot: 'bg-blue-500',
|
||||||
|
},
|
||||||
|
neutral: {
|
||||||
|
container: 'bg-slate-100 text-slate-700 border-slate-200/60',
|
||||||
|
dot: 'bg-slate-400',
|
||||||
|
},
|
||||||
|
purple: {
|
||||||
|
container: 'bg-violet-50 text-violet-700 border-violet-200/60',
|
||||||
|
dot: 'bg-violet-500',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const style = variantStyles[variant]
|
||||||
|
|
||||||
|
return (
|
||||||
|
<span
|
||||||
|
className={clsx(
|
||||||
|
'inline-flex items-center gap-1.5 rounded-full border px-2.5 py-0.5 text-xs font-medium',
|
||||||
|
style.container,
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{dot && <span className={clsx('h-1.5 w-1.5 rounded-full shrink-0', style.dot)} />}
|
||||||
|
{children}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
}
|
||||||
78
src/components/ui/Button.tsx
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
||||||
|
variant?: 'primary' | 'secondary' | 'outline' | 'ghost' | 'danger'
|
||||||
|
size?: 'sm' | 'md' | 'lg'
|
||||||
|
leftIcon?: React.ReactNode
|
||||||
|
rightIcon?: React.ReactNode
|
||||||
|
isLoading?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Button: React.FC<ButtonProps> = ({
|
||||||
|
children,
|
||||||
|
variant = 'primary',
|
||||||
|
size = 'md',
|
||||||
|
leftIcon,
|
||||||
|
rightIcon,
|
||||||
|
isLoading = false,
|
||||||
|
className,
|
||||||
|
disabled,
|
||||||
|
type = 'button',
|
||||||
|
...props
|
||||||
|
}) => {
|
||||||
|
const variantClasses: Record<NonNullable<ButtonProps['variant']>, string> = {
|
||||||
|
primary: 'bg-[#C95C3C] hover:bg-[#B5492B] text-white border border-transparent shadow-sm',
|
||||||
|
secondary: 'bg-slate-100 hover:bg-slate-200 text-slate-800 border border-transparent',
|
||||||
|
outline: 'bg-white hover:bg-slate-50 text-slate-700 border border-slate-200 shadow-sm',
|
||||||
|
ghost: 'bg-transparent hover:bg-slate-100 text-slate-600 hover:text-slate-900 border border-transparent',
|
||||||
|
danger: 'bg-red-600 hover:bg-red-700 text-white border border-transparent shadow-sm',
|
||||||
|
}
|
||||||
|
|
||||||
|
const sizeClasses: Record<NonNullable<ButtonProps['size']>, string> = {
|
||||||
|
sm: 'px-3 py-1.5 text-xs rounded-lg gap-1.5',
|
||||||
|
md: 'px-4 py-2 text-sm rounded-xl gap-2',
|
||||||
|
lg: 'px-5 py-2.5 text-base rounded-xl gap-2.5',
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type={type}
|
||||||
|
disabled={disabled || isLoading}
|
||||||
|
className={clsx(
|
||||||
|
'inline-flex items-center justify-center font-medium transition-colors duration-150 focus:outline-none focus:ring-2 focus:ring-[#C95C3C]/30 disabled:opacity-50 disabled:cursor-not-allowed',
|
||||||
|
variantClasses[variant],
|
||||||
|
sizeClasses[size],
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{isLoading ? (
|
||||||
|
<svg
|
||||||
|
className="animate-spin h-4 w-4 text-current"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<circle
|
||||||
|
className="opacity-25"
|
||||||
|
cx="12"
|
||||||
|
cy="12"
|
||||||
|
r="10"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="4"
|
||||||
|
/>
|
||||||
|
<path
|
||||||
|
className="opacity-75"
|
||||||
|
fill="currentColor"
|
||||||
|
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
) : (
|
||||||
|
leftIcon && <span className="shrink-0">{leftIcon}</span>
|
||||||
|
)}
|
||||||
|
<span>{children}</span>
|
||||||
|
{!isLoading && rightIcon && <span className="shrink-0">{rightIcon}</span>}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
}
|
||||||
34
src/components/ui/Card.tsx
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface CardProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||||
|
children: React.ReactNode
|
||||||
|
padding?: 'none' | 'sm' | 'md' | 'lg'
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Card: React.FC<CardProps> = ({
|
||||||
|
children,
|
||||||
|
padding = 'md',
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}) => {
|
||||||
|
const paddingClasses: Record<NonNullable<CardProps['padding']>, string> = {
|
||||||
|
none: '',
|
||||||
|
sm: 'p-4',
|
||||||
|
md: 'p-5',
|
||||||
|
lg: 'p-6',
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'rounded-2xl bg-white border border-slate-200/80 shadow-sm',
|
||||||
|
paddingClasses[padding],
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
43
src/components/ui/Checkbox.tsx
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface CheckboxProps
|
||||||
|
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type'> {
|
||||||
|
label?: string
|
||||||
|
description?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Checkbox: React.FC<CheckboxProps> = ({
|
||||||
|
label,
|
||||||
|
description,
|
||||||
|
className,
|
||||||
|
id,
|
||||||
|
...props
|
||||||
|
}) => {
|
||||||
|
const checkboxId =
|
||||||
|
id || (label ? `checkbox-${label.replace(/\s+/g, '-').toLowerCase()}` : undefined)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<label
|
||||||
|
htmlFor={checkboxId}
|
||||||
|
className={clsx(
|
||||||
|
'inline-flex items-start gap-2.5 cursor-pointer select-none',
|
||||||
|
props.disabled && 'opacity-60 cursor-not-allowed',
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
id={checkboxId}
|
||||||
|
type="checkbox"
|
||||||
|
className="mt-0.5 h-4 w-4 rounded border-slate-300 text-[#C95C3C] focus:ring-[#C95C3C]/30 accent-[#C95C3C] cursor-pointer"
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
{(label || description) && (
|
||||||
|
<div className="text-sm">
|
||||||
|
{label && <span className="font-medium text-slate-700">{label}</span>}
|
||||||
|
{description && <p className="text-xs text-slate-500 mt-0.5">{description}</p>}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</label>
|
||||||
|
)
|
||||||
|
}
|
||||||
75
src/components/ui/Dropdown.tsx
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
import React, { useState, useRef, useEffect } from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface DropdownItem {
|
||||||
|
label: string
|
||||||
|
onClick: () => void
|
||||||
|
danger?: boolean
|
||||||
|
icon?: React.ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DropdownProps {
|
||||||
|
trigger: React.ReactNode
|
||||||
|
items: DropdownItem[]
|
||||||
|
align?: 'left' | 'right'
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Dropdown: React.FC<DropdownProps> = ({
|
||||||
|
trigger,
|
||||||
|
items,
|
||||||
|
align = 'right',
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
const [open, setOpen] = useState(false)
|
||||||
|
const ref = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleClickOutside = (event: MouseEvent) => {
|
||||||
|
if (ref.current && !ref.current.contains(event.target as Node)) {
|
||||||
|
setOpen(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.addEventListener('mousedown', handleClickOutside)
|
||||||
|
return () => document.removeEventListener('mousedown', handleClickOutside)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div ref={ref} className={clsx('relative inline-block text-left', className)}>
|
||||||
|
<div
|
||||||
|
onClick={() => setOpen((prev) => !prev)}
|
||||||
|
className="cursor-pointer inline-flex items-center"
|
||||||
|
>
|
||||||
|
{trigger}
|
||||||
|
</div>
|
||||||
|
{open && (
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'absolute z-40 mt-2 w-48 rounded-xl bg-white border border-slate-200 shadow-lg py-1.5 focus:outline-none',
|
||||||
|
align === 'right' ? 'right-0' : 'left-0'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{items.map((item, idx) => (
|
||||||
|
<button
|
||||||
|
key={idx}
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
setOpen(false)
|
||||||
|
item.onClick()
|
||||||
|
}}
|
||||||
|
className={clsx(
|
||||||
|
'flex w-full items-center gap-2 px-4 py-2 text-left text-sm transition-colors',
|
||||||
|
item.danger
|
||||||
|
? 'text-red-600 hover:bg-red-50'
|
||||||
|
: 'text-slate-700 hover:bg-slate-50'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{item.icon && <span className="shrink-0">{item.icon}</span>}
|
||||||
|
<span>{item.label}</span>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
58
src/components/ui/Input.tsx
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
||||||
|
label?: string
|
||||||
|
error?: string
|
||||||
|
leftIcon?: React.ReactNode
|
||||||
|
rightElement?: React.ReactNode
|
||||||
|
helperText?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Input = React.forwardRef<HTMLInputElement, InputProps>(
|
||||||
|
({ label, error, leftIcon, rightElement, helperText, className, id, ...props }, ref) => {
|
||||||
|
const inputId = id || (label ? `input-${label.replace(/\s+/g, '-').toLowerCase()}` : undefined)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="w-full">
|
||||||
|
{label && (
|
||||||
|
<label
|
||||||
|
htmlFor={inputId}
|
||||||
|
className="block text-sm font-medium text-slate-700 mb-1.5"
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
<div className="relative flex items-center">
|
||||||
|
{leftIcon && (
|
||||||
|
<div className="absolute left-3.5 text-slate-400 pointer-events-none flex items-center">
|
||||||
|
{leftIcon}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<input
|
||||||
|
ref={ref}
|
||||||
|
id={inputId}
|
||||||
|
className={clsx(
|
||||||
|
'w-full rounded-lg border bg-white px-3.5 py-2 text-sm text-slate-900 placeholder:text-slate-400 transition-colors focus:outline-none focus:ring-2',
|
||||||
|
leftIcon && 'pl-10',
|
||||||
|
rightElement && 'pr-10',
|
||||||
|
error
|
||||||
|
? 'border-red-400 focus:border-red-500 focus:ring-red-500/20'
|
||||||
|
: 'border-slate-200 focus:border-[#C95C3C] focus:ring-[#C95C3C]/20',
|
||||||
|
props.disabled && 'bg-slate-50 text-slate-500 cursor-not-allowed',
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
{rightElement && (
|
||||||
|
<div className="absolute right-3 flex items-center">{rightElement}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{error && <p className="mt-1.5 text-xs text-red-600">{error}</p>}
|
||||||
|
{!error && helperText && <p className="mt-1.5 text-xs text-slate-500">{helperText}</p>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
Input.displayName = 'Input'
|
||||||
80
src/components/ui/Modal.tsx
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
import React, { useEffect } from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface ModalProps {
|
||||||
|
isOpen: boolean
|
||||||
|
onClose: () => void
|
||||||
|
title: string
|
||||||
|
subtitle?: string
|
||||||
|
children: React.ReactNode
|
||||||
|
footer?: React.ReactNode
|
||||||
|
size?: 'sm' | 'md' | 'lg' | 'xl'
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Modal: React.FC<ModalProps> = ({
|
||||||
|
isOpen,
|
||||||
|
onClose,
|
||||||
|
title,
|
||||||
|
subtitle,
|
||||||
|
children,
|
||||||
|
footer,
|
||||||
|
size = 'md',
|
||||||
|
}) => {
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isOpen) return
|
||||||
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
|
if (e.key === 'Escape') {
|
||||||
|
onClose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.addEventListener('keydown', handleKeyDown)
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown)
|
||||||
|
}, [isOpen, onClose])
|
||||||
|
|
||||||
|
if (!isOpen) return null
|
||||||
|
|
||||||
|
const sizeClasses: Record<NonNullable<ModalProps['size']>, string> = {
|
||||||
|
sm: 'max-w-md',
|
||||||
|
md: 'max-w-lg',
|
||||||
|
lg: 'max-w-2xl',
|
||||||
|
xl: 'max-w-4xl',
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
|
||||||
|
<div
|
||||||
|
className="fixed inset-0 bg-slate-900/40 backdrop-blur-[1px] transition-opacity"
|
||||||
|
onClick={onClose}
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'relative z-10 w-full rounded-2xl bg-white border border-slate-200 shadow-xl overflow-hidden',
|
||||||
|
sizeClasses[size]
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex items-start justify-between px-6 pt-5 pb-4 border-b border-slate-100">
|
||||||
|
<div>
|
||||||
|
<h3 className="text-lg font-semibold text-slate-900">{title}</h3>
|
||||||
|
{subtitle && <p className="mt-0.5 text-sm text-slate-500">{subtitle}</p>}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onClose}
|
||||||
|
className="rounded-lg p-1.5 text-slate-400 hover:bg-slate-100 hover:text-slate-600 transition-colors"
|
||||||
|
aria-label="Đóng"
|
||||||
|
>
|
||||||
|
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
||||||
|
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div className="px-6 py-5 max-h-[75vh] overflow-y-auto">{children}</div>
|
||||||
|
{footer && (
|
||||||
|
<div className="flex items-center justify-end gap-2.5 px-6 py-4 bg-slate-50/70 border-t border-slate-100">
|
||||||
|
{footer}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
46
src/components/ui/ProgressBar.tsx
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface ProgressBarProps {
|
||||||
|
value: number
|
||||||
|
max?: number
|
||||||
|
color?: string
|
||||||
|
height?: 'xs' | 'sm' | 'md'
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ProgressBar: React.FC<ProgressBarProps> = ({
|
||||||
|
value,
|
||||||
|
max = 100,
|
||||||
|
color = 'bg-[#C95C3C]',
|
||||||
|
height = 'sm',
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
const percentage = Math.min(100, Math.max(0, (value / max) * 100))
|
||||||
|
|
||||||
|
const heightClasses: Record<NonNullable<ProgressBarProps['height']>, string> = {
|
||||||
|
xs: 'h-1',
|
||||||
|
sm: 'h-1.5',
|
||||||
|
md: 'h-2.5',
|
||||||
|
}
|
||||||
|
|
||||||
|
const isHex = color.startsWith('#')
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'w-full overflow-hidden rounded-full bg-slate-100',
|
||||||
|
heightClasses[height],
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={clsx('h-full rounded-full transition-all duration-300', !isHex && color)}
|
||||||
|
style={{
|
||||||
|
width: `${percentage}%`,
|
||||||
|
...(isHex ? { backgroundColor: color } : {}),
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
63
src/components/ui/RadioGroup.tsx
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface RadioOption {
|
||||||
|
value: string
|
||||||
|
label: string
|
||||||
|
description?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RadioGroupProps {
|
||||||
|
name: string
|
||||||
|
label?: string
|
||||||
|
options: RadioOption[]
|
||||||
|
value: string
|
||||||
|
onChange: (value: string) => void
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const RadioGroup: React.FC<RadioGroupProps> = ({
|
||||||
|
name,
|
||||||
|
label,
|
||||||
|
options,
|
||||||
|
value,
|
||||||
|
onChange,
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<div className={className}>
|
||||||
|
{label && <div className="text-sm font-medium text-slate-700 mb-2">{label}</div>}
|
||||||
|
<div className="space-y-2">
|
||||||
|
{options.map((option) => {
|
||||||
|
const checked = value === option.value
|
||||||
|
return (
|
||||||
|
<label
|
||||||
|
key={option.value}
|
||||||
|
className={clsx(
|
||||||
|
'flex items-start gap-3 rounded-xl border p-3 cursor-pointer transition-colors',
|
||||||
|
checked
|
||||||
|
? 'border-[#C95C3C] bg-orange-50/40'
|
||||||
|
: 'border-slate-200 hover:bg-slate-50'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name={name}
|
||||||
|
value={option.value}
|
||||||
|
checked={checked}
|
||||||
|
onChange={() => onChange(option.value)}
|
||||||
|
className="mt-0.5 h-4 w-4 border-slate-300 text-[#C95C3C] focus:ring-[#C95C3C]/30 accent-[#C95C3C]"
|
||||||
|
/>
|
||||||
|
<div className="text-sm">
|
||||||
|
<div className="font-medium text-slate-800">{option.label}</div>
|
||||||
|
{option.description && (
|
||||||
|
<div className="text-xs text-slate-500 mt-0.5">{option.description}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
61
src/components/ui/SearchInput.tsx
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface SearchInputProps
|
||||||
|
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> {
|
||||||
|
value: string
|
||||||
|
onChange: (value: string) => void
|
||||||
|
onClear?: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const SearchInput: React.FC<SearchInputProps> = ({
|
||||||
|
value,
|
||||||
|
onChange,
|
||||||
|
onClear,
|
||||||
|
placeholder = 'Tìm kiếm...',
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}) => {
|
||||||
|
const handleClear = () => {
|
||||||
|
onChange('')
|
||||||
|
onClear?.()
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={clsx('relative flex items-center', className)}>
|
||||||
|
<svg
|
||||||
|
className="w-4 h-4 text-slate-400 absolute left-3.5 pointer-events-none"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={2}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M21 21l-4.35-4.35m0 0A7.5 7.5 0 103.5 10.5a7.5 7.5 0 0013.15 6.15z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={value}
|
||||||
|
onChange={(e) => onChange(e.target.value)}
|
||||||
|
placeholder={placeholder}
|
||||||
|
className="w-full rounded-xl border border-slate-200 bg-white pl-10 pr-8 py-2 text-sm text-slate-900 placeholder:text-slate-400 focus:outline-none focus:border-[#C95C3C] focus:ring-2 focus:ring-[#C95C3C]/15 transition-colors"
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
{value && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleClear}
|
||||||
|
aria-label="Xóa tìm kiếm"
|
||||||
|
className="absolute right-2.5 p-0.5 text-slate-400 hover:text-slate-600 rounded-md"
|
||||||
|
>
|
||||||
|
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
||||||
|
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
65
src/components/ui/Select.tsx
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface SelectOption {
|
||||||
|
value: string
|
||||||
|
label: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SelectProps
|
||||||
|
extends Omit<React.SelectHTMLAttributes<HTMLSelectElement>, 'onChange'> {
|
||||||
|
label?: string
|
||||||
|
options: SelectOption[]
|
||||||
|
value: string
|
||||||
|
onChange: (value: string) => void
|
||||||
|
error?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Select: React.FC<SelectProps> = ({
|
||||||
|
label,
|
||||||
|
options,
|
||||||
|
value,
|
||||||
|
onChange,
|
||||||
|
error,
|
||||||
|
className,
|
||||||
|
id,
|
||||||
|
...props
|
||||||
|
}) => {
|
||||||
|
const selectId = id || (label ? `select-${label.replace(/\s+/g, '-').toLowerCase()}` : undefined)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={clsx(' inline-block', className)}>
|
||||||
|
{label && (
|
||||||
|
<label htmlFor={selectId} className="block text-sm font-medium text-slate-700 mb-1.5">
|
||||||
|
{label}
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
<div className="relative">
|
||||||
|
<select
|
||||||
|
id={selectId}
|
||||||
|
value={value}
|
||||||
|
onChange={(e) => onChange(e.target.value)}
|
||||||
|
className={clsx(
|
||||||
|
'w-full appearance-none rounded-xl border bg-white pl-3.5 pr-9 py-2 text-sm text-slate-700 font-medium focus:outline-none focus:ring-2 transition-colors cursor-pointer',
|
||||||
|
error
|
||||||
|
? 'border-red-400 focus:border-red-500 focus:ring-red-500/20'
|
||||||
|
: 'border-slate-200 hover:border-slate-300 focus:border-[#C95C3C] focus:ring-[#C95C3C]/15'
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{options.map((opt) => (
|
||||||
|
<option key={opt.value} value={opt.value}>
|
||||||
|
{opt.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<div className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3 text-slate-500">
|
||||||
|
<svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
||||||
|
<path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{error && <p className="mt-1 text-xs text-red-600">{error}</p>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
39
src/components/ui/Spinner.tsx
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface SpinnerProps {
|
||||||
|
size?: 'sm' | 'md' | 'lg'
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Spinner: React.FC<SpinnerProps> = ({ size = 'md', className }) => {
|
||||||
|
const sizeClasses: Record<NonNullable<SpinnerProps['size']>, string> = {
|
||||||
|
sm: 'h-4 w-4',
|
||||||
|
md: 'h-6 w-6',
|
||||||
|
lg: 'h-8 w-8',
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
className={clsx('animate-spin text-[#C95C3C]', sizeClasses[size], className)}
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
aria-label="Đang tải..."
|
||||||
|
>
|
||||||
|
<circle
|
||||||
|
className="opacity-25"
|
||||||
|
cx="12"
|
||||||
|
cy="12"
|
||||||
|
r="10"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="4"
|
||||||
|
/>
|
||||||
|
<path
|
||||||
|
className="opacity-75"
|
||||||
|
fill="currentColor"
|
||||||
|
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
)
|
||||||
|
}
|
||||||
65
src/components/ui/Table.tsx
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export const TableContainer: React.FC<React.HTMLAttributes<HTMLDivElement>> = ({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}) => (
|
||||||
|
<div className={clsx('w-full overflow-x-auto', className)} {...props}>
|
||||||
|
<table className="w-full border-collapse text-left">{children}</table>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const TableHead: React.FC<React.HTMLAttributes<HTMLTableSectionElement>> = ({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}) => (
|
||||||
|
<thead
|
||||||
|
className={clsx('bg-slate-50/70 border-y border-slate-200/80 text-[11px] font-semibold uppercase tracking-wider text-slate-400', className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</thead>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const TableBody: React.FC<React.HTMLAttributes<HTMLTableSectionElement>> = ({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}) => (
|
||||||
|
<tbody className={clsx('divide-y divide-slate-100 bg-white text-sm text-slate-700', className)} {...props}>
|
||||||
|
{children}
|
||||||
|
</tbody>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const TableRow: React.FC<React.HTMLAttributes<HTMLTableRowElement>> = ({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}) => (
|
||||||
|
<tr className={clsx('hover:bg-slate-50/70 transition-colors', className)} {...props}>
|
||||||
|
{children}
|
||||||
|
</tr>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const TableHeaderCell: React.FC<React.ThHTMLAttributes<HTMLTableCellElement>> = ({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}) => (
|
||||||
|
<th className={clsx('px-5 py-3 font-semibold', className)} {...props}>
|
||||||
|
{children}
|
||||||
|
</th>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const TableCell: React.FC<React.TdHTMLAttributes<HTMLTableCellElement>> = ({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}) => (
|
||||||
|
<td className={clsx('px-5 py-3.5 align-middle', className)} {...props}>
|
||||||
|
{children}
|
||||||
|
</td>
|
||||||
|
)
|
||||||
59
src/components/ui/Tabs.tsx
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface TabItem {
|
||||||
|
id: string
|
||||||
|
label: string
|
||||||
|
count?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TabsProps {
|
||||||
|
tabs: TabItem[]
|
||||||
|
activeTab: string
|
||||||
|
onChange: (id: string) => void
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Tabs: React.FC<TabsProps> = ({
|
||||||
|
tabs,
|
||||||
|
activeTab,
|
||||||
|
onChange,
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<div className={clsx('border-b border-slate-200', className)}>
|
||||||
|
<nav className="-mb-px flex space-x-6 overflow-x-auto" aria-label="Tabs">
|
||||||
|
{tabs.map((tab) => {
|
||||||
|
const isActive = tab.id === activeTab
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={tab.id}
|
||||||
|
type="button"
|
||||||
|
onClick={() => onChange(tab.id)}
|
||||||
|
className={clsx(
|
||||||
|
'whitespace-nowrap py-3 px-1 border-b-2 text-sm font-medium transition-colors flex items-center gap-2',
|
||||||
|
isActive
|
||||||
|
? 'border-[#3B5BDB] text-[#3B5BDB] font-semibold'
|
||||||
|
: 'border-transparent text-slate-500 hover:text-slate-800 hover:border-slate-300'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<span>{tab.label}</span>
|
||||||
|
{typeof tab.count === 'number' && (
|
||||||
|
<span
|
||||||
|
className={clsx(
|
||||||
|
'rounded-full px-2 py-0.5 text-xs font-medium',
|
||||||
|
isActive
|
||||||
|
? 'bg-blue-50 text-[#3B5BDB]'
|
||||||
|
: 'bg-slate-100 text-slate-600'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{tab.count}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
46
src/components/ui/Textarea.tsx
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
|
||||||
|
label?: string
|
||||||
|
error?: string
|
||||||
|
helperText?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
|
||||||
|
({ label, error, helperText, className, id, rows = 3, ...props }, ref) => {
|
||||||
|
const textareaId =
|
||||||
|
id || (label ? `textarea-${label.replace(/\s+/g, '-').toLowerCase()}` : undefined)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="w-full">
|
||||||
|
{label && (
|
||||||
|
<label
|
||||||
|
htmlFor={textareaId}
|
||||||
|
className="block text-sm font-medium text-slate-700 mb-1.5"
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
<textarea
|
||||||
|
ref={ref}
|
||||||
|
id={textareaId}
|
||||||
|
rows={rows}
|
||||||
|
className={clsx(
|
||||||
|
'w-full rounded-xl border bg-white px-3.5 py-2.5 text-sm text-slate-900 placeholder:text-slate-400 transition-colors focus:outline-none focus:ring-2',
|
||||||
|
error
|
||||||
|
? 'border-red-400 focus:border-red-500 focus:ring-red-500/20'
|
||||||
|
: 'border-slate-200 focus:border-[#C95C3C] focus:ring-[#C95C3C]/20',
|
||||||
|
props.disabled && 'bg-slate-50 text-slate-500 cursor-not-allowed',
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
{error && <p className="mt-1.5 text-xs text-red-600">{error}</p>}
|
||||||
|
{!error && helperText && <p className="mt-1.5 text-xs text-slate-500">{helperText}</p>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
Textarea.displayName = 'Textarea'
|
||||||
38
src/components/ui/Tooltip.tsx
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export interface TooltipProps {
|
||||||
|
content: string
|
||||||
|
children: React.ReactNode
|
||||||
|
position?: 'top' | 'bottom' | 'left' | 'right'
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Tooltip: React.FC<TooltipProps> = ({
|
||||||
|
content,
|
||||||
|
children,
|
||||||
|
position = 'top',
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
const positionClasses: Record<NonNullable<TooltipProps['position']>, string> = {
|
||||||
|
top: 'bottom-full left-1/2 -translate-x-1/2 mb-2',
|
||||||
|
bottom: 'top-full left-1/2 -translate-x-1/2 mt-2',
|
||||||
|
left: 'right-full top-1/2 -translate-y-1/2 mr-2',
|
||||||
|
right: 'left-full top-1/2 -translate-y-1/2 ml-2',
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={clsx('relative inline-flex group', className)}>
|
||||||
|
{children}
|
||||||
|
<div
|
||||||
|
role="tooltip"
|
||||||
|
className={clsx(
|
||||||
|
'pointer-events-none absolute z-50 whitespace-nowrap rounded-lg bg-slate-900 px-2.5 py-1 text-xs font-medium text-white opacity-0 shadow-md transition-opacity duration-150 group-hover:opacity-100',
|
||||||
|
positionClasses[position]
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{content}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
17
src/components/ui/index.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
export * from './Button'
|
||||||
|
export * from './Input'
|
||||||
|
export * from './SearchInput'
|
||||||
|
export * from './Select'
|
||||||
|
export * from './Modal'
|
||||||
|
export * from './Badge'
|
||||||
|
export * from './Avatar'
|
||||||
|
export * from './Card'
|
||||||
|
export * from './Table'
|
||||||
|
export * from './Tabs'
|
||||||
|
export * from './ProgressBar'
|
||||||
|
export * from './Tooltip'
|
||||||
|
export * from './Dropdown'
|
||||||
|
export * from './Checkbox'
|
||||||
|
export * from './RadioGroup'
|
||||||
|
export * from './Textarea'
|
||||||
|
export * from './Spinner'
|
||||||
128
src/hooks/useAnnotation.ts
Normal file
|
|
@ -0,0 +1,128 @@
|
||||||
|
import { useEffect } from 'react'
|
||||||
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
|
import * as annotationService from '@/services/annotationService'
|
||||||
|
import * as exportService from '@/services/exportService'
|
||||||
|
import * as imageService from '@/services/imageService'
|
||||||
|
import { useAnnotationStore } from '@/stores/annotationStore'
|
||||||
|
|
||||||
|
export function useAnnotation(projectId?: string, imageId?: string) {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
const resolvedProjectId = projectId || 'proj-traffic-det'
|
||||||
|
const resolvedImageId = imageId || `${resolvedProjectId}-img-42`
|
||||||
|
|
||||||
|
const store = useAnnotationStore()
|
||||||
|
|
||||||
|
const imageQuery = useQuery({
|
||||||
|
queryKey: ['image', resolvedProjectId, resolvedImageId],
|
||||||
|
queryFn: () => imageService.getImageById(resolvedProjectId, resolvedImageId),
|
||||||
|
enabled: Boolean(resolvedProjectId && resolvedImageId),
|
||||||
|
})
|
||||||
|
|
||||||
|
const annotationQuery = useQuery({
|
||||||
|
queryKey: ['annotation', resolvedProjectId, resolvedImageId],
|
||||||
|
queryFn: () => annotationService.getAnnotation(resolvedProjectId, resolvedImageId),
|
||||||
|
enabled: Boolean(resolvedProjectId && resolvedImageId),
|
||||||
|
})
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (annotationQuery.data && annotationQuery.data.imageId !== store.currentImageId) {
|
||||||
|
store.setAnnotationData(annotationQuery.data)
|
||||||
|
}
|
||||||
|
}, [annotationQuery.data, store])
|
||||||
|
|
||||||
|
const saveMutation = useMutation({
|
||||||
|
mutationFn: () =>
|
||||||
|
annotationService.saveAnnotation(resolvedProjectId, resolvedImageId, {
|
||||||
|
classificationLabelId: store.classificationLabelId,
|
||||||
|
flags: store.flags,
|
||||||
|
shapes: store.shapes,
|
||||||
|
}),
|
||||||
|
onSuccess: (saved) => {
|
||||||
|
store.markSaved(saved.updatedAt)
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project-images', resolvedProjectId] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const submitMutation = useMutation({
|
||||||
|
mutationFn: async () => {
|
||||||
|
await annotationService.saveAnnotation(resolvedProjectId, resolvedImageId, {
|
||||||
|
classificationLabelId: store.classificationLabelId,
|
||||||
|
flags: store.flags,
|
||||||
|
shapes: store.shapes,
|
||||||
|
})
|
||||||
|
return annotationService.submitForReview(resolvedProjectId, resolvedImageId)
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
store.markSaved('Đã gửi duyệt')
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['image', resolvedProjectId, resolvedImageId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project-images', resolvedProjectId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project', resolvedProjectId] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const approveMutation = useMutation({
|
||||||
|
mutationFn: () => annotationService.approveAnnotation(resolvedProjectId, resolvedImageId),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['image', resolvedProjectId, resolvedImageId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project-images', resolvedProjectId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project', resolvedProjectId] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const rejectMutation = useMutation({
|
||||||
|
mutationFn: (reason?: string) =>
|
||||||
|
annotationService.rejectAnnotation(resolvedProjectId, resolvedImageId, reason),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['image', resolvedProjectId, resolvedImageId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project-images', resolvedProjectId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project', resolvedProjectId] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const exportLabelMe = () => {
|
||||||
|
if (!imageQuery.data) return
|
||||||
|
exportService.downloadImageLabelMeJSON(imageQuery.data, store.shapes)
|
||||||
|
}
|
||||||
|
|
||||||
|
const exportMask = () => {
|
||||||
|
if (!imageQuery.data) return
|
||||||
|
exportService.downloadImageMaskPNG(imageQuery.data, store.shapes)
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectedShape = store.shapes.find((s) => s.id === store.selectedShapeId) ?? null
|
||||||
|
|
||||||
|
return {
|
||||||
|
image: imageQuery.data,
|
||||||
|
isLoading: imageQuery.isLoading || annotationQuery.isLoading,
|
||||||
|
shapes: store.shapes,
|
||||||
|
selectedShapeId: store.selectedShapeId,
|
||||||
|
selectedShape,
|
||||||
|
classificationLabelId: store.classificationLabelId,
|
||||||
|
flags: store.flags,
|
||||||
|
activeLabelId: store.activeLabelId,
|
||||||
|
activeGroupId: store.activeGroupId,
|
||||||
|
lastSavedAt: store.lastSavedAt,
|
||||||
|
isDirty: store.isDirty,
|
||||||
|
canUndo: store.history.past.length > 0,
|
||||||
|
canRedo: store.history.future.length > 0,
|
||||||
|
addShape: store.addShape,
|
||||||
|
updateShape: store.updateShape,
|
||||||
|
deleteShape: store.deleteShape,
|
||||||
|
toggleShapeVisibility: store.toggleShapeVisibility,
|
||||||
|
selectShape: store.selectShape,
|
||||||
|
setClassificationLabel: store.setClassificationLabel,
|
||||||
|
toggleFlag: store.toggleFlag,
|
||||||
|
setActiveLabelId: store.setActiveLabelId,
|
||||||
|
setActiveGroupId: store.setActiveGroupId,
|
||||||
|
undo: store.undo,
|
||||||
|
redo: store.redo,
|
||||||
|
saveAnnotation: saveMutation.mutateAsync,
|
||||||
|
isSaving: saveMutation.isPending,
|
||||||
|
submitForReview: submitMutation.mutateAsync,
|
||||||
|
isSubmitting: submitMutation.isPending,
|
||||||
|
approveAnnotation: approveMutation.mutateAsync,
|
||||||
|
rejectAnnotation: rejectMutation.mutateAsync,
|
||||||
|
exportLabelMe,
|
||||||
|
exportMask,
|
||||||
|
}
|
||||||
|
}
|
||||||
67
src/hooks/useAuth.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
import { useState } from 'react'
|
||||||
|
import * as authService from '@/services/authService'
|
||||||
|
import { useAuthStore } from '@/stores/authStore'
|
||||||
|
import { LoginRequest, UserRole } from '@/types'
|
||||||
|
import { canApproveLabels, canCreateProject, canManageUsers } from '@/utils/permissions'
|
||||||
|
|
||||||
|
export function useAuth() {
|
||||||
|
const { user, isAuthenticated, login: storeLogin, logout: storeLogout, setUser, setRole } = useAuthStore()
|
||||||
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
|
const [error, setError] = useState<string | null>(null)
|
||||||
|
|
||||||
|
const login = async (payload: LoginRequest) => {
|
||||||
|
setIsLoading(true)
|
||||||
|
setError(null)
|
||||||
|
try {
|
||||||
|
const response = await authService.login(payload)
|
||||||
|
storeLogin(response.user, response.accessToken)
|
||||||
|
return response
|
||||||
|
} catch (err) {
|
||||||
|
const message = err instanceof Error ? err.message : 'Đăng nhập thất bại'
|
||||||
|
setError(message)
|
||||||
|
throw err
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const loginWithSSO = async (provider: 'google' | 'microsoft' | 'sso' = 'sso') => {
|
||||||
|
setIsLoading(true)
|
||||||
|
setError(null)
|
||||||
|
try {
|
||||||
|
const response = await authService.loginWithSSO(provider)
|
||||||
|
storeLogin(response.user, response.accessToken)
|
||||||
|
return response
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const logout = async () => {
|
||||||
|
await authService.logout()
|
||||||
|
storeLogout()
|
||||||
|
}
|
||||||
|
|
||||||
|
const switchRole = async (role: UserRole) => {
|
||||||
|
const updated = await authService.switchUserRole(role)
|
||||||
|
setRole(role)
|
||||||
|
setUser(updated)
|
||||||
|
}
|
||||||
|
|
||||||
|
const role: UserRole = user?.role ?? 'admin'
|
||||||
|
|
||||||
|
return {
|
||||||
|
user,
|
||||||
|
role,
|
||||||
|
isAuthenticated,
|
||||||
|
isLoading,
|
||||||
|
error,
|
||||||
|
login,
|
||||||
|
loginWithSSO,
|
||||||
|
logout,
|
||||||
|
switchRole,
|
||||||
|
canManageUsers: canManageUsers(role),
|
||||||
|
canApproveLabels: canApproveLabels(role),
|
||||||
|
canCreateProject: canCreateProject(role),
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/hooks/useAutoSave.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { useEffect } from 'react'
|
||||||
|
import * as annotationService from '@/services/annotationService'
|
||||||
|
import { useAnnotationStore } from '@/stores/annotationStore'
|
||||||
|
|
||||||
|
export function useAutoSave(projectId?: string, imageId?: string, intervalMs = 2500) {
|
||||||
|
const isDirty = useAnnotationStore((s) => s.isDirty)
|
||||||
|
const shapes = useAnnotationStore((s) => s.shapes)
|
||||||
|
const classificationLabelId = useAnnotationStore((s) => s.classificationLabelId)
|
||||||
|
const flags = useAnnotationStore((s) => s.flags)
|
||||||
|
const markSaved = useAnnotationStore((s) => s.markSaved)
|
||||||
|
const lastSavedAt = useAnnotationStore((s) => s.lastSavedAt)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!projectId || !imageId || !isDirty) return
|
||||||
|
|
||||||
|
const timer = window.setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
await annotationService.saveAnnotation(projectId, imageId, {
|
||||||
|
classificationLabelId,
|
||||||
|
flags,
|
||||||
|
shapes,
|
||||||
|
})
|
||||||
|
markSaved('Vừa lưu 1 giây trước')
|
||||||
|
} catch {
|
||||||
|
// Bỏ qua lỗi tự động lưu tạm thời
|
||||||
|
}
|
||||||
|
}, intervalMs)
|
||||||
|
|
||||||
|
return () => window.clearTimeout(timer)
|
||||||
|
}, [projectId, imageId, isDirty, shapes, classificationLabelId, flags, intervalMs, markSaved])
|
||||||
|
|
||||||
|
return {
|
||||||
|
isDirty,
|
||||||
|
lastSavedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/hooks/useCanvas.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { useCanvasStore } from '@/stores/canvasStore'
|
||||||
|
|
||||||
|
export function useCanvas() {
|
||||||
|
const store = useCanvasStore()
|
||||||
|
|
||||||
|
return {
|
||||||
|
activeTool: store.activeTool,
|
||||||
|
zoom: store.zoom,
|
||||||
|
zoomPercent: Math.round(store.zoom * 100),
|
||||||
|
pan: store.pan,
|
||||||
|
showBoxes: store.showBoxes,
|
||||||
|
showMask: store.showMask,
|
||||||
|
maskOpacity: store.maskOpacity,
|
||||||
|
vertexEditMode: store.vertexEditMode,
|
||||||
|
setActiveTool: store.setActiveTool,
|
||||||
|
setZoom: store.setZoom,
|
||||||
|
setPan: store.setPan,
|
||||||
|
setShowBoxes: store.setShowBoxes,
|
||||||
|
toggleShowBoxes: store.toggleShowBoxes,
|
||||||
|
setShowMask: store.setShowMask,
|
||||||
|
toggleShowMask: store.toggleShowMask,
|
||||||
|
setMaskOpacity: store.setMaskOpacity,
|
||||||
|
setVertexEditMode: store.setVertexEditMode,
|
||||||
|
toggleVertexEditMode: store.toggleVertexEditMode,
|
||||||
|
zoomIn: store.zoomIn,
|
||||||
|
zoomOut: store.zoomOut,
|
||||||
|
resetZoom: store.resetZoom,
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/hooks/useDebounce.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
export function useDebounce<T>(value: T, delayMs = 250): T {
|
||||||
|
const [debouncedValue, setDebouncedValue] = useState<T>(value)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const timer = window.setTimeout(() => {
|
||||||
|
setDebouncedValue(value)
|
||||||
|
}, delayMs)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.clearTimeout(timer)
|
||||||
|
}
|
||||||
|
}, [value, delayMs])
|
||||||
|
|
||||||
|
return debouncedValue
|
||||||
|
}
|
||||||
47
src/hooks/useImageNavigation.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import { useNavigate } from 'react-router-dom'
|
||||||
|
import { ImageItem } from '@/types'
|
||||||
|
|
||||||
|
export function useImageNavigation(
|
||||||
|
projectId: string,
|
||||||
|
images: ImageItem[],
|
||||||
|
currentImageId?: string
|
||||||
|
) {
|
||||||
|
const navigate = useNavigate()
|
||||||
|
|
||||||
|
const currentIndex = images.findIndex((img) => img.id === currentImageId)
|
||||||
|
const resolvedIndex = currentIndex >= 0 ? currentIndex : 0
|
||||||
|
|
||||||
|
const currentImage = images[resolvedIndex] ?? null
|
||||||
|
const prevImage = resolvedIndex > 0 ? (images[resolvedIndex - 1] ?? null) : null
|
||||||
|
const nextImage =
|
||||||
|
resolvedIndex < images.length - 1 ? (images[resolvedIndex + 1] ?? null) : null
|
||||||
|
|
||||||
|
const goToImage = (imageId: string) => {
|
||||||
|
navigate(`/projects/${projectId}/annotate/${imageId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const goToPrev = () => {
|
||||||
|
if (prevImage) {
|
||||||
|
goToImage(prevImage.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const goToNext = () => {
|
||||||
|
if (nextImage) {
|
||||||
|
goToImage(nextImage.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
currentImage,
|
||||||
|
currentIndex: resolvedIndex,
|
||||||
|
totalCount: images.length,
|
||||||
|
hasPrev: Boolean(prevImage),
|
||||||
|
hasNext: Boolean(nextImage),
|
||||||
|
prevImage,
|
||||||
|
nextImage,
|
||||||
|
goToImage,
|
||||||
|
goToPrev,
|
||||||
|
goToNext,
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/hooks/useImages.ts
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
import { useState } from 'react'
|
||||||
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
|
import * as imageService from '@/services/imageService'
|
||||||
|
import { ImageAnnotationStatus } from '@/types'
|
||||||
|
|
||||||
|
export function useImages(projectId?: string) {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
const resolvedProjectId = projectId || 'proj-traffic-det'
|
||||||
|
|
||||||
|
const [statusFilter, setStatusFilter] = useState<ImageAnnotationStatus | 'all'>('all')
|
||||||
|
const [assigneeFilter, setAssigneeFilter] = useState<string>('all')
|
||||||
|
const [search, setSearch] = useState<string>('')
|
||||||
|
|
||||||
|
const imagesQuery = useQuery({
|
||||||
|
queryKey: [
|
||||||
|
'project-images',
|
||||||
|
resolvedProjectId,
|
||||||
|
{ status: statusFilter, assigneeId: assigneeFilter, search },
|
||||||
|
],
|
||||||
|
queryFn: () =>
|
||||||
|
imageService.getProjectImages(resolvedProjectId, {
|
||||||
|
status: statusFilter,
|
||||||
|
assigneeId: assigneeFilter,
|
||||||
|
search,
|
||||||
|
}),
|
||||||
|
enabled: Boolean(resolvedProjectId),
|
||||||
|
})
|
||||||
|
|
||||||
|
const uploadMutation = useMutation({
|
||||||
|
mutationFn: (files: { fileName: string; url?: string; width?: number; height?: number }[]) =>
|
||||||
|
imageService.uploadImages(resolvedProjectId, files),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project-images', resolvedProjectId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project', resolvedProjectId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const updateStatusMutation = useMutation({
|
||||||
|
mutationFn: ({ imageId, status }: { imageId: string; status: ImageAnnotationStatus }) =>
|
||||||
|
imageService.updateImageStatus(resolvedProjectId, imageId, status),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project-images', resolvedProjectId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project', resolvedProjectId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
images: imagesQuery.data ?? [],
|
||||||
|
isLoading: imagesQuery.isLoading,
|
||||||
|
statusFilter,
|
||||||
|
assigneeFilter,
|
||||||
|
search,
|
||||||
|
setStatusFilter,
|
||||||
|
setAssigneeFilter,
|
||||||
|
setSearch,
|
||||||
|
uploadImages: uploadMutation.mutateAsync,
|
||||||
|
isUploading: uploadMutation.isPending,
|
||||||
|
updateImageStatus: updateStatusMutation.mutateAsync,
|
||||||
|
}
|
||||||
|
}
|
||||||
147
src/hooks/useKeyboardShortcuts.ts
Normal file
|
|
@ -0,0 +1,147 @@
|
||||||
|
import { useEffect } from 'react'
|
||||||
|
import { useAnnotationStore } from '@/stores/annotationStore'
|
||||||
|
import { useCanvasStore } from '@/stores/canvasStore'
|
||||||
|
import { LabelClass } from '@/types'
|
||||||
|
|
||||||
|
export interface KeyboardShortcutsOptions {
|
||||||
|
classes?: LabelClass[]
|
||||||
|
onSave?: () => void
|
||||||
|
onSubmit?: () => void
|
||||||
|
onPrevImage?: () => void
|
||||||
|
onNextImage?: () => void
|
||||||
|
isClassification?: boolean
|
||||||
|
enabled?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useKeyboardShortcuts({
|
||||||
|
classes = [],
|
||||||
|
onSave,
|
||||||
|
onSubmit,
|
||||||
|
onPrevImage,
|
||||||
|
onNextImage,
|
||||||
|
isClassification = false,
|
||||||
|
enabled = true,
|
||||||
|
}: KeyboardShortcutsOptions = {}) {
|
||||||
|
const setActiveTool = useCanvasStore((s) => s.setActiveTool)
|
||||||
|
const selectedShapeId = useAnnotationStore((s) => s.selectedShapeId)
|
||||||
|
const deleteShape = useAnnotationStore((s) => s.deleteShape)
|
||||||
|
const updateShape = useAnnotationStore((s) => s.updateShape)
|
||||||
|
const setActiveLabelId = useAnnotationStore((s) => s.setActiveLabelId)
|
||||||
|
const setClassificationLabel = useAnnotationStore((s) => s.setClassificationLabel)
|
||||||
|
const undo = useAnnotationStore((s) => s.undo)
|
||||||
|
const redo = useAnnotationStore((s) => s.redo)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!enabled) return
|
||||||
|
|
||||||
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
|
const target = event.target as HTMLElement | null
|
||||||
|
if (
|
||||||
|
target &&
|
||||||
|
(target.tagName === 'INPUT' ||
|
||||||
|
target.tagName === 'TEXTAREA' ||
|
||||||
|
target.tagName === 'SELECT' ||
|
||||||
|
target.isContentEditable)
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = event.key.toLowerCase()
|
||||||
|
|
||||||
|
// Ctrl+S / Cmd+S: Lưu
|
||||||
|
if ((event.ctrlKey || event.metaKey) && key === 's') {
|
||||||
|
event.preventDefault()
|
||||||
|
onSave?.()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ctrl+Z / Cmd+Z: Undo / Redo
|
||||||
|
if ((event.ctrlKey || event.metaKey) && key === 'z') {
|
||||||
|
event.preventDefault()
|
||||||
|
if (event.shiftKey) {
|
||||||
|
redo()
|
||||||
|
} else {
|
||||||
|
undo()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phím tắt công cụ: V (Select), R/W (BBox), P (Polygon)
|
||||||
|
if (!event.ctrlKey && !event.metaKey && !event.altKey) {
|
||||||
|
if (key === 'v') {
|
||||||
|
setActiveTool('select')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (key === 'r' || key === 'w') {
|
||||||
|
setActiveTool('bbox')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (key === 'p') {
|
||||||
|
setActiveTool('polygon')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// A / ArrowLeft: Ảnh trước, D / ArrowRight: Ảnh tiếp
|
||||||
|
if (key === 'a' || event.key === 'ArrowLeft') {
|
||||||
|
onPrevImage?.()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (key === 'd' || event.key === 'ArrowRight') {
|
||||||
|
onNextImage?.()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enter: Nộp duyệt hoặc xác nhận
|
||||||
|
if (event.key === 'Enter' && onSubmit) {
|
||||||
|
event.preventDefault()
|
||||||
|
onSubmit()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete / Backspace: Xóa shape đang chọn
|
||||||
|
if ((event.key === 'Delete' || event.key === 'Backspace') && selectedShapeId) {
|
||||||
|
event.preventDefault()
|
||||||
|
deleteShape(selectedShapeId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phím số 1-9: Chọn lớp nhãn
|
||||||
|
if (/^[1-9]$/.test(key)) {
|
||||||
|
const idx = parseInt(key, 10) - 1
|
||||||
|
const targetClass = classes[idx]
|
||||||
|
if (targetClass) {
|
||||||
|
setActiveLabelId(targetClass.id)
|
||||||
|
if (isClassification) {
|
||||||
|
setClassificationLabel(targetClass.id)
|
||||||
|
} else if (selectedShapeId) {
|
||||||
|
updateShape(selectedShapeId, {
|
||||||
|
labelId: targetClass.id,
|
||||||
|
labelName: targetClass.name,
|
||||||
|
color: targetClass.color,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('keydown', handleKeyDown)
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown)
|
||||||
|
}, [
|
||||||
|
enabled,
|
||||||
|
classes,
|
||||||
|
isClassification,
|
||||||
|
selectedShapeId,
|
||||||
|
setActiveTool,
|
||||||
|
deleteShape,
|
||||||
|
updateShape,
|
||||||
|
setActiveLabelId,
|
||||||
|
setClassificationLabel,
|
||||||
|
undo,
|
||||||
|
redo,
|
||||||
|
onSave,
|
||||||
|
onSubmit,
|
||||||
|
onPrevImage,
|
||||||
|
onNextImage,
|
||||||
|
])
|
||||||
|
}
|
||||||
48
src/hooks/useProjectDetail.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
|
import * as exportService from '@/services/exportService'
|
||||||
|
import * as labelService from '@/services/labelService'
|
||||||
|
import * as projectService from '@/services/projectService'
|
||||||
|
|
||||||
|
export function useProjectDetail(projectId?: string) {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
const resolvedId = projectId || 'proj-traffic-det'
|
||||||
|
|
||||||
|
const projectQuery = useQuery({
|
||||||
|
queryKey: ['project', resolvedId],
|
||||||
|
queryFn: () => projectService.getProjectById(resolvedId),
|
||||||
|
enabled: Boolean(resolvedId),
|
||||||
|
})
|
||||||
|
|
||||||
|
const createLabelMutation = useMutation({
|
||||||
|
mutationFn: (input: { name: string; color?: string }) =>
|
||||||
|
labelService.createLabel(resolvedId, input),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project', resolvedId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const deleteLabelMutation = useMutation({
|
||||||
|
mutationFn: (labelId: string) => labelService.deleteLabel(resolvedId, labelId),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['project', resolvedId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const exportMutation = useMutation({
|
||||||
|
mutationFn: (format: exportService.ExportFormat = 'coco_json') =>
|
||||||
|
exportService.exportProjectLabels(resolvedId, format),
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
project: projectQuery.data,
|
||||||
|
isLoading: projectQuery.isLoading,
|
||||||
|
isError: projectQuery.isError,
|
||||||
|
refetch: projectQuery.refetch,
|
||||||
|
createLabel: createLabelMutation.mutateAsync,
|
||||||
|
deleteLabel: deleteLabelMutation.mutateAsync,
|
||||||
|
exportDataset: exportMutation.mutateAsync,
|
||||||
|
isExporting: exportMutation.isPending,
|
||||||
|
}
|
||||||
|
}
|
||||||
97
src/hooks/useProjects.ts
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
|
import * as projectService from '@/services/projectService'
|
||||||
|
import { useProjectStore } from '@/stores/projectStore'
|
||||||
|
import { CreateProjectInput, Project } from '@/types'
|
||||||
|
|
||||||
|
export function useProjects() {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
const {
|
||||||
|
searchQuery,
|
||||||
|
taskTypeFilter,
|
||||||
|
statusFilter,
|
||||||
|
selectedProjectId,
|
||||||
|
setSearchQuery,
|
||||||
|
setTaskTypeFilter,
|
||||||
|
setStatusFilter,
|
||||||
|
setSelectedProjectId,
|
||||||
|
resetFilters,
|
||||||
|
} = useProjectStore()
|
||||||
|
|
||||||
|
const projectsQuery = useQuery({
|
||||||
|
queryKey: ['projects', { search: searchQuery, taskType: taskTypeFilter, status: statusFilter }],
|
||||||
|
queryFn: () =>
|
||||||
|
projectService.getProjects({
|
||||||
|
search: searchQuery,
|
||||||
|
taskType: taskTypeFilter,
|
||||||
|
status: statusFilter,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const allProjectsQuery = useQuery({
|
||||||
|
queryKey: ['projects', 'all'],
|
||||||
|
queryFn: () => projectService.getProjects(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const activitiesQuery = useQuery({
|
||||||
|
queryKey: ['activities'],
|
||||||
|
queryFn: () => projectService.getRecentActivities(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const createProjectMutation = useMutation({
|
||||||
|
mutationFn: (input: CreateProjectInput) => projectService.createProject(input),
|
||||||
|
onSuccess: (created) => {
|
||||||
|
setSelectedProjectId(created.id)
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['activities'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const updateProjectMutation = useMutation({
|
||||||
|
mutationFn: ({ projectId, updates }: { projectId: string; updates: Partial<Project> }) =>
|
||||||
|
projectService.updateProject(projectId, updates),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const deleteProjectMutation = useMutation({
|
||||||
|
mutationFn: (projectId: string) => projectService.deleteProject(projectId),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const projects = projectsQuery.data ?? []
|
||||||
|
const allProjects = allProjectsQuery.data ?? projects
|
||||||
|
|
||||||
|
const totalImages = allProjects.reduce((acc, p) => acc + p.stats.totalImages, 0)
|
||||||
|
const completedImages = allProjects.reduce((acc, p) => acc + p.stats.completed, 0)
|
||||||
|
const pendingReviewImages = allProjects.reduce((acc, p) => acc + p.stats.pendingReview, 0)
|
||||||
|
|
||||||
|
return {
|
||||||
|
projects,
|
||||||
|
allProjects,
|
||||||
|
activities: activitiesQuery.data ?? [],
|
||||||
|
isLoading: projectsQuery.isLoading,
|
||||||
|
isError: projectsQuery.isError,
|
||||||
|
searchQuery,
|
||||||
|
taskTypeFilter,
|
||||||
|
statusFilter,
|
||||||
|
selectedProjectId,
|
||||||
|
setSearchQuery,
|
||||||
|
setTaskTypeFilter,
|
||||||
|
setStatusFilter,
|
||||||
|
setSelectedProjectId,
|
||||||
|
resetFilters,
|
||||||
|
createProject: createProjectMutation.mutateAsync,
|
||||||
|
isCreating: createProjectMutation.isPending,
|
||||||
|
updateProject: updateProjectMutation.mutateAsync,
|
||||||
|
deleteProject: deleteProjectMutation.mutateAsync,
|
||||||
|
summaryStats: {
|
||||||
|
totalProjects: allProjects.length,
|
||||||
|
totalImages,
|
||||||
|
completedImages,
|
||||||
|
pendingReviewImages,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
79
src/hooks/useUsers.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
import { useState } from 'react'
|
||||||
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
|
import * as userService from '@/services/userService'
|
||||||
|
import { InviteMemberInput, UserRole, UserStatus } from '@/types'
|
||||||
|
|
||||||
|
export function useUsers() {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
const [search, setSearch] = useState('')
|
||||||
|
const [roleFilter, setRoleFilter] = useState<UserRole | 'all'>('all')
|
||||||
|
const [statusFilter, setStatusFilter] = useState<UserStatus | 'all'>('all')
|
||||||
|
|
||||||
|
const membersQuery = useQuery({
|
||||||
|
queryKey: ['members', { search, role: roleFilter, status: statusFilter }],
|
||||||
|
queryFn: () =>
|
||||||
|
userService.getMembers({
|
||||||
|
search,
|
||||||
|
role: roleFilter,
|
||||||
|
status: statusFilter,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const allMembersQuery = useQuery({
|
||||||
|
queryKey: ['members', 'all'],
|
||||||
|
queryFn: () => userService.getMembers(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const permissionsQuery = useQuery({
|
||||||
|
queryKey: ['role-permissions'],
|
||||||
|
queryFn: () => userService.getRolePermissions(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const inviteMutation = useMutation({
|
||||||
|
mutationFn: (input: InviteMemberInput) => userService.inviteMember(input),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['members'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const updateRoleMutation = useMutation({
|
||||||
|
mutationFn: ({ memberId, role }: { memberId: string; role: UserRole }) =>
|
||||||
|
userService.updateMemberRole(memberId, role),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['members'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const toggleStatusMutation = useMutation({
|
||||||
|
mutationFn: (memberId: string) => userService.toggleMemberStatus(memberId),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['members'] })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const allMembers = allMembersQuery.data ?? []
|
||||||
|
|
||||||
|
return {
|
||||||
|
members: membersQuery.data ?? [],
|
||||||
|
allMembers,
|
||||||
|
rolePermissions: permissionsQuery.data ?? [],
|
||||||
|
isLoading: membersQuery.isLoading,
|
||||||
|
search,
|
||||||
|
roleFilter,
|
||||||
|
statusFilter,
|
||||||
|
setSearch,
|
||||||
|
setRoleFilter,
|
||||||
|
setStatusFilter,
|
||||||
|
inviteMember: inviteMutation.mutateAsync,
|
||||||
|
isInviting: inviteMutation.isPending,
|
||||||
|
updateMemberRole: updateRoleMutation.mutateAsync,
|
||||||
|
toggleMemberStatus: toggleStatusMutation.mutateAsync,
|
||||||
|
counts: {
|
||||||
|
total: allMembers.length,
|
||||||
|
admins: allMembers.filter((m) => m.role === 'admin').length,
|
||||||
|
managers: allMembers.filter((m) => m.role === 'manager').length,
|
||||||
|
annotators: allMembers.filter((m) => m.role === 'annotator').length,
|
||||||
|
reviewers: allMembers.filter((m) => m.role === 'reviewer').length,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/layouts/AnnotatorLayout.tsx
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { Outlet } from 'react-router-dom'
|
||||||
|
import { Sidebar } from './components/Sidebar/Sidebar'
|
||||||
|
import { Header } from './components/Header/Header'
|
||||||
|
|
||||||
|
export const AnnotatorLayout: React.FC<{ children?: React.ReactNode }> = ({
|
||||||
|
children,
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<div className="flex h-screen w-screen overflow-hidden bg-[#F8FAFC] text-slate-900">
|
||||||
|
<Sidebar />
|
||||||
|
<div className="flex-1 flex flex-col min-w-0 h-full overflow-hidden">
|
||||||
|
<Header />
|
||||||
|
<main className="flex-1 overflow-hidden">{children || <Outlet />}</main>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
67
src/layouts/AuthLayout.tsx
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { Outlet, Link } from 'react-router-dom'
|
||||||
|
import { showToast } from '../components/feedback/Toast'
|
||||||
|
|
||||||
|
export const AuthLayout: React.FC<{ children?: React.ReactNode }> = ({ children }) => {
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-[#FAF9F6] flex flex-col justify-between text-slate-900">
|
||||||
|
{/* Top Bar */}
|
||||||
|
<header className="w-full px-6 sm:px-10 py-5 flex items-center justify-between border-b border-slate-200/60 bg-white/80 backdrop-blur-sm">
|
||||||
|
<Link to="/login" className="flex items-center gap-3">
|
||||||
|
<div className="flex h-8 w-8 items-center justify-center rounded-lg bg-[#C95C3C] text-white shadow-sm">
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={2.2}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M4 8V6a2 2 0 012-2h2M4 16v2a2 2 0 002 2h2m8-16h2a2 2 0 012 2v2m-4 12h2a2 2 0 002-2v-2"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<span className="text-xl font-bold tracking-tight text-slate-900">
|
||||||
|
danable
|
||||||
|
</span>
|
||||||
|
<span className="hidden sm:inline-block rounded-full border border-slate-200 bg-slate-50 px-2.5 py-0.5 text-[10px] font-semibold uppercase tracking-wider text-slate-500">
|
||||||
|
NỀN TẢNG DỮ LIỆU CHO AI
|
||||||
|
</span>
|
||||||
|
</Link>
|
||||||
|
|
||||||
|
<div className="text-xs text-slate-500">
|
||||||
|
Chưa có tài khoản?{' '}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() =>
|
||||||
|
showToast.info('Vui lòng liên hệ admin@danable.vn để được cấp tài khoản')
|
||||||
|
}
|
||||||
|
className="font-semibold text-[#C95C3C] hover:underline"
|
||||||
|
>
|
||||||
|
Liên hệ quản trị viên ↗
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
{/* Main Content */}
|
||||||
|
<main className="flex-1 flex items-center justify-center p-4 sm:p-8">
|
||||||
|
{children || <Outlet />}
|
||||||
|
</main>
|
||||||
|
|
||||||
|
{/* Footer */}
|
||||||
|
<footer className="w-full px-6 sm:px-10 py-4 border-t border-slate-200/60 bg-white/60 flex flex-col sm:flex-row items-center justify-between gap-2 text-xs text-slate-400">
|
||||||
|
<div>© 2026 danable · Hệ thống gán nhãn dữ liệu thị giác máy tính</div>
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<span className="inline-flex items-center gap-1.5 text-slate-500">
|
||||||
|
<span className="h-1.5 w-1.5 rounded-full bg-emerald-500" />
|
||||||
|
Máy chủ ổn định (v2.4.0)
|
||||||
|
</span>
|
||||||
|
<span>Bảo mật</span>
|
||||||
|
<span>Điều khoản</span>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
20
src/layouts/DashboardLayout.tsx
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { Outlet } from 'react-router-dom'
|
||||||
|
import { Sidebar } from './components/Sidebar/Sidebar'
|
||||||
|
import { Header } from './components/Header/Header'
|
||||||
|
|
||||||
|
export const DashboardLayout: React.FC<{ children?: React.ReactNode }> = ({
|
||||||
|
children,
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<div className="flex min-h-screen bg-[#F8FAFC] text-slate-900">
|
||||||
|
<Sidebar />
|
||||||
|
<div className="flex-1 flex flex-col min-w-0">
|
||||||
|
<Header />
|
||||||
|
<main className="flex-1 overflow-y-auto p-6 lg:p-8">
|
||||||
|
<div className="mx-auto max-w-7xl">{children || <Outlet />}</div>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
90
src/layouts/components/Header/Breadcrumb.tsx
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { Link, useLocation } from 'react-router-dom'
|
||||||
|
|
||||||
|
export interface BreadcrumbItem {
|
||||||
|
label: string
|
||||||
|
to?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BreadcrumbProps {
|
||||||
|
items?: BreadcrumbItem[]
|
||||||
|
}
|
||||||
|
|
||||||
|
const PROJECT_NAMES: Record<string, string> = {
|
||||||
|
'proj-1': 'Phương tiện đô thị',
|
||||||
|
'proj-2': 'Giao thông đô thị',
|
||||||
|
'proj-3': 'Phân vùng đường phố',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Breadcrumb: React.FC<BreadcrumbProps> = ({ items }) => {
|
||||||
|
const location = useLocation()
|
||||||
|
|
||||||
|
const resolvedItems: BreadcrumbItem[] = React.useMemo(() => {
|
||||||
|
if (items && items.length > 0) return items
|
||||||
|
|
||||||
|
const path = location.pathname
|
||||||
|
if (path.startsWith('/users')) {
|
||||||
|
return [
|
||||||
|
{ label: 'Không gian làm việc', to: '/projects' },
|
||||||
|
{ label: 'Quản lý người dùng' },
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts = path.split('/').filter(Boolean)
|
||||||
|
if (parts[0] === 'projects') {
|
||||||
|
const list: BreadcrumbItem[] = [{ label: 'Dự án', to: '/projects' }]
|
||||||
|
if (parts[1]) {
|
||||||
|
const projName = PROJECT_NAMES[parts[1]] || 'Giao thông đô thị'
|
||||||
|
list.push({ label: projName, to: `/projects/${parts[1]}` })
|
||||||
|
if (parts[2] === 'annotate') {
|
||||||
|
list.push({ label: 'Gán nhãn' })
|
||||||
|
} else {
|
||||||
|
list.push({ label: 'Tổng quan' })
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
list.push({ label: 'Danh sách dự án' })
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
return [{ label: 'Dự án', to: '/projects' }, { label: 'Tổng quan' }]
|
||||||
|
}, [items, location.pathname])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<nav className="flex items-center gap-2 text-xs text-slate-500">
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4 text-slate-400 shrink-0"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={1.8}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
{resolvedItems.map((item, idx) => {
|
||||||
|
const isLast = idx === resolvedItems.length - 1
|
||||||
|
return (
|
||||||
|
<React.Fragment key={idx}>
|
||||||
|
{idx > 0 && <span className="text-slate-300">/</span>}
|
||||||
|
{item.to && !isLast ? (
|
||||||
|
<Link
|
||||||
|
to={item.to}
|
||||||
|
className="hover:text-slate-800 transition-colors font-medium"
|
||||||
|
>
|
||||||
|
{item.label}
|
||||||
|
</Link>
|
||||||
|
) : (
|
||||||
|
<span className={isLast ? 'font-medium text-slate-700' : ''}>
|
||||||
|
{item.label}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</React.Fragment>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</nav>
|
||||||
|
)
|
||||||
|
}
|
||||||
22
src/layouts/components/Header/Header.tsx
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { Breadcrumb, BreadcrumbItem } from './Breadcrumb'
|
||||||
|
import { SystemStatus } from './SystemStatus'
|
||||||
|
import { NotificationBell } from './NotificationBell'
|
||||||
|
import { UserMenu } from './UserMenu'
|
||||||
|
|
||||||
|
export interface HeaderProps {
|
||||||
|
breadcrumbItems?: BreadcrumbItem[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Header: React.FC<HeaderProps> = ({ breadcrumbItems }) => {
|
||||||
|
return (
|
||||||
|
<header className="h-14 shrink-0 bg-white border-b border-slate-200/80 px-6 flex items-center justify-between sticky top-0 z-20">
|
||||||
|
<Breadcrumb items={breadcrumbItems} />
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<SystemStatus />
|
||||||
|
<NotificationBell />
|
||||||
|
<UserMenu />
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
)
|
||||||
|
}
|
||||||
120
src/layouts/components/Header/NotificationBell.tsx
Normal file
|
|
@ -0,0 +1,120 @@
|
||||||
|
import React, { useState, useRef, useEffect } from 'react'
|
||||||
|
|
||||||
|
interface NotificationItem {
|
||||||
|
id: string
|
||||||
|
title: string
|
||||||
|
time: string
|
||||||
|
unread?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
const MOCK_NOTIFICATIONS: NotificationItem[] = [
|
||||||
|
{
|
||||||
|
id: 'n1',
|
||||||
|
title: 'Hà Linh đã gửi 18 ảnh chờ duyệt trong dự án Giao thông đô thị',
|
||||||
|
time: '5 phút trước',
|
||||||
|
unread: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'n2',
|
||||||
|
title: 'Tuấn Nam hoàn thành gán nhãn lô #04 (120 ảnh)',
|
||||||
|
time: '1 giờ trước',
|
||||||
|
unread: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'n3',
|
||||||
|
title: 'Xuất bộ nhãn YOLOv8 thành công',
|
||||||
|
time: 'Hôm qua',
|
||||||
|
unread: false,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
export const NotificationBell: React.FC = () => {
|
||||||
|
const [open, setOpen] = useState(false)
|
||||||
|
const [notifications, setNotifications] = useState(MOCK_NOTIFICATIONS)
|
||||||
|
const ref = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleClickOutside = (event: MouseEvent) => {
|
||||||
|
if (ref.current && !ref.current.contains(event.target as Node)) {
|
||||||
|
setOpen(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.addEventListener('mousedown', handleClickOutside)
|
||||||
|
return () => document.removeEventListener('mousedown', handleClickOutside)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const unreadCount = notifications.filter((n) => n.unread).length
|
||||||
|
|
||||||
|
const markAllRead = () => {
|
||||||
|
setNotifications((prev) => prev.map((n) => ({ ...n, unread: false })))
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div ref={ref} className="relative">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setOpen((prev) => !prev)}
|
||||||
|
className="relative rounded-full p-2 text-slate-500 hover:bg-slate-100 hover:text-slate-800 transition-colors"
|
||||||
|
aria-label="Thông báo"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={1.9}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
{unreadCount > 0 && (
|
||||||
|
<span className="absolute top-1.5 right-1.5 h-2 w-2 rounded-full bg-[#C95C3C] ring-2 ring-white" />
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{open && (
|
||||||
|
<div className="absolute right-0 z-50 mt-2 w-80 rounded-2xl border border-slate-200 bg-white shadow-xl overflow-hidden">
|
||||||
|
<div className="flex items-center justify-between border-b border-slate-100 px-4 py-3">
|
||||||
|
<span className="text-xs font-semibold text-slate-900">
|
||||||
|
Thông báo ({unreadCount})
|
||||||
|
</span>
|
||||||
|
{unreadCount > 0 && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={markAllRead}
|
||||||
|
className="text-[11px] font-medium text-[#C95C3C] hover:underline"
|
||||||
|
>
|
||||||
|
Đánh dấu đã đọc
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div className="divide-y divide-slate-100 max-h-72 overflow-y-auto">
|
||||||
|
{notifications.map((item) => (
|
||||||
|
<div
|
||||||
|
key={item.id}
|
||||||
|
className="px-4 py-3 hover:bg-slate-50 transition-colors"
|
||||||
|
>
|
||||||
|
<div className="flex items-start gap-2">
|
||||||
|
{item.unread && (
|
||||||
|
<span className="mt-1.5 h-1.5 w-1.5 rounded-full bg-[#C95C3C] shrink-0" />
|
||||||
|
)}
|
||||||
|
<div>
|
||||||
|
<p className="text-xs text-slate-700 leading-relaxed">
|
||||||
|
{item.title}
|
||||||
|
</p>
|
||||||
|
<span className="mt-1 block text-[11px] text-slate-400">
|
||||||
|
{item.time}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
10
src/layouts/components/Header/SystemStatus.tsx
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export const SystemStatus: React.FC = () => {
|
||||||
|
return (
|
||||||
|
<div className="hidden sm:flex items-center gap-2 text-xs text-slate-600">
|
||||||
|
<span className="h-2 w-2 rounded-full bg-emerald-600" />
|
||||||
|
<span>Hệ thống ổn định</span>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
37
src/layouts/components/Header/UserMenu.tsx
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useNavigate } from 'react-router-dom'
|
||||||
|
import { Dropdown } from '../../../components/ui/Dropdown'
|
||||||
|
import { showToast } from '../../../components/feedback/Toast'
|
||||||
|
|
||||||
|
export const UserMenu: React.FC = () => {
|
||||||
|
const navigate = useNavigate()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dropdown
|
||||||
|
align="right"
|
||||||
|
trigger={
|
||||||
|
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-orange-100 text-xs font-bold text-slate-800 hover:ring-2 hover:ring-orange-200 transition-all">
|
||||||
|
MA
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
items={[
|
||||||
|
{
|
||||||
|
label: 'Hồ sơ cá nhân (Minh Anh)',
|
||||||
|
onClick: () => showToast.info('Đang mở hồ sơ Quản trị viên Minh Anh'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Quản lý người dùng',
|
||||||
|
onClick: () => navigate('/users'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Đăng xuất',
|
||||||
|
danger: true,
|
||||||
|
onClick: () => {
|
||||||
|
showToast.success('Đã đăng xuất thành công')
|
||||||
|
navigate('/login')
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
63
src/layouts/components/Sidebar/RecentProjects.tsx
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { Link, useLocation } from 'react-router-dom'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
interface RecentProjectItem {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
path: string
|
||||||
|
dotColor: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const RECENT_PROJECTS: RecentProjectItem[] = [
|
||||||
|
{
|
||||||
|
id: 'proj-1',
|
||||||
|
name: 'Phương tiện đô thị',
|
||||||
|
path: '/projects/proj-1',
|
||||||
|
dotColor: 'bg-indigo-500',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'proj-2',
|
||||||
|
name: 'Giao thông đô thị',
|
||||||
|
path: '/projects/proj-2',
|
||||||
|
dotColor: 'bg-blue-600',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'proj-3',
|
||||||
|
name: 'Phân vùng đường phố',
|
||||||
|
path: '/projects/proj-3',
|
||||||
|
dotColor: 'bg-violet-500',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
export const RecentProjects: React.FC = () => {
|
||||||
|
const location = useLocation()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div className="px-2 mb-2 text-[10px] font-bold uppercase tracking-wider text-slate-500">
|
||||||
|
DỰ ÁN GẦN ĐÂY
|
||||||
|
</div>
|
||||||
|
<div className="space-y-0.5">
|
||||||
|
{RECENT_PROJECTS.map((project) => {
|
||||||
|
const isCurrent = location.pathname.startsWith(project.path)
|
||||||
|
return (
|
||||||
|
<Link
|
||||||
|
key={project.id}
|
||||||
|
to={project.path}
|
||||||
|
className={clsx(
|
||||||
|
'flex items-center gap-2.5 rounded-lg px-2.5 py-2 text-xs font-medium transition-colors',
|
||||||
|
isCurrent
|
||||||
|
? 'bg-slate-100 text-slate-900 font-semibold'
|
||||||
|
: 'text-slate-600 hover:bg-slate-50 hover:text-slate-900'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<span className={clsx('h-1.5 w-1.5 rounded-full shrink-0', project.dotColor)} />
|
||||||
|
<span className="truncate">{project.name}</span>
|
||||||
|
</Link>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
45
src/layouts/components/Sidebar/Sidebar.tsx
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { Link } from 'react-router-dom'
|
||||||
|
import { WorkspaceSelector } from './WorkspaceSelector'
|
||||||
|
import { SidebarNav } from './SidebarNav'
|
||||||
|
import { RecentProjects } from './RecentProjects'
|
||||||
|
import { StorageUsage } from './StorageUsage'
|
||||||
|
import { UserInfo } from './UserInfo'
|
||||||
|
|
||||||
|
export const Sidebar: React.FC = () => {
|
||||||
|
return (
|
||||||
|
<aside className="w-[240px] shrink-0 bg-white border-r border-slate-200/80 flex flex-col justify-between h-screen sticky top-0 px-3.5 py-4 select-none">
|
||||||
|
<div className="space-y-5">
|
||||||
|
{/* Brand Logo */}
|
||||||
|
<Link to="/projects" className="flex items-center gap-2.5 px-1">
|
||||||
|
<div className="flex h-8 w-8 items-center justify-center rounded-lg bg-[#C95C3C] text-white shadow-sm">
|
||||||
|
<svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.2}>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M4 8V6a2 2 0 012-2h2M4 16v2a2 2 0 002 2h2m8-16h2a2 2 0 012 2v2m-4 12h2a2 2 0 002-2v-2"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<span className="text-xl font-bold tracking-tight text-slate-900">
|
||||||
|
danable
|
||||||
|
</span>
|
||||||
|
</Link>
|
||||||
|
|
||||||
|
{/* Workspace Selector */}
|
||||||
|
<WorkspaceSelector />
|
||||||
|
|
||||||
|
{/* Navigation Links */}
|
||||||
|
<SidebarNav />
|
||||||
|
|
||||||
|
{/* Recent Projects */}
|
||||||
|
<RecentProjects />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-4 pt-4">
|
||||||
|
<StorageUsage />
|
||||||
|
<UserInfo />
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
)
|
||||||
|
}
|
||||||
92
src/layouts/components/Sidebar/SidebarNav.tsx
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { NavLink, useLocation } from 'react-router-dom'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
interface NavItem {
|
||||||
|
to: string
|
||||||
|
label: string
|
||||||
|
badge: number
|
||||||
|
icon: React.ReactNode
|
||||||
|
matchPrefix: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const SidebarNav: React.FC = () => {
|
||||||
|
const location = useLocation()
|
||||||
|
|
||||||
|
const navItems: NavItem[] = [
|
||||||
|
{
|
||||||
|
to: '/projects',
|
||||||
|
label: 'Dự án',
|
||||||
|
badge: 3,
|
||||||
|
matchPrefix: '/projects',
|
||||||
|
icon: (
|
||||||
|
<svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
to: '/users',
|
||||||
|
label: 'Người dùng',
|
||||||
|
badge: 8,
|
||||||
|
matchPrefix: '/users',
|
||||||
|
icon: (
|
||||||
|
<svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div className="px-2 mb-2 text-[10px] font-bold uppercase tracking-wider text-slate-500">
|
||||||
|
KHÔNG GIAN LÀM VIỆC
|
||||||
|
</div>
|
||||||
|
<nav className="space-y-1">
|
||||||
|
{navItems.map((item) => {
|
||||||
|
const isActive =
|
||||||
|
location.pathname === item.to ||
|
||||||
|
location.pathname.startsWith(item.matchPrefix) ||
|
||||||
|
(item.to === '/projects' && location.pathname === '/')
|
||||||
|
|
||||||
|
return (
|
||||||
|
<NavLink
|
||||||
|
key={item.to}
|
||||||
|
to={item.to}
|
||||||
|
className={clsx(
|
||||||
|
'flex items-center justify-between rounded-xl px-3 py-2.5 text-sm font-medium transition-colors',
|
||||||
|
isActive
|
||||||
|
? 'bg-[#C95C3C] text-white shadow-sm'
|
||||||
|
: 'text-slate-700 hover:bg-slate-100'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-2.5">
|
||||||
|
<span className={clsx(isActive ? 'text-white' : 'text-slate-500')}>
|
||||||
|
{item.icon}
|
||||||
|
</span>
|
||||||
|
<span>{item.label}</span>
|
||||||
|
</div>
|
||||||
|
<span
|
||||||
|
className={clsx(
|
||||||
|
'text-xs font-semibold',
|
||||||
|
isActive ? 'text-white/90' : 'text-slate-600'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{item.badge}
|
||||||
|
</span>
|
||||||
|
</NavLink>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
21
src/layouts/components/Sidebar/StorageUsage.tsx
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export const StorageUsage: React.FC = () => {
|
||||||
|
return (
|
||||||
|
<div className="rounded-xl bg-slate-100/80 border border-slate-200/60 p-3.5">
|
||||||
|
<div className="flex items-center justify-between text-xs font-medium text-slate-700 mb-2">
|
||||||
|
<span>Dung lượng dữ liệu</span>
|
||||||
|
<span className="font-semibold text-slate-900">24%</span>
|
||||||
|
</div>
|
||||||
|
<div className="h-1.5 w-full overflow-hidden rounded-full bg-slate-200">
|
||||||
|
<div
|
||||||
|
className="h-full rounded-full bg-[#3B5BDB]"
|
||||||
|
style={{ width: '24%' }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="mt-2 text-[11px] text-slate-500">
|
||||||
|
12 GB / 50 GB đã sử dụng
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
73
src/layouts/components/Sidebar/UserInfo.tsx
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useNavigate } from 'react-router-dom'
|
||||||
|
import { showToast } from '../../../components/feedback/Toast'
|
||||||
|
|
||||||
|
export const UserInfo: React.FC = () => {
|
||||||
|
const navigate = useNavigate()
|
||||||
|
|
||||||
|
const handleLogout = () => {
|
||||||
|
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 px-2 py-1.5 text-xs font-medium text-slate-600 hover:text-slate-900 transition-colors"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4 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="border-t border-slate-200 pt-3 flex items-center justify-between">
|
||||||
|
<div className="flex items-center gap-2.5 min-w-0">
|
||||||
|
<div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-orange-100 text-xs font-bold text-slate-800">
|
||||||
|
MA
|
||||||
|
</div>
|
||||||
|
<div className="min-w-0">
|
||||||
|
<div className="truncate text-xs font-semibold text-slate-900">
|
||||||
|
Minh Anh
|
||||||
|
</div>
|
||||||
|
<div className="truncate text-[11px] text-slate-500">
|
||||||
|
Quản trị viên
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<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>
|
||||||
|
)
|
||||||
|
}
|
||||||
69
src/layouts/components/Sidebar/WorkspaceSelector.tsx
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
import React, { useState } from 'react'
|
||||||
|
import { showToast } from '../../../components/feedback/Toast'
|
||||||
|
|
||||||
|
export const WorkspaceSelector: React.FC = () => {
|
||||||
|
const [open, setOpen] = useState(false)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="relative">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setOpen((prev) => !prev)}
|
||||||
|
className="w-full flex items-center justify-between rounded-xl border border-slate-300/90 bg-white px-3 py-2.5 text-left hover:border-slate-400 transition-colors"
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-2.5 min-w-0">
|
||||||
|
<div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-indigo-50 text-xs font-bold text-slate-700">
|
||||||
|
VL
|
||||||
|
</div>
|
||||||
|
<div className="min-w-0">
|
||||||
|
<div className="truncate text-sm font-semibold text-slate-900 leading-tight">
|
||||||
|
danable
|
||||||
|
</div>
|
||||||
|
<div className="truncate text-[11px] text-slate-500 mt-0.5">
|
||||||
|
Không gian nhóm
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4 text-slate-400 shrink-0"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={1.8}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
d="M8 9l4-4 4 4m0 6l-4 4-4-4"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{open && (
|
||||||
|
<div className="absolute left-0 right-0 z-30 mt-1.5 rounded-xl border border-slate-200 bg-white p-1.5 shadow-lg">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
setOpen(false)
|
||||||
|
showToast.info('Đang ở Không gian nhóm danable')
|
||||||
|
}}
|
||||||
|
className="flex w-full items-center justify-between rounded-lg bg-slate-50 px-2.5 py-2 text-xs font-medium text-slate-800"
|
||||||
|
>
|
||||||
|
<span>danable (Không gian nhóm)</span>
|
||||||
|
<span className="text-[#C95C3C] font-bold">✓</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
setOpen(false)
|
||||||
|
showToast.info('Đã chuyển sang Không gian cá nhân')
|
||||||
|
}}
|
||||||
|
className="mt-0.5 flex w-full items-center justify-between rounded-lg px-2.5 py-2 text-xs text-slate-600 hover:bg-slate-50"
|
||||||
|
>
|
||||||
|
<span>VisionLab R&D</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
11
src/layouts/components/index.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
export * from './Sidebar/Sidebar'
|
||||||
|
export * from './Sidebar/WorkspaceSelector'
|
||||||
|
export * from './Sidebar/SidebarNav'
|
||||||
|
export * from './Sidebar/RecentProjects'
|
||||||
|
export * from './Sidebar/StorageUsage'
|
||||||
|
export * from './Sidebar/UserInfo'
|
||||||
|
export * from './Header/Header'
|
||||||
|
export * from './Header/Breadcrumb'
|
||||||
|
export * from './Header/SystemStatus'
|
||||||
|
export * from './Header/NotificationBell'
|
||||||
|
export * from './Header/UserMenu'
|
||||||
11
src/main.tsx
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import React from 'react'
|
||||||
|
import ReactDOM from 'react-dom/client'
|
||||||
|
import App from './App'
|
||||||
|
import './styles/globals.css'
|
||||||
|
|
||||||
|
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||||
|
<React.StrictMode>
|
||||||
|
<App />
|
||||||
|
</React.StrictMode>
|
||||||
|
)
|
||||||
|
|
||||||
167
src/pages/annotator/AnnotatorPage.tsx
Normal file
|
|
@ -0,0 +1,167 @@
|
||||||
|
import React, { useEffect } from 'react'
|
||||||
|
import { useParams, useSearchParams } from 'react-router-dom'
|
||||||
|
import toast from 'react-hot-toast'
|
||||||
|
import { TaskType } from '@/types'
|
||||||
|
import { useAnnotatorStore } from './annotatorStore'
|
||||||
|
import { AnnotatorHeader } from './components/header/AnnotatorHeader'
|
||||||
|
import { ImageInfo } from './components/header/ImageInfo'
|
||||||
|
import { ToolBar } from './components/canvas/ToolBar'
|
||||||
|
import { AnnotationCanvas } from './components/canvas/AnnotationCanvas'
|
||||||
|
import { ImageCounter } from './components/navigator/ImageCounter'
|
||||||
|
import { ImageNavigator } from './components/navigator/ImageNavigator'
|
||||||
|
import { ClassificationPanel } from './components/sidebar-right/ClassificationPanel'
|
||||||
|
import { ObjectListPanel } from './components/sidebar-right/ObjectListPanel'
|
||||||
|
import { SegmentationPanel } from './components/sidebar-right/SegmentationPanel'
|
||||||
|
|
||||||
|
export const AnnotatorPage: React.FC = () => {
|
||||||
|
const { id } = useParams<{ id: string }>()
|
||||||
|
const [searchParams] = useSearchParams()
|
||||||
|
const taskParam = searchParams.get('task') as TaskType | null
|
||||||
|
|
||||||
|
const project = useAnnotatorStore((s) => s.project)
|
||||||
|
const initProject = useAnnotatorStore((s) => s.initProject)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
initProject(id ?? 'proj-1', taskParam ?? undefined)
|
||||||
|
}, [id, taskParam, initProject])
|
||||||
|
|
||||||
|
// Global keyboard shortcuts for fast annotation
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
|
const target = e.target as HTMLElement | null
|
||||||
|
if (
|
||||||
|
target &&
|
||||||
|
(target.tagName === 'INPUT' ||
|
||||||
|
target.tagName === 'TEXTAREA' ||
|
||||||
|
target.tagName === 'SELECT')
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const state = useAnnotatorStore.getState()
|
||||||
|
|
||||||
|
// Undo / Redo
|
||||||
|
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'z') {
|
||||||
|
e.preventDefault()
|
||||||
|
if (e.shiftKey) {
|
||||||
|
state.redo()
|
||||||
|
} else {
|
||||||
|
state.undo()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'y') {
|
||||||
|
e.preventDefault()
|
||||||
|
state.redo()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1-4 class selection
|
||||||
|
if (['1', '2', '3', '4'].includes(e.key)) {
|
||||||
|
const idx = Number(e.key) - 1
|
||||||
|
const cls = state.project.classes[idx]
|
||||||
|
if (cls) {
|
||||||
|
if (state.project.taskType === 'classification') {
|
||||||
|
state.setClassificationLabel(cls.id)
|
||||||
|
} else {
|
||||||
|
state.updateSelectedShapeClass(cls.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tool shortcuts
|
||||||
|
const keyLower = e.key.toLowerCase()
|
||||||
|
if (keyLower === 'v') {
|
||||||
|
state.setActiveTool('select')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (keyLower === 'b' && state.project.taskType === 'object_detection') {
|
||||||
|
state.setActiveTool('bbox')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (keyLower === 'p' && state.project.taskType === 'segmentation') {
|
||||||
|
state.setActiveTool('polygon')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enter: close draft polygon or approve classification
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
if (
|
||||||
|
state.project.taskType === 'segmentation' &&
|
||||||
|
state.draftPolygonPoints.length >= 3
|
||||||
|
) {
|
||||||
|
e.preventDefault()
|
||||||
|
state.closeDraftPolygon()
|
||||||
|
} else if (state.project.taskType === 'classification') {
|
||||||
|
e.preventDefault()
|
||||||
|
state.updateImageStatus('completed')
|
||||||
|
toast.success('Đã duyệt nhãn ảnh hiện tại')
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escape: cancel draft polygon
|
||||||
|
if (e.key === 'Escape' && state.draftPolygonPoints.length > 0) {
|
||||||
|
state.cancelDraftPolygon()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete / Backspace: remove selected shape
|
||||||
|
if (
|
||||||
|
(e.key === 'Delete' || e.key === 'Backspace') &&
|
||||||
|
state.selectedShapeId &&
|
||||||
|
state.project.taskType !== 'classification'
|
||||||
|
) {
|
||||||
|
e.preventDefault()
|
||||||
|
state.deleteShape(state.selectedShapeId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Left / Right arrow keys: navigate images
|
||||||
|
if (e.key === 'ArrowLeft') {
|
||||||
|
state.prevImage()
|
||||||
|
} else if (e.key === 'ArrowRight') {
|
||||||
|
state.nextImage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('keydown', handleKeyDown)
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-2">
|
||||||
|
{/* Top Project Title & Action Buttons */}
|
||||||
|
<AnnotatorHeader />
|
||||||
|
|
||||||
|
{/* Main 2-Column Workspace */}
|
||||||
|
<div className="grid grid-cols-1 gap-4 lg:grid-cols-12 items-start">
|
||||||
|
{/* Left Column: Image Info + Toolbar + Canvas + Navigator */}
|
||||||
|
<div className="overflow-hidden rounded-2xl border border-slate-200/80 bg-white shadow-2xs lg:col-span-8 xl:col-span-9">
|
||||||
|
{/* File Name, Resolution & Image Counter Bar */}
|
||||||
|
<div className="flex items-center justify-between border-b border-slate-100 px-4 py-2.5">
|
||||||
|
<ImageInfo />
|
||||||
|
<ImageCounter />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Annotation Tools & Zoom Controls */}
|
||||||
|
<ToolBar />
|
||||||
|
|
||||||
|
{/* Interactive SVG + Image Canvas */}
|
||||||
|
<AnnotationCanvas />
|
||||||
|
|
||||||
|
{/* Bottom Hint, Thumbnail Strip & Save/Export Status */}
|
||||||
|
<ImageNavigator />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Right Column: Task-Specific Panel */}
|
||||||
|
<div className="lg:col-span-4 xl:col-span-3">
|
||||||
|
{project.taskType === 'classification' && <ClassificationPanel />}
|
||||||
|
{project.taskType === 'object_detection' && <ObjectListPanel />}
|
||||||
|
{project.taskType === 'segmentation' && <SegmentationPanel />}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
1174
src/pages/annotator/annotatorStore.ts
Normal file
303
src/pages/annotator/components/canvas/AnnotationCanvas.tsx
Normal file
|
|
@ -0,0 +1,303 @@
|
||||||
|
import React, { useRef, useState } from 'react'
|
||||||
|
import { AnnotationShape, BoundingBox, Point2D } from '@/types'
|
||||||
|
import { normalizeBBox } from '@/utils/canvas'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
import { ImageViewer } from './ImageViewer'
|
||||||
|
import { BoundingBoxTool, ResizeHandleCorner } from './BoundingBoxTool'
|
||||||
|
import { PolygonTool } from './PolygonTool'
|
||||||
|
import { PointTool } from './PointTool'
|
||||||
|
import { SelectTool } from './SelectTool'
|
||||||
|
import { CanvasOverlay } from './CanvasOverlay'
|
||||||
|
|
||||||
|
type DragInteraction =
|
||||||
|
| { type: 'draw-bbox'; start: Point2D; current: Point2D }
|
||||||
|
| { type: 'move-bbox'; shapeId: string; startMouse: Point2D; initialBox: BoundingBox }
|
||||||
|
| {
|
||||||
|
type: 'resize-bbox'
|
||||||
|
shapeId: string
|
||||||
|
corner: ResizeHandleCorner
|
||||||
|
startMouse: Point2D
|
||||||
|
initialBox: BoundingBox
|
||||||
|
}
|
||||||
|
| { type: 'drag-vertex'; shapeId: string; vertexIndex: number }
|
||||||
|
|
||||||
|
export const AnnotationCanvas: React.FC = () => {
|
||||||
|
const svgRef = useRef<SVGSVGElement | null>(null)
|
||||||
|
|
||||||
|
const project = useAnnotatorStore((s) => s.project)
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
const activeTool = useAnnotatorStore((s) => s.activeTool)
|
||||||
|
const selectedClassId = useAnnotatorStore((s) => s.selectedClassId)
|
||||||
|
const selectedShapeId = useAnnotatorStore((s) => s.selectedShapeId)
|
||||||
|
const shapesByImageId = useAnnotatorStore((s) => s.shapesByImageId)
|
||||||
|
const showBoxes = useAnnotatorStore((s) => s.showBoxes)
|
||||||
|
const showMasks = useAnnotatorStore((s) => s.showMasks)
|
||||||
|
const maskOpacity = useAnnotatorStore((s) => s.maskOpacity)
|
||||||
|
const vertexEditEnabled = useAnnotatorStore((s) => s.vertexEditEnabled)
|
||||||
|
const zoom = useAnnotatorStore((s) => s.zoom)
|
||||||
|
const draftPolygonPoints = useAnnotatorStore((s) => s.draftPolygonPoints)
|
||||||
|
|
||||||
|
const setSelectedShapeId = useAnnotatorStore((s) => s.setSelectedShapeId)
|
||||||
|
const addBoundingBox = useAnnotatorStore((s) => s.addBoundingBox)
|
||||||
|
const updateBoundingBox = useAnnotatorStore((s) => s.updateBoundingBox)
|
||||||
|
const addDraftPolygonPoint = useAnnotatorStore((s) => s.addDraftPolygonPoint)
|
||||||
|
const closeDraftPolygon = useAnnotatorStore((s) => s.closeDraftPolygon)
|
||||||
|
const updatePolygonVertex = useAnnotatorStore((s) => s.updatePolygonVertex)
|
||||||
|
const insertPolygonVertex = useAnnotatorStore((s) => s.insertPolygonVertex)
|
||||||
|
const addPointShape = useAnnotatorStore((s) => s.addPointShape)
|
||||||
|
|
||||||
|
const [interaction, setInteraction] = useState<DragInteraction | null>(null)
|
||||||
|
const [cursorPoint, setCursorPoint] = useState<Point2D | null>(null)
|
||||||
|
|
||||||
|
if (!currentImage) return null
|
||||||
|
|
||||||
|
const shapes = shapesByImageId[currentImage.id] ?? []
|
||||||
|
const selectedShape = shapes.find((s) => s.id === selectedShapeId)
|
||||||
|
const activeClass =
|
||||||
|
project.classes.find((c) => c.id === selectedClassId) ?? project.classes[0]!
|
||||||
|
|
||||||
|
// Convert client mouse position to 1920x1080 SVG coordinate space
|
||||||
|
const getSvgPoint = (e: React.MouseEvent): Point2D => {
|
||||||
|
const svg = svgRef.current
|
||||||
|
if (!svg) return { x: 0, y: 0 }
|
||||||
|
const rect = svg.getBoundingClientRect()
|
||||||
|
if (rect.width === 0 || rect.height === 0) return { x: 0, y: 0 }
|
||||||
|
const scaleX = currentImage.width / rect.width
|
||||||
|
const scaleY = currentImage.height / rect.height
|
||||||
|
return {
|
||||||
|
x: Math.max(0, Math.min(currentImage.width, (e.clientX - rect.left) * scaleX)),
|
||||||
|
y: Math.max(0, Math.min(currentImage.height, (e.clientY - rect.top) * scaleY)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleCanvasMouseDown = (e: React.MouseEvent) => {
|
||||||
|
if (e.button !== 0) return
|
||||||
|
const pt = getSvgPoint(e)
|
||||||
|
|
||||||
|
if (project.taskType === 'object_detection' && activeTool === 'bbox') {
|
||||||
|
setInteraction({ type: 'draw-bbox', start: pt, current: pt })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.taskType === 'segmentation' && activeTool === 'polygon') {
|
||||||
|
addDraftPolygonPoint(pt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeTool === 'point' && project.taskType !== 'classification') {
|
||||||
|
addPointShape(pt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeTool === 'select') {
|
||||||
|
setSelectedShapeId(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleCanvasMouseMove = (e: React.MouseEvent) => {
|
||||||
|
const pt = getSvgPoint(e)
|
||||||
|
if (draftPolygonPoints.length > 0) {
|
||||||
|
setCursorPoint(pt)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!interaction) return
|
||||||
|
|
||||||
|
if (interaction.type === 'draw-bbox') {
|
||||||
|
setInteraction({ ...interaction, current: pt })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (interaction.type === 'move-bbox') {
|
||||||
|
const dx = pt.x - interaction.startMouse.x
|
||||||
|
const dy = pt.y - interaction.startMouse.y
|
||||||
|
updateBoundingBox(interaction.shapeId, {
|
||||||
|
x: interaction.initialBox.x + dx,
|
||||||
|
y: interaction.initialBox.y + dy,
|
||||||
|
width: interaction.initialBox.width,
|
||||||
|
height: interaction.initialBox.height,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (interaction.type === 'resize-bbox') {
|
||||||
|
const { initialBox, corner, startMouse } = interaction
|
||||||
|
const dx = pt.x - startMouse.x
|
||||||
|
const dy = pt.y - startMouse.y
|
||||||
|
|
||||||
|
let newBox: BoundingBox = { ...initialBox }
|
||||||
|
if (corner === 'nw') {
|
||||||
|
newBox = {
|
||||||
|
x: initialBox.x + dx,
|
||||||
|
y: initialBox.y + dy,
|
||||||
|
width: initialBox.width - dx,
|
||||||
|
height: initialBox.height - dy,
|
||||||
|
}
|
||||||
|
} else if (corner === 'ne') {
|
||||||
|
newBox = {
|
||||||
|
x: initialBox.x,
|
||||||
|
y: initialBox.y + dy,
|
||||||
|
width: initialBox.width + dx,
|
||||||
|
height: initialBox.height - dy,
|
||||||
|
}
|
||||||
|
} else if (corner === 'sw') {
|
||||||
|
newBox = {
|
||||||
|
x: initialBox.x + dx,
|
||||||
|
y: initialBox.y,
|
||||||
|
width: initialBox.width - dx,
|
||||||
|
height: initialBox.height + dy,
|
||||||
|
}
|
||||||
|
} else if (corner === 'se') {
|
||||||
|
newBox = {
|
||||||
|
x: initialBox.x,
|
||||||
|
y: initialBox.y,
|
||||||
|
width: initialBox.width + dx,
|
||||||
|
height: initialBox.height + dy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateBoundingBox(interaction.shapeId, newBox)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (interaction.type === 'drag-vertex') {
|
||||||
|
updatePolygonVertex(interaction.shapeId, interaction.vertexIndex, pt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleCanvasMouseUp = () => {
|
||||||
|
if (!interaction) return
|
||||||
|
if (interaction.type === 'draw-bbox') {
|
||||||
|
const rawBox: BoundingBox = {
|
||||||
|
x: interaction.start.x,
|
||||||
|
y: interaction.start.y,
|
||||||
|
width: interaction.current.x - interaction.start.x,
|
||||||
|
height: interaction.current.y - interaction.start.y,
|
||||||
|
}
|
||||||
|
addBoundingBox(rawBox)
|
||||||
|
}
|
||||||
|
setInteraction(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleDoubleClick = () => {
|
||||||
|
if (project.taskType === 'segmentation' && draftPolygonPoints.length >= 3) {
|
||||||
|
closeDraftPolygon()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const draftBox =
|
||||||
|
interaction?.type === 'draw-bbox'
|
||||||
|
? normalizeBBox(
|
||||||
|
{
|
||||||
|
x: interaction.start.x,
|
||||||
|
y: interaction.start.y,
|
||||||
|
width: interaction.current.x - interaction.start.x,
|
||||||
|
height: interaction.current.y - interaction.start.y,
|
||||||
|
},
|
||||||
|
currentImage.width,
|
||||||
|
currentImage.height
|
||||||
|
)
|
||||||
|
: null
|
||||||
|
|
||||||
|
const scaleFactor = zoom / 75
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="px-4 py-3">
|
||||||
|
<div className="relative overflow-hidden rounded-xl border border-slate-200/80 bg-slate-900">
|
||||||
|
<div
|
||||||
|
className="w-full origin-center transition-transform duration-150"
|
||||||
|
style={{
|
||||||
|
transform: scaleFactor !== 1 ? `scale(${scaleFactor})` : undefined,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
ref={svgRef}
|
||||||
|
viewBox={`0 0 ${currentImage.width} ${currentImage.height}`}
|
||||||
|
className="block aspect-video w-full select-none"
|
||||||
|
style={{
|
||||||
|
cursor:
|
||||||
|
activeTool === 'bbox' || activeTool === 'polygon' || activeTool === 'point'
|
||||||
|
? 'crosshair'
|
||||||
|
: 'default',
|
||||||
|
}}
|
||||||
|
onMouseDown={handleCanvasMouseDown}
|
||||||
|
onMouseMove={handleCanvasMouseMove}
|
||||||
|
onMouseUp={handleCanvasMouseUp}
|
||||||
|
onDoubleClick={handleDoubleClick}
|
||||||
|
>
|
||||||
|
<ImageViewer
|
||||||
|
url={currentImage.url}
|
||||||
|
width={currentImage.width}
|
||||||
|
height={currentImage.height}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{project.taskType === 'object_detection' && (
|
||||||
|
<BoundingBoxTool
|
||||||
|
shapes={shapes}
|
||||||
|
selectedShapeId={selectedShapeId}
|
||||||
|
showBoxes={showBoxes}
|
||||||
|
draftBox={draftBox}
|
||||||
|
activeClassColor={activeClass.color}
|
||||||
|
activeClassName={activeClass.name}
|
||||||
|
onSelectShape={setSelectedShapeId}
|
||||||
|
onStartMoveBox={(e, shape) => {
|
||||||
|
if (!shape.bbox) return
|
||||||
|
setInteraction({
|
||||||
|
type: 'move-bbox',
|
||||||
|
shapeId: shape.id,
|
||||||
|
startMouse: getSvgPoint(e),
|
||||||
|
initialBox: { ...shape.bbox },
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
onStartResizeBox={(e, shape, corner) => {
|
||||||
|
if (!shape.bbox) return
|
||||||
|
setInteraction({
|
||||||
|
type: 'resize-bbox',
|
||||||
|
shapeId: shape.id,
|
||||||
|
corner,
|
||||||
|
startMouse: getSvgPoint(e),
|
||||||
|
initialBox: { ...shape.bbox },
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{project.taskType === 'segmentation' && (
|
||||||
|
<PolygonTool
|
||||||
|
shapes={shapes}
|
||||||
|
selectedShapeId={selectedShapeId}
|
||||||
|
showMasks={showMasks}
|
||||||
|
maskOpacity={maskOpacity}
|
||||||
|
vertexEditEnabled={vertexEditEnabled}
|
||||||
|
draftPolygonPoints={draftPolygonPoints}
|
||||||
|
cursorPoint={cursorPoint}
|
||||||
|
activeClassColor={activeClass.color}
|
||||||
|
onSelectShape={setSelectedShapeId}
|
||||||
|
onStartDragVertex={(_e, shapeId, vertexIndex) => {
|
||||||
|
setInteraction({
|
||||||
|
type: 'drag-vertex',
|
||||||
|
shapeId,
|
||||||
|
vertexIndex,
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
onInsertVertexOnEdge={(shapeId, afterIndex, pt) => {
|
||||||
|
insertPolygonVertex(shapeId, afterIndex, pt)
|
||||||
|
}}
|
||||||
|
onCloseDraftPolygon={closeDraftPolygon}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<PointTool
|
||||||
|
shapes={shapes}
|
||||||
|
selectedShapeId={selectedShapeId}
|
||||||
|
onSelectShape={setSelectedShapeId}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<SelectTool selectedShape={selectedShape} />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Bottom-left dark translucent pill overlay */}
|
||||||
|
<CanvasOverlay />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
163
src/pages/annotator/components/canvas/BoundingBoxTool.tsx
Normal file
|
|
@ -0,0 +1,163 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { AnnotationShape, BoundingBox } from '@/types'
|
||||||
|
|
||||||
|
export type ResizeHandleCorner = 'nw' | 'ne' | 'sw' | 'se'
|
||||||
|
|
||||||
|
export interface BoundingBoxToolProps {
|
||||||
|
shapes: AnnotationShape[]
|
||||||
|
selectedShapeId: string | null
|
||||||
|
showBoxes: boolean
|
||||||
|
draftBox: BoundingBox | null
|
||||||
|
activeClassColor: string
|
||||||
|
activeClassName: string
|
||||||
|
onSelectShape: (shapeId: string) => void
|
||||||
|
onStartMoveBox: (e: React.MouseEvent, shape: AnnotationShape) => void
|
||||||
|
onStartResizeBox: (
|
||||||
|
e: React.MouseEvent,
|
||||||
|
shape: AnnotationShape,
|
||||||
|
corner: ResizeHandleCorner
|
||||||
|
) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const BoundingBoxTool: React.FC<BoundingBoxToolProps> = ({
|
||||||
|
shapes,
|
||||||
|
selectedShapeId,
|
||||||
|
showBoxes,
|
||||||
|
draftBox,
|
||||||
|
activeClassColor,
|
||||||
|
activeClassName,
|
||||||
|
onSelectShape,
|
||||||
|
onStartMoveBox,
|
||||||
|
onStartResizeBox,
|
||||||
|
}) => {
|
||||||
|
if (!showBoxes) return null
|
||||||
|
|
||||||
|
const bboxShapes = shapes.filter((s) => s.type === 'bbox' && s.visible && s.bbox)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<g>
|
||||||
|
{bboxShapes.map((shape) => {
|
||||||
|
const box = shape.bbox!
|
||||||
|
const isSelected = shape.id === selectedShapeId
|
||||||
|
const pillWidth = Math.max(82, shape.labelName.length * 14 + 24)
|
||||||
|
const pillY = Math.max(4, box.y - 38)
|
||||||
|
const isLightTag = shape.color === '#F59E0B' || shape.color === '#06B6D4'
|
||||||
|
|
||||||
|
const corners: { id: ResizeHandleCorner; cx: number; cy: number; cursor: string }[] = [
|
||||||
|
{ id: 'nw', cx: box.x, cy: box.y, cursor: 'nwse-resize' },
|
||||||
|
{ id: 'ne', cx: box.x + box.width, cy: box.y, cursor: 'nesw-resize' },
|
||||||
|
{ id: 'sw', cx: box.x, cy: box.y + box.height, cursor: 'nesw-resize' },
|
||||||
|
{ id: 'se', cx: box.x + box.width, cy: box.y + box.height, cursor: 'nwse-resize' },
|
||||||
|
]
|
||||||
|
|
||||||
|
return (
|
||||||
|
<g key={shape.id}>
|
||||||
|
{/* Main Bounding Box Rectangle */}
|
||||||
|
<rect
|
||||||
|
x={box.x}
|
||||||
|
y={box.y}
|
||||||
|
width={box.width}
|
||||||
|
height={box.height}
|
||||||
|
fill={shape.color}
|
||||||
|
fillOpacity={isSelected ? 0.14 : 0.05}
|
||||||
|
stroke={shape.color}
|
||||||
|
strokeWidth={isSelected ? 4 : 3}
|
||||||
|
className="cursor-move"
|
||||||
|
onMouseDown={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
onSelectShape(shape.id)
|
||||||
|
onStartMoveBox(e, shape)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Top-left Label Pill matching wireframe */}
|
||||||
|
<g
|
||||||
|
className="cursor-pointer"
|
||||||
|
onMouseDown={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
onSelectShape(shape.id)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<rect
|
||||||
|
x={box.x}
|
||||||
|
y={pillY}
|
||||||
|
width={pillWidth}
|
||||||
|
height={32}
|
||||||
|
rx={8}
|
||||||
|
fill={shape.color}
|
||||||
|
fillOpacity={0.92}
|
||||||
|
/>
|
||||||
|
<text
|
||||||
|
x={box.x + pillWidth / 2}
|
||||||
|
y={pillY + 21}
|
||||||
|
fill={isLightTag ? '#0F172A' : '#FFFFFF'}
|
||||||
|
fontSize={17}
|
||||||
|
fontWeight="bold"
|
||||||
|
textAnchor="middle"
|
||||||
|
className="select-none"
|
||||||
|
>
|
||||||
|
{shape.labelName}
|
||||||
|
</text>
|
||||||
|
</g>
|
||||||
|
|
||||||
|
{/* Corner Resize Handles when Selected */}
|
||||||
|
{isSelected &&
|
||||||
|
corners.map((corner) => (
|
||||||
|
<rect
|
||||||
|
key={corner.id}
|
||||||
|
x={corner.cx - 8}
|
||||||
|
y={corner.cy - 8}
|
||||||
|
width={16}
|
||||||
|
height={16}
|
||||||
|
rx={3}
|
||||||
|
fill="#FFFFFF"
|
||||||
|
stroke={shape.color}
|
||||||
|
strokeWidth={3.5}
|
||||||
|
style={{ cursor: corner.cursor }}
|
||||||
|
onMouseDown={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
onStartResizeBox(e, shape, corner.id)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</g>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
|
||||||
|
{/* Active Draft Bounding Box while user drags */}
|
||||||
|
{draftBox && draftBox.width > 4 && draftBox.height > 4 && (
|
||||||
|
<g className="pointer-events-none">
|
||||||
|
<rect
|
||||||
|
x={draftBox.x}
|
||||||
|
y={draftBox.y}
|
||||||
|
width={draftBox.width}
|
||||||
|
height={draftBox.height}
|
||||||
|
fill={activeClassColor}
|
||||||
|
fillOpacity={0.18}
|
||||||
|
stroke={activeClassColor}
|
||||||
|
strokeWidth={3.5}
|
||||||
|
strokeDasharray="8 6"
|
||||||
|
/>
|
||||||
|
<rect
|
||||||
|
x={draftBox.x}
|
||||||
|
y={Math.max(4, draftBox.y - 34)}
|
||||||
|
width={110}
|
||||||
|
height={28}
|
||||||
|
rx={6}
|
||||||
|
fill={activeClassColor}
|
||||||
|
/>
|
||||||
|
<text
|
||||||
|
x={draftBox.x + 55}
|
||||||
|
y={Math.max(4, draftBox.y - 34) + 19}
|
||||||
|
fill="#FFFFFF"
|
||||||
|
fontSize={15}
|
||||||
|
fontWeight="bold"
|
||||||
|
textAnchor="middle"
|
||||||
|
>
|
||||||
|
{activeClassName}
|
||||||
|
</text>
|
||||||
|
</g>
|
||||||
|
)}
|
||||||
|
</g>
|
||||||
|
)
|
||||||
|
}
|
||||||
54
src/pages/annotator/components/canvas/CanvasOverlay.tsx
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const CanvasOverlay: React.FC = () => {
|
||||||
|
const project = useAnnotatorStore((s) => s.project)
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
const classificationByImageId = useAnnotatorStore((s) => s.classificationByImageId)
|
||||||
|
const shapesByImageId = useAnnotatorStore((s) => s.shapesByImageId)
|
||||||
|
const selectedShapeId = useAnnotatorStore((s) => s.selectedShapeId)
|
||||||
|
|
||||||
|
if (!currentImage) return null
|
||||||
|
|
||||||
|
const shapes = shapesByImageId[currentImage.id] ?? []
|
||||||
|
const selectedShape = shapes.find((s) => s.id === selectedShapeId) ?? shapes[1] ?? shapes[0]
|
||||||
|
|
||||||
|
if (project.taskType === 'classification') {
|
||||||
|
const classId = classificationByImageId[currentImage.id] ?? 'cls-car'
|
||||||
|
const cls = project.classes.find((c) => c.id === classId) ?? project.classes[0]
|
||||||
|
return (
|
||||||
|
<div className="pointer-events-none absolute bottom-3 left-3 z-10 inline-flex items-center gap-2 rounded-lg bg-slate-900/80 px-3 py-1.5 text-xs font-medium text-white backdrop-blur-xs">
|
||||||
|
<svg className="h-3.5 w-3.5 text-slate-300" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z" />
|
||||||
|
<line x1="7" y1="7" x2="7.01" y2="7" />
|
||||||
|
</svg>
|
||||||
|
<span>Nhãn ảnh: {cls?.name ?? 'Ô tô'}</span>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.taskType === 'object_detection') {
|
||||||
|
return (
|
||||||
|
<div className="pointer-events-none absolute bottom-3 left-3 z-10 inline-flex items-center gap-2 rounded-lg bg-slate-900/80 px-3 py-1.5 text-xs font-medium text-white backdrop-blur-xs">
|
||||||
|
<svg className="h-3.5 w-3.5 text-slate-300" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3" />
|
||||||
|
</svg>
|
||||||
|
<span>{shapes.length} hộp giới hạn · Gán nhãn thủ công</span>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const vertexCount = selectedShape?.points?.length ?? 13
|
||||||
|
const labelName = selectedShape?.labelName ?? 'Xe buýt'
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="pointer-events-none absolute bottom-3 left-3 z-10 inline-flex items-center gap-2 rounded-lg bg-slate-900/80 px-3 py-1.5 text-xs font-medium text-white backdrop-blur-xs">
|
||||||
|
<svg className="h-3.5 w-3.5 text-slate-300" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<polygon points="12 2 22 8.5 18 21 6 21 2 8.5 12 2" />
|
||||||
|
</svg>
|
||||||
|
<span>
|
||||||
|
Polygon · {labelName} · {vertexCount} đỉnh
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
21
src/pages/annotator/components/canvas/ImageViewer.tsx
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export interface ImageViewerProps {
|
||||||
|
url: string
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ImageViewer: React.FC<ImageViewerProps> = ({ url, width, height }) => {
|
||||||
|
return (
|
||||||
|
<image
|
||||||
|
href={url}
|
||||||
|
x={0}
|
||||||
|
y={0}
|
||||||
|
width={width}
|
||||||
|
height={height}
|
||||||
|
preserveAspectRatio="none"
|
||||||
|
className="select-none pointer-events-none"
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
64
src/pages/annotator/components/canvas/PointTool.tsx
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { AnnotationShape } from '@/types'
|
||||||
|
|
||||||
|
export interface PointToolProps {
|
||||||
|
shapes: AnnotationShape[]
|
||||||
|
selectedShapeId: string | null
|
||||||
|
onSelectShape: (shapeId: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const PointTool: React.FC<PointToolProps> = ({
|
||||||
|
shapes,
|
||||||
|
selectedShapeId,
|
||||||
|
onSelectShape,
|
||||||
|
}) => {
|
||||||
|
const pointShapes = shapes.filter((s) => s.type === 'point' && s.visible && s.point)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<g>
|
||||||
|
{pointShapes.map((shape) => {
|
||||||
|
const pt = shape.point!
|
||||||
|
const isSelected = shape.id === selectedShapeId
|
||||||
|
return (
|
||||||
|
<g
|
||||||
|
key={shape.id}
|
||||||
|
className="cursor-pointer"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
onSelectShape(shape.id)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<circle
|
||||||
|
cx={pt.x}
|
||||||
|
cy={pt.y}
|
||||||
|
r={isSelected ? 12 : 9}
|
||||||
|
fill={shape.color}
|
||||||
|
fillOpacity={0.35}
|
||||||
|
stroke="#FFFFFF"
|
||||||
|
strokeWidth={3}
|
||||||
|
/>
|
||||||
|
<circle cx={pt.x} cy={pt.y} r={5} fill={shape.color} />
|
||||||
|
<rect
|
||||||
|
x={pt.x - 36}
|
||||||
|
y={pt.y - 38}
|
||||||
|
width={72}
|
||||||
|
height={24}
|
||||||
|
rx={6}
|
||||||
|
fill={shape.color}
|
||||||
|
/>
|
||||||
|
<text
|
||||||
|
x={pt.x}
|
||||||
|
y={pt.y - 22}
|
||||||
|
fill="#FFFFFF"
|
||||||
|
fontSize={14}
|
||||||
|
fontWeight="bold"
|
||||||
|
textAnchor="middle"
|
||||||
|
>
|
||||||
|
{shape.labelName}
|
||||||
|
</text>
|
||||||
|
</g>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</g>
|
||||||
|
)
|
||||||
|
}
|
||||||
201
src/pages/annotator/components/canvas/PolygonTool.tsx
Normal file
|
|
@ -0,0 +1,201 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { AnnotationShape, Point2D } from '@/types'
|
||||||
|
|
||||||
|
export interface PolygonToolProps {
|
||||||
|
shapes: AnnotationShape[]
|
||||||
|
selectedShapeId: string | null
|
||||||
|
showMasks: boolean
|
||||||
|
maskOpacity: number
|
||||||
|
vertexEditEnabled: boolean
|
||||||
|
draftPolygonPoints: Point2D[]
|
||||||
|
cursorPoint: Point2D | null
|
||||||
|
activeClassColor: string
|
||||||
|
onSelectShape: (shapeId: string) => void
|
||||||
|
onStartDragVertex: (
|
||||||
|
e: React.MouseEvent,
|
||||||
|
shapeId: string,
|
||||||
|
vertexIndex: number
|
||||||
|
) => void
|
||||||
|
onInsertVertexOnEdge: (
|
||||||
|
shapeId: string,
|
||||||
|
afterIndex: number,
|
||||||
|
pt: Point2D
|
||||||
|
) => void
|
||||||
|
onCloseDraftPolygon: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const PolygonTool: React.FC<PolygonToolProps> = ({
|
||||||
|
shapes,
|
||||||
|
selectedShapeId,
|
||||||
|
showMasks,
|
||||||
|
maskOpacity,
|
||||||
|
vertexEditEnabled,
|
||||||
|
draftPolygonPoints,
|
||||||
|
cursorPoint,
|
||||||
|
activeClassColor,
|
||||||
|
onSelectShape,
|
||||||
|
onStartDragVertex,
|
||||||
|
onInsertVertexOnEdge,
|
||||||
|
onCloseDraftPolygon,
|
||||||
|
}) => {
|
||||||
|
if (!showMasks) return null
|
||||||
|
|
||||||
|
const polygonShapes = shapes.filter(
|
||||||
|
(s) => s.type === 'polygon' && s.visible && s.points && s.points.length >= 3
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<g>
|
||||||
|
{polygonShapes.map((shape) => {
|
||||||
|
const pts = shape.points!
|
||||||
|
const isSelected = shape.id === selectedShapeId
|
||||||
|
const pointsAttr = pts.map((p) => `${p.x},${p.y}`).join(' ')
|
||||||
|
|
||||||
|
// Find top-most vertex to place the label tag cleanly above the shape
|
||||||
|
const topPoint = pts.reduce(
|
||||||
|
(minPt, p) => (p.y < minPt.y ? p : minPt),
|
||||||
|
pts[0]!
|
||||||
|
)
|
||||||
|
const tagText =
|
||||||
|
shape.groupId !== undefined
|
||||||
|
? `${shape.labelName} · group ${shape.groupId}`
|
||||||
|
: shape.labelName
|
||||||
|
const tagWidth = Math.max(135, tagText.length * 11 + 26)
|
||||||
|
const tagX = topPoint.x - 20
|
||||||
|
const tagY = Math.max(8, topPoint.y - 46)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<g key={shape.id}>
|
||||||
|
{/* Polygon Filled Mask */}
|
||||||
|
<polygon
|
||||||
|
points={pointsAttr}
|
||||||
|
fill={shape.color}
|
||||||
|
fillOpacity={maskOpacity / 100}
|
||||||
|
stroke={shape.color}
|
||||||
|
strokeWidth={isSelected ? 4 : 3}
|
||||||
|
strokeLinejoin="round"
|
||||||
|
className="cursor-pointer transition-opacity"
|
||||||
|
onMouseDown={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
onSelectShape(shape.id)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Clickable/Double-clickable Edges for inserting a new vertex */}
|
||||||
|
{isSelected &&
|
||||||
|
vertexEditEnabled &&
|
||||||
|
pts.map((pt, idx) => {
|
||||||
|
const nextPt = pts[(idx + 1) % pts.length]!
|
||||||
|
return (
|
||||||
|
<line
|
||||||
|
key={`edge-${idx}`}
|
||||||
|
x1={pt.x}
|
||||||
|
y1={pt.y}
|
||||||
|
x2={nextPt.x}
|
||||||
|
y2={nextPt.y}
|
||||||
|
stroke="transparent"
|
||||||
|
strokeWidth={14}
|
||||||
|
className="cursor-copy"
|
||||||
|
onDoubleClick={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
const midX = (pt.x + nextPt.x) / 2
|
||||||
|
const midY = (pt.y + nextPt.y) / 2
|
||||||
|
onInsertVertexOnEdge(shape.id, idx, { x: midX, y: midY })
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
|
||||||
|
{/* Label Pill above Selected Polygon (e.g., "Xe buýt · group 2") */}
|
||||||
|
{isSelected && (
|
||||||
|
<g className="pointer-events-none">
|
||||||
|
<rect
|
||||||
|
x={tagX}
|
||||||
|
y={tagY}
|
||||||
|
width={tagWidth}
|
||||||
|
height={34}
|
||||||
|
rx={8}
|
||||||
|
fill={shape.color}
|
||||||
|
fillOpacity={0.88}
|
||||||
|
/>
|
||||||
|
<text
|
||||||
|
x={tagX + tagWidth / 2}
|
||||||
|
y={tagY + 22}
|
||||||
|
fill="#0F172A"
|
||||||
|
fontSize={16}
|
||||||
|
fontWeight="bold"
|
||||||
|
textAnchor="middle"
|
||||||
|
className="select-none"
|
||||||
|
>
|
||||||
|
{tagText}
|
||||||
|
</text>
|
||||||
|
</g>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Draggable Vertex Handles on Selected Polygon */}
|
||||||
|
{isSelected &&
|
||||||
|
pts.map((pt, idx) => (
|
||||||
|
<circle
|
||||||
|
key={`vertex-${idx}`}
|
||||||
|
cx={pt.x}
|
||||||
|
cy={pt.y}
|
||||||
|
r={vertexEditEnabled ? 7.5 : 5.5}
|
||||||
|
fill="#FFFFFF"
|
||||||
|
stroke={shape.color}
|
||||||
|
strokeWidth={3.5}
|
||||||
|
className={
|
||||||
|
vertexEditEnabled ? 'cursor-grab active:cursor-grabbing' : 'cursor-pointer'
|
||||||
|
}
|
||||||
|
onMouseDown={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
if (vertexEditEnabled) {
|
||||||
|
onStartDragVertex(e, shape.id, idx)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</g>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
|
||||||
|
{/* In-progress Draft Polygon while clicking points */}
|
||||||
|
{draftPolygonPoints.length > 0 && (
|
||||||
|
<g>
|
||||||
|
<polyline
|
||||||
|
points={[
|
||||||
|
...draftPolygonPoints.map((p) => `${p.x},${p.y}`),
|
||||||
|
...(cursorPoint ? [`${cursorPoint.x},${cursorPoint.y}`] : []),
|
||||||
|
].join(' ')}
|
||||||
|
fill={activeClassColor}
|
||||||
|
fillOpacity={0.18}
|
||||||
|
stroke={activeClassColor}
|
||||||
|
strokeWidth={3.5}
|
||||||
|
strokeDasharray="8 5"
|
||||||
|
className="pointer-events-none"
|
||||||
|
/>
|
||||||
|
{draftPolygonPoints.map((pt, idx) => {
|
||||||
|
const isFirst = idx === 0
|
||||||
|
return (
|
||||||
|
<circle
|
||||||
|
key={`draft-pt-${idx}`}
|
||||||
|
cx={pt.x}
|
||||||
|
cy={pt.y}
|
||||||
|
r={isFirst ? 10 : 7}
|
||||||
|
fill={isFirst ? activeClassColor : '#FFFFFF'}
|
||||||
|
stroke="#FFFFFF"
|
||||||
|
strokeWidth={3}
|
||||||
|
className="cursor-pointer"
|
||||||
|
onClick={(e) => {
|
||||||
|
if (isFirst && draftPolygonPoints.length >= 3) {
|
||||||
|
e.stopPropagation()
|
||||||
|
onCloseDraftPolygon()
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</g>
|
||||||
|
)}
|
||||||
|
</g>
|
||||||
|
)
|
||||||
|
}
|
||||||
11
src/pages/annotator/components/canvas/SelectTool.tsx
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { AnnotationShape } from '@/types'
|
||||||
|
|
||||||
|
export interface SelectToolProps {
|
||||||
|
selectedShape: AnnotationShape | undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
export const SelectTool: React.FC<SelectToolProps> = ({ selectedShape }) => {
|
||||||
|
if (!selectedShape || !selectedShape.visible) return null
|
||||||
|
return null
|
||||||
|
}
|
||||||
102
src/pages/annotator/components/canvas/ToolBar.tsx
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { AnnotationTool } from '@/types'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
import { UndoRedoButtons } from '../header/UndoRedoButtons'
|
||||||
|
import { ZoomControls } from './ZoomControls'
|
||||||
|
|
||||||
|
export const ToolBar: React.FC = () => {
|
||||||
|
const taskType = useAnnotatorStore((s) => s.project.taskType)
|
||||||
|
const activeTool = useAnnotatorStore((s) => s.activeTool)
|
||||||
|
const setActiveTool = useAnnotatorStore((s) => s.setActiveTool)
|
||||||
|
|
||||||
|
const tools: {
|
||||||
|
id: AnnotationTool
|
||||||
|
label: string
|
||||||
|
shortcut: string
|
||||||
|
enabled: boolean
|
||||||
|
icon: React.ReactNode
|
||||||
|
}[] = [
|
||||||
|
{
|
||||||
|
id: 'select',
|
||||||
|
label: 'Chọn',
|
||||||
|
shortcut: 'V',
|
||||||
|
enabled: true,
|
||||||
|
icon: (
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="m3 3 7.07 16.97 2.51-7.39 7.39-2.51L3 3z" />
|
||||||
|
<path d="m13 13 6 6" />
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'bbox',
|
||||||
|
label: 'Hộp',
|
||||||
|
shortcut: 'B',
|
||||||
|
enabled: taskType === 'object_detection',
|
||||||
|
icon: (
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeDasharray="3 2">
|
||||||
|
<rect x="3" y="3" width="18" height="18" rx="2" />
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'polygon',
|
||||||
|
label: 'Polygon',
|
||||||
|
shortcut: 'P',
|
||||||
|
enabled: taskType === 'segmentation',
|
||||||
|
icon: (
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<polygon points="12 2 22 8.5 18 21 6 21 2 8.5 12 2" />
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'point',
|
||||||
|
label: 'Điểm',
|
||||||
|
shortcut: 'K',
|
||||||
|
enabled: taskType !== 'classification',
|
||||||
|
icon: (
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" />
|
||||||
|
<circle cx="12" cy="10" r="3" />
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-between border-b border-slate-100 px-4 py-2">
|
||||||
|
<div className="flex items-center gap-1.5">
|
||||||
|
{tools.map((tool) => {
|
||||||
|
const isActive = activeTool === tool.id
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={tool.id}
|
||||||
|
type="button"
|
||||||
|
disabled={!tool.enabled}
|
||||||
|
onClick={() => tool.enabled && setActiveTool(tool.id)}
|
||||||
|
title={`${tool.label} (${tool.shortcut})`}
|
||||||
|
className={clsx(
|
||||||
|
'inline-flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-xs font-medium transition-colors',
|
||||||
|
isActive
|
||||||
|
? 'bg-[#FDECE7] text-[#4F46E5] font-semibold'
|
||||||
|
: tool.enabled
|
||||||
|
? 'text-slate-600 hover:bg-slate-100 hover:text-slate-900'
|
||||||
|
: 'cursor-not-allowed text-slate-300'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{tool.icon}
|
||||||
|
<span>{tool.label}</span>
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
|
||||||
|
<div className="mx-1.5 h-4 w-px bg-slate-200" />
|
||||||
|
<UndoRedoButtons />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ZoomControls />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
55
src/pages/annotator/components/canvas/ZoomControls.tsx
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const ZoomControls: React.FC = () => {
|
||||||
|
const zoom = useAnnotatorStore((s) => s.zoom)
|
||||||
|
const zoomIn = useAnnotatorStore((s) => s.zoomIn)
|
||||||
|
const zoomOut = useAnnotatorStore((s) => s.zoomOut)
|
||||||
|
const resetZoom = useAnnotatorStore((s) => s.resetZoom)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-1.5 text-slate-600">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={zoomOut}
|
||||||
|
title="Thu nhỏ"
|
||||||
|
className="inline-flex h-7 w-7 items-center justify-center rounded-lg transition-colors hover:bg-slate-100 hover:text-slate-900"
|
||||||
|
>
|
||||||
|
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<circle cx="11" cy="11" r="8" />
|
||||||
|
<line x1="21" y1="21" x2="16.65" y2="16.65" />
|
||||||
|
<line x1="8" y1="11" x2="14" y2="11" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<span className="min-w-[38px] text-center font-mono text-xs font-medium text-slate-600">
|
||||||
|
{zoom}%
|
||||||
|
</span>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={zoomIn}
|
||||||
|
title="Phóng to"
|
||||||
|
className="inline-flex h-7 w-7 items-center justify-center rounded-lg transition-colors hover:bg-slate-100 hover:text-slate-900"
|
||||||
|
>
|
||||||
|
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<circle cx="11" cy="11" r="8" />
|
||||||
|
<line x1="21" y1="21" x2="16.65" y2="16.65" />
|
||||||
|
<line x1="11" y1="8" x2="11" y2="14" />
|
||||||
|
<line x1="8" y1="11" x2="14" y2="11" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={resetZoom}
|
||||||
|
title="Khớp khung hình (75%)"
|
||||||
|
className="inline-flex h-7 w-7 items-center justify-center rounded-lg transition-colors hover:bg-slate-100 hover:text-slate-900"
|
||||||
|
>
|
||||||
|
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
71
src/pages/annotator/components/header/AnnotatorHeader.tsx
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useNavigate } from 'react-router-dom'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { TaskType } from '@/types'
|
||||||
|
import { TASK_TYPE_LABELS } from '@/app/constants'
|
||||||
|
import { formatNumber } from '@/utils/format'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
import { SaveButton } from './SaveButton'
|
||||||
|
import { SaveAndNextButton } from './SaveAndNextButton'
|
||||||
|
|
||||||
|
export const AnnotatorHeader: React.FC = () => {
|
||||||
|
const navigate = useNavigate()
|
||||||
|
const project = useAnnotatorStore((s) => s.project)
|
||||||
|
const switchTaskMode = useAnnotatorStore((s) => s.switchTaskMode)
|
||||||
|
|
||||||
|
const taskOptions: { type: TaskType; label: string }[] = [
|
||||||
|
{ type: 'classification', label: 'Classification' },
|
||||||
|
{ type: 'object_detection', label: 'Object Detection' },
|
||||||
|
{ type: 'segmentation', label: 'Segmentation' },
|
||||||
|
]
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-wrap items-center justify-between gap-4 pb-3">
|
||||||
|
<div>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<h1 className="text-2xl font-bold tracking-tight text-slate-900">
|
||||||
|
{project.name}
|
||||||
|
</h1>
|
||||||
|
{/* Quick Task Switcher Pill Bar so user can test all 3 Annotator modes effortlessly */}
|
||||||
|
<div className="hidden items-center gap-1 rounded-xl border border-slate-200 bg-white p-0.5 shadow-2xs sm:inline-flex">
|
||||||
|
{taskOptions.map((opt) => {
|
||||||
|
const active = project.taskType === opt.type
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={opt.type}
|
||||||
|
type="button"
|
||||||
|
onClick={() => switchTaskMode(opt.type)}
|
||||||
|
className={clsx(
|
||||||
|
'rounded-lg px-2.5 py-1 text-xs font-medium transition-colors',
|
||||||
|
active
|
||||||
|
? 'bg-[#FDF4F1] text-[#C95C3C] font-semibold'
|
||||||
|
: 'text-slate-500 hover:text-slate-800'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{opt.label}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p className="mt-0.5 text-xs text-slate-500">
|
||||||
|
{TASK_TYPE_LABELS[project.taskType]} · {project.slug} ·{' '}
|
||||||
|
{formatNumber(project.stats.totalImages)} ảnh
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2.5">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => navigate(`/projects/${project.id}`)}
|
||||||
|
className="inline-flex items-center gap-2 rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700 shadow-2xs transition-colors hover:bg-slate-50"
|
||||||
|
>
|
||||||
|
<span>←</span>
|
||||||
|
<span>Về tổng quan</span>
|
||||||
|
</button>
|
||||||
|
<SaveButton />
|
||||||
|
<SaveAndNextButton />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
19
src/pages/annotator/components/header/ImageInfo.tsx
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const ImageInfo: React.FC = () => {
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
|
||||||
|
if (!currentImage) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-2.5">
|
||||||
|
<span className="font-mono text-xs font-semibold text-slate-800">
|
||||||
|
{currentImage.fileName}
|
||||||
|
</span>
|
||||||
|
<span className="text-xs text-slate-400">
|
||||||
|
{currentImage.width} × {currentImage.height} px
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
25
src/pages/annotator/components/header/SaveAndNextButton.tsx
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import React from 'react'
|
||||||
|
import toast from 'react-hot-toast'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const SaveAndNextButton: React.FC = () => {
|
||||||
|
const saveAndNext = useAnnotatorStore((s) => s.saveAndNext)
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
|
||||||
|
const handleSaveAndNext = () => {
|
||||||
|
const fileName = currentImage?.fileName ?? 'ảnh'
|
||||||
|
saveAndNext()
|
||||||
|
toast.success(`Đã lưu ${fileName} và chuyển sang ảnh tiếp theo`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleSaveAndNext}
|
||||||
|
className="inline-flex items-center gap-2 rounded-xl bg-[#C95C3C] px-4 py-2 text-sm font-medium text-white shadow-sm transition-colors hover:bg-[#B5492B] focus:outline-none focus:ring-2 focus:ring-[#C95C3C]/30"
|
||||||
|
>
|
||||||
|
<span>→</span>
|
||||||
|
<span>Lưu & ảnh tiếp</span>
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
}
|
||||||
36
src/pages/annotator/components/header/SaveButton.tsx
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import React from 'react'
|
||||||
|
import toast from 'react-hot-toast'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const SaveButton: React.FC = () => {
|
||||||
|
const saveCurrentAnnotation = useAnnotatorStore((s) => s.saveCurrentAnnotation)
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
|
||||||
|
const handleSave = () => {
|
||||||
|
saveCurrentAnnotation()
|
||||||
|
toast.success(`Đã lưu nhãn cho ${currentImage?.fileName ?? 'ảnh hiện tại'}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleSave}
|
||||||
|
className="inline-flex items-center gap-2 rounded-xl bg-[#C95C3C] px-4 py-2 text-sm font-medium text-white shadow-sm transition-colors hover:bg-[#B5492B] focus:outline-none focus:ring-2 focus:ring-[#C95C3C]/30"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4 text-white/90"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
>
|
||||||
|
<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z" />
|
||||||
|
<polyline points="17 21 17 13 7 13 7 21" />
|
||||||
|
<polyline points="7 3 7 8 15 8" />
|
||||||
|
</svg>
|
||||||
|
<span>Lưu</span>
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
}
|
||||||
54
src/pages/annotator/components/header/UndoRedoButtons.tsx
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const UndoRedoButtons: React.FC = () => {
|
||||||
|
const undo = useAnnotatorStore((s) => s.undo)
|
||||||
|
const redo = useAnnotatorStore((s) => s.redo)
|
||||||
|
const canUndo = useAnnotatorStore((s) => s.historyPast.length > 0)
|
||||||
|
const canRedo = useAnnotatorStore((s) => s.historyFuture.length > 0)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="inline-flex items-center gap-1">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={undo}
|
||||||
|
disabled={!canUndo}
|
||||||
|
title="Hoàn tác (Ctrl+Z)"
|
||||||
|
className="inline-flex h-8 w-8 items-center justify-center rounded-lg text-slate-600 transition-colors hover:bg-slate-100 hover:text-slate-900 disabled:cursor-not-allowed disabled:opacity-40"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
>
|
||||||
|
<path d="M3 7v6h6" />
|
||||||
|
<path d="M21 17a9 9 0 0 0-9-9 9 9 0 0 0-6 2.3L3 13" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={redo}
|
||||||
|
disabled={!canRedo}
|
||||||
|
title="Làm lại (Ctrl+Y)"
|
||||||
|
className="inline-flex h-8 w-8 items-center justify-center rounded-lg text-slate-600 transition-colors hover:bg-slate-100 hover:text-slate-900 disabled:cursor-not-allowed disabled:opacity-40"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
>
|
||||||
|
<path d="M21 7v6h-6" />
|
||||||
|
<path d="M3 17a9 9 0 0 1 9-9 9 9 0 0 1 6 2.3l3 2.7" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
40
src/pages/annotator/components/navigator/ImageCounter.tsx
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { formatNumber } from '@/utils/format'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const ImageCounter: React.FC = () => {
|
||||||
|
const displayImageCounter = useAnnotatorStore((s) => s.displayImageCounter)
|
||||||
|
const totalImages = useAnnotatorStore((s) => s.project.stats.totalImages)
|
||||||
|
const prevImage = useAnnotatorStore((s) => s.prevImage)
|
||||||
|
const nextImage = useAnnotatorStore((s) => s.nextImage)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="inline-flex items-center gap-3 text-xs font-medium text-slate-600">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={prevImage}
|
||||||
|
title="Ảnh trước"
|
||||||
|
className="inline-flex h-6 w-6 items-center justify-center rounded-md text-slate-500 transition-colors hover:bg-slate-100 hover:text-slate-900"
|
||||||
|
>
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<polyline points="15 18 9 12 15 6" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<span className="font-mono text-xs text-slate-600">
|
||||||
|
{displayImageCounter} / {formatNumber(totalImages)}
|
||||||
|
</span>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={nextImage}
|
||||||
|
title="Ảnh tiếp theo"
|
||||||
|
className="inline-flex h-6 w-6 items-center justify-center rounded-md text-slate-500 transition-colors hover:bg-slate-100 hover:text-slate-900"
|
||||||
|
>
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<polyline points="9 18 15 12 9 6" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
38
src/pages/annotator/components/navigator/ImageNavigator.tsx
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
import { ThumbnailStrip } from './ThumbnailStrip'
|
||||||
|
import { SaveStatus } from './SaveStatus'
|
||||||
|
|
||||||
|
export const ImageNavigator: React.FC = () => {
|
||||||
|
const taskType = useAnnotatorStore((s) => s.project.taskType)
|
||||||
|
|
||||||
|
const leftHint =
|
||||||
|
taskType === 'classification'
|
||||||
|
? 'Chọn lớp ở bảng bên phải · Không tạo shape'
|
||||||
|
: taskType === 'object_detection'
|
||||||
|
? 'Kéo để tạo box · Kéo góc để chỉnh kích thước'
|
||||||
|
: 'Click để thêm đỉnh · Enter để đóng polygon'
|
||||||
|
|
||||||
|
const rightShortcut =
|
||||||
|
taskType === 'classification'
|
||||||
|
? 'Chọn [V]'
|
||||||
|
: taskType === 'object_detection'
|
||||||
|
? 'Hộp [B]'
|
||||||
|
: 'Polygon [P]'
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{/* Hint Bar above Thumbnail Strip */}
|
||||||
|
<div className="flex items-center justify-between border-t border-slate-100 px-4 py-2 text-[11px] text-slate-500">
|
||||||
|
<span>{leftHint}</span>
|
||||||
|
<span className="font-mono text-slate-400">{rightShortcut}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 6 Image Thumbnails */}
|
||||||
|
<ThumbnailStrip />
|
||||||
|
|
||||||
|
{/* Bottom Save & Export Status Bar */}
|
||||||
|
<SaveStatus />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
63
src/pages/annotator/components/navigator/SaveStatus.tsx
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
import React from 'react'
|
||||||
|
import toast from 'react-hot-toast'
|
||||||
|
import { exportToLabelMeJSON } from '@/utils/labelme'
|
||||||
|
import { exportSegmentationMaskPNG } from '@/utils/mask'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const SaveStatus: React.FC = () => {
|
||||||
|
const project = useAnnotatorStore((s) => s.project)
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
const shapesByImageId = useAnnotatorStore((s) => s.shapesByImageId)
|
||||||
|
const lastSavedAt = useAnnotatorStore((s) => s.lastSavedAt)
|
||||||
|
|
||||||
|
const shapes = currentImage ? shapesByImageId[currentImage.id] ?? [] : []
|
||||||
|
|
||||||
|
const handleExportLabelMe = () => {
|
||||||
|
if (!currentImage) return
|
||||||
|
exportToLabelMeJSON(currentImage, shapes, true)
|
||||||
|
toast.success(`Đã xuất ${currentImage.fileName.replace(/\.[^/.]+$/, '')}_labelme.json`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleExportMask = () => {
|
||||||
|
if (!currentImage) return
|
||||||
|
exportSegmentationMaskPNG(currentImage, shapes, true)
|
||||||
|
toast.success(`Đã xuất ${currentImage.fileName.replace(/\.[^/.]+$/, '')}_mask.png`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const rightStatusSummary =
|
||||||
|
project.taskType === 'classification'
|
||||||
|
? `Chờ duyệt · ${project.stats.pendingReview} ảnh`
|
||||||
|
: project.taskType === 'object_detection'
|
||||||
|
? `Chờ duyệt · ${project.stats.pendingReview} ảnh`
|
||||||
|
: `Đang làm · ${project.stats.inProgress} ảnh`
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-wrap items-center justify-between border-t border-slate-100 px-4 py-2.5 text-xs">
|
||||||
|
<div className="flex items-center gap-1.5 font-medium text-[#15803D]">
|
||||||
|
<span>✓</span>
|
||||||
|
<span>Đã lưu lúc {lastSavedAt}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{project.taskType === 'segmentation' && (
|
||||||
|
<div className="flex items-center gap-5">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleExportLabelMe}
|
||||||
|
className="font-semibold text-[#4F46E5] transition-colors hover:text-[#3730A3] hover:underline"
|
||||||
|
>
|
||||||
|
Xuất LabelMe JSON ↓
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleExportMask}
|
||||||
|
className="font-semibold text-[#4F46E5] transition-colors hover:text-[#3730A3] hover:underline"
|
||||||
|
>
|
||||||
|
Xuất mask PNG ↓
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="text-slate-500">{rightStatusSummary}</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
54
src/pages/annotator/components/navigator/ThumbnailStrip.tsx
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const ThumbnailStrip: React.FC = () => {
|
||||||
|
const images = useAnnotatorStore((s) => s.images)
|
||||||
|
const currentImageIndex = useAnnotatorStore((s) => s.currentImageIndex)
|
||||||
|
const selectImageByIndex = useAnnotatorStore((s) => s.selectImageByIndex)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="grid grid-cols-6 gap-2.5 px-4 py-2.5">
|
||||||
|
{images.map((img, index) => {
|
||||||
|
const isActive = index === currentImageIndex
|
||||||
|
// Shorten "urban_00124.jpg" -> "..00124.jpg" matching wireframe
|
||||||
|
const shortName = img.fileName.replace(/^[a-z]+_/, '..')
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={img.id}
|
||||||
|
type="button"
|
||||||
|
onClick={() => selectImageByIndex(index)}
|
||||||
|
className="group flex flex-col items-center gap-1 focus:outline-none"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'relative aspect-video w-full overflow-hidden rounded-lg border-2 transition-all',
|
||||||
|
isActive
|
||||||
|
? 'border-[#4F46E5] shadow-xs'
|
||||||
|
: 'border-transparent opacity-85 hover:opacity-100'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={img.thumbnailUrl}
|
||||||
|
alt={img.fileName}
|
||||||
|
className="h-full w-full object-cover"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<span
|
||||||
|
className={clsx(
|
||||||
|
'font-mono text-[10px] transition-colors',
|
||||||
|
isActive
|
||||||
|
? 'font-semibold text-[#4F46E5]'
|
||||||
|
: 'text-slate-400 group-hover:text-slate-600'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{shortName}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,100 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
import { StatusBadge } from './StatusBadge'
|
||||||
|
import { ImageFlagCheckboxes } from './ImageFlagCheckboxes'
|
||||||
|
import { ReviewActions } from './ReviewActions'
|
||||||
|
|
||||||
|
export const ClassificationPanel: React.FC = () => {
|
||||||
|
const classes = useAnnotatorStore((s) => s.project.classes)
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
const classificationByImageId = useAnnotatorStore((s) => s.classificationByImageId)
|
||||||
|
const statusByImageId = useAnnotatorStore((s) => s.statusByImageId)
|
||||||
|
const setClassificationLabel = useAnnotatorStore((s) => s.setClassificationLabel)
|
||||||
|
|
||||||
|
if (!currentImage) return null
|
||||||
|
|
||||||
|
const activeClassId = classificationByImageId[currentImage.id] ?? 'cls-car'
|
||||||
|
const activeClass = classes.find((c) => c.id === activeClassId) ?? classes[0]
|
||||||
|
const currentStatus = statusByImageId[currentImage.id] ?? 'pending_review'
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col space-y-3.5 rounded-2xl border border-slate-200/80 bg-white p-5 shadow-2xs">
|
||||||
|
{/* Header */}
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h3 className="text-sm font-bold text-slate-900">Nhãn cấp ảnh</h3>
|
||||||
|
<span className="text-xs font-medium text-[#4F46E5]">1 nhãn / ảnh</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Status Badge */}
|
||||||
|
<div>
|
||||||
|
<StatusBadge status={currentStatus} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p className="text-xs text-slate-500">Chọn một lớp cho toàn bộ ảnh.</p>
|
||||||
|
|
||||||
|
{/* Class Radio Cards */}
|
||||||
|
<div className="space-y-2.5">
|
||||||
|
{classes.map((cls, index) => {
|
||||||
|
const isSelected = cls.id === activeClassId
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={cls.id}
|
||||||
|
type="button"
|
||||||
|
onClick={() => setClassificationLabel(cls.id)}
|
||||||
|
className={clsx(
|
||||||
|
'flex w-full items-center justify-between rounded-xl border px-3.5 py-3 text-left transition-all',
|
||||||
|
isSelected
|
||||||
|
? 'border-[#4F46E5] bg-[#FDECE7] shadow-2xs'
|
||||||
|
: 'border-slate-200 bg-white hover:border-slate-300'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-2.5">
|
||||||
|
<span
|
||||||
|
className={clsx(
|
||||||
|
'flex h-4 w-4 items-center justify-center rounded-full border transition-colors',
|
||||||
|
isSelected
|
||||||
|
? 'border-[#4F46E5] bg-[#4F46E5]'
|
||||||
|
: 'border-slate-300 bg-white'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{isSelected && (
|
||||||
|
<span className="h-1.5 w-1.5 rounded-full bg-white" />
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
|
<span className="text-xs font-semibold text-slate-900">
|
||||||
|
{cls.name}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<span className="font-mono text-xs text-slate-400">
|
||||||
|
{cls.shortcutKey ?? index + 1}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Image Flags */}
|
||||||
|
<ImageFlagCheckboxes />
|
||||||
|
|
||||||
|
{/* Reviewer Info Box */}
|
||||||
|
<div className="rounded-xl bg-slate-50 p-3.5">
|
||||||
|
<p className="text-xs font-semibold text-slate-800">
|
||||||
|
{currentImage.submittedBy ?? 'Hà Linh'} đã gửi nhãn {activeClass?.name ?? 'Ô tô'}
|
||||||
|
</p>
|
||||||
|
<p className="mt-1 text-[11px] text-slate-500">
|
||||||
|
{currentImage.submittedAt ?? 'Hôm nay, 09:42'} · Người duyệt:{' '}
|
||||||
|
{currentImage.reviewerName ?? 'Thu Hà'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Review Actions */}
|
||||||
|
<ReviewActions approveLabel="Duyệt nhãn" targetStatusOnApprove="completed" />
|
||||||
|
|
||||||
|
<p className="text-[11px] text-slate-400">
|
||||||
|
Enter để duyệt · 1–4 chọn lớp
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const ImageFlagCheckboxes: React.FC = () => {
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
const flagsByImageId = useAnnotatorStore((s) => s.flagsByImageId)
|
||||||
|
const toggleFlag = useAnnotatorStore((s) => s.toggleFlag)
|
||||||
|
|
||||||
|
if (!currentImage) return null
|
||||||
|
|
||||||
|
const flags = flagsByImageId[currentImage.id] ?? {
|
||||||
|
blurryOrUncertain: false,
|
||||||
|
needsRecheck: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-2 pt-1">
|
||||||
|
<h4 className="text-xs font-bold text-slate-800">Cờ cấp ảnh</h4>
|
||||||
|
<label className="flex cursor-pointer items-center gap-2 text-xs text-slate-600 hover:text-slate-900">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={flags.blurryOrUncertain}
|
||||||
|
onChange={() => toggleFlag('blurryOrUncertain')}
|
||||||
|
className="h-3.5 w-3.5 rounded-xs border-slate-300 text-[#C95C3C] focus:ring-[#C95C3C]"
|
||||||
|
/>
|
||||||
|
<span>Ảnh mờ / khó xác định</span>
|
||||||
|
</label>
|
||||||
|
<label className="flex cursor-pointer items-center gap-2 text-xs text-slate-600 hover:text-slate-900">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={flags.needsRecheck}
|
||||||
|
onChange={() => toggleFlag('needsRecheck')}
|
||||||
|
className="h-3.5 w-3.5 rounded-xs border-slate-300 text-[#C95C3C] focus:ring-[#C95C3C]"
|
||||||
|
/>
|
||||||
|
<span>Cần kiểm tra lại</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export const LabelSelector: React.FC = () => {
|
||||||
|
const classes = useAnnotatorStore((s) => s.project.classes)
|
||||||
|
const selectedClassId = useAnnotatorStore((s) => s.selectedClassId)
|
||||||
|
const activeGroupId = useAnnotatorStore((s) => s.activeGroupId)
|
||||||
|
const updateSelectedShapeClass = useAnnotatorStore((s) => s.updateSelectedShapeClass)
|
||||||
|
const setActiveGroupId = useAnnotatorStore((s) => s.setActiveGroupId)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="grid grid-cols-3 gap-2.5 pt-1">
|
||||||
|
<div className="col-span-2">
|
||||||
|
<label className="mb-1 block text-[11px] font-medium text-slate-500">
|
||||||
|
Nhãn
|
||||||
|
</label>
|
||||||
|
<div className="relative">
|
||||||
|
<select
|
||||||
|
value={selectedClassId}
|
||||||
|
onChange={(e) => updateSelectedShapeClass(e.target.value)}
|
||||||
|
className="w-full appearance-none rounded-xl border border-slate-200 bg-white px-3 py-2 pr-8 text-xs font-semibold text-slate-800 focus:border-[#4F46E5] focus:outline-none"
|
||||||
|
>
|
||||||
|
{classes.map((cls) => (
|
||||||
|
<option key={cls.id} value={cls.id}>
|
||||||
|
{cls.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<span className="pointer-events-none absolute inset-y-0 right-2.5 flex items-center text-slate-400">
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<polyline points="6 9 12 15 18 9" />
|
||||||
|
</svg>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="mb-1 block text-[11px] font-medium text-slate-500">
|
||||||
|
Mã nhóm
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min={1}
|
||||||
|
max={99}
|
||||||
|
value={activeGroupId}
|
||||||
|
onChange={(e) => setActiveGroupId(Math.max(1, Number(e.target.value) || 1))}
|
||||||
|
className="w-full rounded-xl border border-slate-200 bg-white px-3 py-2 text-xs font-semibold text-slate-800 focus:border-[#4F46E5] focus:outline-none"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
100
src/pages/annotator/components/sidebar-right/ObjectItem.tsx
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { AnnotationShape, TaskType } from '@/types'
|
||||||
|
|
||||||
|
export interface ObjectItemProps {
|
||||||
|
shape: AnnotationShape
|
||||||
|
isSelected: boolean
|
||||||
|
taskType: TaskType
|
||||||
|
onSelect: () => void
|
||||||
|
onToggleVisible: () => void
|
||||||
|
onDelete: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ObjectItem: React.FC<ObjectItemProps> = ({
|
||||||
|
shape,
|
||||||
|
isSelected,
|
||||||
|
taskType,
|
||||||
|
onSelect,
|
||||||
|
onToggleVisible,
|
||||||
|
onDelete,
|
||||||
|
}) => {
|
||||||
|
const indexFormatted = String(shape.index).padStart(2, '0')
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
onClick={onSelect}
|
||||||
|
className={clsx(
|
||||||
|
'group cursor-pointer rounded-xl border px-3.5 py-2.5 transition-all',
|
||||||
|
isSelected
|
||||||
|
? 'border-[#C95C3C]/30 bg-[#FDECE7]/85'
|
||||||
|
: 'border-slate-200/90 bg-white hover:border-slate-300'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex items-center justify-between gap-2">
|
||||||
|
<div className="flex items-center gap-2.5">
|
||||||
|
<span
|
||||||
|
className="h-2.5 w-2.5 shrink-0 rounded-xs"
|
||||||
|
style={{ backgroundColor: shape.color }}
|
||||||
|
/>
|
||||||
|
<span className="text-xs font-semibold text-slate-800">
|
||||||
|
{shape.labelName}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span className="font-mono text-[11px] text-slate-400">
|
||||||
|
{taskType === 'segmentation' ? `#${indexFormatted}` : indexFormatted}
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
onToggleVisible()
|
||||||
|
}}
|
||||||
|
title={shape.visible ? 'Ẩn đối tượng' : 'Hiện đối tượng'}
|
||||||
|
className={clsx(
|
||||||
|
'rounded-md p-0.5 transition-colors',
|
||||||
|
shape.visible
|
||||||
|
? 'text-slate-500 hover:text-slate-800'
|
||||||
|
: 'text-slate-300 hover:text-slate-500'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
|
||||||
|
<circle cx="12" cy="12" r="3" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
onDelete()
|
||||||
|
}}
|
||||||
|
title="Xóa đối tượng (Delete)"
|
||||||
|
className="hidden rounded-md p-0.5 text-slate-400 transition-colors hover:text-red-600 group-hover:inline-flex"
|
||||||
|
>
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<polyline points="3 6 5 6 21 6" />
|
||||||
|
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Selected subtitle: BBox coordinates or Polygon vertex/group metadata */}
|
||||||
|
{isSelected && taskType === 'object_detection' && shape.bbox && (
|
||||||
|
<div className="mt-1.5 font-mono text-[11px] text-slate-500">
|
||||||
|
x: {shape.bbox.x} y: {shape.bbox.y} w: {shape.bbox.width} h:{' '}
|
||||||
|
{shape.bbox.height}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{isSelected && taskType === 'segmentation' && shape.points && (
|
||||||
|
<div className="mt-1 font-mono text-[11px] text-slate-500">
|
||||||
|
polygon · {shape.points.length} đỉnh · group {shape.groupId ?? 1}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
141
src/pages/annotator/components/sidebar-right/ObjectListPanel.tsx
Normal file
|
|
@ -0,0 +1,141 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
import { ObjectItem } from './ObjectItem'
|
||||||
|
import { StatusBadge } from './StatusBadge'
|
||||||
|
import { ReviewActions } from './ReviewActions'
|
||||||
|
|
||||||
|
export const ObjectListPanel: React.FC = () => {
|
||||||
|
const classes = useAnnotatorStore((s) => s.project.classes)
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
const shapesByImageId = useAnnotatorStore((s) => s.shapesByImageId)
|
||||||
|
const statusByImageId = useAnnotatorStore((s) => s.statusByImageId)
|
||||||
|
const selectedShapeId = useAnnotatorStore((s) => s.selectedShapeId)
|
||||||
|
const selectedClassId = useAnnotatorStore((s) => s.selectedClassId)
|
||||||
|
const showBoxes = useAnnotatorStore((s) => s.showBoxes)
|
||||||
|
|
||||||
|
const setShowBoxes = useAnnotatorStore((s) => s.setShowBoxes)
|
||||||
|
const setSelectedShapeId = useAnnotatorStore((s) => s.setSelectedShapeId)
|
||||||
|
const updateSelectedShapeClass = useAnnotatorStore((s) => s.updateSelectedShapeClass)
|
||||||
|
const toggleShapeVisibility = useAnnotatorStore((s) => s.toggleShapeVisibility)
|
||||||
|
const toggleAllShapesVisibility = useAnnotatorStore((s) => s.toggleAllShapesVisibility)
|
||||||
|
const deleteShape = useAnnotatorStore((s) => s.deleteShape)
|
||||||
|
|
||||||
|
if (!currentImage) return null
|
||||||
|
|
||||||
|
const shapes = shapesByImageId[currentImage.id] ?? []
|
||||||
|
const currentStatus = statusByImageId[currentImage.id] ?? 'pending_review'
|
||||||
|
|
||||||
|
const getCountByClass = (classId: string) =>
|
||||||
|
shapes.filter((s) => s.labelId === classId).length
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col space-y-3.5 rounded-2xl border border-slate-200/80 bg-white p-5 shadow-2xs">
|
||||||
|
{/* Header */}
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h3 className="text-sm font-bold text-slate-900">Danh sách đối tượng</h3>
|
||||||
|
<span className="text-xs font-medium text-[#4F46E5]">{shapes.length} hình</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Show Bounding Boxes Toggle Row */}
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<label className="flex cursor-pointer items-center gap-2 text-xs text-slate-600">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={showBoxes}
|
||||||
|
onChange={(e) => setShowBoxes(e.target.checked)}
|
||||||
|
className="h-3.5 w-3.5 rounded-xs border-slate-300 text-slate-700 focus:ring-[#C95C3C]"
|
||||||
|
/>
|
||||||
|
<span>Hiện hộp giới hạn</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={toggleAllShapesVisibility}
|
||||||
|
title="Ẩn/Hiện tất cả"
|
||||||
|
className="text-slate-500 hover:text-slate-800"
|
||||||
|
>
|
||||||
|
<svg className="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
|
||||||
|
<circle cx="12" cy="12" r="3" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Object Cards List */}
|
||||||
|
<div className="max-h-[260px] space-y-2 overflow-y-auto pr-0.5">
|
||||||
|
{shapes.map((shape) => (
|
||||||
|
<ObjectItem
|
||||||
|
key={shape.id}
|
||||||
|
shape={shape}
|
||||||
|
isSelected={shape.id === selectedShapeId}
|
||||||
|
taskType="object_detection"
|
||||||
|
onSelect={() => setSelectedShapeId(shape.id)}
|
||||||
|
onToggleVisible={() => toggleShapeVisibility(shape.id)}
|
||||||
|
onDelete={() => deleteShape(shape.id)}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Project Classes Pills */}
|
||||||
|
<div className="space-y-2 pt-1">
|
||||||
|
<h4 className="text-xs font-bold text-slate-800">Lớp trong dự án</h4>
|
||||||
|
<div className="flex flex-wrap gap-1.5">
|
||||||
|
{classes.map((cls) => {
|
||||||
|
const count = getCountByClass(cls.id)
|
||||||
|
const isActive = cls.id === selectedClassId
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={cls.id}
|
||||||
|
type="button"
|
||||||
|
onClick={() => updateSelectedShapeClass(cls.id)}
|
||||||
|
className={clsx(
|
||||||
|
'inline-flex items-center gap-1.5 rounded-lg border px-2.5 py-1 text-xs font-medium transition-colors',
|
||||||
|
isActive
|
||||||
|
? 'border-[#C95C3C]/40 bg-[#FDF4F1] text-slate-900'
|
||||||
|
: 'border-slate-200 bg-slate-50 text-slate-700 hover:bg-slate-100'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className="h-1.5 w-1.5 rounded-full"
|
||||||
|
style={{ backgroundColor: cls.color }}
|
||||||
|
/>
|
||||||
|
<span>
|
||||||
|
{cls.name} · {count}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Active Class Dropdown */}
|
||||||
|
<div className="flex items-center gap-1.5 pt-1 text-xs text-slate-500">
|
||||||
|
<span>Lớp đang chọn:</span>
|
||||||
|
<select
|
||||||
|
value={selectedClassId}
|
||||||
|
onChange={(e) => updateSelectedShapeClass(e.target.value)}
|
||||||
|
className="cursor-pointer bg-transparent font-medium text-slate-700 focus:outline-none"
|
||||||
|
>
|
||||||
|
{classes.map((cls) => (
|
||||||
|
<option key={cls.id} value={cls.id}>
|
||||||
|
{cls.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Image Status Row */}
|
||||||
|
<div className="flex items-center justify-between pt-1">
|
||||||
|
<span className="text-xs text-slate-500">Trạng thái ảnh</span>
|
||||||
|
<StatusBadge status={currentStatus} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Review Actions */}
|
||||||
|
<ReviewActions
|
||||||
|
approveLabel={`Duyệt ${shapes.length} nhãn`}
|
||||||
|
targetStatusOnApprove="completed"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
import React from 'react'
|
||||||
|
import toast from 'react-hot-toast'
|
||||||
|
import { useAnnotatorStore } from '../../annotatorStore'
|
||||||
|
|
||||||
|
export interface ReviewActionsProps {
|
||||||
|
approveLabel: string
|
||||||
|
targetStatusOnApprove?: 'completed' | 'pending_review'
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ReviewActions: React.FC<ReviewActionsProps> = ({
|
||||||
|
approveLabel,
|
||||||
|
targetStatusOnApprove = 'completed',
|
||||||
|
}) => {
|
||||||
|
const updateImageStatus = useAnnotatorStore((s) => s.updateImageStatus)
|
||||||
|
const currentImage = useAnnotatorStore((s) => s.images[s.currentImageIndex])
|
||||||
|
|
||||||
|
const handleReject = () => {
|
||||||
|
updateImageStatus('in_progress')
|
||||||
|
toast(`Đã trả lại ảnh ${currentImage?.fileName ?? ''} để chỉnh sửa`, {
|
||||||
|
icon: '↩️',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleApprove = () => {
|
||||||
|
updateImageStatus(targetStatusOnApprove)
|
||||||
|
toast.success(
|
||||||
|
targetStatusOnApprove === 'pending_review'
|
||||||
|
? `Đã gửi duyệt ${currentImage?.fileName ?? ''}`
|
||||||
|
: `Đã duyệt nhãn cho ${currentImage?.fileName ?? ''}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-2.5 pt-1">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleReject}
|
||||||
|
className="inline-flex items-center justify-center rounded-xl border border-slate-200 bg-white px-4 py-2 text-xs font-semibold text-slate-800 shadow-2xs transition-colors hover:bg-slate-50"
|
||||||
|
>
|
||||||
|
Trả lại
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleApprove}
|
||||||
|
className="inline-flex flex-1 items-center justify-center gap-1.5 rounded-xl bg-[#C95C3C] px-4 py-2 text-xs font-semibold text-white shadow-sm transition-colors hover:bg-[#B5492B]"
|
||||||
|
>
|
||||||
|
<span>✓</span>
|
||||||
|
<span>{approveLabel}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||