2025-12-07 19:32:53 +02:00
|
|
|
import React from 'react';
|
2026-09-07 22:11:47 +01:00
|
|
|
import { ScrollableOverlay } from '@/components/ui/ScrollableOverlay';
|
2025-12-07 19:32:53 +02:00
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
} from '@/components/ui/dialog';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
2025-12-19 17:22:02 +02:00
|
|
|
import { Input } from '@/components/ui/input';
|
2025-12-07 19:32:53 +02:00
|
|
|
import { useDirectoryStore } from '@/stores/useDirectoryStore';
|
2026-01-06 21:31:04 +02:00
|
|
|
import { useProjectsStore } from '@/stores/useProjectsStore';
|
2026-08-10 16:10:03 +03:00
|
|
|
import { useUIStore } from '@/stores/useUIStore';
|
2026-05-07 01:10:17 +03:00
|
|
|
import { useGitIdentitiesStore } from '@/stores/useGitIdentitiesStore';
|
2026-08-10 16:10:03 +03:00
|
|
|
import { useSessionUIStore } from '@/sync/session-ui-store';
|
2025-12-07 19:32:53 +02:00
|
|
|
import { useFileSystemAccess } from '@/hooks/useFileSystemAccess';
|
2026-04-27 15:45:14 +03:00
|
|
|
import { cn } from '@/lib/utils';
|
2026-01-19 02:50:40 +02:00
|
|
|
import { toast } from '@/components/ui';
|
2026-05-07 01:10:17 +03:00
|
|
|
import { IdentityDropdown } from '@/components/views/git/GitHeader';
|
2026-06-02 00:43:05 +03:00
|
|
|
import { runtimeFetch } from '@/lib/runtime-fetch';
|
2025-12-07 19:32:53 +02:00
|
|
|
import { useDeviceInfo } from '@/lib/device';
|
|
|
|
|
import { MobileOverlayPanel } from '@/components/ui/MobileOverlayPanel';
|
2026-05-13 13:26:15 +03:00
|
|
|
import { Icon } from "@/components/icon/Icon";
|
2026-04-27 15:45:14 +03:00
|
|
|
import { opencodeClient } from '@/lib/opencode/client';
|
2026-04-26 14:03:39 +03:00
|
|
|
import { useI18n } from '@/lib/i18n';
|
2026-08-26 10:59:04 +03:00
|
|
|
import { formatShortcutForDisplay } from '@/lib/shortcuts';
|
2026-08-07 01:49:44 +03:00
|
|
|
import {
|
|
|
|
|
isFilesystemError,
|
|
|
|
|
type FilesystemErrorReason,
|
|
|
|
|
} from '@/lib/api/files-errors';
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
interface DirectoryExplorerDialogProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
type BrowseEntry = {
|
|
|
|
|
name: string;
|
|
|
|
|
path: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type BrowseRow =
|
|
|
|
|
| { type: 'up'; value: 'browse:up'; name: string; path: string | null; disabled?: false }
|
|
|
|
|
| { type: 'directory'; value: string; name: string; path: string; disabled: boolean };
|
|
|
|
|
|
|
|
|
|
const isRootPath = (value: string): boolean => value === '/';
|
|
|
|
|
|
|
|
|
|
const normalizeSeparators = (value: string): string => value.replace(/\\/g, '/');
|
|
|
|
|
|
|
|
|
|
const trimTrailingSeparators = (value: string): string => {
|
|
|
|
|
if (!value || isRootPath(value)) return value;
|
|
|
|
|
let result = value;
|
|
|
|
|
while (result.length > 1 && result.endsWith('/')) {
|
|
|
|
|
result = result.slice(0, -1);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hasTrailingPathSeparator = (value: string): boolean => value.endsWith('/');
|
|
|
|
|
|
|
|
|
|
const ensureBrowseDirectoryPath = (value: string): string => {
|
|
|
|
|
const trimmed = value.trim();
|
|
|
|
|
if (!trimmed || hasTrailingPathSeparator(trimmed)) return trimmed;
|
|
|
|
|
return `${trimmed}/`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getLastPathSeparatorIndex = (value: string): number => value.lastIndexOf('/');
|
|
|
|
|
|
|
|
|
|
const getBrowseDirectoryPath = (value: string): string => {
|
|
|
|
|
if (hasTrailingPathSeparator(value)) return value;
|
|
|
|
|
const lastSeparator = getLastPathSeparatorIndex(value);
|
|
|
|
|
if (lastSeparator < 0) return value;
|
|
|
|
|
return value.slice(0, lastSeparator + 1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getBrowseLeafPathSegment = (value: string): string => {
|
|
|
|
|
const lastSeparator = getLastPathSeparatorIndex(value);
|
|
|
|
|
return value.slice(lastSeparator + 1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getBrowseParentPath = (value: string): string | null => {
|
|
|
|
|
const trimmed = trimTrailingSeparators(value.trim());
|
|
|
|
|
if (!trimmed || trimmed === '~' || trimmed === '~/' || trimmed === '/') return null;
|
|
|
|
|
const lastSeparator = getLastPathSeparatorIndex(trimmed);
|
|
|
|
|
if (lastSeparator < 0) return null;
|
|
|
|
|
if (trimmed.startsWith('~/') && lastSeparator <= 1) return '~/';
|
|
|
|
|
if (lastSeparator === 0) return '/';
|
|
|
|
|
return `${trimmed.slice(0, lastSeparator)}/`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const canNavigateUp = (value: string): boolean => hasTrailingPathSeparator(value) && getBrowseParentPath(value) !== null;
|
|
|
|
|
|
|
|
|
|
const appendBrowsePathSegment = (currentPath: string, segment: string): string => (
|
|
|
|
|
`${getBrowseDirectoryPath(currentPath)}${segment}/`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const normalizeDirectoryPath = (path: string | null | undefined): string | null => {
|
|
|
|
|
if (!path) return null;
|
|
|
|
|
const normalized = trimTrailingSeparators(normalizeSeparators(path.trim()));
|
|
|
|
|
if (!normalized) return null;
|
|
|
|
|
return normalized.toLowerCase();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const displayPathToAbsolutePath = (value: string, homeDirectory: string): string => {
|
|
|
|
|
const trimmed = value.trim();
|
|
|
|
|
if (trimmed === '~') return homeDirectory;
|
|
|
|
|
if (trimmed.startsWith('~/')) return `${homeDirectory}${trimmed.slice(1)}`;
|
|
|
|
|
return trimmed;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isPrimaryModifierPressed = (event: React.KeyboardEvent<HTMLInputElement>): boolean => {
|
|
|
|
|
const isMac = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform);
|
|
|
|
|
return isMac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const focusPathInput = (input: HTMLInputElement | null): void => {
|
|
|
|
|
if (!input) return;
|
|
|
|
|
input.focus({ preventScroll: true });
|
|
|
|
|
const valueLength = input.value.length;
|
|
|
|
|
input.setSelectionRange(valueLength, valueLength);
|
|
|
|
|
input.scrollLeft = input.scrollWidth;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resolveFreshFilesystemHome = async (): Promise<string | null> => {
|
|
|
|
|
try {
|
2026-06-02 00:43:05 +03:00
|
|
|
const response = await runtimeFetch('/api/fs/home', {
|
2026-04-27 15:45:14 +03:00
|
|
|
method: 'GET',
|
|
|
|
|
headers: { Accept: 'application/json' },
|
|
|
|
|
});
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
const data = await response.json() as { home?: unknown };
|
|
|
|
|
if (typeof data.home === 'string' && data.home.trim().length > 0) {
|
|
|
|
|
return normalizeSeparators(data.home.trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Fall back to the client helper below.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return opencodeClient.getFilesystemHome().catch(() => null);
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
export const DirectoryExplorerDialog: React.FC<DirectoryExplorerDialogProps> = ({
|
|
|
|
|
open,
|
|
|
|
|
onOpenChange,
|
|
|
|
|
}) => {
|
2026-04-26 14:03:39 +03:00
|
|
|
const { t } = useI18n();
|
2026-04-26 16:24:07 +03:00
|
|
|
const homeDirectory = useDirectoryStore((s) => s.homeDirectory);
|
2026-04-27 15:45:14 +03:00
|
|
|
const projects = useProjectsStore((s) => s.projects);
|
2026-04-26 16:24:07 +03:00
|
|
|
const addProject = useProjectsStore((s) => s.addProject);
|
2026-08-10 16:10:03 +03:00
|
|
|
const setSessionSwitcherOpen = useUIStore((s) => s.setSessionSwitcherOpen);
|
|
|
|
|
const openNewSessionDraft = useSessionUIStore((s) => s.openNewSessionDraft);
|
2026-08-05 10:26:30 +03:00
|
|
|
const addProjects = useProjectsStore((s) => s.addProjects);
|
2026-05-07 01:10:17 +03:00
|
|
|
const gitIdentityProfiles = useGitIdentitiesStore((s) => s.profiles);
|
|
|
|
|
const globalGitIdentity = useGitIdentitiesStore((s) => s.globalIdentity);
|
|
|
|
|
const defaultGitIdentityId = useGitIdentitiesStore((s) => s.defaultGitIdentityId);
|
|
|
|
|
const loadGitIdentityProfiles = useGitIdentitiesStore((s) => s.loadProfiles);
|
|
|
|
|
const loadGlobalGitIdentity = useGitIdentitiesStore((s) => s.loadGlobalIdentity);
|
|
|
|
|
const loadDefaultGitIdentityId = useGitIdentitiesStore((s) => s.loadDefaultGitIdentityId);
|
2026-08-07 01:49:44 +03:00
|
|
|
const { canRequestAccess, requestAccess, startAccessing } = useFileSystemAccess();
|
2025-12-07 19:32:53 +02:00
|
|
|
const { isMobile } = useDeviceInfo();
|
2026-04-27 15:45:14 +03:00
|
|
|
const inputRef = React.useRef<HTMLInputElement>(null);
|
|
|
|
|
const addButtonRef = React.useRef<HTMLButtonElement>(null);
|
|
|
|
|
const rowRefs = React.useRef(new Map<string, HTMLButtonElement>());
|
|
|
|
|
const [dialogHomeDirectory, setDialogHomeDirectory] = React.useState('');
|
|
|
|
|
const [query, setQuery] = React.useState('~/');
|
|
|
|
|
const [entries, setEntries] = React.useState<BrowseEntry[]>([]);
|
|
|
|
|
const [isLoading, setIsLoading] = React.useState(false);
|
|
|
|
|
const [isBrowseDirectoryMissing, setIsBrowseDirectoryMissing] = React.useState(false);
|
2026-08-07 01:49:44 +03:00
|
|
|
const [browseErrorReason, setBrowseErrorReason] = React.useState<FilesystemErrorReason | null>(null);
|
|
|
|
|
const [browseReloadKey, setBrowseReloadKey] = React.useState(0);
|
2026-04-27 15:45:14 +03:00
|
|
|
const [highlightedIndex, setHighlightedIndex] = React.useState(0);
|
|
|
|
|
const [isConfirming, setIsConfirming] = React.useState(false);
|
|
|
|
|
const [isOpeningFinder, setIsOpeningFinder] = React.useState(false);
|
|
|
|
|
const [addButtonWidth, setAddButtonWidth] = React.useState(0);
|
2026-05-07 01:10:17 +03:00
|
|
|
const [isCloneMode, setIsCloneMode] = React.useState(false);
|
|
|
|
|
const [cloneRemoteUrl, setCloneRemoteUrl] = React.useState('');
|
|
|
|
|
const [selectedGitIdentityId, setSelectedGitIdentityId] = React.useState<string | null>(null);
|
2026-06-04 18:49:37 +03:00
|
|
|
const [showHidden, setShowHidden] = React.useState(false);
|
2026-08-05 10:26:30 +03:00
|
|
|
const [selectedPaths, setSelectedPaths] = React.useState<string[]>([]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const explorerRootDirectory = dialogHomeDirectory || homeDirectory;
|
|
|
|
|
|
|
|
|
|
const addedProjectPaths = React.useMemo(() => new Set(
|
|
|
|
|
projects
|
|
|
|
|
.map((project) => normalizeDirectoryPath(project.path))
|
|
|
|
|
.filter((path): path is string => Boolean(path))
|
|
|
|
|
), [projects]);
|
2025-12-19 17:22:02 +02:00
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
React.useEffect(() => {
|
2026-04-27 15:45:14 +03:00
|
|
|
if (!open) return;
|
|
|
|
|
setQuery('~/');
|
|
|
|
|
setEntries([]);
|
|
|
|
|
setHighlightedIndex(0);
|
|
|
|
|
setIsConfirming(false);
|
|
|
|
|
setIsOpeningFinder(false);
|
2026-05-07 01:10:17 +03:00
|
|
|
setIsCloneMode(false);
|
|
|
|
|
setCloneRemoteUrl('');
|
|
|
|
|
setSelectedGitIdentityId(null);
|
2026-06-04 18:49:37 +03:00
|
|
|
setShowHidden(false);
|
2026-08-05 10:26:30 +03:00
|
|
|
setSelectedPaths([]);
|
2026-04-27 15:45:14 +03:00
|
|
|
requestAnimationFrame(() => focusPathInput(inputRef.current));
|
|
|
|
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
const resolveHome = async () => {
|
|
|
|
|
const resolved = await resolveFreshFilesystemHome();
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
setDialogHomeDirectory(resolved || homeDirectory || '');
|
|
|
|
|
requestAnimationFrame(() => focusPathInput(inputRef.current));
|
|
|
|
|
};
|
|
|
|
|
void resolveHome();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, [homeDirectory, open]);
|
|
|
|
|
|
2026-05-07 01:10:17 +03:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!open) return;
|
|
|
|
|
void loadGitIdentityProfiles();
|
|
|
|
|
void loadGlobalGitIdentity();
|
|
|
|
|
void loadDefaultGitIdentityId();
|
|
|
|
|
}, [loadDefaultGitIdentityId, loadGitIdentityProfiles, loadGlobalGitIdentity, open]);
|
|
|
|
|
|
|
|
|
|
const availableGitIdentities = React.useMemo(() => {
|
|
|
|
|
const unique = new Map<string, NonNullable<typeof globalGitIdentity>>();
|
|
|
|
|
if (globalGitIdentity) {
|
|
|
|
|
unique.set(globalGitIdentity.id, globalGitIdentity);
|
|
|
|
|
}
|
|
|
|
|
for (const profile of gitIdentityProfiles) {
|
|
|
|
|
unique.set(profile.id, profile);
|
|
|
|
|
}
|
|
|
|
|
return Array.from(unique.values());
|
|
|
|
|
}, [gitIdentityProfiles, globalGitIdentity]);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!open || !isCloneMode || selectedGitIdentityId !== null) return;
|
|
|
|
|
const defaultId = typeof defaultGitIdentityId === 'string' ? defaultGitIdentityId.trim() : '';
|
|
|
|
|
if (defaultId && availableGitIdentities.some((identity) => identity.id === defaultId)) {
|
|
|
|
|
setSelectedGitIdentityId(defaultId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const firstSshIdentity = availableGitIdentities.find((identity) => identity.authType === 'ssh' || identity.sshKey);
|
|
|
|
|
if (firstSshIdentity) {
|
|
|
|
|
setSelectedGitIdentityId(firstSshIdentity.id);
|
|
|
|
|
}
|
|
|
|
|
}, [availableGitIdentities, defaultGitIdentityId, isCloneMode, open, selectedGitIdentityId]);
|
|
|
|
|
|
|
|
|
|
const selectedGitIdentity = React.useMemo(
|
|
|
|
|
() => availableGitIdentities.find((identity) => identity.id === selectedGitIdentityId) ?? null,
|
|
|
|
|
[availableGitIdentities, selectedGitIdentityId]
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const browseDirectoryDisplayPath = React.useMemo(() => getBrowseDirectoryPath(query), [query]);
|
|
|
|
|
const browseFilterQuery = React.useMemo(
|
|
|
|
|
() => (hasTrailingPathSeparator(query) ? '' : getBrowseLeafPathSegment(query)),
|
|
|
|
|
[query]
|
|
|
|
|
);
|
|
|
|
|
const browseDirectoryAbsolutePath = React.useMemo(
|
|
|
|
|
() => explorerRootDirectory ? displayPathToAbsolutePath(browseDirectoryDisplayPath, explorerRootDirectory) : '',
|
|
|
|
|
[browseDirectoryDisplayPath, explorerRootDirectory]
|
|
|
|
|
);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2026-04-27 15:45:14 +03:00
|
|
|
if (!open || !browseDirectoryAbsolutePath) {
|
|
|
|
|
setEntries([]);
|
2026-08-07 01:49:44 +03:00
|
|
|
setBrowseErrorReason(null);
|
2025-12-07 19:32:53 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2026-04-27 15:45:14 +03:00
|
|
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
setIsBrowseDirectoryMissing(false);
|
2026-08-07 01:49:44 +03:00
|
|
|
setBrowseErrorReason(null);
|
2026-04-27 15:45:14 +03:00
|
|
|
opencodeClient.listLocalDirectory(browseDirectoryAbsolutePath)
|
|
|
|
|
.then((result) => {
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
setIsBrowseDirectoryMissing(false);
|
2026-08-07 01:49:44 +03:00
|
|
|
setBrowseErrorReason(null);
|
2026-04-27 15:45:14 +03:00
|
|
|
const nextEntries = result
|
|
|
|
|
.filter((entry) => entry.isDirectory)
|
|
|
|
|
.map((entry) => ({
|
|
|
|
|
name: entry.name,
|
|
|
|
|
path: normalizeSeparators(entry.path),
|
|
|
|
|
}))
|
|
|
|
|
.sort((left, right) => left.name.localeCompare(right.name));
|
|
|
|
|
setEntries(nextEntries);
|
|
|
|
|
})
|
2026-08-07 01:49:44 +03:00
|
|
|
.catch((error) => {
|
2026-04-27 15:45:14 +03:00
|
|
|
if (!cancelled) {
|
|
|
|
|
setEntries([]);
|
2026-08-07 01:49:44 +03:00
|
|
|
const reason = isFilesystemError(error) ? error.reason : 'unknown';
|
|
|
|
|
setBrowseErrorReason(reason);
|
|
|
|
|
setIsBrowseDirectoryMissing(reason === 'not-found');
|
2026-04-27 15:45:14 +03:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
if (!cancelled) setIsLoading(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-08-07 01:49:44 +03:00
|
|
|
}, [browseDirectoryAbsolutePath, browseReloadKey, open]);
|
2026-04-27 15:45:14 +03:00
|
|
|
|
|
|
|
|
const filteredEntries = React.useMemo(() => {
|
|
|
|
|
const lowerFilter = browseFilterQuery.toLowerCase();
|
|
|
|
|
const includeHidden = showHidden || browseFilterQuery.startsWith('.');
|
|
|
|
|
return entries.filter((entry) => (
|
|
|
|
|
entry.name.toLowerCase().startsWith(lowerFilter) && (includeHidden || !entry.name.startsWith('.'))
|
|
|
|
|
));
|
|
|
|
|
}, [browseFilterQuery, entries, showHidden]);
|
|
|
|
|
|
|
|
|
|
const rows = React.useMemo<BrowseRow[]>(() => {
|
|
|
|
|
const nextRows: BrowseRow[] = [];
|
|
|
|
|
if (canNavigateUp(query)) {
|
|
|
|
|
nextRows.push({ type: 'up', value: 'browse:up', name: '..', path: getBrowseParentPath(query) });
|
|
|
|
|
}
|
|
|
|
|
for (const entry of filteredEntries) {
|
|
|
|
|
const normalized = normalizeDirectoryPath(entry.path);
|
|
|
|
|
nextRows.push({
|
|
|
|
|
type: 'directory',
|
|
|
|
|
value: `browse:${entry.path}`,
|
|
|
|
|
name: entry.name,
|
|
|
|
|
path: entry.path,
|
|
|
|
|
disabled: Boolean(normalized && addedProjectPaths.has(normalized)),
|
|
|
|
|
});
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
2026-04-27 15:45:14 +03:00
|
|
|
return nextRows;
|
|
|
|
|
}, [addedProjectPaths, filteredEntries, query]);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
setHighlightedIndex(0);
|
|
|
|
|
}, [query, rows.length]);
|
|
|
|
|
|
2026-08-05 10:26:30 +03:00
|
|
|
// Selections apply to the currently browsed directory: navigating into
|
|
|
|
|
// another folder clears the pending batch so the primary action always
|
|
|
|
|
// reflects the visible picker state.
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
setSelectedPaths([]);
|
|
|
|
|
}, [browseDirectoryAbsolutePath]);
|
|
|
|
|
|
|
|
|
|
const selectionPaths = React.useMemo(
|
|
|
|
|
() => selectedPaths.filter((path) => {
|
|
|
|
|
const normalized = normalizeDirectoryPath(path);
|
|
|
|
|
return Boolean(normalized && !addedProjectPaths.has(normalized));
|
|
|
|
|
}),
|
|
|
|
|
[addedProjectPaths, selectedPaths]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const togglePathSelection = React.useCallback((path: string) => {
|
|
|
|
|
setSelectedPaths((prev) => (
|
|
|
|
|
prev.includes(path) ? prev.filter((entry) => entry !== path) : [...prev, path]
|
|
|
|
|
));
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const targetPath = React.useMemo(() => {
|
|
|
|
|
if (!explorerRootDirectory) return '';
|
|
|
|
|
return trimTrailingSeparators(displayPathToAbsolutePath(query, explorerRootDirectory));
|
|
|
|
|
}, [explorerRootDirectory, query]);
|
|
|
|
|
const normalizedTargetPath = normalizeDirectoryPath(targetPath);
|
|
|
|
|
const isAlreadyAdded = Boolean(normalizedTargetPath && addedProjectPaths.has(normalizedTargetPath));
|
|
|
|
|
const exactEntry = React.useMemo(() => {
|
|
|
|
|
if (!browseFilterQuery) return null;
|
|
|
|
|
return filteredEntries.find((entry) => entry.name === browseFilterQuery) ?? null;
|
|
|
|
|
}, [browseFilterQuery, filteredEntries]);
|
|
|
|
|
const shouldCreateTarget = Boolean(
|
|
|
|
|
targetPath
|
|
|
|
|
&& !isAlreadyAdded
|
2026-08-07 01:49:44 +03:00
|
|
|
&& (browseErrorReason === null || browseErrorReason === 'not-found')
|
2026-04-27 15:45:14 +03:00
|
|
|
&& (
|
|
|
|
|
(hasTrailingPathSeparator(query) && isBrowseDirectoryMissing)
|
|
|
|
|
|| (!hasTrailingPathSeparator(query) && browseFilterQuery.trim().length > 0 && exactEntry === null)
|
|
|
|
|
)
|
|
|
|
|
);
|
2026-08-07 01:49:44 +03:00
|
|
|
const canAddProject = !isConfirming
|
|
|
|
|
&& !isOpeningFinder
|
|
|
|
|
&& browseErrorReason !== 'os-permission'
|
|
|
|
|
&& browseErrorReason !== 'invalid-response'
|
|
|
|
|
&& browseErrorReason !== 'unknown'
|
2026-08-05 10:26:30 +03:00
|
|
|
&& ((!isCloneMode && selectionPaths.length > 0) || (!isAlreadyAdded && Boolean(targetPath)));
|
2026-05-07 01:10:17 +03:00
|
|
|
const canSubmitClone = canAddProject && cloneRemoteUrl.trim().length > 0;
|
2026-04-27 15:45:14 +03:00
|
|
|
const highlightedRow = rows[highlightedIndex] ?? null;
|
|
|
|
|
const hasHighlightedBrowseItem = Boolean(
|
2026-08-05 11:19:44 +08:00
|
|
|
highlightedRow && (highlightedRow.type === 'up' || highlightedRow.type === 'directory')
|
2026-04-27 15:45:14 +03:00
|
|
|
);
|
2026-08-26 10:59:04 +03:00
|
|
|
const submitModifierLabel = formatShortcutForDisplay('mod');
|
2026-08-05 10:26:30 +03:00
|
|
|
const submitActionLabel = !isCloneMode && selectionPaths.length > 0
|
|
|
|
|
? t('directoryExplorerDialog.actions.addSelected')
|
|
|
|
|
: isAlreadyAdded
|
|
|
|
|
? t('directoryExplorerDialog.actions.alreadyAdded')
|
|
|
|
|
: isCloneMode
|
|
|
|
|
? isConfirming
|
|
|
|
|
? t('directoryExplorerDialog.actions.cloning')
|
|
|
|
|
: t('directoryExplorerDialog.actions.cloneAndAdd')
|
|
|
|
|
: isConfirming
|
|
|
|
|
? t('directoryExplorerDialog.actions.adding')
|
|
|
|
|
: shouldCreateTarget
|
|
|
|
|
? t('directoryExplorerDialog.actions.createAndAdd')
|
|
|
|
|
: t('directoryExplorerDialog.actions.addProject');
|
2026-04-27 15:45:14 +03:00
|
|
|
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
|
const button = addButtonRef.current;
|
|
|
|
|
if (!button) return;
|
|
|
|
|
|
|
|
|
|
const updateWidth = () => setAddButtonWidth(Math.ceil(button.getBoundingClientRect().width));
|
|
|
|
|
updateWidth();
|
|
|
|
|
|
|
|
|
|
if (typeof ResizeObserver === 'undefined') return;
|
|
|
|
|
const observer = new ResizeObserver(updateWidth);
|
|
|
|
|
observer.observe(button);
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
}, [submitActionLabel]);
|
|
|
|
|
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
|
const input = inputRef.current;
|
|
|
|
|
if (!input) return;
|
|
|
|
|
input.scrollLeft = input.scrollWidth;
|
|
|
|
|
}, [addButtonWidth, query]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
|
if (!open) return;
|
|
|
|
|
focusPathInput(inputRef.current);
|
|
|
|
|
}, [open]);
|
|
|
|
|
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
|
const row = rows[highlightedIndex];
|
|
|
|
|
if (!row) return;
|
|
|
|
|
rowRefs.current.get(row.value)?.scrollIntoView({ block: 'nearest' });
|
|
|
|
|
}, [highlightedIndex, rows]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
const handleClose = React.useCallback(() => {
|
|
|
|
|
onOpenChange(false);
|
|
|
|
|
}, [onOpenChange]);
|
|
|
|
|
|
2026-08-10 16:10:03 +03:00
|
|
|
const openProjectDraft = React.useCallback((projectId: string, projectPath: string) => {
|
|
|
|
|
if (isMobile) setSessionSwitcherOpen(false);
|
|
|
|
|
openNewSessionDraft({ selectedProjectId: projectId, directoryOverride: projectPath });
|
|
|
|
|
handleClose();
|
2026-08-24 16:36:41 +03:00
|
|
|
}, [handleClose, isMobile, openNewSessionDraft, setSessionSwitcherOpen]);
|
2026-08-10 16:10:03 +03:00
|
|
|
|
2026-08-19 10:38:53 +11:00
|
|
|
const handleQuickAdd = React.useCallback(async (event: React.MouseEvent, path: string) => {
|
2026-05-23 14:37:51 -04:00
|
|
|
event.stopPropagation();
|
|
|
|
|
const normalized = normalizeDirectoryPath(path);
|
|
|
|
|
if (normalized && addedProjectPaths.has(normalized)) return;
|
2026-08-19 10:38:53 +11:00
|
|
|
const project = await addProject(path);
|
2026-08-10 16:10:03 +03:00
|
|
|
if (!project) {
|
2026-05-23 14:37:51 -04:00
|
|
|
toast.error(t('directoryExplorerDialog.toast.failedToAddProject'), {
|
|
|
|
|
description: t('directoryExplorerDialog.toast.selectValidDirectoryPath'),
|
|
|
|
|
});
|
2026-08-10 16:10:03 +03:00
|
|
|
return;
|
2026-05-23 14:37:51 -04:00
|
|
|
}
|
2026-08-10 16:10:03 +03:00
|
|
|
openProjectDraft(project.id, project.path);
|
|
|
|
|
}, [addProject, addedProjectPaths, openProjectDraft, t]);
|
2026-05-23 14:37:51 -04:00
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const finalizeSelection = React.useCallback(async (target: string) => {
|
2026-08-05 10:26:30 +03:00
|
|
|
if (isConfirming) return;
|
2026-04-27 15:45:14 +03:00
|
|
|
const normalized = normalizeDirectoryPath(target);
|
2026-08-05 10:26:30 +03:00
|
|
|
// Batch selections supersede the single-target flow. Only the single-target
|
|
|
|
|
// flow is blocked by an already-added (or missing) directory.
|
|
|
|
|
const selectionToAdd = isCloneMode
|
|
|
|
|
? []
|
|
|
|
|
: selectedPaths.filter((path) => {
|
|
|
|
|
const selectionNormalized = normalizeDirectoryPath(path);
|
|
|
|
|
return Boolean(selectionNormalized && !addedProjectPaths.has(selectionNormalized));
|
|
|
|
|
});
|
|
|
|
|
if (selectionToAdd.length === 0 && (!target || (normalized && addedProjectPaths.has(normalized)))) return;
|
2026-05-07 01:10:17 +03:00
|
|
|
let selectedTarget = target;
|
2026-04-27 15:45:14 +03:00
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
setIsConfirming(true);
|
|
|
|
|
try {
|
2026-08-28 16:41:29 +02:00
|
|
|
const shouldCreateSelection = !isCloneMode && shouldCreateTarget && normalizeDirectoryPath(target) === normalizeDirectoryPath(targetPath);
|
2026-05-07 01:10:17 +03:00
|
|
|
if (isCloneMode) {
|
|
|
|
|
const remoteUrl = cloneRemoteUrl.trim();
|
|
|
|
|
if (!remoteUrl) {
|
|
|
|
|
toast.error(t('directoryExplorerDialog.toast.cloneUrlRequired'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const result = await opencodeClient.cloneRepository({
|
|
|
|
|
remoteUrl,
|
|
|
|
|
destinationPath: target,
|
|
|
|
|
gitIdentityId: selectedGitIdentity?.id ?? null,
|
|
|
|
|
});
|
|
|
|
|
selectedTarget = result.path;
|
2026-08-05 10:26:30 +03:00
|
|
|
} else if (selectionToAdd.length > 0) {
|
2026-08-28 17:50:34 +02:00
|
|
|
// Batch path wins over single-target create: with checkboxes ticked,
|
|
|
|
|
// the user wants the selections added, not a fresh directory created
|
|
|
|
|
// for whatever happens to be typed in the filter.
|
2026-08-28 18:59:47 +02:00
|
|
|
const added = await addProjects(selectionToAdd);
|
2026-08-05 10:26:30 +03:00
|
|
|
if (added.length === 0) {
|
|
|
|
|
toast.error(t('directoryExplorerDialog.toast.failedToAddProject'), {
|
|
|
|
|
description: t('directoryExplorerDialog.toast.selectValidDirectoryPath'),
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-29 00:50:22 +03:00
|
|
|
toast.success(t('directoryExplorerDialog.toast.addedProjects', { count: added.length }));
|
2026-08-05 10:26:30 +03:00
|
|
|
setSelectedPaths([]);
|
|
|
|
|
handleClose();
|
|
|
|
|
return;
|
2026-08-28 17:50:34 +02:00
|
|
|
} else if (shouldCreateSelection) {
|
|
|
|
|
await opencodeClient.createDirectory(target, { asProject: true });
|
2026-08-05 10:26:30 +03:00
|
|
|
}
|
2026-08-19 10:38:53 +11:00
|
|
|
const project = await addProject(selectedTarget);
|
2026-08-10 16:10:03 +03:00
|
|
|
if (!project) {
|
2026-04-26 14:03:39 +03:00
|
|
|
toast.error(t('directoryExplorerDialog.toast.failedToAddProject'), {
|
|
|
|
|
description: t('directoryExplorerDialog.toast.selectValidDirectoryPath'),
|
2026-01-06 21:31:04 +02:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-10 16:10:03 +03:00
|
|
|
openProjectDraft(project.id, project.path);
|
2025-12-07 19:32:53 +02:00
|
|
|
} catch (error) {
|
2026-04-26 14:03:39 +03:00
|
|
|
toast.error(t('directoryExplorerDialog.toast.failedToSelectDirectory'), {
|
|
|
|
|
description: error instanceof Error ? error.message : t('directoryExplorerDialog.toast.unknownError'),
|
2025-12-07 19:32:53 +02:00
|
|
|
});
|
|
|
|
|
} finally {
|
|
|
|
|
setIsConfirming(false);
|
|
|
|
|
}
|
2026-08-05 10:26:30 +03:00
|
|
|
}, [addProject, addProjects, addedProjectPaths, cloneRemoteUrl, handleClose, isCloneMode, isConfirming, openProjectDraft, selectedGitIdentity?.id, selectedPaths, shouldCreateTarget, targetPath, t]);
|
2026-04-27 15:45:14 +03:00
|
|
|
|
|
|
|
|
const browseToDisplayPath = React.useCallback((displayPath: string) => {
|
|
|
|
|
setQuery(ensureBrowseDirectoryPath(displayPath));
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const browseToEntry = React.useCallback((entry: BrowseEntry) => {
|
|
|
|
|
setQuery(appendBrowsePathSegment(query, entry.name));
|
|
|
|
|
}, [query]);
|
2025-12-19 17:22:02 +02:00
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const executeRow = React.useCallback((row: BrowseRow | null) => {
|
|
|
|
|
if (!row) return;
|
|
|
|
|
if (row.type === 'up') {
|
|
|
|
|
if (row.path) browseToDisplayPath(row.path);
|
2026-01-08 23:53:03 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2026-04-27 15:45:14 +03:00
|
|
|
browseToEntry(row);
|
|
|
|
|
}, [browseToDisplayPath, browseToEntry]);
|
2026-01-08 23:53:03 +02:00
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const handleOpenInFinder = React.useCallback(async () => {
|
2026-08-07 01:49:44 +03:00
|
|
|
if (!canRequestAccess || isOpeningFinder) return;
|
2026-04-27 15:45:14 +03:00
|
|
|
setIsOpeningFinder(true);
|
|
|
|
|
try {
|
|
|
|
|
const result = await requestAccess(targetPath);
|
|
|
|
|
if (!result.success || !result.path) {
|
|
|
|
|
if (result.error && result.error !== 'Directory selection cancelled') {
|
|
|
|
|
toast.error(t('directoryExplorerDialog.toast.failedToSelectDirectory'), {
|
|
|
|
|
description: result.error,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-08 23:53:03 +02:00
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const accessResult = await startAccessing(result.path);
|
|
|
|
|
if (!accessResult.success) {
|
|
|
|
|
toast.error(t('directoryExplorerDialog.toast.failedToOpenDirectory'), {
|
|
|
|
|
description: accessResult.error || t('directoryExplorerDialog.toast.desktopCouldNotGrantAccess'),
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-12-19 17:22:02 +02:00
|
|
|
|
2026-08-28 19:15:39 +02:00
|
|
|
// Clear pending selections so the Finder-sourced target is honored
|
|
|
|
|
// instead of silently being absorbed by the batch branch.
|
|
|
|
|
setSelectedPaths([]);
|
2026-04-27 15:45:14 +03:00
|
|
|
await finalizeSelection(result.path);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
toast.error(t('directoryExplorerDialog.toast.failedToSelectDirectory'), {
|
|
|
|
|
description: error instanceof Error ? error.message : t('directoryExplorerDialog.toast.unknownError'),
|
|
|
|
|
});
|
|
|
|
|
} finally {
|
|
|
|
|
setIsOpeningFinder(false);
|
|
|
|
|
}
|
2026-08-07 01:49:44 +03:00
|
|
|
}, [canRequestAccess, finalizeSelection, isOpeningFinder, requestAccess, startAccessing, t, targetPath]);
|
2025-12-19 17:22:02 +02:00
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const handleKeyDown = React.useCallback((event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
|
if (event.key === 'ArrowDown') {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
setHighlightedIndex((index) => Math.min(rows.length - 1, index + 1));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (event.key === 'ArrowUp') {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
setHighlightedIndex((index) => Math.max(0, index - 1));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-05 10:26:30 +03:00
|
|
|
if (event.key === ' ') {
|
2026-08-28 17:50:34 +02:00
|
|
|
// Only treat Space as a selection toggle when the user is actively
|
|
|
|
|
// browsing a directory (trailing slash or no filter typing). When
|
|
|
|
|
// the input is in path-entry mode, Space is a literal character
|
|
|
|
|
// and must reach the input value.
|
|
|
|
|
if (hasTrailingPathSeparator(query)) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (highlightedRow && highlightedRow.type === 'directory' && !highlightedRow.disabled) {
|
|
|
|
|
togglePathSelection(highlightedRow.path);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2026-08-05 10:26:30 +03:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-27 15:45:14 +03:00
|
|
|
if (event.key === 'Enter') {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (isPrimaryModifierPressed(event)) {
|
|
|
|
|
void finalizeSelection(targetPath);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (hasHighlightedBrowseItem) {
|
|
|
|
|
executeRow(highlightedRow);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (event.key === 'Backspace' && query === '') {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
handleClose();
|
|
|
|
|
}
|
2026-08-05 10:26:30 +03:00
|
|
|
}, [executeRow, finalizeSelection, handleClose, hasHighlightedBrowseItem, highlightedRow, query, rows.length, targetPath, togglePathSelection]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-08 23:53:03 +02:00
|
|
|
const showHiddenToggle = (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-06-04 18:49:37 +03:00
|
|
|
onClick={() => setShowHidden((value) => !value)}
|
2026-04-27 15:45:14 +03:00
|
|
|
className="flex flex-shrink-0 items-center gap-2 rounded-lg px-2 py-1 typography-meta text-muted-foreground transition-colors hover:bg-interactive-hover/40"
|
2026-01-08 23:53:03 +02:00
|
|
|
>
|
2026-05-13 13:26:15 +03:00
|
|
|
{showHidden ? <Icon name="checkbox" className="h-4 w-4 text-primary" /> : <Icon name="checkbox-blank" className="h-4 w-4" />}
|
2026-04-26 14:03:39 +03:00
|
|
|
{t('directoryExplorerDialog.toggle.showHidden')}
|
2026-01-08 23:53:03 +02:00
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const inputSection = (
|
2026-05-07 01:10:17 +03:00
|
|
|
<div className="px-2.5 py-1.5">
|
|
|
|
|
{isCloneMode ? (
|
|
|
|
|
<div className="mb-1.5 flex items-center gap-1.5">
|
|
|
|
|
<Input
|
|
|
|
|
value={cloneRemoteUrl}
|
|
|
|
|
onChange={(event) => setCloneRemoteUrl(event.target.value)}
|
|
|
|
|
placeholder={t('directoryExplorerDialog.clone.remoteUrlPlaceholder')}
|
|
|
|
|
className="min-w-0 flex-1 border-border/60 bg-[var(--surface-elevated)] font-mono typography-ui-label shadow-none"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
autoCorrect="off"
|
|
|
|
|
autoCapitalize="off"
|
|
|
|
|
/>
|
|
|
|
|
<IdentityDropdown
|
|
|
|
|
activeProfile={selectedGitIdentity}
|
|
|
|
|
identities={availableGitIdentities}
|
|
|
|
|
onSelect={(profile) => setSelectedGitIdentityId(profile.id)}
|
|
|
|
|
isApplying={isConfirming}
|
|
|
|
|
iconOnly
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2026-04-27 15:45:14 +03:00
|
|
|
) : null}
|
2026-05-07 01:10:17 +03:00
|
|
|
<div className="relative">
|
2026-05-13 13:26:15 +03:00
|
|
|
<Icon name="folder-add" className="pointer-events-none absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground/80" />
|
2026-05-07 01:10:17 +03:00
|
|
|
<Input
|
|
|
|
|
ref={inputRef}
|
|
|
|
|
value={query}
|
|
|
|
|
onChange={(event) => setQuery(normalizeSeparators(event.target.value))}
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
placeholder={t('directoryExplorerDialog.pathInput.placeholder')}
|
|
|
|
|
className="border-transparent bg-transparent pl-9 font-mono typography-ui-label shadow-none focus-visible:ring-0"
|
|
|
|
|
style={!isMobile && addButtonWidth > 0 ? { paddingRight: `${addButtonWidth + 24}px` } : undefined}
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
autoCorrect="off"
|
|
|
|
|
autoCapitalize="off"
|
|
|
|
|
/>
|
|
|
|
|
{!isMobile ? (
|
|
|
|
|
<Button
|
|
|
|
|
ref={addButtonRef}
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="xs"
|
|
|
|
|
tabIndex={-1}
|
|
|
|
|
className="absolute right-1.5 top-1/2 h-7 -translate-y-1/2 gap-1 px-2 typography-meta"
|
|
|
|
|
disabled={isCloneMode ? !canSubmitClone : !canAddProject}
|
|
|
|
|
onMouseDown={(event) => event.preventDefault()}
|
|
|
|
|
onClick={() => void finalizeSelection(targetPath)}
|
|
|
|
|
title={submitActionLabel}
|
|
|
|
|
>
|
|
|
|
|
{submitActionLabel}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
2026-01-08 23:53:03 +02:00
|
|
|
</div>
|
2025-12-19 17:22:02 +02:00
|
|
|
);
|
|
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const resultsSection = (
|
|
|
|
|
<div className="relative min-h-0 flex-1 overflow-hidden rounded-xl border border-border/60 bg-[var(--surface-elevated)] shadow-sm">
|
2026-09-07 22:11:47 +01:00
|
|
|
<ScrollableOverlay outerClassName="max-h-[min(28rem,58vh)]" className="p-2">
|
2026-04-27 15:45:14 +03:00
|
|
|
<div className="px-2 pb-1 pt-0.5 typography-meta font-medium uppercase tracking-wide text-muted-foreground/80">
|
|
|
|
|
{t('directoryExplorerDialog.browse.directories')}
|
|
|
|
|
</div>
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
<div className="py-10 text-center typography-ui-label text-muted-foreground">
|
|
|
|
|
{t('directoryExplorerDialog.browse.loading')}
|
|
|
|
|
</div>
|
2026-08-07 01:49:44 +03:00
|
|
|
) : browseErrorReason && browseErrorReason !== 'not-found' ? (
|
|
|
|
|
<div className="flex flex-col items-center gap-3 px-4 py-10 text-center">
|
2026-08-07 02:29:27 +03:00
|
|
|
<div className="typography-ui-label text-status-error">
|
2026-08-07 01:49:44 +03:00
|
|
|
{browseErrorReason === 'os-permission'
|
|
|
|
|
? t('directoryExplorerDialog.browse.permissionDenied')
|
|
|
|
|
: t('directoryExplorerDialog.browse.loadFailed')}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
{browseErrorReason === 'os-permission' && canRequestAccess ? (
|
|
|
|
|
<Button size="xs" onClick={() => void handleOpenInFinder()} disabled={isOpeningFinder}>
|
|
|
|
|
{t('directoryExplorerDialog.browse.grantAccess')}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
<Button variant="outline" size="xs" onClick={() => setBrowseReloadKey((key) => key + 1)}>
|
|
|
|
|
{t('directoryExplorerDialog.browse.retry')}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-04-27 15:45:14 +03:00
|
|
|
) : rows.length === 0 ? (
|
|
|
|
|
<div className="py-10 text-center typography-ui-label text-muted-foreground">
|
|
|
|
|
{t('directoryExplorerDialog.browse.empty')}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="space-y-0.5">
|
|
|
|
|
{rows.map((row, index) => {
|
|
|
|
|
const isActive = index === highlightedIndex;
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
key={row.value}
|
|
|
|
|
ref={(node) => {
|
|
|
|
|
if (node) {
|
|
|
|
|
rowRefs.current.set(row.value, node);
|
|
|
|
|
} else {
|
|
|
|
|
rowRefs.current.delete(row.value);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
type="button"
|
|
|
|
|
onMouseEnter={() => setHighlightedIndex(index)}
|
|
|
|
|
onMouseDown={(event) => event.preventDefault()}
|
|
|
|
|
onClick={() => executeRow(row)}
|
|
|
|
|
className={cn(
|
|
|
|
|
'flex w-full cursor-pointer items-center gap-2 rounded-lg px-2 py-1.5 text-left transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary/50',
|
|
|
|
|
isActive && 'bg-interactive-selection text-interactive-selection-foreground',
|
|
|
|
|
!isActive && 'hover:bg-interactive-hover/50',
|
2026-08-05 11:19:44 +08:00
|
|
|
row.type === 'directory' && row.disabled && 'opacity-45'
|
2026-04-27 15:45:14 +03:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{row.type === 'up' ? (
|
2026-05-13 13:26:15 +03:00
|
|
|
<Icon name="arrow-left-s" className="h-4 w-4 flex-shrink-0 text-muted-foreground/80" />
|
2026-04-27 15:45:14 +03:00
|
|
|
) : (
|
2026-05-13 13:26:15 +03:00
|
|
|
<Icon name="folder-6" className="h-4 w-4 flex-shrink-0 text-muted-foreground/80" />
|
2026-04-27 15:45:14 +03:00
|
|
|
)}
|
|
|
|
|
<span className="flex min-w-0 flex-1 items-center gap-1.5">
|
|
|
|
|
<span className="truncate typography-ui-label text-foreground">{row.name}</span>
|
|
|
|
|
</span>
|
|
|
|
|
{row.type === 'directory' && row.disabled ? (
|
|
|
|
|
<span className="rounded-full border border-border/60 px-2 py-0.5 typography-meta text-muted-foreground">
|
|
|
|
|
{t('directoryExplorerDialog.browse.addedBadge')}
|
|
|
|
|
</span>
|
2026-05-23 14:37:51 -04:00
|
|
|
) : row.type === 'directory' ? (
|
2026-08-05 10:26:30 +03:00
|
|
|
<>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onMouseDown={(event) => event.stopPropagation()}
|
2026-08-28 09:52:17 +02:00
|
|
|
onClick={(event) => { event.stopPropagation(); togglePathSelection(row.path); }}
|
2026-08-05 10:26:30 +03:00
|
|
|
title={t('directoryExplorerDialog.browse.selectForAdd')}
|
|
|
|
|
aria-label={t('directoryExplorerDialog.browse.selectForAdd')}
|
|
|
|
|
aria-pressed={selectedPaths.includes(row.path)}
|
|
|
|
|
className="flex-shrink-0 rounded p-0.5 text-muted-foreground transition-colors hover:bg-interactive-hover/60 hover:text-foreground"
|
|
|
|
|
>
|
|
|
|
|
<Icon
|
|
|
|
|
name={selectedPaths.includes(row.path) ? 'checkbox' : 'checkbox-blank'}
|
|
|
|
|
className="h-4 w-4"
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onMouseDown={(event) => event.stopPropagation()}
|
|
|
|
|
onClick={(event) => handleQuickAdd(event, row.path)}
|
|
|
|
|
className="flex-shrink-0 rounded-full p-1 text-muted-foreground transition-colors hover:bg-interactive-hover/60 hover:text-foreground"
|
|
|
|
|
title={t('directoryExplorerDialog.browse.quickAdd')}
|
|
|
|
|
>
|
|
|
|
|
<Icon name="add" className="h-3.5 w-3.5" />
|
|
|
|
|
</button>
|
|
|
|
|
</>
|
2026-04-27 15:45:14 +03:00
|
|
|
) : null}
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-09-07 22:11:47 +01:00
|
|
|
</ScrollableOverlay>
|
2025-12-19 17:22:02 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const content = (
|
|
|
|
|
<div className="flex min-h-0 flex-1 flex-col gap-3">
|
|
|
|
|
{inputSection}
|
|
|
|
|
{resultsSection}
|
2025-12-19 17:22:02 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const footerHints = (
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-3 gap-y-1 typography-micro text-muted-foreground">
|
|
|
|
|
<span className="inline-flex items-center gap-1">
|
2026-05-13 13:26:15 +03:00
|
|
|
<Icon name="arrow-up-s" className="h-3.5 w-3.5" />
|
|
|
|
|
<Icon name="arrow-down-s" className="-ml-1 h-3.5 w-3.5" />
|
2026-04-27 15:45:14 +03:00
|
|
|
{t('directoryExplorerDialog.footer.navigate')}
|
|
|
|
|
</span>
|
|
|
|
|
<span className="inline-flex items-center gap-1">
|
2026-05-13 13:26:15 +03:00
|
|
|
<Icon name="corner-down-left" className="h-3.5 w-3.5" />
|
2026-04-27 15:45:14 +03:00
|
|
|
{t('directoryExplorerDialog.footer.select')}
|
|
|
|
|
</span>
|
|
|
|
|
<span className="inline-flex items-center gap-1">
|
|
|
|
|
<span>{submitModifierLabel}</span>
|
2026-05-13 13:26:15 +03:00
|
|
|
<Icon name="corner-down-left" className="h-3.5 w-3.5" />
|
2026-04-27 15:45:14 +03:00
|
|
|
{t('directoryExplorerDialog.footer.add')}
|
|
|
|
|
</span>
|
2026-01-08 23:53:03 +02:00
|
|
|
</div>
|
2025-12-07 19:32:53 +02:00
|
|
|
);
|
|
|
|
|
|
2026-04-27 15:45:14 +03:00
|
|
|
const renderFooter = () => (
|
2025-12-07 19:32:53 +02:00
|
|
|
<>
|
2026-04-27 15:45:14 +03:00
|
|
|
{!isMobile ? footerHints : null}
|
|
|
|
|
<div className={cn('flex w-full flex-row justify-end gap-2 sm:w-auto', isMobile && 'justify-stretch')}>
|
2026-08-07 01:49:44 +03:00
|
|
|
{canRequestAccess ? (
|
2026-05-07 01:10:17 +03:00
|
|
|
<Button variant="ghost" size="xs" onClick={handleOpenInFinder} disabled={isConfirming || isOpeningFinder || isCloneMode}>
|
2026-04-27 15:45:14 +03:00
|
|
|
{isOpeningFinder ? t('directoryExplorerDialog.actions.openingFinder') : t('directoryExplorerDialog.actions.openInFinder')}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
2026-08-05 10:26:30 +03:00
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="xs"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setIsCloneMode((value) => !value);
|
|
|
|
|
setSelectedPaths([]);
|
|
|
|
|
}}
|
|
|
|
|
disabled={isConfirming || isOpeningFinder}
|
|
|
|
|
className={cn(isMobile && 'flex-1')}
|
|
|
|
|
>
|
2026-05-07 01:10:17 +03:00
|
|
|
{isCloneMode ? t('directoryExplorerDialog.actions.addLocalProject') : t('directoryExplorerDialog.actions.cloneRepository')}
|
2026-04-27 15:45:14 +03:00
|
|
|
</Button>
|
|
|
|
|
{isMobile ? (
|
2026-05-07 01:10:17 +03:00
|
|
|
<Button size="xs" onClick={() => void finalizeSelection(targetPath)} disabled={isCloneMode ? !canSubmitClone : !canAddProject} className="flex-1">
|
2026-04-27 15:45:14 +03:00
|
|
|
{submitActionLabel}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
2025-12-07 19:32:53 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (isMobile) {
|
|
|
|
|
return (
|
|
|
|
|
<MobileOverlayPanel
|
|
|
|
|
open={open}
|
2026-04-27 15:45:14 +03:00
|
|
|
onClose={handleClose}
|
2026-04-26 14:03:39 +03:00
|
|
|
title={t('directoryExplorerDialog.title')}
|
2026-08-02 16:25:16 +03:00
|
|
|
// Height only — the width stays on MobileOverlayPanel's shared max-w-lg
|
|
|
|
|
// so this sheet matches every other mobile overlay on wide screens.
|
|
|
|
|
className="h-[88dvh] max-h-[720px]"
|
2026-04-26 21:24:40 +08:00
|
|
|
contentMaxHeightClassName="flex-1"
|
2026-04-27 15:45:14 +03:00
|
|
|
footer={<div className="flex flex-col gap-2">{renderFooter()}</div>}
|
2025-12-07 19:32:53 +02:00
|
|
|
>
|
2026-04-27 15:45:14 +03:00
|
|
|
<div className="flex h-full min-h-0 flex-col gap-3">
|
|
|
|
|
<div className="flex justify-end">{showHiddenToggle}</div>
|
|
|
|
|
{content}
|
|
|
|
|
</div>
|
2025-12-07 19:32:53 +02:00
|
|
|
</MobileOverlayPanel>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
|
|
|
<DialogContent
|
2026-04-27 15:45:14 +03:00
|
|
|
className="flex w-full max-w-xl flex-col gap-0 overflow-hidden p-0 sm:max-h-[80vh]"
|
2026-05-17 23:31:28 +03:00
|
|
|
initialFocus={false}
|
2025-12-07 19:32:53 +02:00
|
|
|
>
|
2026-04-27 15:45:14 +03:00
|
|
|
<DialogHeader className="px-5 pb-2 pt-5">
|
|
|
|
|
<div className="flex items-start justify-between gap-4">
|
|
|
|
|
<div>
|
|
|
|
|
<DialogTitle>{t('directoryExplorerDialog.title')}</DialogTitle>
|
|
|
|
|
<DialogDescription className="mt-2">{t('directoryExplorerDialog.description')}</DialogDescription>
|
|
|
|
|
</div>
|
|
|
|
|
{showHiddenToggle}
|
|
|
|
|
</div>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<div className="min-h-0 flex-1 px-2 pb-0">{content}</div>
|
|
|
|
|
<DialogFooter className="flex w-full flex-col gap-3 px-5 py-3 sm:flex-row sm:items-center sm:justify-between">
|
|
|
|
|
{renderFooter()}
|
2025-12-07 19:32:53 +02:00
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|