-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathagents-project-worktree-tab.tsx
More file actions
778 lines (715 loc) · 29.5 KB
/
Copy pathagents-project-worktree-tab.tsx
File metadata and controls
778 lines (715 loc) · 29.5 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
import { useState, useEffect, useCallback, useMemo, useRef } from "react"
import { useListKeyboardNav } from "./use-list-keyboard-nav"
import { useAtomValue, useSetAtom } from "jotai"
import { trpc } from "../../../lib/trpc"
import { Button, buttonVariants } from "../../ui/button"
import { Input } from "../../ui/input"
import { Plus, Trash2 } from "lucide-react"
import { AIPenIcon, ExternalLinkIcon, FolderFilledIcon, ImageIcon } from "../../ui/icons"
import { invalidateProjectIcon } from "../../../lib/hooks/use-project-icon"
import { ProjectIcon } from "../../ui/project-icon"
import finderIcon from "../../../assets/app-icons/finder.png"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
} from "../../ui/select"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "../../ui/alert-dialog"
import { toast } from "sonner"
import { COMMAND_PROMPTS } from "../../../features/agents/commands"
import {
agentsSettingsDialogOpenAtom,
selectedAgentChatIdAtom,
selectedProjectAtom,
} from "../../../lib/atoms"
import { cn } from "../../../lib/utils"
import { ResizableSidebar } from "../../ui/resizable-sidebar"
import { settingsProjectsSidebarWidthAtom } from "../../../features/agents/atoms"
import { toSelectedProject } from "../../../features/agents/lib/selected-project"
// --- Detail Panel ---
function ProjectDetail({ projectId }: { projectId: string }) {
const utils = trpc.useUtils()
// Get config for selected project
const { data: configData } =
trpc.worktreeConfig.get.useQuery(
{ projectId },
{ enabled: !!projectId },
)
// Save mutation (auto-save, no toast on success — only on error)
const saveMutation = trpc.worktreeConfig.save.useMutation({
onError: (err) => {
toast.error(`Failed to save: ${err.message}`)
},
})
// For "Fill with AI" - create chat and close settings
const setSettingsDialogOpen = useSetAtom(agentsSettingsDialogOpenAtom)
const setSelectedChatId = useSetAtom(selectedAgentChatIdAtom)
const setSelectedProject = useSetAtom(selectedProjectAtom)
const createChatMutation = trpc.chats.create.useMutation({
onSuccess: (data) => {
setSettingsDialogOpen(false)
setSelectedChatId(data.id)
},
})
// Get project info
const { data: project } = trpc.projects.get.useQuery(
{ id: projectId },
{ enabled: !!projectId },
)
const syncProjectState = useCallback((updatedProject: NonNullable<typeof project>) => {
utils.projects.get.setData({ id: projectId }, updatedProject)
utils.projects.list.setData(undefined, (oldProjects) => {
if (!oldProjects) return [updatedProject]
const exists = oldProjects.some((candidate) => candidate.id === updatedProject.id)
if (!exists) return [updatedProject, ...oldProjects]
return oldProjects.map((candidate) =>
candidate.id === updatedProject.id ? updatedProject : candidate,
)
})
setSelectedProject((current) =>
current?.id === updatedProject.id ? toSelectedProject(updatedProject) : current,
)
}, [projectId, setSelectedProject, utils.projects.get, utils.projects.list])
// Rename mutation
const renameMutation = trpc.projects.rename.useMutation({
onSuccess: (updatedProject) => {
if (updatedProject) {
syncProjectState(updatedProject)
}
toast.success("Project renamed")
},
onError: (err) => {
toast.error(`Failed to rename: ${err.message}`)
},
})
// Delete project mutation
const deleteMutation = trpc.projects.delete.useMutation({
onSuccess: (deletedProject) => {
if (deletedProject) {
utils.projects.get.setData({ id: projectId }, undefined)
utils.projects.list.setData(undefined, (oldProjects) =>
oldProjects?.filter((candidate) => candidate.id !== deletedProject.id) ?? [],
)
}
toast.success("Project removed from list")
setSelectedProject((current) => {
if (current?.id === projectId) {
return null
}
return current
})
},
onError: (err) => {
toast.error(`Failed to delete project: ${err.message}`)
},
})
// Icon mutations
const uploadIconMutation = trpc.projects.uploadIcon.useMutation({
onSuccess: (data) => {
if (!data) return // User cancelled file picker
invalidateProjectIcon(projectId)
syncProjectState(data)
toast.success("Icon updated")
},
onError: (err) => {
toast.error(`Failed to upload icon: ${err.message}`)
},
})
const removeIconMutation = trpc.projects.removeIcon.useMutation({
onSuccess: (updatedProject) => {
invalidateProjectIcon(projectId)
if (updatedProject) {
syncProjectState(updatedProject)
}
toast.success("Icon removed")
},
})
const [showDeleteDialog, setShowDeleteDialog] = useState(false)
// Project name editing
const [projectName, setProjectName] = useState("")
const savedNameRef = useRef("")
useEffect(() => {
if (project?.name) {
setProjectName(project.name)
savedNameRef.current = project.name
}
}, [project?.name])
const handleNameBlur = useCallback(async () => {
const trimmed = projectName.trim()
if (!trimmed || trimmed === savedNameRef.current) {
setProjectName(savedNameRef.current)
return
}
renameMutation.mutate({ id: projectId, name: trimmed })
savedNameRef.current = trimmed
}, [projectName, projectId, renameMutation])
// Local state
const [saveTarget, setSaveTarget] = useState<"cursor" | "1code">("1code")
const [commands, setCommands] = useState<string[]>([""])
const [unixCommands, setUnixCommands] = useState<string[]>([])
const [windowsCommands, setWindowsCommands] = useState<string[]>([])
const [showPlatformSpecific, setShowPlatformSpecific] = useState(false)
// Ref to track last saved state for dirty checking
const savedConfigRef = useRef<string>("")
const configReadyRef = useRef(false)
// Sync from server data
useEffect(() => {
if (configData) {
const newSaveTarget = configData.source === "cursor" ? "cursor" : "1code"
setSaveTarget(newSaveTarget)
let newCommands: string[] = [""]
let newUnix: string[] = []
let newWin: string[] = []
if (configData.config) {
const isComment = (s: string) => s.trimStart().startsWith("#")
const filterComments = (arr: string[]) => arr.filter((s) => !isComment(s))
const generic = configData.config["setup-worktree"]
const genericArr = Array.isArray(generic)
? filterComments(generic)
: generic && !isComment(generic)
? [generic]
: []
newCommands = genericArr.length > 0 ? [...genericArr, ""] : [""]
const unix = configData.config["setup-worktree-unix"]
const win = configData.config["setup-worktree-windows"]
newUnix = Array.isArray(unix) ? filterComments(unix) : unix && !isComment(unix) ? [unix] : []
newWin = Array.isArray(win) ? filterComments(win) : win && !isComment(win) ? [win] : []
if (unix || win) {
setShowPlatformSpecific(true)
}
}
setCommands(newCommands)
setUnixCommands(newUnix)
setWindowsCommands(newWin)
// Snapshot the initial state so doSave won't fire on first render
savedConfigRef.current = JSON.stringify({
commands: newCommands,
unixCommands: newUnix,
windowsCommands: newWin,
saveTarget: newSaveTarget,
})
configReadyRef.current = true
}
}, [configData])
const doSave = useCallback(() => {
if (!projectId || !configReadyRef.current) return
const currentState = JSON.stringify({ commands, unixCommands, windowsCommands, saveTarget })
if (currentState === savedConfigRef.current) return
const config: Record<string, string[]> = {}
const filteredCommands = commands.filter((c) => c.trim())
const filteredUnix = unixCommands.filter((c) => c.trim())
const filteredWin = windowsCommands.filter((c) => c.trim())
if (filteredCommands.length > 0) config["setup-worktree"] = filteredCommands
if (filteredUnix.length > 0) config["setup-worktree-unix"] = filteredUnix
if (filteredWin.length > 0) config["setup-worktree-windows"] = filteredWin
saveMutation.mutate({ projectId, config, target: saveTarget })
savedConfigRef.current = currentState
}, [projectId, commands, unixCommands, windowsCommands, saveTarget, saveMutation])
const updateCommand = (index: number, value: string, list: string[], setter: (v: string[]) => void) => {
const newList = [...list]
newList[index] = value
setter(newList)
}
const pendingSaveRef = useRef(false)
const removeCommand = (index: number, list: string[], setter: (v: string[]) => void, allowEmpty = false) => {
if (!allowEmpty && list.length <= 1) return
setter(list.filter((_, i) => i !== index))
pendingSaveRef.current = true
}
// Save after state updates from remove or saveTarget change
useEffect(() => {
if (pendingSaveRef.current) {
pendingSaveRef.current = false
doSave()
}
}, [commands, unixCommands, windowsCommands, saveTarget, doSave])
const addCommand = (list: string[], setter: (v: string[]) => void) => {
setter([...list, ""])
}
const cursorExists = configData?.available?.cursor?.exists ?? false
const openInFinderMutation = trpc.external.openInFinder.useMutation()
const handleOpenInFinder = () => {
if (project?.path) {
openInFinderMutation.mutate(project.path)
}
}
// Helper to render a command list with add/remove
const renderCommandList = (
list: string[],
setter: (v: string[]) => void,
placeholder: string,
allowEmpty = false,
) => (
<div className="space-y-2">
{list.map((cmd, i) => (
<div key={i} className="flex items-center gap-2">
<Input
value={cmd}
onChange={(e) => updateCommand(i, e.target.value, list, setter)}
onBlur={doSave}
placeholder={placeholder}
className="flex-1 font-mono text-sm"
/>
{(allowEmpty || list.length > 1) && (
<button
type="button"
className="h-8 w-8 flex items-center justify-center rounded-md text-muted-foreground hover:text-destructive transition-colors"
onClick={() => removeCommand(i, list, setter, allowEmpty)}
>
<Trash2 className="h-3.5 w-3.5" />
</button>
)}
</div>
))}
<button
type="button"
className="flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors"
onClick={() => addCommand(list, setter)}
>
<Plus className="h-3.5 w-3.5" />
Add command
</button>
</div>
)
return (
<div className="h-full overflow-y-auto">
<div className="max-w-2xl mx-auto p-6 space-y-6">
{/* ── General ── */}
<div>
<h4 className="text-sm font-medium text-foreground mb-2">General</h4>
<div className="bg-background rounded-lg border border-border overflow-hidden">
{/* Name */}
<div className="flex items-center justify-between p-4">
<div className="flex-1">
<span className="text-sm font-medium text-foreground">Name</span>
<p className="text-sm text-muted-foreground">Display name for this project</p>
</div>
<div className="flex-shrink-0 w-80">
<Input
value={projectName}
onChange={(e) => setProjectName(e.target.value)}
onBlur={handleNameBlur}
className="w-full"
placeholder="Project name"
/>
</div>
</div>
{/* Icon */}
<div className="flex items-center justify-between p-4 border-t border-border">
<div className="flex-1">
<span className="text-sm font-medium text-foreground">Icon</span>
<p className="text-sm text-muted-foreground">Project avatar in sidebar</p>
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<button
type="button"
className="relative h-10 w-10 rounded-lg border border-border overflow-hidden flex items-center justify-center cursor-pointer bg-muted group/icon"
onClick={() => uploadIconMutation.mutate({ id: projectId })}
title="Click to change icon"
>
<ProjectIcon project={project} className="h-full w-full" />
<div className="absolute inset-0 flex items-center justify-center bg-black/50 opacity-0 group-hover/icon:opacity-100 transition-opacity duration-150">
<ImageIcon className="h-4 w-4 text-white" />
</div>
</button>
{project?.iconPath && (
<Button
variant="ghost"
size="sm"
className="text-muted-foreground hover:text-foreground"
onClick={() => removeIconMutation.mutate({ id: projectId })}
>
Reset
</Button>
)}
</div>
</div>
{/* Path */}
<div className="flex items-center justify-between p-4 border-t border-border">
<div className="flex-1 min-w-0 mr-4">
<span className="text-sm font-medium text-foreground">Path</span>
<p className="text-sm text-muted-foreground truncate">{project?.path || "—"}</p>
</div>
<Button
variant="outline"
size="sm"
className="gap-1.5 flex-shrink-0 pl-2"
onClick={handleOpenInFinder}
disabled={!project?.path}
>
<img src={finderIcon} alt="" className="h-3.5 w-3.5" />
Finder
</Button>
</div>
{/* Repository */}
{project?.gitOwner && project?.gitRepo && (
<div className="flex items-center justify-between p-4 border-t border-border">
<div className="flex-1">
<span className="text-sm font-medium text-foreground">Repository</span>
<p className="text-sm text-muted-foreground">
{project.gitOwner}/{project.gitRepo}
</p>
</div>
{project.gitProvider === "github" && (
<Button
variant="outline"
size="sm"
className="gap-1.5 flex-shrink-0 pl-2"
onClick={() => {
window.open(
`https://github.com/${project.gitOwner}/${project.gitRepo}`,
"_blank",
)
}}
>
<ExternalLinkIcon className="h-3.5 w-3.5" />
GitHub
</Button>
)}
</div>
)}
</div>
</div>
{/* ── Config ── */}
<div>
<h4 className="text-sm font-medium text-foreground mb-2">Config</h4>
<div className="bg-background rounded-lg border border-border overflow-hidden">
<div className="flex items-center justify-between p-4">
<div className="flex-1">
<span className="text-sm font-medium text-foreground">Config file</span>
<p className="text-sm text-muted-foreground">Where worktree setup is stored</p>
</div>
<Select
value={saveTarget}
onValueChange={(v) => {
setSaveTarget(v as "cursor" | "1code")
pendingSaveRef.current = true
}}
>
<SelectTrigger className="w-auto px-3">
<span className="text-sm font-mono">
{saveTarget === "cursor" ? ".cursor/worktrees.json" : ".1code/worktree.json"}
</span>
</SelectTrigger>
<SelectContent>
<SelectItem value="1code">.1code/worktree.json</SelectItem>
{cursorExists && (
<SelectItem value="cursor">.cursor/worktrees.json</SelectItem>
)}
</SelectContent>
</Select>
</div>
</div>
</div>
{/* ── Worktree ── */}
<div>
<div className="flex items-center justify-between mb-2">
<h4 className="text-sm font-medium text-foreground">Worktree</h4>
<Button
variant="ghost"
size="sm"
className="gap-1.5 shrink-0"
onClick={() => {
const prompt = COMMAND_PROMPTS["worktree-setup"]
if (prompt && projectId) {
createChatMutation.mutate({
projectId,
name: "Worktree Setup",
initialMessageParts: [{ type: "text", text: prompt }],
useWorktree: false,
mode: "agent",
})
}
}}
disabled={!projectId || createChatMutation.isPending}
>
<AIPenIcon className="h-3.5 w-3.5" />
Fill with AI
</Button>
</div>
<div className="bg-background rounded-lg border border-border overflow-hidden">
{/* Setup commands */}
<div className="p-4 space-y-3">
<div>
<span className="text-sm font-medium text-foreground">Setup Commands</span>
<p className="text-sm text-muted-foreground">
Run after worktree creation.{" "}
<button
type="button"
className="font-mono text-xs bg-muted px-1 py-0.5 rounded hover:text-foreground transition-colors cursor-pointer"
onClick={() => {
navigator.clipboard.writeText("$ROOT_WORKTREE_PATH")
toast.success("Copied to clipboard")
}}
title="Click to copy"
>
$ROOT_WORKTREE_PATH
</button>
{" "}for main repo.
</p>
</div>
{renderCommandList(commands, setCommands, "bun install && cp $ROOT_WORKTREE_PATH/.env .env")}
</div>
{/* Platform overrides — macOS/Linux */}
{(unixCommands.length > 0 || showPlatformSpecific) && (
<div className="p-4 border-t border-border space-y-3">
<div className="flex items-center justify-between">
<span className="text-sm font-medium text-foreground">macOS / Linux</span>
{unixCommands.length === 0 && (
<span className="text-sm text-muted-foreground">Falls back to commands above</span>
)}
</div>
{renderCommandList(unixCommands, setUnixCommands, "brew install deps", true)}
</div>
)}
{/* Platform overrides — Windows */}
{(windowsCommands.length > 0 || showPlatformSpecific) && (
<div className="p-4 border-t border-border space-y-3">
<div className="flex items-center justify-between">
<span className="text-sm font-medium text-foreground">Windows</span>
{windowsCommands.length === 0 && (
<span className="text-sm text-muted-foreground">Falls back to commands above</span>
)}
</div>
{renderCommandList(windowsCommands, setWindowsCommands, "npm ci", true)}
</div>
)}
{/* Add platform overrides link */}
{!showPlatformSpecific && unixCommands.length === 0 && windowsCommands.length === 0 && (
<div className="p-4 border-t border-border">
<button
type="button"
className="flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors"
onClick={() => setShowPlatformSpecific(true)}
>
<Plus className="h-3.5 w-3.5" />
Add platform-specific overrides
</button>
</div>
)}
</div>
</div>
{/* ── Danger Zone ── */}
<div>
<h4 className="text-sm font-medium text-foreground mb-2">Danger Zone</h4>
<div className="bg-background rounded-lg border border-border overflow-hidden">
<div className="flex items-center justify-between p-4">
<div className="flex-1">
<span className="text-sm font-medium text-foreground">Remove Project</span>
<p className="text-sm text-muted-foreground">
Remove from your list. Files on disk will not be deleted.
</p>
</div>
<AlertDialog open={showDeleteDialog} onOpenChange={setShowDeleteDialog}>
<AlertDialogTrigger asChild>
<Button
variant="outline"
size="sm"
className="gap-1.5 hover:text-destructive hover:border-destructive/30 hover:bg-destructive/10"
>
<Trash2 className="h-3.5 w-3.5" />
Remove
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Remove Project?</AlertDialogTitle>
<AlertDialogDescription>
This will remove "{project?.name}" from your project list. Your files will not be deleted.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={() => deleteMutation.mutate({ id: projectId })}
disabled={deleteMutation.isPending}
className={buttonVariants({ variant: "destructive" })}
>
{deleteMutation.isPending ? "Removing..." : "Remove"}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</div>
</div>
</div>
</div>
)
}
// --- Main Two-Panel Component ---
export function AgentsProjectsTab() {
const selectedProject = useAtomValue(selectedProjectAtom)
const [selectedProjectId, setSelectedProjectId] = useState<string | null>(null)
const [searchQuery, setSearchQuery] = useState("")
const searchInputRef = useRef<HTMLInputElement>(null)
// Focus search on "/" hotkey
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.key === "/" && !e.metaKey && !e.ctrlKey && !e.altKey) {
const tag = (e.target as HTMLElement)?.tagName
if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") return
e.preventDefault()
searchInputRef.current?.focus()
}
}
document.addEventListener("keydown", handler)
return () => document.removeEventListener("keydown", handler)
}, [])
const { data: projects, isLoading } = trpc.projects.list.useQuery()
const openFolderMutation = trpc.projects.openFolder.useMutation({
onSuccess: (project) => {
if (project) {
setSelectedProjectId(project.id)
}
},
})
// Filter projects by search
const filteredProjects = useMemo(() => {
if (!projects) return []
if (!searchQuery.trim()) return projects
const q = searchQuery.toLowerCase()
return projects.filter(
(p) =>
p.name.toLowerCase().includes(q) ||
p.path?.toLowerCase().includes(q) ||
p.gitRepo?.toLowerCase().includes(q),
)
}, [projects, searchQuery])
const allProjectIds = useMemo(
() => filteredProjects.map((p) => p.id),
[filteredProjects]
)
const { containerRef: listRef, onKeyDown: listKeyDown } = useListKeyboardNav({
items: allProjectIds,
selectedItem: selectedProjectId,
onSelect: setSelectedProjectId,
})
// Auto-select first project
useEffect(() => {
if (selectedProjectId || isLoading) return
if (projects && projects.length > 0) {
setSelectedProjectId(projects[0]!.id)
}
}, [projects, selectedProjectId, isLoading])
// Sync selection from global selectedProject (e.g., toast action)
useEffect(() => {
if (!selectedProject?.id) return
setSelectedProjectId(selectedProject.id)
}, [selectedProject?.id])
return (
<div className="flex h-full overflow-hidden">
{/* Left sidebar - project list */}
<ResizableSidebar
isOpen={true}
onClose={() => {}}
widthAtom={settingsProjectsSidebarWidthAtom}
minWidth={200}
maxWidth={400}
side="left"
animationDuration={0}
initialWidth={240}
exitWidth={240}
disableClickToClose={true}
>
<div className="flex flex-col h-full bg-background border-r overflow-hidden" style={{ borderRightWidth: "0.5px" }}>
{/* Search + Add */}
<div className="px-2 pt-2 flex-shrink-0 flex items-center gap-1.5">
<input
ref={searchInputRef}
placeholder="Search projects..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
onKeyDown={listKeyDown}
className="h-7 w-full rounded-lg text-sm bg-muted border border-input px-3 placeholder:text-muted-foreground/40 outline-none"
/>
<button
onClick={() => openFolderMutation.mutate()}
className="h-7 w-7 shrink-0 flex items-center justify-center rounded-lg text-muted-foreground hover:text-foreground hover:bg-foreground/5 transition-colors cursor-pointer"
title="Add project folder"
>
<Plus className="h-4 w-4" />
</button>
</div>
{/* Project list */}
<div ref={listRef} onKeyDown={listKeyDown} tabIndex={-1} className="flex-1 overflow-y-auto px-2 pt-2 pb-2 outline-none">
{isLoading ? (
<div className="flex items-center justify-center h-full">
<FolderFilledIcon className="h-5 w-5 text-muted-foreground animate-pulse" />
</div>
) : !projects || projects.length === 0 ? (
<div className="flex flex-col items-center justify-center h-full text-center px-4">
<FolderFilledIcon className="h-8 w-8 text-border mb-3" />
<p className="text-sm text-muted-foreground mb-1">No projects</p>
<button
onClick={() => openFolderMutation.mutate()}
className="text-xs text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
>
Add your first project
</button>
</div>
) : filteredProjects.length === 0 ? (
<div className="flex items-center justify-center py-8">
<p className="text-xs text-muted-foreground">No results found</p>
</div>
) : (
<div className="space-y-0.5">
{filteredProjects.map((project) => {
const isSelected = selectedProjectId === project.id
return (
<button
key={project.id}
data-item-id={project.id}
onClick={() => setSelectedProjectId(project.id)}
className={cn(
"w-full text-left py-1.5 px-2 rounded-md transition-colors duration-150 cursor-pointer outline-none focus-visible:outline focus-visible:outline-2 focus-visible:outline-ring/70 focus-visible:-outline-offset-2",
isSelected
? "bg-foreground/5 text-foreground"
: "text-muted-foreground hover:bg-foreground/5 hover:text-foreground",
)}
>
<div className="flex items-center gap-2">
<ProjectIcon project={project} className="h-4 w-4" />
<span className="text-sm truncate flex-1">
{project.name}
</span>
</div>
</button>
)
})}
</div>
)}
</div>
</div>
</ResizableSidebar>
{/* Right content - detail panel */}
<div className="flex-1 min-w-0 h-full overflow-hidden">
{selectedProjectId ? (
<ProjectDetail projectId={selectedProjectId} />
) : (
<div className="flex flex-col items-center justify-center h-full text-center px-4">
<FolderFilledIcon className="h-12 w-12 text-border mb-4" />
<p className="text-sm text-muted-foreground">
{projects && projects.length > 0
? "Select a project to view settings"
: "No projects added yet"}
</p>
</div>
)}
</div>
</div>
)
}
// Keep legacy export for backward compatibility
export const AgentsProjectWorktreeTab = AgentsProjectsTab