Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): preserve tasks when shrinking command center (#91727)

Dylan Martin0b6da9118e4c
diff --git a/products/desktop/packages/core/src/command-center/grid.test.ts b/products/desktop/packages/core/src/command-center/grid.test.ts
index 57260d3f0391..bc45d6deaf5e 100644
--- a/products/desktop/packages/core/src/command-center/grid.test.ts
+++ b/products/desktop/packages/core/src/command-center/grid.test.ts
@@ -19,6 +19,7 @@ import {
makeTerminalCellValue,
reflowCells,
resizeCells,
+ resizeCellsForLayout,
} from "./grid";
describe("getGridDimensions / getCellCount", () => {
@@ -168,6 +169,43 @@ describe("resizeCells", () => {
});
});
+describe("resizeCellsForLayout", () => {
+ it("packs occupied cells when the grid capacity shrinks", () => {
+ expect(
+ resizeCellsForLayout(
+ ["a", "b", "c", null, "deleted-task", "d"],
+ "3x2",
+ "2x2",
+ [0, 1, 2, 5],
+ ),
+ ).toEqual(["a", "b", "c", "d"]);
+ });
+
+ it.each([
+ {
+ from: "3x1",
+ to: "2x2",
+ cells: ["a", null, "c"],
+ occupiedCellIndices: [0, 2],
+ expected: ["a", null, null, null],
+ },
+ {
+ from: "3x2",
+ to: "2x3",
+ cells: ["a", null, null, null, "b", null],
+ occupiedCellIndices: [0, 4],
+ expected: ["a", null, null, "b", null, null],
+ },
+ ] as const)(
+ "preserves coordinates when resizing from $from to $to without reducing capacity",
+ ({ from, to, cells, occupiedCellIndices, expected }) => {
+ expect(
+ resizeCellsForLayout(cells, from, to, occupiedCellIndices),
+ ).toEqual(expected);
+ },
+ );
+});
+
describe("clampZoom", () => {
it.each([
{ input: 0.1, expected: 0.5 },
diff --git a/products/desktop/packages/core/src/command-center/grid.ts b/products/desktop/packages/core/src/command-center/grid.ts
index 2052377e9146..d256804e7aa4 100644
--- a/products/desktop/packages/core/src/command-center/grid.ts
+++ b/products/desktop/packages/core/src/command-center/grid.ts
@@ -220,6 +220,24 @@ export function reflowCells(
return next;
}
+export function resizeCellsForLayout(
+ cells: readonly (string | null)[],
+ from: LayoutPreset,
+ to: LayoutPreset,
+ occupiedCellIndices?: readonly number[],
+): (string | null)[] {
+ const newCount = getCellCount(to);
+ const isShrinking = newCount < getCellCount(from);
+ if (!occupiedCellIndices || !isShrinking) {
+ return reflowCells(cells, from, to);
+ }
+
+ const occupiedCells = occupiedCellIndices
+ .map((index) => cells[index])
+ .filter((cell): cell is string => cell != null);
+ return resizeCells(occupiedCells, newCount);
+}
+
export function clampZoom(value: number): number {
return Math.round(Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, value)) * 10) / 10;
}
diff --git a/products/desktop/packages/ui/src/features/command-center/commandCenterStore.ts b/products/desktop/packages/ui/src/features/command-center/commandCenterStore.ts
index d93d5df8a874..35171c0dc3ab 100644
--- a/products/desktop/packages/ui/src/features/command-center/commandCenterStore.ts
+++ b/products/desktop/packages/ui/src/features/command-center/commandCenterStore.ts
@@ -6,7 +6,6 @@ import {
type LayoutPreset,
makeCanvasCellValue,
makeTerminalCellValue,
- reflowCells,
resizeCells,
ZOOM_STEP,
} from "@posthog/core/command-center/grid";
@@ -35,7 +34,7 @@ interface CommandCenterStoreState {
}
interface CommandCenterStoreActions {
- setLayout: (preset: LayoutPreset) => void;
+ setLayout: (preset: LayoutPreset, cells: readonly (string | null)[]) => void;
setActiveTask: (taskId: string | null) => void;
setActiveCell: (cellIndex: number | null) => void;
assignTask: (cellIndex: number, taskId: string) => void;
@@ -80,10 +79,9 @@ export const useCommandCenterStore = create<CommandCenterStore>()(
(set) => ({
...COMMAND_CENTER_INITIAL_STATE,
- setLayout: (preset) =>
+ setLayout: (preset, cells) =>
set((state) => {
const newCount = getCellCount(preset);
- const cells = reflowCells(state.cells, state.layout, preset);
const activeTaskId = cells.includes(state.activeTaskId)
? state.activeTaskId
: null;
@@ -98,7 +96,7 @@ export const useCommandCenterStore = create<CommandCenterStore>()(
? state.activeCellIndex
: null,
layout: preset,
- cells,
+ cells: [...cells],
};
}),
diff --git a/products/desktop/packages/ui/src/features/command-center/components/CommandCenterToolbar.tsx b/products/desktop/packages/ui/src/features/command-center/components/CommandCenterToolbar.tsx
index 9e8792108a69..eb9a1b680f98 100644
--- a/products/desktop/packages/ui/src/features/command-center/components/CommandCenterToolbar.tsx
+++ b/products/desktop/packages/ui/src/features/command-center/components/CommandCenterToolbar.tsx
@@ -3,7 +3,7 @@ import {
MagnifyingGlassPlus,
Trash,
} from "@phosphor-icons/react";
-import { reflowCells } from "@posthog/core/command-center/grid";
+import { resizeCellsForLayout } from "@posthog/core/command-center/grid";
import { Flex, Select, Text } from "@radix-ui/themes";
import { useCallback } from "react";
import {
@@ -98,14 +98,17 @@ export function CommandCenterToolbar({
const handleSetLayout = useCallback(
(preset: LayoutPreset) => {
- const { cells, layout: current } = useCommandCenterStore.getState();
- // Cells keep their row and column across a resize, so what falls off is
- // whatever the reflow didn't keep — not simply the tail.
- const kept = reflowCells(cells, current, preset);
+ const { cells, layout: currentLayout } = useCommandCenterStore.getState();
+ const kept = resizeCellsForLayout(
+ cells,
+ currentLayout,
+ preset,
+ occupiedCellIndices,
+ );
+ setLayout(preset, kept);
destroyTerminalCells(cells.filter((cell) => !kept.includes(cell)));
- setLayout(preset);
},
- [setLayout],
+ [occupiedCellIndices, setLayout],
);
const handleOptimize = useCallback(() => {
diff --git a/products/desktop/packages/ui/src/features/command-center/placeTaskInCommandCenter.ts b/products/desktop/packages/ui/src/features/command-center/placeTaskInCommandCenter.ts
index 997bfd8f10e2..e8dcfc07b8c0 100644
--- a/products/desktop/packages/ui/src/features/command-center/placeTaskInCommandCenter.ts
+++ b/products/desktop/packages/ui/src/features/command-center/placeTaskInCommandCenter.ts
@@ -2,6 +2,7 @@ import {
type ExpandDirection,
getExpandedLayout,
getExpansionCellIndex,
+ resizeCellsForLayout,
} from "@posthog/core/command-center/grid";
import { planCommandCenterPlacement } from "@posthog/core/command-center/placement";
import { navigateToCommandCenter } from "@posthog/ui/router/navigationBridge";
@@ -117,7 +118,10 @@ export function expandCanvasInCommandCenterInto(
const expanded = getExpandedLayout(state.layout, direction);
if (!expanded) return;
- state.setLayout(expanded);
+ state.setLayout(
+ expanded,
+ resizeCellsForLayout(state.cells, state.layout, expanded),
+ );
useCommandCenterStore
.getState()
.setCanvasCell(getExpansionCellIndex(expanded, direction, slot), canvasId);
@@ -136,7 +140,10 @@ export function expandTasksInCommandCenterInto(
const expanded = getExpandedLayout(state.layout, direction);
if (!expanded) return;
- state.setLayout(expanded);
+ state.setLayout(
+ expanded,
+ resizeCellsForLayout(state.cells, state.layout, expanded),
+ );
useCommandCenterStore
.getState()
.assignTask(getExpansionCellIndex(expanded, direction, slot), firstTaskId);