Skip to content

Commit a17e094

Browse files
committed
Refactor directory selection to extract branch loading logic
Extract duplicated git branch loading and population logic into a shared loadAndPopulateBranches function to reduce code duplication and improve maintainability.
1 parent 7998e03 commit a17e094

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

renderer.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,29 @@ let terminalSettings: TerminalSettings = { ...DEFAULT_SETTINGS };
216216
// Track activity timers for each session
217217
const activityTimers = new Map<string, NodeJS.Timeout>();
218218

219+
async function loadAndPopulateBranches(
220+
directory: string,
221+
selectElement: HTMLSelectElement,
222+
selectedBranch?: string
223+
): Promise<void> {
224+
const branches = await ipcRenderer.invoke("get-branches", directory);
225+
selectElement.innerHTML = "";
226+
227+
if (branches.length === 0) {
228+
selectElement.innerHTML = '<option value="">No git repository found</option>';
229+
} else {
230+
branches.forEach((branch: string) => {
231+
const option = document.createElement("option");
232+
option.value = branch;
233+
option.textContent = branch;
234+
if (branch === selectedBranch) {
235+
option.selected = true;
236+
}
237+
selectElement.appendChild(option);
238+
});
239+
}
240+
}
241+
219242
function createTerminalUI(sessionId: string) {
220243
const themeColors = THEME_PRESETS[terminalSettings.theme] || THEME_PRESETS["macos-dark"];
221244

@@ -805,22 +828,7 @@ document.getElementById("new-session")?.addEventListener("click", async () => {
805828
projectDirInput.value = lastSettings.projectDir;
806829

807830
// Load git branches for the last directory
808-
const branches = await ipcRenderer.invoke("get-branches", lastSettings.projectDir);
809-
parentBranchSelect.innerHTML = "";
810-
811-
if (branches.length === 0) {
812-
parentBranchSelect.innerHTML = '<option value="">No git repository found</option>';
813-
} else {
814-
branches.forEach((branch: string) => {
815-
const option = document.createElement("option");
816-
option.value = branch;
817-
option.textContent = branch;
818-
if (branch === lastSettings.parentBranch) {
819-
option.selected = true;
820-
}
821-
parentBranchSelect.appendChild(option);
822-
});
823-
}
831+
await loadAndPopulateBranches(lastSettings.projectDir, parentBranchSelect, lastSettings.parentBranch);
824832
}
825833

826834
// Set last used coding agent
@@ -855,19 +863,7 @@ browseDirBtn?.addEventListener("click", async () => {
855863
projectDirInput.value = dir;
856864

857865
// Load git branches
858-
const branches = await ipcRenderer.invoke("get-branches", dir);
859-
parentBranchSelect.innerHTML = "";
860-
861-
if (branches.length === 0) {
862-
parentBranchSelect.innerHTML = '<option value="">No git repository found</option>';
863-
} else {
864-
branches.forEach((branch: string) => {
865-
const option = document.createElement("option");
866-
option.value = branch;
867-
option.textContent = branch;
868-
parentBranchSelect.appendChild(option);
869-
});
870-
}
866+
await loadAndPopulateBranches(dir, parentBranchSelect);
871867
}
872868
});
873869

0 commit comments

Comments
 (0)