|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useEffect, useState } from 'react'; |
| 4 | +import { Task } from '../../types/task'; |
| 5 | +// import { User } from '../../types/user'; // Assuming a User type might be needed later |
| 6 | +import { useTasks } from '../../hooks/useTasks'; // To interact with tasks |
| 7 | + |
| 8 | +// Placeholder for sub-components that will be created in later steps |
| 9 | +import TaskHeader from './TaskHeader'; |
| 10 | +import TaskDescription from './TaskDescription'; |
| 11 | +import SubTasksList from './SubTasksList'; |
| 12 | +import TaskComments from './TaskComments'; |
| 13 | +// import TaskAssignment from './TaskAssignment'; // TaskAssignment is now part of TaskAttributesSidebar |
| 14 | +import TaskAttributesSidebar from './TaskAttributesSidebar'; |
| 15 | + |
| 16 | +interface NewTaskViewProps { |
| 17 | + taskId: string | null; |
| 18 | + onClose: () => void; |
| 19 | +} |
| 20 | + |
| 21 | +const NewTaskView: React.FC<NewTaskViewProps> = ({ taskId, onClose }) => { |
| 22 | + const { tasks, updateTask, getTaskById } = useTasks(); // Assuming getTaskById is available or can be added |
| 23 | + const [task, setTask] = useState<Task | null>(null); |
| 24 | + const [loading, setLoading] = useState<boolean>(false); |
| 25 | + const [error, setError] = useState<string | null>(null); |
| 26 | + |
| 27 | + // Ref for the main scrollable container to manage focus and scroll for navigation. |
| 28 | + const mainContentRef = React.useRef<HTMLDivElement>(null); |
| 29 | + |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (taskId) { |
| 33 | + setLoading(true); |
| 34 | + setError(null); |
| 35 | + const currentTask = tasks.find(t => t.id === taskId); |
| 36 | + if (currentTask) { |
| 37 | + setTask(currentTask); |
| 38 | + // Focus the main content area when a new task is loaded for keyboard navigation |
| 39 | + mainContentRef.current?.focus(); |
| 40 | + } else { |
| 41 | + setError('Task not found.'); |
| 42 | + } |
| 43 | + setLoading(false); |
| 44 | + } else { |
| 45 | + setTask(null); |
| 46 | + } |
| 47 | + }, [taskId, tasks]); |
| 48 | + |
| 49 | + const handleUpdateTask = async (updatedFields: Partial<Task>) => { |
| 50 | + if (task) { |
| 51 | + const updatedTaskData = { ...task, ...updatedFields }; |
| 52 | + // Optimistic update |
| 53 | + setTask(updatedTaskData); |
| 54 | + try { |
| 55 | + await updateTask(updatedTaskData); |
| 56 | + // Optionally re-fetch or confirm save |
| 57 | + } catch (e) { |
| 58 | + setError('Failed to update task.'); |
| 59 | + // Revert optimistic update if necessary |
| 60 | + setTask(tasks.find(t => t.id === task.id) || null); |
| 61 | + } |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + // Keyboard navigation handler |
| 66 | + const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => { |
| 67 | + if (!mainContentRef.current) return; |
| 68 | + |
| 69 | + // Using J for down, K for up (like Vim) |
| 70 | + // This is a basic implementation. A more robust solution would involve |
| 71 | + // identifying focusable elements within sections and moving between them. |
| 72 | + if (event.key === 'j') { |
| 73 | + event.preventDefault(); |
| 74 | + mainContentRef.current.scrollBy({ top: 100, behavior: 'smooth' }); // Scroll down |
| 75 | + } else if (event.key === 'k') { |
| 76 | + event.preventDefault(); |
| 77 | + mainContentRef.current.scrollBy({ top: -100, behavior: 'smooth' }); // Scroll up |
| 78 | + } else if (event.key === 'Escape') { |
| 79 | + // Check if any input/textarea is focused, if so, let them handle Esc first (e.g. to blur) |
| 80 | + const activeElement = document.activeElement; |
| 81 | + if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) { |
| 82 | + (activeElement as HTMLElement).blur(); |
| 83 | + } else { |
| 84 | + onClose(); // Close panel if no input is focused |
| 85 | + } |
| 86 | + } |
| 87 | + // Other shortcuts can be added here: |
| 88 | + // 'e' to edit description/title, 'c' to comment, etc. |
| 89 | + }; |
| 90 | + |
| 91 | + if (!taskId) { |
| 92 | + return null; // Or a message indicating no task is selected |
| 93 | + } |
| 94 | + |
| 95 | + if (loading) { |
| 96 | + return <div className="p-6 text-center">Loading task details...</div>; |
| 97 | + } |
| 98 | + |
| 99 | + if (error) { |
| 100 | + return <div className="p-6 text-center text-red-500">{error}</div>; |
| 101 | + } |
| 102 | + |
| 103 | + if (!task) { |
| 104 | + return <div className="p-6 text-center">Select a task to view its details.</div>; |
| 105 | + } |
| 106 | + |
| 107 | + return ( |
| 108 | + // Add onKeyDown to the outermost div of the task view that should capture these events. |
| 109 | + // It might be better on the `main` element if the sidebar shouldn't capture these. |
| 110 | + <div |
| 111 | + className="flex flex-col md:flex-row h-full bg-bg-primary text-text-primary focus:outline-none" // Added focus:outline-none |
| 112 | + role="main" |
| 113 | + onKeyDown={handleKeyDown} |
| 114 | + tabIndex={-1} |
| 115 | + ref={mainContentRef} |
| 116 | + > |
| 117 | + {/* Main Content Area (Left/Center) */} |
| 118 | + <div className="flex-1 flex flex-col min-w-0 max-h-full"> {/* Ensure max-h-full for proper scrolling */} |
| 119 | + <TaskHeader task={task} onUpdateTask={handleUpdateTask} onClosePanel={onClose} /> |
| 120 | + |
| 121 | + <main |
| 122 | + className="flex-1 overflow-y-auto p-4 md:p-6 space-y-6 divide-y divide-border-default/50 focus:outline-none custom-scrollbar" // Softer divider, custom scrollbar |
| 123 | + > |
| 124 | + <TaskDescription task={task} onUpdateTask={handleUpdateTask} /> |
| 125 | + <SubTasksList parentTask={task} onUpdateParentTask={handleUpdateTask} /> |
| 126 | + <TaskComments taskId={task.id} currentUser={{id: 'user-1', name: 'Current User', avatarUrl: 'https://i.pravatar.cc/150?u=current'}} /> |
| 127 | + </main> |
| 128 | + |
| 129 | + {/* Removed redundant Quick Actions bar, as they are now contextual */} |
| 130 | + </div> |
| 131 | + |
| 132 | + {/* Attributes Sidebar (Right) */} |
| 133 | + <TaskAttributesSidebar task={task} onUpdateTask={handleUpdateTask} /> |
| 134 | + {/* |
| 135 | + Original structure idea: |
| 136 | + <div className="flex flex-1 overflow-hidden"> |
| 137 | + <main className="flex-1 overflow-y-auto p-6 space-y-6"> |
| 138 | + <TaskDescription task={task} onUpdateTask={handleUpdateTask} /> |
| 139 | + <SubTasksList task={task} onUpdateTask={handleUpdateTask} /> |
| 140 | + <TaskComments taskId={task.id} /> |
| 141 | + </main> |
| 142 | + <TaskAttributesSidebar task={task} onUpdateTask={handleUpdateTask} /> |
| 143 | + </div> |
| 144 | + // Quick actions could be part of the header or a separate bar |
| 145 | + */} |
| 146 | + </div> |
| 147 | + ); |
| 148 | +}; |
| 149 | + |
| 150 | +export default NewTaskView; |
0 commit comments