|
| 1 | +I'll analyze the codebase and create a detailed implementation plan for adding a context menu to tabs with "Close other tabs" and "Close tabs to the right" functionality. |
| 2 | + |
| 3 | +Let me start by examining the current structure and understanding the dependencies: |
| 4 | +Now let me check if ContextMenu from Radix UI is already being used and examine those implementations: |
| 5 | +Perfect! Now let me check the package.json to see if Radix UI Context Menu is already installed: |
| 6 | +Now let me check if there are additional methods needed in the tab store: |
| 7 | +Perfect! Now I have all the information I need to create a comprehensive implementation plan. Let me present the plan: |
| 8 | +# Implementation Plan: Context Menu for Tabs |
| 9 | + |
| 10 | +**Task ID:** cabe8ff5-bedc-4ea4-8769-0ef1a96f94ae |
| 11 | +**Generated:** 2025-10-14 |
| 12 | + |
| 13 | +## Summary |
| 14 | + |
| 15 | +This implementation will add right-click context menu functionality to tabs in the TabBar component, allowing users to: |
| 16 | +1. **Close other tabs** - Closes all tabs except the one that was right-clicked |
| 17 | +2. **Close tabs to the right** - Closes all tabs positioned to the right of the clicked tab |
| 18 | + |
| 19 | +The implementation leverages the existing **Radix UI Themes ContextMenu** component (already installed and used in `TaskItem.tsx` and `LogView.tsx`) and extends the `tabStore` with two new methods for the closing operations. |
| 20 | + |
| 21 | +## Implementation Steps |
| 22 | + |
| 23 | +### 1. Analysis |
| 24 | +- [x] **Identified relevant files**: `TabBar.tsx` and `tabStore.ts` |
| 25 | +- [x] **Reviewed existing patterns**: |
| 26 | + - `TaskItem.tsx` demonstrates comprehensive ContextMenu usage with multiple items, shortcuts, and sub-menus |
| 27 | + - `LogView.tsx` shows simpler ContextMenu implementation |
| 28 | + - Both use `@radix-ui/themes` ContextMenu (not the standalone package) |
| 29 | +- [x] **Verified dependencies**: `@radix-ui/themes` already installed (v3.2.1), no additional packages needed |
| 30 | +- [x] **Reviewed store patterns**: `closeTab` method exists and provides template for new methods |
| 31 | + |
| 32 | +### 2. Changes Required |
| 33 | + |
| 34 | +#### Files to Modify |
| 35 | +1. **src/renderer/stores/tabStore.ts** |
| 36 | + - Add `closeOtherTabs(tabId: string)` to interface and implementation |
| 37 | + - Add `closeTabsToRight(tabId: string)` to interface and implementation |
| 38 | + |
| 39 | +2. **src/renderer/components/TabBar.tsx** |
| 40 | + - Import `ContextMenu` from `@radix-ui/themes` |
| 41 | + - Import new store methods |
| 42 | + - Wrap tab elements with ContextMenu components |
| 43 | + - Add menu content with two action items |
| 44 | + |
| 45 | +#### No Dependencies to Add |
| 46 | +All required dependencies (`@radix-ui/themes`, `react`, `zustand`) are already installed. |
| 47 | + |
| 48 | +### 3. Implementation |
| 49 | + |
| 50 | +#### Core Functionality Changes |
| 51 | + |
| 52 | +**A. Store Methods (tabStore.ts)** |
| 53 | + |
| 54 | +Add to `TabStore` interface: |
| 55 | +```typescript |
| 56 | +closeOtherTabs: (tabId: string) => void; |
| 57 | +closeTabsToRight: (tabId: string) => void; |
| 58 | +``` |
| 59 | + |
| 60 | +Implementation logic: |
| 61 | +- **closeOtherTabs**: |
| 62 | + - Keep only the tab with the specified `tabId` |
| 63 | + - Set the kept tab as active |
| 64 | + - Ensure at least one tab remains (no-op if only one tab exists) |
| 65 | + |
| 66 | +- **closeTabsToRight**: |
| 67 | + - Find index of the specified tab |
| 68 | + - Filter out all tabs with index > specified tab's index |
| 69 | + - Update `activeTabId` if current active tab is being closed (select the rightmost remaining tab) |
| 70 | + - No-op if the tab is already the rightmost tab |
| 71 | + |
| 72 | +**B. TabBar Component Updates (TabBar.tsx)** |
| 73 | + |
| 74 | +Structure for each tab: |
| 75 | +```typescript |
| 76 | +<ContextMenu.Root> |
| 77 | + <ContextMenu.Trigger> |
| 78 | + {/* Existing tab Flex component */} |
| 79 | + </ContextMenu.Trigger> |
| 80 | + <ContextMenu.Content> |
| 81 | + <ContextMenu.Item |
| 82 | + disabled={tabs.length === 1} |
| 83 | + onSelect={() => closeOtherTabs(tab.id)} |
| 84 | + > |
| 85 | + Close other tabs |
| 86 | + </ContextMenu.Item> |
| 87 | + <ContextMenu.Item |
| 88 | + disabled={index === tabs.length - 1} |
| 89 | + onSelect={() => closeTabsToRight(tab.id)} |
| 90 | + > |
| 91 | + Close tabs to the right |
| 92 | + </ContextMenu.Item> |
| 93 | + </ContextMenu.Content> |
| 94 | +</ContextMenu.Root> |
| 95 | +``` |
| 96 | + |
| 97 | +Key integration points: |
| 98 | +- Maintain existing drag-and-drop functionality |
| 99 | +- Preserve keyboard shortcuts |
| 100 | +- Keep existing close button behavior |
| 101 | +- Maintain tab styling and interactions |
| 102 | + |
| 103 | +#### Testing & Validation |
| 104 | + |
| 105 | +Manual testing checklist: |
| 106 | +- [ ] Right-click first tab → "Close other tabs" closes all others |
| 107 | +- [ ] Right-click middle tab → both options work correctly |
| 108 | +- [ ] Right-click last tab → "Close tabs to the right" is disabled |
| 109 | +- [ ] Single tab → both options are disabled |
| 110 | +- [ ] Active tab indicator updates correctly when active tab is closed |
| 111 | +- [ ] At least one tab always remains open |
| 112 | +- [ ] Existing keyboard shortcuts (Cmd/Ctrl+W, Cmd/Ctrl+Shift+[, etc.) still work |
| 113 | +- [ ] Drag and drop still functions correctly |
| 114 | +- [ ] Close button (X) on tabs still works |
| 115 | + |
| 116 | +## File Changes |
| 117 | + |
| 118 | +### New Files |
| 119 | +``` |
| 120 | +None - No new files required |
| 121 | +``` |
| 122 | + |
| 123 | +### Modified Files |
| 124 | + |
| 125 | +**src/renderer/stores/tabStore.ts** |
| 126 | +- Add `closeOtherTabs` method signature to `TabStore` interface (line ~11) |
| 127 | +- Add `closeTabsToRight` method signature to `TabStore` interface (line ~12) |
| 128 | +- Implement `closeOtherTabs` method in store (after `closeTab` method, ~line 70) |
| 129 | +- Implement `closeTabsToRight` method in store (after `closeOtherTabs` method, ~line 80) |
| 130 | + |
| 131 | +**src/renderer/components/TabBar.tsx** |
| 132 | +- Add `ContextMenu` to imports from `@radix-ui/themes` (line 2) |
| 133 | +- Destructure `closeOtherTabs` and `closeTabsToRight` from `useTabStore()` (line ~17) |
| 134 | +- Wrap the tab `Flex` component (line ~137) with `ContextMenu.Root` and `ContextMenu.Trigger` |
| 135 | +- Add `ContextMenu.Content` after the trigger with menu items |
| 136 | +- Calculate disable conditions for menu items based on `tabs.length` and `index` |
| 137 | + |
| 138 | +## Considerations |
| 139 | + |
| 140 | +### Key Architectural Decisions |
| 141 | + |
| 142 | +1. **Using Radix UI Themes ContextMenu**: Following existing patterns in the codebase (`TaskItem.tsx`, `LogView.tsx`) rather than introducing the standalone `@radix-ui/react-context-menu` package |
| 143 | + |
| 144 | +2. **Store-based operations**: All closing logic is handled in the Zustand store, keeping components focused on UI concerns |
| 145 | + |
| 146 | +3. **Consistency with existing patterns**: The context menu structure mirrors `TaskItem.tsx` which has sophisticated menu handling |
| 147 | + |
| 148 | +### Potential Risks and Mitigation |
| 149 | + |
| 150 | +**Risk**: Closing tabs might interfere with drag-and-drop operations |
| 151 | +- **Mitigation**: Context menu trigger wraps the existing tab element without modifying its event handlers |
| 152 | + |
| 153 | +**Risk**: Active tab selection might behave unexpectedly when multiple tabs are closed |
| 154 | +- **Mitigation**: Store methods explicitly handle active tab reassignment, selecting appropriate fallback tabs |
| 155 | + |
| 156 | +**Risk**: User might accidentally close all important tabs |
| 157 | +- **Mitigation**: Always keep at least one tab open; consider adding confirmation for destructive actions (future enhancement) |
| 158 | + |
| 159 | +### Testing Approach |
| 160 | + |
| 161 | +1. **Edge Case Testing**: |
| 162 | + - Single tab scenario (both options disabled) |
| 163 | + - Last tab scenario (close right disabled) |
| 164 | + - Closing active tab (verify proper tab selection) |
| 165 | + - Multiple rapid operations |
| 166 | + |
| 167 | +2. **Integration Testing**: |
| 168 | + - Verify keyboard shortcuts remain functional |
| 169 | + - Test drag-and-drop still works |
| 170 | + - Ensure tab reordering doesn't affect context menu behavior |
| 171 | + |
| 172 | +3. **User Experience Testing**: |
| 173 | + - Context menu appears on right-click |
| 174 | + - Menu items are appropriately disabled |
| 175 | + - Visual feedback is clear |
| 176 | + - Operations complete as expected |
| 177 | + |
| 178 | +### Additional Notes |
| 179 | + |
| 180 | +- The implementation should maintain the current tab persistence (handled by Zustand persist middleware) |
| 181 | +- No changes to the tab types are needed (`TabState` interface remains unchanged) |
| 182 | +- The context menu should follow the theme of the application (handled automatically by Radix UI Themes) |
| 183 | +- Consider adding keyboard shortcuts for these actions in a future iteration (e.g., similar to browser behavior) |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +*Generated by PostHog AI Coding Agent* |
0 commit comments