Files
paseo/packages/app/src/stores/navigation-active-workspace-store.ts
T
Mohamed BoudraandGitHub 868479c493 Fix workspace navigation regression on web (#772)
Centralize workspace navigation behind a small route-derived API so the workspace shell reads active selection from the current Expo Router route instead of a separately synchronized store.

Remove the stale layout-driven active-workspace write that regressed web after 02e5b564.
2026-05-06 13:22:31 +08:00

73 lines
2.1 KiB
TypeScript

import { router, useLocalSearchParams, usePathname, type Href } from "expo-router";
import {
buildHostWorkspaceRoute,
decodeWorkspaceIdFromPathSegment,
parseHostWorkspaceRouteFromPathname,
} from "@/utils/host-routes";
export interface ActiveWorkspaceSelection {
serverId: string;
workspaceId: string;
}
interface NavigateToWorkspaceOptions {
currentPathname?: string | null;
}
let lastWorkspaceSelection: ActiveWorkspaceSelection | null = null;
function getParamValue(value: string | string[] | undefined): string {
if (typeof value === "string") {
return value.trim();
}
if (Array.isArray(value)) {
const firstValue = value[0];
return typeof firstValue === "string" ? firstValue.trim() : "";
}
return "";
}
function parseWorkspaceSelectionFromRouteParams(params: {
serverId?: string | string[];
workspaceId?: string | string[];
}): ActiveWorkspaceSelection | null {
const serverId = getParamValue(params.serverId);
const workspaceValue = getParamValue(params.workspaceId);
const workspaceId = workspaceValue ? decodeWorkspaceIdFromPathSegment(workspaceValue) : null;
if (!serverId || !workspaceId) {
return null;
}
return { serverId, workspaceId };
}
export function navigateToWorkspace(
serverId: string,
workspaceId: string,
_options: NavigateToWorkspaceOptions = {},
) {
lastWorkspaceSelection = { serverId, workspaceId };
router.dismissTo(buildHostWorkspaceRoute(serverId, workspaceId) as Href);
}
export function navigateToLastWorkspace(): boolean {
if (!lastWorkspaceSelection) {
return false;
}
navigateToWorkspace(lastWorkspaceSelection.serverId, lastWorkspaceSelection.workspaceId);
return true;
}
export function useActiveWorkspaceSelection(): ActiveWorkspaceSelection | null {
const params = useLocalSearchParams<{
serverId?: string | string[];
workspaceId?: string | string[];
}>();
const selection =
parseHostWorkspaceRouteFromPathname(usePathname()) ??
parseWorkspaceSelectionFromRouteParams(params);
if (selection) {
lastWorkspaceSelection = selection;
}
return selection;
}