diff --git a/extensions/cli/src/ui/components/ResponsiveRepoDisplay.tsx b/extensions/cli/src/ui/components/ResponsiveRepoDisplay.tsx index 2a3417179cf..7954a969d7a 100644 --- a/extensions/cli/src/ui/components/ResponsiveRepoDisplay.tsx +++ b/extensions/cli/src/ui/components/ResponsiveRepoDisplay.tsx @@ -1,6 +1,7 @@ import { Text } from "ink"; import React, { useMemo } from "react"; +import { useGitBranch } from "../hooks/useGitBranch.js"; import { useTerminalSize } from "../hooks/useTerminalSize.js"; import { getResponsiveRepoText } from "../hooks/useTUIChatHooks.js"; @@ -13,6 +14,8 @@ export const ResponsiveRepoDisplay: React.FC = ({ remoteUrl, }) => { const { columns } = useTerminalSize(); + // Watch for git branch changes with 1 second polling interval + const currentBranch = useGitBranch(1000); const repoText = useMemo(() => { // Calculate available width for repo display @@ -20,7 +23,7 @@ export const ResponsiveRepoDisplay: React.FC = ({ const availableWidth = Math.floor(columns / 2); return getResponsiveRepoText(remoteUrl, availableWidth); - }, [remoteUrl, columns]); + }, [remoteUrl, columns, currentBranch]); // Don't render if no text to show if (!repoText) { diff --git a/extensions/cli/src/ui/hooks/useGitBranch.ts b/extensions/cli/src/ui/hooks/useGitBranch.ts new file mode 100644 index 00000000000..ac4174d5584 --- /dev/null +++ b/extensions/cli/src/ui/hooks/useGitBranch.ts @@ -0,0 +1,32 @@ +import { useEffect, useState } from "react"; + +import { getGitBranch } from "../../util/git.js"; + +/** + * Custom hook to watch for git branch changes + * @param pollInterval Interval in milliseconds to check for branch changes (default: 1000ms) + * @returns The current git branch name or null if not in a git repo + */ +export function useGitBranch(pollInterval: number = 1000): string | null { + const [branch, setBranch] = useState(() => getGitBranch()); + + useEffect(() => { + // Poll for branch changes + const intervalId = setInterval(() => { + const currentBranch = getGitBranch(); + setBranch((prevBranch) => { + // Only update if branch has changed to avoid unnecessary re-renders + if (currentBranch !== prevBranch) { + return currentBranch; + } + return prevBranch; + }); + }, pollInterval); + + return () => { + clearInterval(intervalId); + }; + }, [pollInterval]); + + return branch; +}