Skip to content

Commit 6cc35a7

Browse files
authored
fix: don't reset TUI menu cursor on back (#181)
## Description <!-- Provide a brief description of your changes --> **Note:** PR titles should follow [Conventional Commits](https://www.conventionalcommits.org/) format (e.g., `feat(devbox): add support for custom env vars` or `fix(snapshot): resolve pagination issue`) as they are used for automatic release notes generation. ## Type of Change <!-- Mark the relevant option with an 'x' --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update - [ ] Code refactoring - [ ] Performance improvement - [ ] Test updates ## Related Issues <!-- Link to related issues using #issue-number --> Closes # ## Changes Made <!-- Describe the changes in detail --> ## Testing <!-- Describe how you tested your changes --> - [ ] I have tested locally - [ ] I have added/updated tests - [ ] All existing tests pass ## Checklist - [ ] My code follows the code style of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published ## Screenshots (if applicable) <!-- Add screenshots to help explain your changes --> ## Additional Notes <!-- Any additional information that reviewers should know -->
1 parent 4e84f30 commit 6cc35a7

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/components/MainMenu.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useUpdateCheck } from "../hooks/useUpdateCheck.js";
1212
import { useVerticalLayout } from "../hooks/useVerticalLayout.js";
1313
import { useBetaFeatures } from "../store/betaFeatureStore.js";
1414
import type { BetaFeature } from "../store/betaFeatureStore.js";
15+
import { useMenuStore } from "../store/menuStore.js";
1516

1617
interface MenuItem {
1718
key: string;
@@ -96,9 +97,9 @@ const BetaBadge = () => (
9697

9798
export const MainMenu = ({ onSelect }: MainMenuProps) => {
9899
const { exit } = useApp();
99-
const [selectedIndex, setSelectedIndex] = React.useState(0);
100100
const { stdout } = useStdout();
101101
const { isFeatureEnabled } = useBetaFeatures();
102+
const { selectedKey, setSelectedKey } = useMenuStore();
102103

103104
// Filter menu items based on beta feature flags
104105
const menuItems = React.useMemo(() => {
@@ -110,6 +111,22 @@ export const MainMenu = ({ onSelect }: MainMenuProps) => {
110111
});
111112
}, [isFeatureEnabled]);
112113

114+
// Calculate initial index from persisted key
115+
const initialIndex = React.useMemo(() => {
116+
const index = menuItems.findIndex((item) => item.key === selectedKey);
117+
return index >= 0 ? index : 0;
118+
}, [selectedKey, menuItems]);
119+
120+
const [selectedIndex, setSelectedIndex] = React.useState(initialIndex);
121+
122+
// Persist selection when it changes
123+
React.useEffect(() => {
124+
const currentKey = menuItems[selectedIndex]?.key;
125+
if (currentKey && currentKey !== selectedKey) {
126+
setSelectedKey(currentKey);
127+
}
128+
}, [selectedIndex, menuItems, selectedKey, setSelectedKey]);
129+
113130
// Get raw terminal dimensions, responding to resize events
114131
// Default to 20 rows / 80 cols if we can't detect
115132
const getTerminalDimensions = React.useCallback(() => {

src/store/menuStore.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { create } from "zustand";
2+
3+
interface MenuState {
4+
selectedKey: string;
5+
setSelectedKey: (key: string) => void;
6+
}
7+
8+
export const useMenuStore = create<MenuState>((set) => ({
9+
selectedKey: "devboxes",
10+
setSelectedKey: (key) => set({ selectedKey: key }),
11+
}));

0 commit comments

Comments
 (0)