-
Notifications
You must be signed in to change notification settings - Fork 6.3k
[RFC] Add worktree-aware directory switcher #8450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bfdd5b3
45d912f
68bf21e
e8dd52c
631f9f3
69beb34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,14 @@ | ||
| import React, { useState } from 'react'; | ||
| import { FolderDot } from 'lucide-react'; | ||
| import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
| import { Check, FolderDot, FolderOpen, GitBranch, Plus } from 'lucide-react'; | ||
| import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/Tooltip'; | ||
| import { | ||
| DropdownMenu, | ||
| DropdownMenuContent, | ||
| DropdownMenuItem, | ||
| DropdownMenuLabel, | ||
| DropdownMenuSeparator, | ||
| DropdownMenuTrigger, | ||
| } from '../ui/dropdown-menu'; | ||
| import { updateWorkingDir } from '../../api'; | ||
| import { toast } from 'react-toastify'; | ||
| import { defineMessages, useIntl } from '../../i18n'; | ||
|
|
@@ -10,6 +18,30 @@ const i18n = defineMessages({ | |
| id: 'dirSwitcher.failedToUpdateWorkingDir', | ||
| defaultMessage: 'Failed to update working directory', | ||
| }, | ||
| currentDirectory: { | ||
| id: 'dirSwitcher.currentDirectory', | ||
| defaultMessage: 'Current directory', | ||
| }, | ||
| gitWorktrees: { | ||
| id: 'dirSwitcher.gitWorktrees', | ||
| defaultMessage: 'Git worktrees', | ||
| }, | ||
| recentDirectories: { | ||
| id: 'dirSwitcher.recentDirectories', | ||
| defaultMessage: 'Recent directories', | ||
| }, | ||
| chooseDirectory: { | ||
| id: 'dirSwitcher.chooseDirectory', | ||
| defaultMessage: 'Choose directory…', | ||
| }, | ||
| openInFinder: { | ||
| id: 'dirSwitcher.openInFinder', | ||
| defaultMessage: 'Open in file manager', | ||
| }, | ||
| noWorktreesFound: { | ||
| id: 'dirSwitcher.noWorktreesFound', | ||
| defaultMessage: 'No worktrees found', | ||
| }, | ||
| }); | ||
|
|
||
| interface DirSwitcherProps { | ||
|
|
@@ -32,25 +64,38 @@ export const DirSwitcher: React.FC<DirSwitcherProps> = ({ | |
| const intl = useIntl(); | ||
| const [isTooltipOpen, setIsTooltipOpen] = useState(false); | ||
| const [isDirectoryChooserOpen, setIsDirectoryChooserOpen] = useState(false); | ||
| const [isMenuOpen, setIsMenuOpen] = useState(false); | ||
| const [recentDirs, setRecentDirs] = useState<string[]>([]); | ||
| const [worktreeDirs, setWorktreeDirs] = useState<string[]>([]); | ||
| const refreshVersionRef = useRef(0); | ||
|
|
||
| const handleDirectoryChange = async () => { | ||
| if (isDirectoryChooserOpen) return; | ||
| setIsDirectoryChooserOpen(true); | ||
| const refreshMenuData = useCallback(async () => { | ||
| const version = ++refreshVersionRef.current; | ||
| setRecentDirs([]); | ||
| setWorktreeDirs([]); | ||
|
|
||
| let result; | ||
| try { | ||
| result = await window.electron.directoryChooser(); | ||
| } finally { | ||
| setIsDirectoryChooserOpen(false); | ||
| } | ||
| const [recent, worktrees] = await Promise.all([ | ||
| window.electron.listRecentDirs().catch(() => []), | ||
| window.electron.listGitWorktreeDirs(workingDir).catch(() => []), | ||
| ]); | ||
|
|
||
| if (result.canceled || result.filePaths.length === 0) { | ||
| if (version !== refreshVersionRef.current) return; | ||
|
|
||
| setRecentDirs(recent); | ||
| setWorktreeDirs(worktrees); | ||
| }, [workingDir]); | ||
|
|
||
| useEffect(() => { | ||
| if (!isMenuOpen) { | ||
| return; | ||
| } | ||
|
|
||
| const newDir = result.filePaths[0]; | ||
| void refreshMenuData(); | ||
|
Comment on lines
+88
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The menu can render stale Useful? React with 👍 / 👎. |
||
| }, [isMenuOpen, refreshMenuData]); | ||
|
|
||
| const applyDirectoryChange = async (newDir: string) => { | ||
| window.electron.addRecentDir(newDir); | ||
| setRecentDirs((previous) => [newDir, ...previous.filter((dir) => dir !== newDir)].slice(0, 10)); | ||
|
|
||
| if (sessionId) { | ||
| onWorkingDirChange?.(newDir); | ||
|
|
@@ -71,41 +116,140 @@ export const DirSwitcher: React.FC<DirSwitcherProps> = ({ | |
| } | ||
| }; | ||
|
|
||
| const handleDirectoryChange = async () => { | ||
| if (isDirectoryChooserOpen) return; | ||
| setIsDirectoryChooserOpen(true); | ||
|
|
||
| let result; | ||
| try { | ||
| result = await window.electron.directoryChooser(); | ||
| } finally { | ||
| setIsDirectoryChooserOpen(false); | ||
| } | ||
|
|
||
| if (result.canceled || result.filePaths.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const newDir = result.filePaths[0]; | ||
| await applyDirectoryChange(newDir); | ||
| }; | ||
|
|
||
| const handleSelectDirectory = async (newDir: string) => { | ||
| if (newDir === workingDir) { | ||
| setIsMenuOpen(false); | ||
| return; | ||
| } | ||
|
|
||
| setIsMenuOpen(false); | ||
| await applyDirectoryChange(newDir); | ||
| }; | ||
|
|
||
| const handleDirectoryClick = async (event: React.MouseEvent) => { | ||
| if (isDirectoryChooserOpen) { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| return; | ||
| } | ||
|
|
||
| const isCmdOrCtrlClick = event.metaKey || event.ctrlKey; | ||
|
|
||
| if (isCmdOrCtrlClick) { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| await window.electron.openDirectoryInExplorer(workingDir); | ||
| } else { | ||
| await handleDirectoryChange(); | ||
| } | ||
| }; | ||
|
|
||
| const filteredWorktreeDirs = useMemo( | ||
| () => worktreeDirs.filter((dir) => dir && dir !== workingDir), | ||
| [worktreeDirs, workingDir] | ||
| ); | ||
|
|
||
| const filteredRecentDirs = useMemo( | ||
| () => recentDirs.filter((dir) => dir && dir !== workingDir), | ||
| [recentDirs, workingDir] | ||
| ); | ||
|
|
||
| return ( | ||
| <TooltipProvider> | ||
| <Tooltip | ||
| open={isTooltipOpen && !isDirectoryChooserOpen} | ||
| open={isTooltipOpen && !isDirectoryChooserOpen && !isMenuOpen} | ||
| onOpenChange={(open) => { | ||
| if (!isDirectoryChooserOpen) setIsTooltipOpen(open); | ||
| if (!isDirectoryChooserOpen && !isMenuOpen) setIsTooltipOpen(open); | ||
| }} | ||
| > | ||
| <TooltipTrigger asChild> | ||
| <button | ||
| className={`z-[100] ${isDirectoryChooserOpen ? 'opacity-50' : 'hover:cursor-pointer hover:text-text-primary'} text-text-primary/70 text-xs flex items-center transition-colors pl-1 [&>svg]:size-4 ${className}`} | ||
| onClick={handleDirectoryClick} | ||
| disabled={isDirectoryChooserOpen} | ||
| > | ||
| <FolderDot className="mr-1" size={16} /> | ||
| <div className="max-w-[200px] truncate [direction:rtl]">{workingDir}</div> | ||
| </button> | ||
| </TooltipTrigger> | ||
| <DropdownMenu open={isMenuOpen} onOpenChange={setIsMenuOpen}> | ||
| <TooltipTrigger asChild> | ||
| <DropdownMenuTrigger asChild> | ||
| <button | ||
| className={`z-[100] ${isDirectoryChooserOpen ? 'opacity-50' : 'hover:cursor-pointer hover:text-text-primary'} text-text-primary/70 text-xs flex items-center transition-colors pl-1 [&>svg]:size-4 ${className}`} | ||
| onClick={handleDirectoryClick} | ||
| disabled={isDirectoryChooserOpen} | ||
| > | ||
| <FolderDot className="mr-1" size={16} /> | ||
| <div className="max-w-[200px] truncate [direction:rtl]">{workingDir}</div> | ||
| </button> | ||
| </DropdownMenuTrigger> | ||
| </TooltipTrigger> | ||
| <DropdownMenuContent className="w-80" side="top" align="start"> | ||
| <DropdownMenuLabel>{intl.formatMessage(i18n.currentDirectory)}</DropdownMenuLabel> | ||
| <DropdownMenuItem | ||
| onSelect={() => void window.electron.openDirectoryInExplorer(workingDir)} | ||
| > | ||
| <FolderOpen className="mr-2 h-4 w-4" /> | ||
| <span className="truncate">{workingDir}</span> | ||
| <Check className="ml-auto h-4 w-4" /> | ||
| </DropdownMenuItem> | ||
|
|
||
| <DropdownMenuSeparator /> | ||
| <DropdownMenuLabel>{intl.formatMessage(i18n.gitWorktrees)}</DropdownMenuLabel> | ||
| {filteredWorktreeDirs.length > 0 ? ( | ||
| filteredWorktreeDirs.map((dir) => ( | ||
| <DropdownMenuItem | ||
| key={`worktree-${dir}`} | ||
| onSelect={() => void handleSelectDirectory(dir)} | ||
| > | ||
| <GitBranch className="mr-2 h-4 w-4" /> | ||
| <span className="truncate">{dir}</span> | ||
| </DropdownMenuItem> | ||
| )) | ||
| ) : ( | ||
| <DropdownMenuItem disabled> | ||
| <GitBranch className="mr-2 h-4 w-4" /> | ||
| <span>{intl.formatMessage(i18n.noWorktreesFound)}</span> | ||
| </DropdownMenuItem> | ||
| )} | ||
|
|
||
| {filteredRecentDirs.length > 0 && ( | ||
| <> | ||
| <DropdownMenuSeparator /> | ||
| <DropdownMenuLabel>{intl.formatMessage(i18n.recentDirectories)}</DropdownMenuLabel> | ||
| {filteredRecentDirs.map((dir) => ( | ||
| <DropdownMenuItem | ||
| key={`recent-${dir}`} | ||
| onSelect={() => void handleSelectDirectory(dir)} | ||
| > | ||
| <FolderDot className="mr-2 h-4 w-4" /> | ||
| <span className="truncate">{dir}</span> | ||
| </DropdownMenuItem> | ||
| ))} | ||
| </> | ||
| )} | ||
|
|
||
| <DropdownMenuSeparator /> | ||
| <DropdownMenuItem onSelect={() => void handleDirectoryChange()}> | ||
| <Plus className="mr-2 h-4 w-4" /> | ||
| <span>{intl.formatMessage(i18n.chooseDirectory)}</span> | ||
| </DropdownMenuItem> | ||
| <DropdownMenuItem | ||
| onSelect={() => void window.electron.openDirectoryInExplorer(workingDir)} | ||
| > | ||
| <FolderOpen className="mr-2 h-4 w-4" /> | ||
| <span>{intl.formatMessage(i18n.openInFinder)}</span> | ||
| </DropdownMenuItem> | ||
| </DropdownMenuContent> | ||
| </DropdownMenu> | ||
| <TooltipContent side="top">{workingDir}</TooltipContent> | ||
| </Tooltip> | ||
| </TooltipProvider> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.