Skip to content

Commit 08aaae5

Browse files
committed
feat(mobile, app-shell): native connection switcher and connectionRemoved in the mobile shell
The header organization button opens the shell's native switcher when the bridge announces showConnectionSwitcher; useNativeHostRouting answers the shell's connectionRemoved event through event.waitUntil. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTVYu9vDSx28X2zi7h
1 parent 04e0c2c commit 08aaae5

9 files changed

Lines changed: 385 additions & 17 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@object-ui/mobile': patch
3+
'@object-ui/app-shell': patch
4+
---
5+
6+
feat(mobile, app-shell): inside the ObjectStack mobile shell, the header's organization button opens the shell's native connection switcher, and the Console answers the shell's `connectionRemoved` event
7+
8+
Clause-②: yes (widening)
9+
Metadata-protocol: none
10+
11+
The mobile shell's bridge (`window.ObjectStackNative`) gained `showConnectionSwitcher()` and the `objectstack-native:connectionRemoved` window event. The Console now uses both:
12+
13+
- **Organization button.** When the shell announces `has('showConnectionSwitcher')`, `WorkspaceSwitcher`'s button calls `showConnectionSwitcher()`, which opens the shell's own sheet of saved organizations and company servers. The web organization menu does not open. In a plain browser, or in a shell that does not announce the capability, the button opens the web menu as before.
14+
- **Removed connection.** `useNativeHostRouting` listens for `objectstack-native:connectionRemoved` while the bridge is present and calls `event.waitUntil()` with the promise its new optional `onConnectionRemoved` returns. Omitted, the promise is already resolved, so the shell is released at once. No Console path registers a device push token yet, so there is nothing to unregister today. The option is where that unregister step goes once registration exists.
15+
16+
`@object-ui/mobile` adds `showConnectionSwitcher` to `ObjectStackNative` and `NativeHostMethod`, and exports `nativeShowConnectionSwitcher()`, `nativeHostCan(host, method)` (the wrappers' own feature-detect), `NATIVE_CONNECTION_REMOVED_EVENT` and the `NativeConnectionRemovedEvent` type. `@object-ui/app-shell` now depends on `@object-ui/mobile`.

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ui/packages/app-shell/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"@object-ui/fields": "workspace:*",
8484
"@object-ui/i18n": "workspace:*",
8585
"@object-ui/layout": "workspace:*",
86+
"@object-ui/mobile": "workspace:*",
8687
"@object-ui/permissions": "workspace:*",
8788
"@object-ui/plugin-editor": "workspace:*",
8889
"@object-ui/providers": "workspace:*",

‎ui/packages/app-shell/src/layout/WorkspaceSwitcher.tsx‎

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
* OrganizationsPage), plus shortcuts to manage members / create a workspace.
1515
* - No org context at all: renders nothing.
1616
*
17+
* Inside the ObjectStack mobile shell (`@object-ui/mobile`), when the shell
18+
* announces `showConnectionSwitcher`, the same button opens the shell's native
19+
* switcher instead of the dropdown: it lists the saved organizations and
20+
* company servers the shell holds, which the web menu cannot see. In a plain
21+
* browser, or a shell without the capability, the dropdown opens as before.
22+
*
1723
* Under `group` tenancy posture (ADR-0105) the switcher's meaning changes:
1824
* the active organization is only the WRITE target — reads span every
1925
* organization the member belongs to — so the dropdown labels it as the
@@ -25,6 +31,7 @@ import { useNavigate } from 'react-router-dom';
2531
import { useAuth } from '@object-ui/auth';
2632
import type { AuthOrganization } from '@object-ui/auth';
2733
import { useObjectTranslation } from '@object-ui/i18n';
34+
import { nativeHostCan, nativeShowConnectionSwitcher, useNativeHost } from '@object-ui/mobile';
2835
import {
2936
DropdownMenu,
3037
DropdownMenuTrigger,
@@ -45,6 +52,7 @@ export function WorkspaceSwitcher() {
4552
const { organizations, activeOrganization, switchOrganization, getAuthConfig } = useAuth();
4653
const [multiOrgDisabled, setMultiOrgDisabled] = useState(false);
4754
const isGroupPosture = useTenancyPosture() === 'group';
55+
const nativeSwitcher = nativeHostCan(useNativeHost(), 'showConnectionSwitcher');
4856

4957
useEffect(() => {
5058
let cancelled = false;
@@ -100,15 +108,42 @@ export function WorkspaceSwitcher() {
100108
});
101109
};
102110

103-
return (
104-
<DropdownMenu>
105-
<DropdownMenuTrigger
106-
className="ml-2 inline-flex max-w-[14rem] items-center gap-1.5 rounded-md px-1.5 py-1 text-sm font-medium text-foreground/80 transition-colors hover:bg-accent hover:text-foreground"
111+
const triggerClassName =
112+
'ml-2 inline-flex max-w-[14rem] items-center gap-1.5 rounded-md px-1.5 py-1 text-sm font-medium text-foreground/80 transition-colors hover:bg-accent hover:text-foreground';
113+
const triggerContent = (
114+
<>
115+
<OrgBadge name={current.name} />
116+
<span className="hidden truncate sm:inline">{current.name}</span>
117+
<ChevronsUpDown className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
118+
</>
119+
);
120+
121+
// The shell's sheet replaces the web menu; switching there is the shell's
122+
// own flow, so nothing is sent from here.
123+
if (nativeSwitcher) {
124+
return (
125+
<button
126+
type="button"
127+
className={triggerClassName}
107128
data-testid="workspace-switcher"
129+
data-native-switcher=""
130+
onClick={() => {
131+
void nativeShowConnectionSwitcher().then((result) => {
132+
if (result.status === 'failed') {
133+
console.warn('[WorkspaceSwitcher] native connection switcher failed', result.code, result.message);
134+
}
135+
});
136+
}}
108137
>
109-
<OrgBadge name={current.name} />
110-
<span className="hidden truncate sm:inline">{current.name}</span>
111-
<ChevronsUpDown className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
138+
{triggerContent}
139+
</button>
140+
);
141+
}
142+
143+
return (
144+
<DropdownMenu>
145+
<DropdownMenuTrigger className={triggerClassName} data-testid="workspace-switcher">
146+
{triggerContent}
112147
</DropdownMenuTrigger>
113148
<DropdownMenuContent align="start" className="w-60">
114149
<DropdownMenuLabel className="text-xs text-muted-foreground">
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* WorkspaceSwitcher inside the ObjectStack mobile shell.
5+
*
6+
* When the shell's bridge (`window.ObjectStackNative`, read through
7+
* `@object-ui/mobile`) announces `showConnectionSwitcher`, the header's
8+
* organization button opens the shell's native switcher and the web menu does
9+
* not open. With no bridge, or a bridge without that capability, the web menu
10+
* opens exactly as before. The dropdown is the real one, opened by a real
11+
* click, so "the web menu opens" is observed, not assumed.
12+
*/
13+
14+
import '@testing-library/jest-dom/vitest';
15+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
16+
import { render, screen, cleanup } from '@testing-library/react';
17+
import userEvent from '@testing-library/user-event';
18+
19+
vi.mock('@object-ui/i18n', async (importOriginal) => ({
20+
...(await importOriginal<Record<string, unknown>>()),
21+
useObjectTranslation: () => ({
22+
t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key),
23+
}),
24+
}));
25+
26+
vi.mock('react-router-dom', async (importOriginal) => ({
27+
...(await importOriginal<Record<string, unknown>>()),
28+
useNavigate: () => vi.fn(),
29+
}));
30+
31+
let authState: Record<string, unknown>;
32+
vi.mock('@object-ui/auth', async (importOriginal) => ({
33+
...(await importOriginal<Record<string, unknown>>()),
34+
useAuth: () => authState,
35+
}));
36+
37+
import { NATIVE_HOST_GLOBAL } from '@object-ui/mobile';
38+
import { WorkspaceSwitcher } from '../WorkspaceSwitcher';
39+
40+
const ORGS = [
41+
{ id: 'org_a', name: 'Plant A', slug: 'plant-a' },
42+
{ id: 'org_b', name: 'Plant B', slug: 'plant-b' },
43+
];
44+
45+
const BASE_METHODS = ['getPushToken', 'haptic', 'openExternal', 'navigate', 'resetServer'];
46+
47+
function installBridge(capabilities: string[]) {
48+
const showConnectionSwitcher = vi.fn(async () => null);
49+
(window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL] = {
50+
version: 1,
51+
platform: 'ios',
52+
appVersion: '2.1.0',
53+
capabilities,
54+
safeArea: { top: 47, bottom: 34, left: 0, right: 0 },
55+
has: (capability: string) => capabilities.includes(capability),
56+
getPushToken: vi.fn(async () => null),
57+
haptic: vi.fn(async () => null),
58+
openExternal: vi.fn(async () => null),
59+
navigate: vi.fn(async () => null),
60+
resetServer: vi.fn(async () => null),
61+
showConnectionSwitcher,
62+
ready: vi.fn(),
63+
routeChanged: vi.fn(),
64+
};
65+
return showConnectionSwitcher;
66+
}
67+
68+
beforeEach(() => {
69+
delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL];
70+
authState = {
71+
organizations: ORGS,
72+
activeOrganization: ORGS[0],
73+
switchOrganization: vi.fn(),
74+
getAuthConfig: vi.fn().mockResolvedValue({ features: { multiOrgEnabled: true } }),
75+
};
76+
});
77+
78+
afterEach(() => {
79+
cleanup();
80+
delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL];
81+
});
82+
83+
describe('WorkspaceSwitcher in the mobile shell', () => {
84+
it('with showConnectionSwitcher announced, the button opens the native switcher and no web menu', async () => {
85+
const showConnectionSwitcher = installBridge([...BASE_METHODS, 'showConnectionSwitcher']);
86+
render(<WorkspaceSwitcher />);
87+
88+
const button = screen.getByTestId('workspace-switcher');
89+
expect(button).toHaveAttribute('data-native-switcher');
90+
expect(button).not.toHaveAttribute('aria-haspopup');
91+
92+
await userEvent.click(button);
93+
94+
expect(showConnectionSwitcher).toHaveBeenCalledTimes(1);
95+
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
96+
expect(screen.queryByText('Plant B')).not.toBeInTheDocument();
97+
expect(authState.switchOrganization).not.toHaveBeenCalled();
98+
});
99+
100+
it('control: with no bridge, the button opens the web organization menu', async () => {
101+
render(<WorkspaceSwitcher />);
102+
103+
const button = screen.getByTestId('workspace-switcher');
104+
expect(button).not.toHaveAttribute('data-native-switcher');
105+
106+
await userEvent.click(button);
107+
108+
expect(await screen.findByRole('menu')).toBeInTheDocument();
109+
expect(screen.getByText('Switch organization')).toBeInTheDocument();
110+
expect(screen.getByText('Plant B')).toBeInTheDocument();
111+
});
112+
113+
it('control: a shell without the capability keeps the web menu and never calls the native switcher', async () => {
114+
const showConnectionSwitcher = installBridge(BASE_METHODS);
115+
render(<WorkspaceSwitcher />);
116+
117+
const button = screen.getByTestId('workspace-switcher');
118+
expect(button).not.toHaveAttribute('data-native-switcher');
119+
120+
await userEvent.click(button);
121+
122+
expect(await screen.findByRole('menu')).toBeInTheDocument();
123+
expect(screen.getByText('Plant B')).toBeInTheDocument();
124+
expect(showConnectionSwitcher).not.toHaveBeenCalled();
125+
});
126+
});

0 commit comments

Comments
 (0)