aboutsummaryrefslogtreecommitdiffstats
path: root/renderer/src/components/Header.tsx
blob: dc88a73664d7d96cacc169a4ea2d438848a84aad (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import React, { useState, useEffect } from "react";
import type { Project, Session, Phase } from "../types";
import { formatSessionLabel } from "../utils/timeFormat";

const api = window.api;

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;
  gitBranch: string | null;
  onOpenSettings: () => 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,
  gitBranch,
  onOpenSettings,
}: 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);
  };

  // ── Maximize ─────────────────────────────────────────────────
  const [isMaximized, setIsMaximized] = useState(false);

  useEffect(() => {
    // Returns the unsubscribe function; React cleanup calls it on unmount.
    // On macOS, clicking the native green traffic light also fires this,
    // keeping the glyph accurate when native controls are used.
    return api.onWindowMaximized(setIsMaximized);
  }, []);

  // ── Branch copy ──────────────────────────────────────────────
  const [copied, setCopied] = useState(false);

  const handleCopyBranch = () => {
    if (!gitBranch) return;
    navigator.clipboard.writeText(gitBranch);
    setCopied(true);
    setTimeout(() => setCopied(false), 1500);
  };

  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}>
                    {formatSessionLabel(s.name, s.updated_at)}
                  </option>
                ))}
              </select>
            )}
            <button onClick={onCreateSession}>+ Session</button>
            {selectedSession &&
              onRenameSession &&
              !isRenamingSession &&
              selectedSession.phase !== "implement" && (
                <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>
        )}

        {/* ── Branch badge ── */}
        {selectedSession && gitBranch && (
          <button
            className={["branch-badge", copied ? "branch-copied" : ""]
              .filter(Boolean)
              .join(" ")}
            onClick={handleCopyBranch}
            title={copied ? "Copied!" : `Click to copy: ${gitBranch}`}
          >{gitBranch}
          </button>
        )}

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

        {/* ── Maximize toggle ── */}
        <button
          className="maximize-btn"
          onClick={() => api.toggleMaximize()}
          title={isMaximized ? "Restore window" : "Maximize window"}
        >
          {isMaximized ? '⊡' : '□'}
        </button>

        {/* ── Settings button ── */}
        <button className="settings-btn" onClick={onOpenSettings} title="Settings">
          &#9881;
        </button>
      </div>
    </header>
  );
}