Skip to content

Commit cdbae45

Browse files
authored
Refactor: Extract branch loading logic into shared function (#15)
* 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. * Simplify loadAndPopulateBranches to use module-level variable Remove selectElement parameter and use the module-level parentBranchSelect variable directly, simplifying the function signature.
1 parent 7998e03 commit cdbae45

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

renderer.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,28 @@ 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+
selectedBranch?: string
222+
): Promise<void> {
223+
const branches = await ipcRenderer.invoke("get-branches", directory);
224+
parentBranchSelect.innerHTML = "";
225+
226+
if (branches.length === 0) {
227+
parentBranchSelect.innerHTML = '<option value="">No git repository found</option>';
228+
} else {
229+
branches.forEach((branch: string) => {
230+
const option = document.createElement("option");
231+
option.value = branch;
232+
option.textContent = branch;
233+
if (branch === selectedBranch) {
234+
option.selected = true;
235+
}
236+
parentBranchSelect.appendChild(option);
237+
});
238+
}
239+
}
240+
219241
function createTerminalUI(sessionId: string) {
220242
const themeColors = THEME_PRESETS[terminalSettings.theme] || THEME_PRESETS["macos-dark"];
221243

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

807829
// 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-
}
830+
await loadAndPopulateBranches(lastSettings.projectDir, lastSettings.parentBranch);
824831
}
825832

826833
// Set last used coding agent
@@ -855,19 +862,7 @@ browseDirBtn?.addEventListener("click", async () => {
855862
projectDirInput.value = dir;
856863

857864
// 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-
}
865+
await loadAndPopulateBranches(dir);
871866
}
872867
});
873868

0 commit comments

Comments
 (0)