Files
Gildas Garcia 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

85 lines
2.6 KiB
TypeScript

import { useParams } from 'common'
import { GitMerge } from 'lucide-react'
import { useRouter } from 'next/router'
import { toast } from 'sonner'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { useBranchUpdateMutation } from '@/data/branches/branch-update-mutation'
import { useBranchesQuery } from '@/data/branches/branches-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useTrack } from '@/lib/telemetry/track'
export const MergeRequestButton = () => {
const { ref } = useParams()
const router = useRouter()
const { data: projectDetails } = useSelectedProjectQuery()
const track = useTrack()
const projectRef = projectDetails?.parent_project_ref || ref
const { data: branches } = useBranchesQuery({ projectRef }, { enabled: Boolean(projectDetails) })
const { mutate: updateBranch, isPending: isUpdating } = useBranchUpdateMutation({
onError: () => {
toast.error(`Failed to open merge request`)
},
})
const selectedBranch = branches?.find((branch) => branch.project_ref === ref)
if (!projectRef || !selectedBranch || selectedBranch.is_default || selectedBranch.git_branch)
return null
const hasReviewRequested = !!selectedBranch.review_requested_at
const buttonLabel = hasReviewRequested ? 'Review merge request' : 'Open merge request'
const handleClick = () => {
track('header_merge_request_button_clicked', { hasReviewRequested })
if (hasReviewRequested) {
router.push(`/project/${selectedBranch.project_ref}/merge`)
} else {
updateBranch(
{
branchRef: selectedBranch.project_ref,
projectRef,
requestReview: true,
},
{
onSuccess: () => {
toast.success('Merge request created')
router.push(`/project/${selectedBranch.project_ref}/merge`)
track('branch_create_merge_request_button_clicked', {
branchType: selectedBranch.persistent ? 'persistent' : 'preview',
origin: 'header',
})
},
}
)
}
}
return (
<ButtonTooltip
variant="default"
className="rounded-full w-[26px] h-[26px]"
onClick={handleClick}
loading={isUpdating}
tooltip={{
content: {
text: buttonLabel,
side: 'bottom',
align: 'center',
},
}}
icon={
<div className="relative">
{hasReviewRequested && (
<span className="w-1 h-1 absolute top-0 right-0 rounded-full bg-brand" />
)}
<GitMerge size={16} strokeWidth={1.5} />
</div>
}
/>
)
}