aboutsummaryrefslogtreecommitdiffstats
path: root/renderer/src/components/Header.tsx
blob: a435519aa2437505520a01fac0d25a80bf6bb58d (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
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
198
199
200
import React, { useState } from "react";
import type { Project, Session, Phase } from "../types";

type Theme = "dark" | "light";

interface HeaderProps {
  projects: Project[];
  sessions: Session[];
  selectedProject: Project | null;
  selectedSession: Session | null;
  onSelectProject: (project: Project | null) => void;
  onSelectSession: (session: Session | null) => void;
  onCreateProject: () => void;
  onCreateSession: () => void;
  onDeleteProject?: (id: string) => void;
  onDeleteSession?: (id: string) => void;
  onRenameSession?: (id: string, name: string) => void;
  theme: Theme;
  onToggleTheme: () => void;
}

const phaseLabels: Record<Phase, string> = {
  research: "Research",
  plan: "Plan",
  implement: "Implement",
};

const phases: Phase[] = ["research", "plan", "implement"];

export function Header({
  projects,
  sessions,
  selectedProject,
  selectedSession,
  onSelectProject,
  onSelectSession,
  onCreateProject,
  onCreateSession,
  onDeleteProject,
  onDeleteSession,
  onRenameSession,
  theme,
  onToggleTheme,
}: HeaderProps) {
  const handleDeleteProject = () => {
    if (!selectedProject || !onDeleteProject) return;
    if (confirm(`Delete project "${selectedProject.name}"? This cannot be undone.`)) {
      onDeleteProject(selectedProject.id);
    }
  };

  const handleDeleteSession = () => {
    if (!selectedSession || !onDeleteSession) return;
    if (confirm(`Delete session "${selectedSession.name}"? This cannot be undone.`)) {
      onDeleteSession(selectedSession.id);
    }
  };

  const [isRenamingSession, setIsRenamingSession] = useState(false);
  const [renameValue, setRenameValue] = useState("");
  // Guard against double-commit (onKeyDown Enter → unmount → onBlur)
  const renameCommitted = React.useRef(false);

  const startRename = () => {
    if (!selectedSession) return;
    renameCommitted.current = false;
    setRenameValue(selectedSession.name);
    setIsRenamingSession(true);
  };

  const commitRename = () => {
    if (renameCommitted.current) return;
    renameCommitted.current = true;
    if (selectedSession && onRenameSession && renameValue.trim()) {
      onRenameSession(selectedSession.id, renameValue.trim());
    }
    setIsRenamingSession(false);
  };

  const cancelRename = () => {
    renameCommitted.current = true; // prevent blur from committing after cancel
    setIsRenamingSession(false);
  };

  return (
    <header className="header">
      <div className="header-left">
        {/* ── Wordmark ── */}
        <span className="app-wordmark">Claude Flow</span>

        <select
          value={selectedProject?.id || ""}
          onChange={(e) => {
            const project = projects.find((p) => p.id === e.target.value);
            onSelectProject(project || null);
            onSelectSession(null);
          }}
        >
          <option value="">Select Project...</option>
          {projects.map((p) => (
            <option key={p.id} value={p.id}>
              {p.name}
            </option>
          ))}
        </select>
        <button onClick={onCreateProject}>+ Project</button>
        {selectedProject && onDeleteProject && (
          <button
            onClick={handleDeleteProject}
            className="btn-delete"
            title="Delete project"
          >
            🗑️
          </button>
        )}

        {selectedProject && (
          <>
            {isRenamingSession ? (
              <input
                autoFocus
                value={renameValue}
                onChange={(e) => setRenameValue(e.target.value)}
                onKeyDown={(e) => {
                  if (e.key === "Enter") commitRename();
                  if (e.key === "Escape") cancelRename();
                }}
                onBlur={commitRename}
                className="session-rename-input"
              />
            ) : (
              <select
                value={selectedSession?.id || ""}
                onChange={(e) => {
                  const session = sessions.find((s) => s.id === e.target.value);
                  onSelectSession(session || null);
                }}
              >
                <option value="">Select Session...</option>
                {sessions.map((s) => (
                  <option key={s.id} value={s.id}>
                    {s.name}
                  </option>
                ))}
              </select>
            )}
            <button onClick={onCreateSession}>+ Session</button>
            {selectedSession && onRenameSession && !isRenamingSession && (
              <button
                onClick={startRename}
                className="btn-rename"
                title="Rename session"
              >
                ✏️
              </button>
            )}
            {selectedSession && onDeleteSession && (
              <button
                onClick={handleDeleteSession}
                className="btn-delete"
                title="Delete session"
              >
                🗑️
              </button>
            )}
          </>
        )}
      </div>

      <div className="header-right">
        {selectedSession && (
          <div className="phase-indicator">
            {phases.map((phase) => {
              const phaseIndex = phases.indexOf(phase);
              const currentIndex = phases.indexOf(selectedSession.phase);
              const isComplete = phaseIndex < currentIndex;
              const isActive = phase === selectedSession.phase;

              return (
                <span
                  key={phase}
                  className={`phase-step ${isActive ? "active" : ""} ${
                    isComplete ? "complete" : ""
                  }`}
                >
                  {phaseLabels[phase]}
                </span>
              );
            })}
          </div>
        )}

        {/* ── Theme toggle ── */}
        <button className="theme-toggle" onClick={onToggleTheme}>
          {theme === "dark" ? "[light]" : "[dark]"}
        </button>
      </div>
    </header>
  );
}