BranchSelector.tsx
| 1 | interface BranchSelectorProps { |
| 2 | repoName: string; |
| 3 | branches: string[]; |
| 4 | currentRef: string; |
| 5 | view: "tree" | "commits" | "blob"; |
| 6 | /** subpath for tree, full file path for blob */ |
| 7 | path?: string; |
| 8 | } |
| 9 | |
| 10 | export function BranchSelector({ |
| 11 | repoName, |
| 12 | branches, |
| 13 | currentRef, |
| 14 | view, |
| 15 | path, |
| 16 | }: BranchSelectorProps) { |
| 17 | if (branches.length === 0) return ""; |
| 18 | const isDetached = !branches.includes(currentRef); |
| 19 | const shortRef = |
| 20 | isDetached && currentRef.length > 8 |
| 21 | ? currentRef.slice(0, 8) |
| 22 | : currentRef; |
| 23 | return ( |
| 24 | <form |
| 25 | method="GET" |
| 26 | action={`/${repoName}/branch-switch`} |
| 27 | class="branch-selector" |
| 28 | > |
| 29 | <input type="hidden" name="view" value={view} /> |
| 30 | {path && <input type="hidden" name="path" value={path} />} |
| 31 | <span class="branch-selector-icon">⎇</span> |
| 32 | <select |
| 33 | name="rev" |
| 34 | class="branch-select" |
| 35 | onchange="this.form.submit()" |
| 36 | > |
| 37 | {isDetached && ( |
| 38 | <option value={currentRef} selected> |
| 39 | {shortRef} (detached) |
| 40 | </option> |
| 41 | )} |
| 42 | {branches.map((b) => ( |
| 43 | <option |
| 44 | value={b} |
| 45 | selected={b === currentRef ? true : undefined} |
| 46 | > |
| 47 | {b} |
| 48 | </option> |
| 49 | ))} |
| 50 | </select> |
| 51 | <noscript> |
| 52 | <button type="submit" class="btn btn-sm"> |
| 53 | Go |
| 54 | </button> |
| 55 | </noscript> |
| 56 | </form> |
| 57 | ); |
| 58 | } |
| 59 |