import React, { useRef, useState, useEffect } from 'react'; import { Button, Flex, Input, InputRef } from '@/shared/antd-imports'; import { useTranslation } from 'react-i18next'; import { nanoid } from '@reduxjs/toolkit'; import { useAppDispatch } from '@/hooks/useAppDispatch'; import { useAppSelector } from '@/hooks/useAppSelector'; import { themeWiseColor } from '@/utils/themeWiseColor'; import { useSocket } from '@/socket/socketContext'; import { SocketEvents } from '@/shared/socket-events'; import { useAuthService } from '@/hooks/useAuth'; import { IProjectTask } from '@/types/project/projectTasksViewModel.types'; import { addTaskToGroup } from '@/features/enhanced-kanban/enhanced-kanban.slice'; import { ITaskCreateRequest } from '@/types/tasks/task-create-request.types'; interface EnhancedKanbanCreateTaskCardProps { sectionId: string; setShowNewCard: (x: boolean) => void; position?: 'top' | 'bottom'; } const EnhancedKanbanCreateTaskCard: React.FC = ({ sectionId, setShowNewCard, position = 'bottom', }) => { const { t } = useTranslation('kanban-board'); const dispatch = useAppDispatch(); const { socket } = useSocket(); const currentSession = useAuthService().getCurrentSession(); const [newTaskName, setNewTaskName] = useState(''); const [creatingTask, setCreatingTask] = useState(false); const cardRef = useRef(null); const inputRef = useRef(null); const themeMode = useAppSelector(state => state.themeReducer.mode); const projectId = useAppSelector(state => state.projectReducer.projectId); const groupBy = useAppSelector(state => state.enhancedKanbanReducer.groupBy); useEffect(() => { const timer = setTimeout(() => { inputRef.current?.focus(); }, 100); return () => clearTimeout(timer); }, []); const createRequestBody = (): ITaskCreateRequest | null => { if (!projectId || !currentSession) return null; const body: ITaskCreateRequest = { project_id: projectId, name: newTaskName.trim(), reporter_id: currentSession.id, team_id: currentSession.team_id, }; if (groupBy === 'status') body.status_id = sectionId; else if (groupBy === 'priority') body.priority_id = sectionId; else if (groupBy === 'phase') body.phase_id = sectionId; return body; }; const resetForm = () => { setNewTaskName(''); setCreatingTask(false); setShowNewCard(false); setTimeout(() => { inputRef.current?.focus(); }, 100); }; const resetForNextTask = () => { setNewTaskName(''); setCreatingTask(false); // Keep the card visible for creating the next task setTimeout(() => { inputRef.current?.focus(); }, 100); }; const handleAddTask = async () => { if (creatingTask || !projectId || !currentSession || newTaskName.trim() === '') return; const body = createRequestBody(); if (!body) { setCreatingTask(true); setShowNewCard(true); return; } // Real-time socket event handler const eventHandler = (task: IProjectTask) => { // Only reset the form - the global handler will add the task to Redux socket?.off(SocketEvents.QUICK_TASK.toString(), eventHandler); resetForNextTask(); }; socket?.once(SocketEvents.QUICK_TASK.toString(), eventHandler); socket?.emit(SocketEvents.QUICK_TASK.toString(), JSON.stringify(body)); }; const handleCancel = () => { setNewTaskName(''); setShowNewCard(false); setCreatingTask(false); }; const handleBlur = () => { if (newTaskName.trim() === '') { setCreatingTask(false); setShowNewCard(false); } }; return ( setNewTaskName(e.target.value)} onPressEnter={handleAddTask} onBlur={handleBlur} placeholder={t('newTaskNamePlaceholder')} style={{ width: '100%', borderRadius: 6, padding: 8, }} disabled={creatingTask} /> {newTaskName.trim() && ( )} ); }; export default EnhancedKanbanCreateTaskCard;