diff --git a/frontend/src/components/settings/git/modal/ViewBranches.jsx b/frontend/src/components/settings/git/modal/ViewBranches.jsx index 2947eec..76a2922 100644 --- a/frontend/src/components/settings/git/modal/ViewBranches.jsx +++ b/frontend/src/components/settings/git/modal/ViewBranches.jsx @@ -14,7 +14,8 @@ import { ArrowRightCircle, Loader, CloudUpload, - Search + Search, + Check } from 'lucide-react'; import Tooltip from '../../../ui/Tooltip'; import Alert from '../../../ui/Alert'; @@ -33,8 +34,8 @@ const SettingsBranchModal = ({ const [branchOffMode, setBranchOffMode] = useState(null); const [newBranchName, setNewBranchName] = useState(''); const [validBranchName, setValidBranchName] = useState(true); - const [branchToDelete, setBranchToDelete] = useState(null); const [loadingAction, setLoadingAction] = useState(''); + const [confirmAction, setConfirmAction] = useState(null); useEffect(() => { if (isOpen) { @@ -68,39 +69,9 @@ const SettingsBranchModal = ({ setBranchOffMode(null); setNewBranchName(''); setValidBranchName(true); - setBranchToDelete(null); setLoadingAction(''); setSearchTerm(''); - }; - - const handleCheckout = async branchName => { - setLoadingAction(`checkout-${branchName}`); - try { - const response = await checkoutBranch(branchName); - if (response.success) { - await fetchBranches(); - onBranchChange(); - Alert.success('Branch checked out successfully'); - onClose(); - } else { - Alert.error(response.error); - } - } catch (error) { - if ( - error.response && - error.response.status === 400 && - error.response.data.error - ) { - Alert.error(error.response.data.error); - } else { - Alert.error( - 'An unexpected error occurred while checking out the branch.' - ); - console.error('Error checking out branch:', error); - } - } finally { - setLoadingAction(''); - } + setConfirmAction(null); }; const handleBranchOff = async () => { @@ -157,57 +128,97 @@ const SettingsBranchModal = ({ window.open(branchUrl, '_blank'); }; - const confirmDeleteBranch = branchName => { - setBranchToDelete(branchName); + const handleCheckout = async branchName => { + if (confirmAction === `checkout-${branchName}`) { + setLoadingAction(`checkout-${branchName}`); + try { + const response = await checkoutBranch(branchName); + if (response.success) { + await fetchBranches(); + onBranchChange(); + Alert.success('Branch checked out successfully'); + onClose(); + } else { + Alert.error(response.error); + } + } catch (error) { + if ( + error.response && + error.response.status === 400 && + error.response.data.error + ) { + Alert.error(error.response.data.error); + } else { + Alert.error( + 'An unexpected error occurred while checking out the branch.' + ); + console.error('Error checking out branch:', error); + } + } finally { + setLoadingAction(''); + setConfirmAction(null); + } + } else { + setConfirmAction(`checkout-${branchName}`); + } }; - const handleDeleteBranch = async () => { - if (branchToDelete && branchToDelete.toLowerCase() === 'main') { + const handleDeleteBranch = async branchName => { + if (branchName.toLowerCase() === 'main') { Alert.warning("The 'main' branch cannot be deleted."); return; } - setLoadingAction(`delete-${branchToDelete}`); - try { - const response = await deleteBranch(branchToDelete); - if (response.success) { - onBranchChange(); - await fetchBranches(); - Alert.success( - `Branch '${branchToDelete}' deleted successfully` + if (confirmAction === `delete-${branchName}`) { + setLoadingAction(`delete-${branchName}`); + try { + const response = await deleteBranch(branchName); + if (response.success) { + onBranchChange(); + await fetchBranches(); + Alert.success( + `Branch '${branchName}' deleted successfully` + ); + } else { + Alert.error(response.error); + } + } catch (error) { + Alert.error( + 'An unexpected error occurred while deleting the branch.' ); - setBranchToDelete(null); - } else { - Alert.error(response.error); + console.error('Error deleting branch:', error); + } finally { + setLoadingAction(''); + setConfirmAction(null); } - } catch (error) { - Alert.error( - 'An unexpected error occurred while deleting the branch.' - ); - console.error('Error deleting branch:', error); - } finally { - setLoadingAction(''); + } else { + setConfirmAction(`delete-${branchName}`); } }; const handlePushToRemote = async branchName => { - setLoadingAction(`push-${branchName}`); - try { - const response = await pushBranchToRemote(branchName); - if (response.success) { - Alert.success( - `Branch '${branchName}' pushed to remote successfully` + if (confirmAction === `push-${branchName}`) { + setLoadingAction(`push-${branchName}`); + try { + const response = await pushBranchToRemote(branchName); + if (response.success) { + Alert.success( + `Branch '${branchName}' pushed to remote successfully` + ); + await fetchBranches(); + } else { + Alert.error(response.error); + } + } catch (error) { + Alert.error( + 'An unexpected error occurred while pushing the branch to remote.' ); - await fetchBranches(); - } else { - Alert.error(response.error); + console.error('Error pushing branch to remote:', error); + } finally { + setLoadingAction(''); + setConfirmAction(null); } - } catch (error) { - Alert.error( - 'An unexpected error occurred while pushing the branch to remote.' - ); - console.error('Error pushing branch to remote:', error); - } finally { - setLoadingAction(''); + } else { + setConfirmAction(`push-${branchName}`); } }; @@ -265,7 +276,13 @@ const SettingsBranchModal = ({
{/* Keep existing buttons with updated styles */} {branch.name !== currentBranch && ( - +
)} - - {branchToDelete && ( -
-

- Are you sure you want to delete the branch{' '} - {branchToDelete}? This action - cannot be undone. -

-
- - -
-
- )} );