Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -13,14 +14,16 @@ export const ResponsiveRepoDisplay: React.FC<ResponsiveRepoDisplayProps> = ({
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
// Reserve space for margins, mode indicator, context percentage, and other UI elements
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) {
Expand Down
32 changes: 32 additions & 0 deletions extensions/cli/src/ui/hooks/useGitBranch.ts
Original file line number Diff line number Diff line change
@@ -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<string | null>(() => 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;
}
Loading