diff --git a/frontend/src/components/settings/SettingsBranchModal.jsx b/frontend/src/components/settings/SettingsBranchModal.jsx index 494be01..36b6a94 100644 --- a/frontend/src/components/settings/SettingsBranchModal.jsx +++ b/frontend/src/components/settings/SettingsBranchModal.jsx @@ -1,17 +1,34 @@ -import React, { useState, useEffect } from 'react'; -import Modal from '../ui/Modal'; -import { getBranches, checkoutBranch, createBranch, deleteBranch } from '../../api/api'; -import { ExternalLink, Trash2, GitBranchPlus, ArrowRightCircle, Loader } from 'lucide-react'; -import Tooltip from '../ui/Tooltip'; -import Alert from '../ui/Alert'; +import React, { useState, useEffect } from "react"; +import Modal from "../ui/Modal"; +import { + getBranches, + checkoutBranch, + createBranch, + deleteBranch, +} from "../../api/api"; +import { + ExternalLink, + Trash2, + GitBranchPlus, + ArrowRightCircle, + Loader, +} from "lucide-react"; +import Tooltip from "../ui/Tooltip"; +import Alert from "../ui/Alert"; -const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranchChange }) => { +const SettingsBranchModal = ({ + isOpen, + onClose, + repoUrl, + currentBranch, + onBranchChange, +}) => { const [branches, setBranches] = useState([]); const [branchOffMode, setBranchOffMode] = useState(null); - const [newBranchName, setNewBranchName] = useState(''); + const [newBranchName, setNewBranchName] = useState(""); const [validBranchName, setValidBranchName] = useState(true); const [branchToDelete, setBranchToDelete] = useState(null); - const [loadingAction, setLoadingAction] = useState(''); + const [loadingAction, setLoadingAction] = useState(""); useEffect(() => { if (isOpen) { @@ -26,19 +43,19 @@ const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranch if (response.success && response.data.branches) { setBranches(response.data.branches); } else { - console.error('Error fetching branches:', response.data.error); + console.error("Error fetching branches:", response.data.error); } } catch (error) { - console.error('Error fetching branches:', error); + console.error("Error fetching branches:", error); } }; const resetForm = () => { setBranchOffMode(null); - setNewBranchName(''); + setNewBranchName(""); setValidBranchName(true); setBranchToDelete(null); - setLoadingAction(''); + setLoadingAction(""); }; const handleCheckout = async (branchName, isNewBranch = false) => { @@ -49,56 +66,68 @@ const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranch // Refresh branches after successful checkout await fetchBranches(); // Notify parent component to update the status - onBranchChange(); // <-- Call the callback to update status in the parent component + onBranchChange(); // <-- Call the callback to update status in the parent component if (!isNewBranch) { - Alert.success('Branch checked out successfully'); + Alert.success("Branch checked out successfully"); } onClose(); // Close the modal } else { Alert.error(response.error); // Use the Alert component for error } } catch (error) { - if (error.response && error.response.status === 400 && error.response.data.error) { + if ( + error.response && + error.response.status === 400 && + error.response.data.error + ) { Alert.error(error.response.data.error); // Show alert with specific backend error message } else { - Alert.error('An unexpected error occurred while checking out the branch.'); - console.error('Error checking out branch:', error); + Alert.error( + "An unexpected error occurred while checking out the branch." + ); + console.error("Error checking out branch:", error); } } finally { - setLoadingAction(''); + setLoadingAction(""); } }; const handleBranchOff = async () => { - setLoadingAction('branchOff'); + setLoadingAction("branchOff"); if (newBranchName && validBranchName) { try { const response = await createBranch(newBranchName, branchOffMode); if (response.success) { // Checkout the new branch without showing the checkout alert await handleCheckout(newBranchName, true); - Alert.success('Branch created and checked out successfully'); + Alert.success("Branch created and checked out successfully"); } else { Alert.error(response.error); // Handle known errors from the backend } } catch (error) { - if (error.response && error.response.status === 400 && error.response.data.error) { + if ( + error.response && + error.response.status === 400 && + error.response.data.error + ) { Alert.error(error.response.data.error); // Specific error from the backend } else { - console.error('Error branching off:', error); // Log unexpected errors - Alert.error('An unexpected error occurred while creating the branch. Please try again.'); + console.error("Error branching off:", error); // Log unexpected errors + Alert.error( + "An unexpected error occurred while creating the branch. Please try again." + ); } } finally { - setLoadingAction(''); + setLoadingAction(""); } } else { - Alert.error('Please enter a valid branch name.'); + Alert.error("Please enter a valid branch name."); } }; const handleBranchOffClick = (branchName) => { setBranchOffMode(branchName); - setNewBranchName(''); + setNewBranchName(""); setValidBranchName(true); }; @@ -110,7 +139,7 @@ const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranch const handleOpenInGitHub = (branchName) => { const branchUrl = `${repoUrl}/tree/${encodeURIComponent(branchName)}`; - window.open(branchUrl, '_blank'); + window.open(branchUrl, "_blank"); }; const confirmDeleteBranch = (branchName) => { @@ -118,7 +147,7 @@ const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranch }; const handleDeleteBranch = async () => { - if (branchToDelete && branchToDelete.toLowerCase() === 'main') { + if (branchToDelete && branchToDelete.toLowerCase() === "main") { Alert.warning("The 'main' branch cannot be deleted."); return; } @@ -126,7 +155,7 @@ const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranch try { const response = await deleteBranch(branchToDelete); if (response.success) { - onBranchChange(); // <-- Call the callback to update status in the parent component + onBranchChange(); // <-- Call the callback to update status in the parent component await fetchBranches(); // Refresh the list after deletion Alert.success(`Branch '${branchToDelete}' deleted successfully`); setBranchToDelete(null); @@ -134,43 +163,60 @@ const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranch Alert.error(response.error); // Use the Alert component for error } } catch (error) { - Alert.error('An unexpected error occurred while deleting the branch.'); - console.error('Error deleting branch:', error); + Alert.error("An unexpected error occurred while deleting the branch."); + console.error("Error deleting branch:", error); } finally { - setLoadingAction(''); + setLoadingAction(""); } }; return ( -
-
-

Branches:

-
    +
    +
    +

    + Branches +

    +
      {branches.map((branch, index) => (
    • -
      - - {branch.name ? branch.name : 'Unknown Branch'} {/* Fallback if branch.name is undefined */} +
      +
      + + {branch.name || "Unknown Branch"}
      -
      +
      {branch.name !== currentBranch && ( @@ -178,37 +224,38 @@ const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranch - {branch.name !== currentBranch && branch.name.toLowerCase() !== 'main' && ( - - - - )} + {branch.name !== currentBranch && + branch.name.toLowerCase() !== "main" && ( + + + + )}
      @@ -217,37 +264,54 @@ const SettingsBranchModal = ({ isOpen, onClose, repoUrl, currentBranch, onBranch
    {branchOffMode && ( -
    - validateBranchName(e.target.value)} - placeholder={`New branch from ${branchOffMode}`} - className={`flex-grow p-2 border rounded bg-gray-800 text-gray-300 ${!validBranchName ? 'border-red-500' : ''}`} - /> - +
    +

    + Create New Branch +

    +
    + validateBranchName(e.target.value)} + placeholder={`New branch from ${branchOffMode}`} + className={`flex-grow p-2 border rounded bg-gray-800 text-gray-300 focus:ring-2 focus:ring-blue-500 transition-all ${ + !validBranchName ? "border-red-500" : "border-gray-600" + }`} + /> + +
    )} {branchToDelete && ( -
    -

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

    -
    +
    +

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

    +