Files
Gildas GarciaandIvan Vasilov 96d43099bb chore: refactor Button API so that it can be used a standard button (#46880)
## Problem

Our `<Button>` component breaks the default `button` contract by
redefining the `type` prop to set its variant (`primary`, `default`,
etc) instead of the button type (`submit`, `button`, etc).
This is confusing and forces to write more code when using it with
shadcn components that expect/inject the standard button props.

## Solution

- rename the `type` prop to `variant`
- rename the `htmlType` prop to `type`
- propagate the changes where necessary
- format code

## How to test

As this is just prop renaming, if it builds it's ok

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2026-06-16 23:59:58 +02:00

106 lines
3.4 KiB
TypeScript

import { Check, GitMerge, Shield } from 'lucide-react'
import { useState } from 'react'
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
Popover,
PopoverContent,
PopoverTrigger,
ScrollArea,
} from 'ui'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { Branch } from '@/data/branches/branches-query'
interface BranchSelectorProps {
branches: Branch[]
onBranchSelected?: (branch: Branch) => void
disabled?: boolean
isUpdating?: boolean
variant?: 'primary' | 'outline'
align?: 'end' | 'center'
}
export const BranchSelector = ({
branches,
onBranchSelected,
disabled = false,
isUpdating = false,
variant = 'primary',
align = 'end',
}: BranchSelectorProps) => {
const [open, setOpen] = useState(false)
const [selectedBranch, setSelectedBranch] = useState<Branch | null>(null)
const availableBranches = branches.filter((branch) => !branch.is_default)
const handleBranchSelect = (branch: Branch) => {
setSelectedBranch(branch)
setOpen(false)
onBranchSelected?.(branch)
}
return (
<Popover open={open} onOpenChange={setOpen} modal={false}>
<PopoverTrigger asChild>
<ButtonTooltip
icon={<GitMerge size={14} strokeWidth={1.5} />}
variant={variant}
disabled={
disabled || isUpdating || branches.length === 0 || availableBranches.length === 0
}
tooltip={{
content: {
side: 'bottom',
align,
text:
branches.length === 0 || availableBranches.length === 0
? 'Create a branch first to start a merge request'
: undefined,
},
}}
>
{isUpdating ? 'Creating...' : 'New merge request'}
</ButtonTooltip>
</PopoverTrigger>
<PopoverContent className="p-0 w-80" side="bottom" align="end">
<Command>
<CommandInput placeholder="Find branch to review..." />
<CommandList>
<CommandEmpty>No available branches found</CommandEmpty>
<CommandGroup>
<ScrollArea className="max-h-[210px] overflow-y-auto">
{availableBranches.map((branch) => (
<CommandItem
key={branch.id}
value={branch.name.replaceAll('"', '')}
className="cursor-pointer w-full flex items-center justify-between"
onSelect={() => handleBranchSelect(branch)}
disabled={isUpdating || !!branch.git_branch || !!branch.review_requested_at}
>
<div className="flex items-center gap-2">
{branch.is_default && <Shield size={14} className="text-amber-900" />}
<span className="truncate" title={branch.name}>
{branch.name}
</span>
</div>
{selectedBranch?.id === branch.id && (
<Check size={14} strokeWidth={1.5} className="text-brand" />
)}
{branch.git_branch && <span>Synced to a Git branch</span>}
{branch.review_requested_at && <span>Merge request opened</span>}
</CommandItem>
))}
</ScrollArea>
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)
}