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