Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import { useAppNavigationHost } from "@/lib/app-navigation-host";
import { AppFileExternalNavigationHost } from "./AppFileExternalNavigationHost";

const openPreferred = vi.hoisted(() => vi.fn());
const recordAccepted = vi.fn();

vi.mock("@/hooks/useResolvedLiveFileTarget", () => ({
useResolvedLiveFileTarget: () => ({
useResolvedLiveFileTarget: (target: { path: string }) => ({
status: "available",
absolutePath: "/workspace/src/example.ts",
absolutePath: `/workspace/${target.path}`,
hostId: "host_1",
openContext: { kind: "local" },
}),
Expand All @@ -32,28 +33,55 @@ vi.mock("@/hooks/useLocalOpenTargets", () => ({
function Probe() {
const navigation = useAppNavigationHost();
return (
<button
type="button"
onClick={() =>
navigation.openFileExternally({
target: {
kind: "workspace",
environmentId: "env_1",
path: "src/example.ts",
},
location: { kind: "line", line: 12, column: 3 },
})
}
>
Open external
</button>
<>
<button
type="button"
onClick={() =>
navigation.openFileExternally({
target: {
kind: "workspace",
environmentId: "env_1",
path: "src/example.ts",
},
location: { kind: "line", line: 12, column: 3 },
})
}
>
Open external
</button>
<button
type="button"
onClick={() => {
const firstAccepted = navigation.openFileExternally({
target: {
kind: "workspace",
environmentId: "env_1",
path: "src/first.ts",
},
location: { kind: "line", line: 10, column: 2 },
});
const secondAccepted = navigation.openFileExternally({
target: {
kind: "workspace",
environmentId: "env_1",
path: "src/second.ts",
},
location: { kind: "line", line: 20, column: 4 },
});
recordAccepted(firstAccepted, secondAccepted);
}}
>
Open two external
</button>
</>
);
}

afterEach(() => {
cleanup();
openPreferred.mockReset();
openPreferred.mockResolvedValue(true);
recordAccepted.mockReset();
});

describe("AppFileExternalNavigationHost", () => {
Expand All @@ -74,4 +102,28 @@ describe("AppFileExternalNavigationHost", () => {
{ timeout: 5_000 },
);
});

it("dispatches queued intents once each in FIFO order", async () => {
render(
<AppFileExternalNavigationHost>
<Probe />
</AppFileExternalNavigationHost>,
);
fireEvent.click(screen.getByRole("button", { name: "Open two external" }));

expect(recordAccepted).toHaveBeenCalledWith(true, true);
await waitFor(() => expect(openPreferred).toHaveBeenCalledTimes(2), {
timeout: 5_000,
});
expect(openPreferred).toHaveBeenNthCalledWith(1, {
columnNumber: 2,
lineNumber: 10,
path: "/workspace/src/first.ts",
});
expect(openPreferred).toHaveBeenNthCalledWith(2, {
columnNumber: 4,
lineNumber: 20,
path: "/workspace/src/second.ts",
});
});
});
17 changes: 13 additions & 4 deletions apps/app/src/components/plugin/AppFileExternalNavigationHost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ const LazyAppFileExternalNavigationDispatcher = lazy(() =>
),
);

interface ExternalFileIntentRequest {
id: number;
intent: ExperimentalFileOpenOptions;
}

/** App-wide preferred-external file dispatcher; discovery starts on activation. */
export function AppFileExternalNavigationHost({
children,
}: {
children: ReactNode;
}) {
const [queue, setQueue] = useState<ExperimentalFileOpenOptions[]>([]);
const [queue, setQueue] = useState<ExternalFileIntentRequest[]>([]);
const queueRef = useRef(queue);
const replaceQueue = useCallback((next: ExperimentalFileOpenOptions[]) => {
const nextRequestIdRef = useRef(0);
const replaceQueue = useCallback((next: ExternalFileIntentRequest[]) => {
queueRef.current = next;
setQueue(next);
}, []);
Expand All @@ -38,7 +44,9 @@ export function AppFileExternalNavigationHost({
}
// Public SDK callers are parsed by useBbNavigate before capabilities are
// invoked; this host only queues that already-normalized internal value.
replaceQueue([...queueRef.current, intent]);
const request = { id: nextRequestIdRef.current, intent };
nextRequestIdRef.current += 1;
replaceQueue([...queueRef.current, request]);
return true;
},
[replaceQueue],
Expand All @@ -58,7 +66,8 @@ export function AppFileExternalNavigationHost({
{current === null ? null : (
<Suspense fallback={null}>
<LazyAppFileExternalNavigationDispatcher
intent={current}
key={current.id}
intent={current.intent}
onSettled={settleCurrent}
/>
</Suspense>
Expand Down
Loading