feat: scaffold complete danable frontend architecture with all pages, layouts, stores, and 3 annotator modes

This commit is contained in:
quanganh2k4 2026-10-30 00:33:07 +07:00
commit 78848dd7d1
173 changed files with 18649 additions and 0 deletions

View file

@ -0,0 +1,226 @@
import { create } from 'zustand'
import { AnnotationShape, ImageAnnotationData } from '@/types'
export interface AnnotationSnapshot {
shapes: AnnotationShape[]
classificationLabelId?: string
flags: {
blurryOrUncertain: boolean
needsRecheck: boolean
}
}
export interface AnnotationStoreState {
currentImageId: string | null
shapes: AnnotationShape[]
selectedShapeId: string | null
classificationLabelId?: string
flags: {
blurryOrUncertain: boolean
needsRecheck: boolean
}
activeLabelId: string
activeGroupId: number
history: {
past: AnnotationSnapshot[]
future: AnnotationSnapshot[]
}
isDirty: boolean
lastSavedAt: string
setAnnotationData: (data: ImageAnnotationData) => void
addShape: (shape: Omit<AnnotationShape, 'index'> & { index?: number }) => void
updateShape: (shapeId: string, updates: Partial<AnnotationShape>) => void
deleteShape: (shapeId: string) => void
toggleShapeVisibility: (shapeId: string) => void
selectShape: (shapeId: string | null) => void
setClassificationLabel: (labelId: string) => void
toggleFlag: (flagKey: 'blurryOrUncertain' | 'needsRecheck') => void
setActiveLabelId: (labelId: string) => void
setActiveGroupId: (groupId: number) => void
undo: () => void
redo: () => void
markSaved: (savedAtText?: string) => void
}
function createSnapshot(state: {
shapes: AnnotationShape[]
classificationLabelId?: string
flags: { blurryOrUncertain: boolean; needsRecheck: boolean }
}): AnnotationSnapshot {
return {
shapes: state.shapes.map((s) => ({
...s,
bbox: s.bbox ? { ...s.bbox } : undefined,
points: s.points ? s.points.map((pt) => ({ ...pt })) : undefined,
point: s.point ? { ...s.point } : undefined,
})),
classificationLabelId: state.classificationLabelId,
flags: { ...state.flags },
}
}
export const useAnnotationStore = create<AnnotationStoreState>((set) => ({
currentImageId: null,
shapes: [],
selectedShapeId: null,
classificationLabelId: 'cls-car',
flags: {
blurryOrUncertain: false,
needsRecheck: false,
},
activeLabelId: 'cls-car',
activeGroupId: 1,
history: {
past: [],
future: [],
},
isDirty: false,
lastSavedAt: 'Vừa lưu 2 giây trước',
setAnnotationData: (data) =>
set({
currentImageId: data.imageId,
shapes: data.shapes.map((s, idx) => ({ ...s, index: idx + 1 })),
selectedShapeId: data.shapes[0]?.id ?? null,
classificationLabelId: data.classificationLabelId ?? 'cls-car',
flags: {
blurryOrUncertain: Boolean(data.flags?.blurryOrUncertain),
needsRecheck: Boolean(data.flags?.needsRecheck),
},
history: { past: [], future: [] },
isDirty: false,
lastSavedAt: data.updatedAt || 'Vừa lưu 2 giây trước',
}),
addShape: (newShape) =>
set((state) => {
const snapshot = createSnapshot(state)
const nextIndex = state.shapes.length + 1
const shapeWithIndex: AnnotationShape = {
...newShape,
index: newShape.index ?? nextIndex,
}
return {
shapes: [...state.shapes, shapeWithIndex],
selectedShapeId: shapeWithIndex.id,
history: {
past: [...state.history.past.slice(-29), snapshot],
future: [],
},
isDirty: true,
}
}),
updateShape: (shapeId, updates) =>
set((state) => {
const snapshot = createSnapshot(state)
const nextShapes = state.shapes.map((s) => (s.id === shapeId ? { ...s, ...updates } : s))
return {
shapes: nextShapes,
history: {
past: [...state.history.past.slice(-29), snapshot],
future: [],
},
isDirty: true,
}
}),
deleteShape: (shapeId) =>
set((state) => {
const snapshot = createSnapshot(state)
const filtered = state.shapes
.filter((s) => s.id !== shapeId)
.map((s, idx) => ({ ...s, index: idx + 1 }))
return {
shapes: filtered,
selectedShapeId: state.selectedShapeId === shapeId ? (filtered[0]?.id ?? null) : state.selectedShapeId,
history: {
past: [...state.history.past.slice(-29), snapshot],
future: [],
},
isDirty: true,
}
}),
toggleShapeVisibility: (shapeId) =>
set((state) => ({
shapes: state.shapes.map((s) => (s.id === shapeId ? { ...s, visible: !s.visible } : s)),
isDirty: true,
})),
selectShape: (shapeId) => set({ selectedShapeId: shapeId }),
setClassificationLabel: (labelId) =>
set((state) => {
const snapshot = createSnapshot(state)
return {
classificationLabelId: labelId,
activeLabelId: labelId,
history: {
past: [...state.history.past.slice(-29), snapshot],
future: [],
},
isDirty: true,
}
}),
toggleFlag: (flagKey) =>
set((state) => {
const snapshot = createSnapshot(state)
return {
flags: {
...state.flags,
[flagKey]: !state.flags[flagKey],
},
history: {
past: [...state.history.past.slice(-29), snapshot],
future: [],
},
isDirty: true,
}
}),
setActiveLabelId: (activeLabelId) => set({ activeLabelId }),
setActiveGroupId: (activeGroupId) => set({ activeGroupId }),
undo: () =>
set((state) => {
const previous = state.history.past[state.history.past.length - 1]
if (!previous) return state
const currentSnapshot = createSnapshot(state)
return {
shapes: previous.shapes,
classificationLabelId: previous.classificationLabelId,
flags: previous.flags,
history: {
past: state.history.past.slice(0, -1),
future: [currentSnapshot, ...state.history.future],
},
isDirty: true,
}
}),
redo: () =>
set((state) => {
const next = state.history.future[0]
if (!next) return state
const currentSnapshot = createSnapshot(state)
return {
shapes: next.shapes,
classificationLabelId: next.classificationLabelId,
flags: next.flags,
history: {
past: [...state.history.past, currentSnapshot],
future: state.history.future.slice(1),
},
isDirty: true,
}
}),
markSaved: (savedAtText = 'Vừa lưu 1 giây trước') =>
set({
isDirty: false,
lastSavedAt: savedAtText,
}),
}))

47
src/stores/authStore.ts Normal file
View file

@ -0,0 +1,47 @@
import { create } from 'zustand'
import { STORAGE_KEYS } from '@/app/constants'
import { DEFAULT_USER } from '@/services/mockData'
import { User, UserRole } from '@/types'
import { getStorageItem, removeStorageItem, setStorageItem } from '@/utils/storage'
export interface AuthState {
user: User | null
isAuthenticated: boolean
login: (user?: User, token?: string) => void
logout: () => void
setUser: (user: User | null) => void
setRole: (role: UserRole) => void
}
const initialUser = getStorageItem<User | null>(STORAGE_KEYS.CURRENT_USER, DEFAULT_USER)
export const useAuthStore = create<AuthState>((set) => ({
user: initialUser ?? DEFAULT_USER,
isAuthenticated: true,
login: (user = DEFAULT_USER, token = 'mock-jwt-token') => {
setStorageItem(STORAGE_KEYS.CURRENT_USER, user)
setStorageItem(STORAGE_KEYS.ACCESS_TOKEN, token)
set({ user, isAuthenticated: true })
},
logout: () => {
removeStorageItem(STORAGE_KEYS.CURRENT_USER)
removeStorageItem(STORAGE_KEYS.ACCESS_TOKEN)
removeStorageItem(STORAGE_KEYS.REFRESH_TOKEN)
set({ user: null, isAuthenticated: false })
},
setUser: (user) => {
if (user) {
setStorageItem(STORAGE_KEYS.CURRENT_USER, user)
set({ user, isAuthenticated: true })
} else {
set({ user: null, isAuthenticated: false })
}
},
setRole: (role) =>
set((state) => {
const current = state.user ?? DEFAULT_USER
const updated: User = { ...current, role }
setStorageItem(STORAGE_KEYS.CURRENT_USER, updated)
return { user: updated }
}),
}))

61
src/stores/canvasStore.ts Normal file
View file

@ -0,0 +1,61 @@
import { create } from 'zustand'
import { AnnotationTool, Point2D } from '@/types'
import { clamp } from '@/utils/canvas'
export interface CanvasStoreState {
activeTool: AnnotationTool
zoom: number
pan: Point2D
showBoxes: boolean
showMask: boolean
maskOpacity: number
vertexEditMode: boolean
setActiveTool: (tool: AnnotationTool) => void
setZoom: (zoom: number) => void
setPan: (pan: Point2D) => void
setShowBoxes: (show: boolean) => void
toggleShowBoxes: () => void
setShowMask: (show: boolean) => void
toggleShowMask: () => void
setMaskOpacity: (opacity: number) => void
setVertexEditMode: (enabled: boolean) => void
toggleVertexEditMode: () => void
zoomIn: () => void
zoomOut: () => void
resetZoom: () => void
}
export const useCanvasStore = create<CanvasStoreState>((set) => ({
activeTool: 'bbox',
zoom: 0.75,
pan: { x: 0, y: 0 },
showBoxes: true,
showMask: true,
maskOpacity: 35,
vertexEditMode: false,
setActiveTool: (activeTool) => set({ activeTool }),
setZoom: (zoom) => set({ zoom: clamp(Number(zoom.toFixed(2)), 0.25, 4) }),
setPan: (pan) => set({ pan }),
setShowBoxes: (showBoxes) => set({ showBoxes }),
toggleShowBoxes: () => set((s) => ({ showBoxes: !s.showBoxes })),
setShowMask: (showMask) => set({ showMask }),
toggleShowMask: () => set((s) => ({ showMask: !s.showMask })),
setMaskOpacity: (maskOpacity) => set({ maskOpacity: clamp(maskOpacity, 0, 100) }),
setVertexEditMode: (vertexEditMode) => set({ vertexEditMode }),
toggleVertexEditMode: () => set((s) => ({ vertexEditMode: !s.vertexEditMode })),
zoomIn: () =>
set((s) => ({
zoom: clamp(Number((s.zoom + 0.15).toFixed(2)), 0.25, 4),
})),
zoomOut: () =>
set((s) => ({
zoom: clamp(Number((s.zoom - 0.15).toFixed(2)), 0.25, 4),
})),
resetZoom: () =>
set({
zoom: 0.75,
pan: { x: 0, y: 0 },
}),
}))

5
src/stores/index.ts Normal file
View file

@ -0,0 +1,5 @@
export * from './authStore'
export * from './projectStore'
export * from './annotationStore'
export * from './canvasStore'
export * from './uiStore'

View file

@ -0,0 +1,31 @@
import { create } from 'zustand'
import { ProjectStatus, TaskType } from '@/types'
export interface ProjectStoreState {
searchQuery: string
taskTypeFilter: TaskType | 'all'
statusFilter: ProjectStatus | 'all'
selectedProjectId: string
setSearchQuery: (query: string) => void
setTaskTypeFilter: (taskType: TaskType | 'all') => void
setStatusFilter: (status: ProjectStatus | 'all') => void
setSelectedProjectId: (projectId: string) => void
resetFilters: () => void
}
export const useProjectStore = create<ProjectStoreState>((set) => ({
searchQuery: '',
taskTypeFilter: 'all',
statusFilter: 'all',
selectedProjectId: 'proj-traffic-det',
setSearchQuery: (searchQuery) => set({ searchQuery }),
setTaskTypeFilter: (taskTypeFilter) => set({ taskTypeFilter }),
setStatusFilter: (statusFilter) => set({ statusFilter }),
setSelectedProjectId: (selectedProjectId) => set({ selectedProjectId }),
resetFilters: () =>
set({
searchQuery: '',
taskTypeFilter: 'all',
statusFilter: 'all',
}),
}))

32
src/stores/uiStore.ts Normal file
View file

@ -0,0 +1,32 @@
import { create } from 'zustand'
export interface UIStoreState {
isCreateProjectModalOpen: boolean
isInviteUserModalOpen: boolean
isUploadModalOpen: boolean
sidebarCollapsed: boolean
openCreateProjectModal: () => void
closeCreateProjectModal: () => void
openInviteUserModal: () => void
closeInviteUserModal: () => void
openUploadModal: () => void
closeUploadModal: () => void
toggleSidebar: () => void
setSidebarCollapsed: (collapsed: boolean) => void
}
export const useUIStore = create<UIStoreState>((set) => ({
isCreateProjectModalOpen: false,
isInviteUserModalOpen: false,
isUploadModalOpen: false,
sidebarCollapsed: false,
openCreateProjectModal: () => set({ isCreateProjectModalOpen: true }),
closeCreateProjectModal: () => set({ isCreateProjectModalOpen: false }),
openInviteUserModal: () => set({ isInviteUserModalOpen: true }),
closeInviteUserModal: () => set({ isInviteUserModalOpen: false }),
openUploadModal: () => set({ isUploadModalOpen: true }),
closeUploadModal: () => set({ isUploadModalOpen: false }),
toggleSidebar: () => set((s) => ({ sidebarCollapsed: !s.sidebarCollapsed })),
setSidebarCollapsed: (sidebarCollapsed) => set({ sidebarCollapsed }),
}))