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 @@ -8,26 +8,13 @@ import {
} from "renderer/routes/_authenticated/components/utils/paneLifecycleRows";
import { useCollections } from "renderer/routes/_authenticated/providers/CollectionsProvider";
import type { AppCollections } from "renderer/routes/_authenticated/providers/CollectionsProvider/collections";
import { isSidebarWorkspaceVisible } from "renderer/routes/_authenticated/providers/CollectionsProvider/dashboardSidebarLocal";
import {
getNextTabOrder,
getPrependTabOrder,
isSidebarWorkspaceVisible,
} from "renderer/routes/_authenticated/providers/CollectionsProvider/dashboardSidebarLocal";
import { PROJECT_CUSTOM_COLORS } from "shared/constants/project-colors";

function getNextTabOrder(items: Array<{ tabOrder: number }>): number {
const maxTabOrder = items.reduce(
(maxValue, item) => Math.max(maxValue, item.tabOrder),
0,
);
return maxTabOrder + 1;
}

function getPrependTabOrder(items: Array<{ tabOrder: number }>): number {
if (items.length === 0) return 1;
const minTabOrder = items.reduce(
(minValue, item) => Math.min(minValue, item.tabOrder),
Number.POSITIVE_INFINITY,
);
return minTabOrder - 1;
}

type ProjectTopLevelItem = {
type: "workspace" | "section";
id: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./schema";
export * from "./sidebarVisibility";
export * from "./tabOrder";
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Lower tabOrder = appears earlier in the sidebar (queries sort ASC).
* Prepending therefore picks one less than the smallest existing tabOrder
* so the new item lands at the top regardless of whether existing items
* are positive (newly defaulted) or negative (after prior prepends).
*/
export function getPrependTabOrder(items: Array<{ tabOrder: number }>): number {
if (items.length === 0) return 1;
const minTabOrder = items.reduce(
(minValue, item) => Math.min(minValue, item.tabOrder),
Number.POSITIVE_INFINITY,
);
return minTabOrder - 1;
}

export function getNextTabOrder(items: Array<{ tabOrder: number }>): number {
const maxTabOrder = items.reduce(
(maxValue, item) => Math.max(maxValue, item.tabOrder),
0,
);
return maxTabOrder + 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { authClient } from "renderer/lib/auth-client";
import { getHostServiceClientByUrl } from "renderer/lib/host-service-client";
import type { PaneViewerData } from "renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/types";
import { useCollections } from "renderer/routes/_authenticated/providers/CollectionsProvider";
import {
getPrependTabOrder,
isSidebarWorkspaceVisible,
} from "renderer/routes/_authenticated/providers/CollectionsProvider/dashboardSidebarLocal";
import { useLocalHostService } from "renderer/routes/_authenticated/providers/LocalHostServiceProvider";
import { appendLaunchesToPaneLayout } from "./appendLaunchesToPaneLayout";
import {
Expand Down Expand Up @@ -82,12 +86,26 @@ export function useWorkspaceCreates(): UseWorkspaceCreatesApi {
},
);
} else {
const projectId = result.workspace.projectId;
const topLevelItems = [
...Array.from(collections.v2WorkspaceLocalState.state.values())
.filter(
(item) =>
item.sidebarState.projectId === projectId &&
item.sidebarState.sectionId === null &&
isSidebarWorkspaceVisible(item),
)
.map((item) => ({ tabOrder: item.sidebarState.tabOrder })),
...Array.from(collections.v2SidebarSections.state.values())
.filter((item) => item.projectId === projectId)
.map((item) => ({ tabOrder: item.tabOrder })),
];
Comment on lines +90 to +102
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Duplicated top-level-items filter logic

The workspace + section filtering here is a near-verbatim copy of the private getProjectTopLevelItems function in useDashboardSidebarState.ts. If that function's filter criteria evolve (e.g., a new visibility condition is added), this site will silently diverge, causing getPrependTabOrder to compute from a different set than everything else — leading to subtle ordering bugs. Consider extracting getProjectTopLevelItems (or a lightweight equivalent that accepts collections + projectId and returns Array<{ tabOrder: number }>) into the shared dashboardSidebarLocal/ module alongside the tab-order utilities.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop/src/renderer/stores/workspace-creates/useWorkspaceCreates.ts
Line: 90-102

Comment:
**Duplicated top-level-items filter logic**

The workspace + section filtering here is a near-verbatim copy of the private `getProjectTopLevelItems` function in `useDashboardSidebarState.ts`. If that function's filter criteria evolve (e.g., a new visibility condition is added), this site will silently diverge, causing `getPrependTabOrder` to compute from a different set than everything else — leading to subtle ordering bugs. Consider extracting `getProjectTopLevelItems` (or a lightweight equivalent that accepts collections + projectId and returns `Array<{ tabOrder: number }>`) into the shared `dashboardSidebarLocal/` module alongside the tab-order utilities.

How can I resolve this? If you propose a fix, please make it concise.

collections.v2WorkspaceLocalState.insert({
workspaceId: result.workspace.id,
createdAt: new Date(),
sidebarState: {
projectId: result.workspace.projectId,
tabOrder: 0,
projectId,
tabOrder: getPrependTabOrder(topLevelItems),
sectionId: null,
changesFilter: { kind: "all" },
activeTab: "changes",
Expand Down
Loading