|
1 | 1 | import { useState } from "react"; |
2 | 2 | import { useTheme } from "../../../Contexts/ThemeProvider"; |
3 | | -// import type { Branch } from "./GitTypes"; |
4 | | -import { ChevronDown, GitBranch } from "lucide-react"; |
| 3 | +import { ChevronDown, GitBranch, Plus } from "lucide-react"; |
5 | 4 | import { useEditorCollaboration } from "../../../Contexts/EditorContext"; |
6 | | - |
7 | | -// interface BranchSelectorProps { |
8 | | -// branches: Branch[]; |
9 | | -// onBranchSelect: (branchName: string) => void; |
10 | | -// } |
| 5 | +import CreateBranch from "./CreateBranch"; |
11 | 6 |
|
12 | 7 | const BranchSelector = () => { |
13 | 8 | const { theme } = useTheme(); |
14 | 9 | const [isOpen, setIsOpen] = useState(false); |
15 | | - const { codespace, activeSessionIndex, setActiveSessionIndex } = |
16 | | - useEditorCollaboration(); |
| 10 | + const [showCreateBranch, setShowCreateBranch] = useState(false); |
| 11 | + const { |
| 12 | + codespace, |
| 13 | + activeSessionIndex, |
| 14 | + setActiveSessionIndex, |
| 15 | + createBranchWithSession, |
| 16 | + } = useEditorCollaboration(); |
17 | 17 |
|
18 | | - // Get the current active branch |
19 | | - // const activeBranch = branches.find((b) => b.isActive)?.name || "main"; |
20 | 18 | const branchSessions = codespace?.sessions || []; |
21 | | - |
22 | 19 | const activeBranch = branchSessions[activeSessionIndex].name || "main"; |
23 | 20 |
|
24 | 21 | const toggleDropdown = () => setIsOpen(!isOpen); |
25 | 22 |
|
26 | | - // const handleBranchSelect = (branchName: string) => { |
27 | | - // onBranchSelect(branchName); |
28 | | - // setIsOpen(false); |
29 | | - // }; |
| 23 | + const handleCreateBranch = async (branchName: string) => { |
| 24 | + setShowCreateBranch(false); |
| 25 | + createBranchWithSession(branchName); |
| 26 | + }; |
30 | 27 |
|
31 | | - return ( |
32 | | - <div className="relative"> |
33 | | - <button |
34 | | - className={`flex items-center justify-between w-full px-3 py-2 text-sm font-medium ${theme.surface} ${theme.border} border ${theme.text} ${theme.hover} rounded-md`} |
35 | | - onClick={toggleDropdown} |
36 | | - aria-haspopup="true" |
37 | | - aria-expanded={isOpen} |
38 | | - > |
39 | | - <span className="flex items-center"> |
40 | | - <GitBranch className="w-4 h-4 mr-2" /> |
41 | | - {activeBranch} |
42 | | - </span> |
43 | | - <ChevronDown className="w-4 h-4" /> |
44 | | - </button> |
| 28 | + if (showCreateBranch) { |
| 29 | + return ( |
| 30 | + <CreateBranch |
| 31 | + onCreateBranch={handleCreateBranch} |
| 32 | + onCancel={() => setShowCreateBranch(false)} |
| 33 | + /> |
| 34 | + ); |
| 35 | + } |
45 | 36 |
|
46 | | - {isOpen && ( |
47 | | - <div |
48 | | - className={`absolute z-10 w-full mt-1 overflow-auto rounded-md shadow-lg ${theme.surface} ${theme.border} border max-h-60`} |
| 37 | + return ( |
| 38 | + <div className="space-y-2"> |
| 39 | + <div className="relative"> |
| 40 | + <button |
| 41 | + className={`flex items-center justify-between w-full px-3 py-2 text-sm font-medium ${theme.surface} ${theme.border} border ${theme.text} ${theme.hover} rounded-md`} |
| 42 | + onClick={toggleDropdown} |
| 43 | + aria-haspopup="true" |
| 44 | + aria-expanded={isOpen} |
49 | 45 | > |
50 | | - <ul className="py-1 text-sm" role="menu" aria-orientation="vertical"> |
51 | | - {branchSessions.map((branch, index) => ( |
52 | | - <li key={branch.branchId}> |
| 46 | + <span className="flex items-center"> |
| 47 | + <GitBranch className="w-4 h-4 mr-2" /> |
| 48 | + {activeBranch} |
| 49 | + </span> |
| 50 | + <ChevronDown className="w-4 h-4" /> |
| 51 | + </button> |
| 52 | + |
| 53 | + {isOpen && ( |
| 54 | + <div |
| 55 | + className={`absolute z-10 w-full mt-1 overflow-auto rounded-md shadow-lg ${theme.surface} ${theme.border} border max-h-60`} |
| 56 | + > |
| 57 | + <ul |
| 58 | + className="py-1 text-sm" |
| 59 | + role="menu" |
| 60 | + aria-orientation="vertical" |
| 61 | + > |
| 62 | + {branchSessions.map((branch, index) => { |
| 63 | + return ( |
| 64 | + <li key={branch.branchId}> |
| 65 | + <button |
| 66 | + className={`block w-full text-left px-4 py-2 ${ |
| 67 | + theme.text |
| 68 | + } ${index === activeSessionIndex ? theme.active : ""} ${ |
| 69 | + theme.hover |
| 70 | + }`} |
| 71 | + role="menuitem" |
| 72 | + onClick={() => setActiveSessionIndex(index)} |
| 73 | + > |
| 74 | + {branch.name} |
| 75 | + </button> |
| 76 | + </li> |
| 77 | + ); |
| 78 | + })} |
| 79 | + <li className={`border-t ${theme.border}`}> |
53 | 80 | <button |
54 | | - className={`block w-full text-left px-4 py-2 ${theme.text} ${ |
55 | | - index === activeSessionIndex ? theme.active : "" |
56 | | - } ${theme.hover}`} |
57 | | - role="menuitem" |
58 | | - // onClick={() => handleBranchSelect(branch.name)} |
59 | | - onClick={() => setActiveSessionIndex(index)} |
| 81 | + className={`flex items-center w-full text-left px-4 py-2 ${theme.text} ${theme.hover}`} |
| 82 | + onClick={() => { |
| 83 | + setShowCreateBranch(true); |
| 84 | + setIsOpen(false); |
| 85 | + }} |
60 | 86 | > |
61 | | - {branch.name} |
| 87 | + <Plus className="w-4 h-4 mr-2" /> |
| 88 | + Create new branch |
62 | 89 | </button> |
63 | 90 | </li> |
64 | | - ))} |
65 | | - </ul> |
66 | | - </div> |
67 | | - )} |
| 91 | + </ul> |
| 92 | + </div> |
| 93 | + )} |
| 94 | + </div> |
68 | 95 | </div> |
69 | 96 | ); |
70 | 97 | }; |
|
0 commit comments