forked from oyeolamilekan/appshots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectSwitcher.tsx
More file actions
301 lines (280 loc) · 8.8 KB
/
Copy pathProjectSwitcher.tsx
File metadata and controls
301 lines (280 loc) · 8.8 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* ProjectSwitcher Component
*
* Dropdown component for managing projects. Allows users to:
* - View all projects
* - Switch between projects
* - Create new projects
* - Rename existing projects
* - Delete projects
*/
import { useState, useRef, useEffect } from "react";
import {
FolderOpen,
Plus,
ChevronDown,
Pencil,
Trash2,
Check,
X,
} from "lucide-react";
import { useEditor } from "../../context/EditorContext";
import type { Project } from "../../types";
/**
* ProjectItem - Individual project in the list
*/
interface ProjectItemProps {
project: Project;
isActive: boolean;
onSelect: () => void;
onRename: (name: string) => void;
onDelete: () => void;
canDelete: boolean;
}
const ProjectItem = ({
project,
isActive,
onSelect,
onRename,
onDelete,
canDelete,
}: ProjectItemProps) => {
const [isEditing, setIsEditing] = useState(false);
const [editName, setEditName] = useState(project.name);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (isEditing && inputRef.current) {
inputRef.current.focus();
inputRef.current.select();
}
}, [isEditing]);
const handleSave = () => {
if (editName.trim()) {
onRename(editName.trim());
}
setIsEditing(false);
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === "Enter") {
handleSave();
} else if (e.key === "Escape") {
setEditName(project.name);
setIsEditing(false);
}
};
if (isEditing) {
return (
<div className="flex items-center gap-2 px-3 py-2">
<input
ref={inputRef}
type="text"
value={editName}
onChange={(e) => setEditName(e.target.value)}
onKeyDown={handleKeyDown}
onBlur={handleSave}
className="flex-1 px-2 py-1 text-sm bg-zinc-800 border border-zinc-600 rounded text-white focus:outline-none focus:border-violet-500"
/>
<button
onClick={handleSave}
className="p-1 hover:bg-zinc-700 rounded text-green-400"
>
<Check className="w-4 h-4" />
</button>
<button
onClick={() => {
setEditName(project.name);
setIsEditing(false);
}}
className="p-1 hover:bg-zinc-700 rounded text-red-400"
>
<X className="w-4 h-4" />
</button>
</div>
);
}
return (
<div
className={`group flex items-center justify-between px-3 py-2 cursor-pointer transition-colors ${
isActive
? "bg-violet-600/20 text-violet-400"
: "hover:bg-zinc-800 text-zinc-300"
}`}
onClick={onSelect}
>
<div className="flex items-center gap-2 min-w-0 flex-1">
<FolderOpen className="w-4 h-4 flex-shrink-0" />
<span className="truncate text-sm">{project.name}</span>
<span className="text-xs text-zinc-500">
({project.screenshots.length})
</span>
</div>
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<button
onClick={(e) => {
e.stopPropagation();
setIsEditing(true);
}}
className="p-1 hover:bg-zinc-700 rounded text-zinc-400 hover:text-white"
title="Rename project"
>
<Pencil className="w-3.5 h-3.5" />
</button>
{canDelete && (
<button
onClick={(e) => {
e.stopPropagation();
onDelete();
}}
className="p-1 hover:bg-zinc-700 rounded text-zinc-400 hover:text-red-400"
title="Delete project"
>
<Trash2 className="w-3.5 h-3.5" />
</button>
)}
</div>
</div>
);
};
/**
* ProjectSwitcher - Main dropdown component
*/
export const ProjectSwitcher = () => {
const {
projects,
activeProjectId,
activeProject,
createProject,
renameProject,
deleteProject,
switchProject,
} = useEditor();
const [isOpen, setIsOpen] = useState(false);
const [isCreating, setIsCreating] = useState(false);
const [newProjectName, setNewProjectName] = useState("");
const dropdownRef = useRef<HTMLDivElement>(null);
const newProjectInputRef = useRef<HTMLInputElement>(null);
// Close dropdown when clicking outside
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(e.target as Node)
) {
setIsOpen(false);
setIsCreating(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, []);
// Focus new project input when creating
useEffect(() => {
if (isCreating && newProjectInputRef.current) {
newProjectInputRef.current.focus();
}
}, [isCreating]);
const handleCreateProject = () => {
if (newProjectName.trim()) {
createProject(newProjectName.trim());
setNewProjectName("");
setIsCreating(false);
setIsOpen(false);
}
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === "Enter") {
handleCreateProject();
} else if (e.key === "Escape") {
setIsCreating(false);
setNewProjectName("");
}
};
return (
<div className="relative" ref={dropdownRef}>
{/* Trigger button */}
<button
onClick={() => setIsOpen(!isOpen)}
className="w-full flex items-center justify-between gap-2 px-3 py-2 bg-zinc-800 hover:bg-zinc-700 border border-zinc-700 rounded-lg text-white text-sm transition-colors"
>
<div className="flex items-center gap-2 min-w-0">
<FolderOpen className="w-4 h-4 text-violet-400 flex-shrink-0" />
<span className="truncate">{activeProject.name}</span>
</div>
<ChevronDown
className={`w-4 h-4 text-zinc-400 flex-shrink-0 transition-transform ${
isOpen ? "rotate-180" : ""
}`}
/>
</button>
{/* Dropdown menu */}
{isOpen && (
<div className="absolute top-full left-0 mt-1 w-full bg-zinc-900 border border-zinc-700 rounded-lg shadow-xl z-50 overflow-hidden">
{/* Header */}
<div className="px-3 py-2 border-b border-zinc-800">
<span className="text-xs font-medium text-zinc-500 uppercase tracking-wide">
Projects
</span>
</div>
{/* Project list */}
<div className="max-h-64 overflow-y-auto">
{projects.map((project) => (
<ProjectItem
key={project.id}
project={project}
isActive={project.id === activeProjectId}
onSelect={() => {
switchProject(project.id);
setIsOpen(false);
}}
onRename={(name) => renameProject(project.id, name)}
onDelete={() => deleteProject(project.id)}
canDelete={projects.length > 1}
/>
))}
</div>
{/* Create new project */}
<div className="border-t border-zinc-800">
{isCreating ? (
<div className="p-2 flex items-center gap-2">
<input
ref={newProjectInputRef}
type="text"
value={newProjectName}
onChange={(e) => setNewProjectName(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Project name"
className="flex-1 px-2 py-1.5 text-sm bg-zinc-800 border border-zinc-600 rounded text-white placeholder-zinc-500 focus:outline-none focus:border-violet-500"
/>
<button
onClick={handleCreateProject}
disabled={!newProjectName.trim()}
className="p-1.5 bg-violet-600 hover:bg-violet-500 disabled:bg-zinc-700 disabled:text-zinc-500 rounded text-white transition-colors"
>
<Check className="w-4 h-4" />
</button>
<button
onClick={() => {
setIsCreating(false);
setNewProjectName("");
}}
className="p-1.5 hover:bg-zinc-700 rounded text-zinc-400"
>
<X className="w-4 h-4" />
</button>
</div>
) : (
<button
onClick={() => setIsCreating(true)}
className="w-full flex items-center gap-2 px-3 py-2 text-sm text-violet-400 hover:bg-zinc-800 transition-colors"
>
<Plus className="w-4 h-4" />
New Project
</button>
)}
</div>
</div>
)}
</div>
);
};
export default ProjectSwitcher;