aboutsummaryrefslogtreecommitdiff
path: root/desktop/src/components/TaskItem.tsx
blob: bb4b148419c560960c3b8dc0f7d7f6642e32103b (plain)
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
import { useState, useRef, useEffect } from "react";
import type { Task } from "../lib/commands";

interface TaskItemProps {
  task: Task;
  index: number;
  total: number;
  onEdit: (index: number, title: string) => void;
  onDelete: (index: number) => void;
  onToggle: (index: number) => void;
  onReorder: (index: number, direction: "up" | "down") => void;
}

export default function TaskItem({
  task,
  index,
  total: _total,
  onEdit,
  onDelete,
  onToggle,
  onReorder: _onReorder,
}: TaskItemProps) {
  const [isEditing, setIsEditing] = useState(false);
  const [editTitle, setEditTitle] = useState(task.title);
  const inputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    if (isEditing && inputRef.current) {
      inputRef.current.focus();
    }
  }, [isEditing]);

  function handleSubmit() {
    if (editTitle.trim() && editTitle !== task.title) {
      onEdit(index, editTitle.trim());
    } else {
      setEditTitle(task.title);
    }
    setIsEditing(false);
  }

  function handleKeyDown(e: React.KeyboardEvent) {
    if (e.key === "Enter") {
      handleSubmit();
    } else if (e.key === "Escape") {
      setEditTitle(task.title);
      setIsEditing(false);
    }
  }

  return (
    <div className={`task-item ${task.is_done ? "done" : ""}`}>
      <button
        className="task-checkbox"
        onClick={() => onToggle(index)}
        aria-label={task.is_done ? "Mark incomplete" : "Mark complete"}
      >
        {task.is_done ? (
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
            <path d="M9 12l2 2 4-4" />
          </svg>
        ) : (
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
          </svg>
        )}
      </button>

      {isEditing ? (
        <input
          ref={inputRef}
          type="text"
          className="task-edit-input"
          value={editTitle}
          onChange={(e) => setEditTitle(e.target.value)}
          onBlur={handleSubmit}
          onKeyDown={handleKeyDown}
        />
      ) : (
        <div className="task-title" onDoubleClick={() => setIsEditing(true)}>
          <span className="task-title-text">{task.title}</span>
        </div>
      )}

      <div className="task-actions">
        <button
          className="task-action-btn edit"
          onClick={() => setIsEditing(true)}
          aria-label="Edit task"
          title="Edit"
        >
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <path d="M12 20h9" />
            <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z" />
          </svg>
        </button>
        <button
          className="task-action-btn delete"
          onClick={() => onDelete(index)}
          aria-label="Delete task"
          title="Delete"
        >
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <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>
  );
}