import { useQuery, useQueryClient } from '@tanstack/react-query' import { Link } from '@tanstack/react-router' import { useParams } from 'common' import { partition } from 'lodash' import { ClockIcon, ExternalLinkIcon, InfinityIcon, MoreVerticalIcon, PencilIcon, RedoIcon, RefreshCwIcon, ShieldIcon, Trash2Icon, } from 'lucide-react' import { useState } from 'react' import { toast } from 'sonner' import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from 'ui' import { ConfirmationModal } from 'ui-patterns/Dialogs/ConfirmationModal' import { BranchingEmptyState } from './BranchingEmptyState' import { BranchLoader, BranchManagementSection, BranchRow, BranchRowLoader } from './BranchPanels' import { EditBranchModal } from './EditBranchModal' import { PreviewBranchesEmptyState } from './EmptyStates' import { SwitchToPreviewModal } from './SwitchToPreviewModal' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { DropdownMenuItemTooltip } from '@/components/ui/DropdownMenuItemTooltip' import { TextConfirmModal } from '@/components/ui/TextConfirmModalWrapper' import { useBranchPushMutation } from '@/data/branches/branch-push-mutation' import { branchQueryOptions } from '@/data/branches/branch-query' import { useBranchResetMutation } from '@/data/branches/branch-reset-mutation' import { useBranchRestoreMutation } from '@/data/branches/branch-restore-mutation' import { useBranchUpdateMutation } from '@/data/branches/branch-update-mutation' import type { Branch } from '@/data/branches/branches-query' import { branchKeys } from '@/data/branches/keys' import { useBranchGrant } from '@/hooks/branches/useBranchGrant' import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { IS_PLATFORM } from '@/lib/constants' interface OverviewProps { isGithubConnected: boolean isLoading: boolean isSuccess: boolean repo: string mainBranch: Branch previewBranches: Branch[] onSelectCreateBranch: () => void onSelectDeleteBranch: (branch: Branch) => void generateCreatePullRequestURL: (branchName?: string) => string } export const Overview = ({ isGithubConnected, isLoading, isSuccess, repo, mainBranch, previewBranches, onSelectCreateBranch, onSelectDeleteBranch, generateCreatePullRequestURL, }: OverviewProps) => { const [scheduledForDeletionBranches, aliveBranches] = partition( previewBranches, (branch) => branch.deletion_scheduled_at !== undefined ) const [persistentBranches, ephemeralBranches] = partition( aliveBranches, (branch) => branch.persistent ) const { ref: projectRef } = useParams() const { data: selectedOrg } = useSelectedOrganizationQuery() const { hasAccess: hasAccessToPersistentBranching, isLoading: isLoadingEntitlement } = useCheckEntitlements('branching_persistent') const showEmptyState = isSuccess && previewBranches.length === 0 return ( <> {isLoading && } {isSuccess && mainBranch !== undefined && ( {mainBranch.name} } repo={repo} rowActions={} /> )} {isSuccess && mainBranch === undefined && (
main
)}
{showEmptyState ? ( ) : ( <> {/* Persistent Branches Section */} {(isLoading || isLoadingEntitlement) && } {isSuccess && !isLoadingEntitlement && !hasAccessToPersistentBranching && IS_PLATFORM && persistentBranches.length === 0 && (

Upgrade to unlock persistent branches

Persistent branches are long-lived, cannot be reset, and are ideal for staging environments.

)} {isSuccess && !isLoadingEntitlement && hasAccessToPersistentBranching && persistentBranches.length === 0 && (

No persistent branches

Persistent branches are long-lived, cannot be reset, and are ideal for staging environments.

)} {isSuccess && !isLoadingEntitlement && persistentBranches.map((branch) => { return ( onSelectDeleteBranch(branch)} generateCreatePullRequestURL={generateCreatePullRequestURL} /> } /> ) })}
{/* Ephemeral/Preview Branches Section */} {isLoading && } {isSuccess && ephemeralBranches.length === 0 && ( )} {isSuccess && ephemeralBranches.map((branch) => { return ( onSelectDeleteBranch(branch)} generateCreatePullRequestURL={generateCreatePullRequestURL} /> } /> ) })} {/* Scheduled for deletion branches section */} {isLoading && } {isSuccess && scheduledForDeletionBranches.length === 0 && (

No branches scheduled for deletion

)} {isSuccess && scheduledForDeletionBranches.map((branch) => { return ( onSelectDeleteBranch(branch)} generateCreatePullRequestURL={generateCreatePullRequestURL} /> } /> ) })}
)} ) } // Row actions for preview branches (non-main) const PreviewBranchActions = ({ branch, onSelectDeleteBranch, generateCreatePullRequestURL, }: { branch: Branch repo: string onSelectDeleteBranch: () => void generateCreatePullRequestURL: (branchName?: string) => string }) => { const queryClient = useQueryClient() const { project_ref: branchRef, parent_project_ref: projectRef } = branch const branchGrant = useBranchGrant('GET /v1/branches/{branch_id_or_ref}', { projectRef, isDefault: branch.is_default, }) const updateBranchGrant = useBranchGrant('PATCH /v1/branches/{branch_id_or_ref}', { projectRef, isDefault: branch.is_default, }) const pushBranchGrant = useBranchGrant('POST /v1/branches/{branch_id_or_ref}/push', { projectRef, isDefault: branch.is_default, }) const resetBranchGrant = useBranchGrant('POST /v1/branches/{branch_id_or_ref}/reset', { projectRef, isDefault: branch.is_default, }) const restoreBranchGrant = useBranchGrant('POST /v1/branches/{branch_id_or_ref}/restore', { projectRef, isDefault: branch.is_default, }) const deleteBranchGrant = useBranchGrant('DELETE /v1/branches/{branch_id_or_ref}', { projectRef, isDefault: branch.is_default, }) const canUpdateBranches = updateBranchGrant.status === 'granted' const canPushBranches = pushBranchGrant.status === 'granted' const canResetBranches = resetBranchGrant.status === 'granted' const canRestoreBranches = restoreBranchGrant.status === 'granted' const canDeleteBranches = deleteBranchGrant.status === 'granted' const { data } = useQuery(branchQueryOptions({ projectRef, branchRef, grant: branchGrant.grant })) const isBranchActiveHealthy = data?.status === 'ACTIVE_HEALTHY' const isPersistentBranch = branch.persistent const { hasAccess: hasAccessToPersistentBranching } = useCheckEntitlements('branching_persistent') const [showConfirmResetModal, setShowConfirmResetModal] = useState(false) const [showBranchModeSwitch, setShowBranchModeSwitch] = useState(false) const [ showPersistentBranchDeleteConfirmationModal, setShowPersistentBranchDeleteConfirmationModal, ] = useState(false) const [showEditBranchModal, setShowEditBranchModal] = useState(false) const [showConfirmRetriggersModal, setShowConfirmRetriggersModal] = useState(false) const { mutate: resetBranch, isPending: isResetting } = useBranchResetMutation({ onSuccess() { toast.success('Success! Please allow a few seconds for the branch to reset.') setShowConfirmResetModal(false) }, }) const { mutate: updateBranch, isPending: isUpdatingBranch } = useBranchUpdateMutation({ onSuccess() { toast.success('Successfully updated branch') setShowBranchModeSwitch(false) if (projectRef) { queryClient.invalidateQueries({ queryKey: branchKeys.list(projectRef) }) } }, }) const { mutate: restoreBranch } = useBranchRestoreMutation({ onSuccess() { toast.success('Success! Please allow a few minutes for the branch to restore.') setShowBranchModeSwitch(false) }, }) const { mutate: branchPushMutate, isPending: isRetriggering } = useBranchPushMutation({ onSuccess() { toast.success('Success! Please allow a few minutes for the branch to update.') setShowConfirmRetriggersModal(false) }, onError: (data) => { toast.error(`Failed to trigger workflow: ${data.message}`) }, }) const onRetriggerBranch = () => { if (pushBranchGrant.status !== 'granted') return branchPushMutate({ grant: pushBranchGrant.grant, branchRef, projectRef }) } const onRestoreBranch = () => { if (restoreBranchGrant.status !== 'granted') return restoreBranch({ grant: restoreBranchGrant.grant, branchRef, projectRef }) } const onConfirmReset = () => { if (resetBranchGrant.status !== 'granted') return resetBranch({ grant: resetBranchGrant.grant, branchRef, projectRef }) } const onTogglePersistent = () => { if (updateBranchGrant.status !== 'granted') return updateBranch({ grant: updateBranchGrant.grant, branchRef, projectRef, persistent: !branch.persistent, }) } const getResetDisabledReason = () => { if (!canResetBranches) return 'You need additional permissions to reset branches' if (!isBranchActiveHealthy) { return 'Branch is still initializing. Please wait for it to become healthy before resetting.' } return undefined } const getModeSwitchDisabledReason = () => { if (!canUpdateBranches) return 'You need additional permissions to edit branches' if (!isBranchActiveHealthy) { return 'Branch is still initializing. Please wait for it to become healthy before switching.' } if (!branch.persistent && !hasAccessToPersistentBranching) { return 'Upgrade your plan to access persistent branches' } return undefined } const onDeleteBranch = (e: Event | React.MouseEvent) => { if (isPersistentBranch) { setShowPersistentBranchDeleteConfirmationModal(true) } else { e.stopPropagation() onSelectDeleteBranch() } } return ( <>