-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortableTask.tsx
More file actions
197 lines (172 loc) · 6.23 KB
/
SortableTask.tsx
File metadata and controls
197 lines (172 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import { Button } from "@/components/ui/button";
import { GripVertical, Trash2, CalendarCheck, Pause } from "lucide-react";
import React from "react";
import { TaskTimerButton } from "./TaskTimerButton";
import { TaskTitleField } from "./TaskTitleField";
import { TaskEstimatedTimeField } from "./TaskEstimatedTimeField";
import { StartTimeField } from "./StartTimeField";
import { EndTimeField } from "./EndTimeField";
import { TaskCategoryField } from "./TaskCategoryField";
import { TaskMetaInfo } from "./TaskMetaInfo";
import { Task } from "@/types/task";
import { useTaskContext } from "@/contexts/TaskContext";
import { getTodayDateString } from "@/lib/utils";
interface SortableTaskProps {
task: Task;
}
const SortableTask = ({ task }: SortableTaskProps) => {
// TaskContextから必要な値を取得
const { taskEdit, taskActions, lastTaskEndTime, categories } =
useTaskContext();
const { attributes, listeners, setNodeRef, transform, transition } =
useSortable({
id: task.id,
});
const style = {
transform: CSS.Transform.toString(transform),
transition,
};
// キーボードショートカットの処理
const handleTaskKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
// 編集中の場合はショートカットを無効化
if (taskEdit.editingField?.taskId === task.id) return;
switch (e.key.toLowerCase()) {
case "s": {
if (!task.start_time) {
e.preventDefault();
taskActions.handleTaskTimer(task.id, "start");
}
break;
}
case "e": {
if (task.start_time && !task.end_time) {
e.preventDefault();
taskActions.handleTaskTimer(task.id, "stop");
}
break;
}
case "d": {
e.preventDefault();
taskActions.handleDelete(task.id);
break;
}
case "arrowup":
case "arrowdown": {
// 上下キーのデフォルト動作を防ぐ
e.preventDefault();
// 親コンポーネントにイベントを伝播させる
e.stopPropagation();
// 現在フォーカスされている要素を取得
const currentFocus = document.activeElement;
if (!currentFocus) return;
// タスク要素を取得
const taskElements = Array.from(
document.querySelectorAll("[data-task-id]"),
);
const currentIndex = taskElements.indexOf(currentFocus as HTMLElement);
if (currentIndex === -1) return;
// 上下キーに応じて次のタスクを選択
let nextIndex;
if (e.key.toLowerCase() === "arrowup") {
nextIndex = Math.max(0, currentIndex - 1);
} else {
nextIndex = Math.min(taskElements.length - 1, currentIndex + 1);
}
// 次のタスクにフォーカスを移動
(taskElements[nextIndex] as HTMLElement).focus();
break;
}
}
};
// カテゴリの色を取得
const selectedCategory = categories.find(
(cat) => cat.id === task.category_id,
);
const categoryColor = selectedCategory?.color || "#6b7280";
// 今日の日付を取得(JST)
const todayFormatted = getTodayDateString();
// タスクが今日のものかどうか
const isToday = task.task_date === todayFormatted;
// タスクが完了しているかどうか
const isCompleted = !!task.end_time;
// タスクが実行中かどうか
const isRunning = !!task.start_time && !task.end_time;
return (
<div
ref={setNodeRef}
style={{
...style,
borderLeft: `4px solid ${categoryColor}`,
}}
className={`flex items-center justify-between rounded-md border p-4 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 ${
isCompleted ? "bg-gray-50 opacity-70" : ""
}`}
onKeyDown={handleTaskKeyDown}
data-task-id={task.id}
{...attributes}
{...listeners}
>
<div className="flex items-center gap-4">
<div className="cursor-grab active:cursor-grabbing">
<GripVertical className="h-4 w-4" style={{ color: categoryColor }} />
</div>
<TaskTimerButton task={task} categoryColor={categoryColor} />
<div className="flex-grow">
<TaskTitleField
task={task}
categoryColor={categoryColor}
isCompleted={isCompleted}
/>
<div className="flex gap-3 text-sm text-muted-foreground">
<TaskEstimatedTimeField task={task} categoryColor={categoryColor} />
<StartTimeField
task={task}
lastTaskEndTime={lastTaskEndTime}
categoryColor={categoryColor}
/>
<EndTimeField task={task} categoryColor={categoryColor} />
<TaskCategoryField task={task} categories={categories} />
<TaskMetaInfo task={task} categoryColor={categoryColor} />
</div>
</div>
</div>
<div className="flex gap-2 items-center">
{/* 中断ボタン:実行中のタスクのみ表示 */}
{isRunning && (
<Button
size="icon"
variant="outline"
onClick={() => taskActions.handlePauseTask(task)}
className="h-8 w-8 text-orange-500 hover:text-orange-700 hover:bg-orange-50"
title="中断"
>
<Pause className="h-4 w-4" />
</Button>
)}
{/* 今日に移動ボタン:完了していない&今日以外のタスクに表示 */}
{!isCompleted && !isToday && (
<Button
size="icon"
variant="outline"
onClick={() => taskActions.handleMoveToToday(task.id)}
className="h-8 w-8 text-blue-500 hover:text-blue-700 hover:bg-blue-50"
title="今日に移動"
>
<CalendarCheck className="h-4 w-4" />
</Button>
)}
<Button
size="icon"
variant="outline"
onClick={() => taskActions.handleDelete(task.id)}
className="h-8 w-8 text-red-500 hover:text-red-700 hover:bg-red-50"
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
</div>
);
};
export default SortableTask;