refactor(webui): simplify directory picker state and filtering

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-08 20:33:15 +08:00
1 parent 66c8d41f66
commit 8e8b14897e
3 files changed
+17 -15

No files matched your search

+3 -1
View File
@@ -56,10 +56,12 @@ def browse_workspace_directories(
path = path.parent
partial = True
path = path.resolve(strict=True)
folded_query = query.casefold()
matches: list[ProjectDirectory] = []
with os.scandir(path) as entries:
for entry in entries:
if not entry.name.casefold().startswith(prefix) or query.casefold() not in entry.name.casefold():
name = entry.name.casefold()
if not name.startswith(prefix) or folded_query not in name:
continue
try:
if not entry.is_dir():
+4 -3
View File
@@ -178,7 +178,8 @@ def workspaces_payload(
can_use_full_access: bool,
folder_picker_available: bool = False,
) -> dict[str, Any]:
default_access_mode = read_webui_default_access_mode()
state = read_webui_workspace_state()
default_access_mode = state["default_access_mode"]
default_scope = (
default_workspace_scope(
default_workspace,
@@ -195,11 +196,11 @@ def workspaces_payload(
"host": {"name": socket.gethostname(), "platform": platform.system()},
"favorite_projects": [
{"name": Path(path).name or path, "path": path}
for path in read_webui_workspace_state()["favorite_projects"]
for path in state["favorite_projects"]
],
"recent_projects": [
{"name": Path(path).name or path, "path": path}
for path in read_webui_workspace_state()["recent_projects"]
for path in state["recent_projects"]
],
"controls": {
"can_change_project": can_change_project,
@@ -53,7 +53,7 @@ function WorkspaceFolderMenu({ path, pinned, busy, onToggle, onRequestFocus, chi
);
}
type PickerOption = ProjectDirectory & { kind: "directory" | "manual"; group: string };
type PickerOption = ProjectDirectory & { kind: "directory" | "manual" };
type PickerColumn = { path: string; options: PickerOption[]; selectedPath: string | null; hidden: boolean };
function WorkspacePickerTooltip({ label, children }: { label: string; children: ReactElement }) {
@@ -144,8 +144,7 @@ export function WorkspaceProjectPicker({ isHero, connected = false, disabled, sc
columnsRef.current = element;
setColumnsElement(element);
}, []);
const [columnCapacity, setColumnCapacity] = useState(3);
const visibleColumns = columnCapacity;
const [visibleColumns, setVisibleColumns] = useState(3);
const pickerSession = useRef(0);
const completionRequest = useRef(0);
const triggerRef = useRef<HTMLButtonElement>(null);
@@ -222,7 +221,6 @@ export function WorkspaceProjectPicker({ isHero, connected = false, disabled, sc
}, [open, pickingFolder, pathError]);
const ancestorPaths = JSON.stringify(previousColumns.filter(column => column.hidden !== showHidden).map(column => column.path));
const directoryGroup = t("workspace.picker.directories");
useEffect(() => {
if (!open || !canBrowse || !directoryCache) return;
const paths: string[] = JSON.parse(ancestorPaths);
@@ -232,17 +230,17 @@ export function WorkspaceProjectPicker({ isHero, connected = false, disabled, sc
if (!active) return;
setPreviousColumns(columns => columns.map(column => {
const result = results.find(result => sameWorkspacePath(column.path, result.path));
if (!result || !sameWorkspacePath(column.path, result.path)) return column;
return { ...column, hidden: showHidden, options: result.entries.map(entry => ({ ...entry, kind: "directory" as const, group: directoryGroup })) };
if (!result) return column;
return { ...column, hidden: showHidden, options: result.entries.map(entry => ({ ...entry, kind: "directory" as const })) };
}));
}).catch((err: Error) => { if (active) setPathError(err.message); });
return () => { active = false; };
}, [open, canBrowse, directoryCache, ancestorPaths, showHidden, directoryGroup]);
}, [open, canBrowse, directoryCache, ancestorPaths, showHidden]);
useLayoutEffect(() => {
const viewport = columnsElement;
if (!open || !viewport) return;
const measure = () => setColumnCapacity(Math.max(1, Math.min(3, Math.floor(viewport.clientWidth / 240))));
const measure = () => setVisibleColumns(Math.max(1, Math.min(3, Math.floor(viewport.clientWidth / 240))));
measure();
const observer = new ResizeObserver(measure);
observer.observe(viewport);
@@ -355,8 +353,8 @@ export function WorkspaceProjectPicker({ isHero, connected = false, disabled, sc
...(currentProjectScope ? [{ name: projectLabel, path: currentProjectScope.project_path }] : []),
].filter((project, index, all) => all.findIndex(item => sameWorkspacePath(item.path, project.path)) === index && !isFavorite(project.path));
const options: PickerOption[] = directory && !loading
? directory.entries.map(entry => ({ ...entry, kind: "directory", group: directoryGroup })) : [];
if (!canBrowse && absoluteDraft) options.push({ name: t("workspace.dialog.usePath"), path: pathDraft.trim(), kind: "manual", group: directoryGroup });
? directory.entries.map(entry => ({ ...entry, kind: "directory" })) : [];
if (!canBrowse && absoluteDraft) options.push({ name: t("workspace.dialog.usePath"), path: pathDraft.trim(), kind: "manual" });
function openShortcut(project: ProjectDirectory) {
if (!canBrowse) { void chooseProject(project.path, project.name); return; }
setBasePath(project.path);
@@ -380,7 +378,8 @@ export function WorkspaceProjectPicker({ isHero, connected = false, disabled, sc
try {
const result = await directoryCache.load(request.path, request.query, showHidden || request.query.startsWith("."));
if (id !== completionRequest.current || session !== pickerSession.current) return;
const first = result.entries.find(entry => entry.name.toLocaleLowerCase().startsWith(request.query.toLocaleLowerCase()));
const prefix = request.query.toLocaleLowerCase();
const first = result.entries.find(entry => entry.name.toLocaleLowerCase().startsWith(prefix));
if (first) navigate(first.path);
} catch (err) {
if (id === completionRequest.current && session === pickerSession.current) setPathError((err as Error).message);