Skip to content
Merged
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
Expand Up @@ -25,29 +25,26 @@ const FILE_CATEGORIES: Array<{
export function SidebarControl() {
const { isSidebarOpen, toggleSidebar } = useSidebarStore();

// Get active workspace for file opening
const { workspaceId } = useParams({ strict: false });
const { data: workspace } = electronTrpc.workspaces.get.useQuery(
{ id: workspaceId ?? "" },
{ enabled: !!workspaceId },
);
const worktreePath = workspace?.worktreePath;

// Get base branch for changes query
const { baseBranch, selectFile } = useChangesStore();
const { getBaseBranch, selectFile } = useChangesStore();
const baseBranch = getBaseBranch(worktreePath || "");
const { data: branchData } = electronTrpc.changes.getBranches.useQuery(
{ worktreePath: worktreePath || "" },
{ enabled: !!worktreePath && !isSidebarOpen },
);
const effectiveBaseBranch = baseBranch ?? branchData?.defaultBranch ?? "main";

// Get changes status - only query when sidebar is closed (we need it to open first file)
const { data: status } = electronTrpc.changes.getStatus.useQuery(
{ worktreePath: worktreePath || "", defaultBranch: effectiveBaseBranch },
{ enabled: !!worktreePath && !isSidebarOpen },
);

// Access tabs store for file opening
const addFileViewerPane = useTabsStore((s) => s.addFileViewerPane);
const trpcUtils = electronTrpc.useUtils();

Expand Down Expand Up @@ -76,7 +73,6 @@ export function SidebarControl() {
const openFirstFile = useCallback(() => {
if (!workspaceId || !worktreePath || !status) return;

// Find the first file in priority order
let firstFile: ChangedFile | undefined;
let category: ChangeCategory | undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export function ChangesContent() {
);
const worktreePath = workspace?.worktreePath;

const { baseBranch } = useChangesStore();
const { getBaseBranch } = useChangesStore();
const baseBranch = getBaseBranch(worktreePath || "");
const { data: branchData } = electronTrpc.changes.getBranches.useQuery(
{ worktreePath: worktreePath || "" },
{ enabled: !!worktreePath },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export function ChangesView({ onFileOpen, isExpandedView }: ChangesViewProps) {
);
const worktreePath = workspace?.worktreePath;

const { baseBranch } = useChangesStore();
const { getBaseBranch } = useChangesStore();
const baseBranch = getBaseBranch(worktreePath || "");
const { data: branchData } = electronTrpc.changes.getBranches.useQuery(
{ worktreePath: worktreePath || "" },
{ enabled: !!worktreePath },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ interface ChangesHeaderProps {
}

function BaseBranchSelector({ worktreePath }: { worktreePath: string }) {
const { baseBranch, setBaseBranch } = useChangesStore();
const { getBaseBranch, setBaseBranch } = useChangesStore();
const baseBranch = getBaseBranch(worktreePath);
const { data: branchData, isLoading } =
electronTrpc.changes.getBranches.useQuery(
{ worktreePath },
Expand All @@ -46,8 +47,11 @@ function BaseBranchSelector({ worktreePath }: { worktreePath: string }) {
});

const handleBranchSelect = (branch: string) => {
if (branch === branchData?.defaultBranch && baseBranch === null) return;
setBaseBranch(branch);
if (branch === branchData?.defaultBranch) {
setBaseBranch(worktreePath, null);
} else {
setBaseBranch(worktreePath, branch);
}
};

return (
Expand Down
27 changes: 22 additions & 5 deletions apps/desktop/src/renderer/stores/changes/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface ChangesState {
viewMode: DiffViewMode;
fileListViewMode: FileListViewMode;
expandedSections: Record<ChangeCategory, boolean>;
baseBranch: string | null;
baseBranch: Record<string, string | null>;
showRenderedMarkdown: Record<string, boolean>;
hideUnchangedRegions: boolean;

Expand All @@ -34,7 +34,8 @@ interface ChangesState {
setFileListViewMode: (mode: FileListViewMode) => void;
toggleSection: (section: ChangeCategory) => void;
setSectionExpanded: (section: ChangeCategory, expanded: boolean) => void;
setBaseBranch: (branch: string | null) => void;
setBaseBranch: (worktreePath: string, branch: string | null) => void;
getBaseBranch: (worktreePath: string) => string | null;
toggleRenderedMarkdown: (worktreePath: string) => void;
getShowRenderedMarkdown: (worktreePath: string) => boolean;
toggleHideUnchangedRegions: () => void;
Expand All @@ -51,7 +52,7 @@ const initialState = {
staged: true,
unstaged: true,
},
baseBranch: null,
baseBranch: {} as Record<string, string | null>,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
showRenderedMarkdown: {} as Record<string, boolean>,
hideUnchangedRegions: false,
};
Expand Down Expand Up @@ -110,8 +111,18 @@ export const useChangesStore = create<ChangesState>()(
});
},

setBaseBranch: (branch) => {
set({ baseBranch: branch });
setBaseBranch: (worktreePath, branch) => {
const { baseBranch } = get();
set({
baseBranch: {
...baseBranch,
[worktreePath]: branch,
},
});
},

getBaseBranch: (worktreePath) => {
return get().baseBranch[worktreePath] ?? null;
},

toggleRenderedMarkdown: (worktreePath) => {
Expand Down Expand Up @@ -144,6 +155,12 @@ export const useChangesStore = create<ChangesState>()(
}),
{
name: "changes-store",
version: 1,
migrate: (persisted) => {
const state = persisted as Record<string, unknown>;
state.baseBranch = {};
return state as unknown as ChangesState;
},
partialize: (state) => ({
selectedFiles: state.selectedFiles,
viewMode: state.viewMode,
Expand Down
Loading