Skip to content

Commit c962ab9

Browse files
committed
feat(app-shell): the native switcher button renders at one membership too
In the mobile shell the sheet lists the shell's connections across company servers, so the button no longer waits for a second membership on this server; the read-only organization indicator steps aside so the header draws the organization once. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTVYu9vDSx28X2zi7h
1 parent dec7cd0 commit c962ab9

4 files changed

Lines changed: 132 additions & 38 deletions

File tree

‎.changeset/29458-native-connection-switcher.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Metadata-protocol: none
1010

1111
The mobile shell's bridge (`window.ObjectStackNative`) gained `showConnectionSwitcher()` and the `objectstack-native:connectionRemoved` window event. The Console now uses both:
1212

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.
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 the shell the button renders whenever there is an active organization, at one membership too, since the sheet lists the shell's connections across company servers; `CurrentOrganizationIndicator` then renders nothing, so the organization is drawn once. In a plain browser, or in a shell that does not announce the capability, both render as before.
1414
- **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.
1515

1616
`@object-ui/mobile` adds `showConnectionSwitcher` to `NativeHostMethod` and, as an optional method (an older shell does not carry it), to `ObjectStackNative`, 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`.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,29 @@
3333
* `@objectstack/spec/security`, which measured +237 KB on the eager chunk. That
3434
* file's docblock carries the measurement and the reasoning.
3535
*
36+
* Inside the mobile shell, when it announces `showConnectionSwitcher`, this
37+
* renders nothing: `WorkspaceSwitcher` then draws the same badge and name, at
38+
* the same widths, as the button that opens the shell's native switcher.
39+
*
3640
* An absent or unrecognized posture (older server, config fetch still in
3741
* flight or failed) renders nothing, matching how every other posture-keyed
3842
* affordance in this repo fails toward today's rendering.
3943
*/
4044

4145
import { useAuth } from '@object-ui/auth';
4246
import { useObjectTranslation } from '@object-ui/i18n';
47+
import { nativeHostCan, useNativeHost } from '@object-ui/mobile';
4348
import { OrgBadge } from './OrgBadge.js';
4449
import { postureHasOrgWall, useTenancyPosture } from '../hooks/useTenancyPosture.js';
4550

4651
export function CurrentOrganizationIndicator() {
4752
const { t } = useObjectTranslation();
4853
const { organizations, activeOrganization } = useAuth();
4954
const posture = useTenancyPosture();
55+
const nativeSwitcher = nativeHostCan(useNativeHost(), 'showConnectionSwitcher');
56+
57+
// The shell's switcher button carries the organization instead.
58+
if (nativeSwitcher) return null;
5059

5160
// No organization wall in force ⇒ the top bar stays organization-silent,
5261
// exactly as it is today. Also covers the not-yet-resolved config.

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

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
* Header-left organization (workspace) switcher — the standard place users
55
* expect "which org am I in / switch org" to live (Linear/Vercel/GitHub style).
66
*
7-
* - Single-org users (the vast majority): render NOTHING. With one org there is
8-
* nothing to switch to, so a dropdown here would control nothing. The
9-
* switcher is a multi-org affordance. Where a tenancy wall IS in force the
7+
* - Single-org users (the vast majority): render NOTHING on the web. With one
8+
* org there is nothing to switch to, so a dropdown here would control
9+
* nothing. The web switcher is a multi-org affordance (the mobile shell's
10+
* native switcher, below, is not). Where a tenancy wall IS in force the
1011
* org name still matters at one membership — `CurrentOrganizationIndicator`
1112
* renders it read-only there (objectui#5287), without a control.
1213
* - Multi-org users: the active org name + a dropdown to switch orgs inline
@@ -15,10 +16,12 @@
1516
* - No org context at all: renders nothing.
1617
*
1718
* 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.
19+
* announces `showConnectionSwitcher`, the button opens the shell's native
20+
* switcher instead of the dropdown, and it renders whenever there is an active
21+
* organization, at any membership count: the sheet lists the connections the
22+
* shell holds across company servers, which this server's membership list
23+
* cannot count. In a plain browser, or a shell without the capability, the
24+
* rules above apply unchanged.
2225
*
2326
* Under `group` tenancy posture (ADR-0105) the switcher's meaning changes:
2427
* the active organization is only the WRITE target — reads span every
@@ -79,35 +82,6 @@ export function WorkspaceSwitcher() {
7982
// rather than an empty switcher or a guessed name.
8083
if (!current) return null;
8184

82-
// Single-org: render nothing. With only one org there's nothing to switch to,
83-
// so a dropdown here would be a control that controls nothing. The switcher
84-
// appears only once a second org exists. On a walled deployment the org NAME
85-
// is still load-bearing context at one membership — `CurrentOrganizationIndicator`
86-
// renders it read-only for exactly this case (objectui#5287); this rule is
87-
// unchanged.
88-
if (orgList.length <= 1) return null;
89-
90-
const handleSwitch = (org: AuthOrganization) => {
91-
if (org.id === current.id) return;
92-
// objectui#13231 — ask BEFORE `set-active` is sent. Asking at the reload
93-
// would be too late: the server has already switched, and the page holding
94-
// the edits reloads itself for the new organization (dropping them, and its
95-
// leave prompt with them) before the document load starts. On Cancel
96-
// nothing is sent: the organization, the URL and the edits stay.
97-
confirmUnsavedLeave(async () => {
98-
try {
99-
await switchOrganization(org.id);
100-
// switchOrganization only updates state; reload to the console root so the
101-
// new active org propagates to every data scope app-wide AND
102-
// RootLandingRedirect resolves the right landing (single-app workspace →
103-
// that app, not the redundant launcher). Same as OrganizationsPage.
104-
window.location.href = resolveRootUrl();
105-
} catch (err) {
106-
console.error('[WorkspaceSwitcher] switch failed', err);
107-
}
108-
});
109-
};
110-
11185
const triggerClassName =
11286
'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';
11387
const triggerContent = (
@@ -140,6 +114,35 @@ export function WorkspaceSwitcher() {
140114
);
141115
}
142116

117+
// Single-org: render nothing. With only one org there's nothing to switch to,
118+
// so a dropdown here would be a control that controls nothing. The switcher
119+
// appears only once a second org exists. On a walled deployment the org NAME
120+
// is still load-bearing context at one membership — `CurrentOrganizationIndicator`
121+
// renders it read-only for exactly this case (objectui#5287); this rule is
122+
// unchanged.
123+
if (orgList.length <= 1) return null;
124+
125+
const handleSwitch = (org: AuthOrganization) => {
126+
if (org.id === current.id) return;
127+
// objectui#13231 — ask BEFORE `set-active` is sent. Asking at the reload
128+
// would be too late: the server has already switched, and the page holding
129+
// the edits reloads itself for the new organization (dropping them, and its
130+
// leave prompt with them) before the document load starts. On Cancel
131+
// nothing is sent: the organization, the URL and the edits stay.
132+
confirmUnsavedLeave(async () => {
133+
try {
134+
await switchOrganization(org.id);
135+
// switchOrganization only updates state; reload to the console root so the
136+
// new active org propagates to every data scope app-wide AND
137+
// RootLandingRedirect resolves the right landing (single-app workspace →
138+
// that app, not the redundant launcher). Same as OrganizationsPage.
139+
window.location.href = resolveRootUrl();
140+
} catch (err) {
141+
console.error('[WorkspaceSwitcher] switch failed', err);
142+
}
143+
});
144+
};
145+
143146
return (
144147
<DropdownMenu>
145148
<DropdownMenuTrigger className={triggerClassName} data-testid="workspace-switcher">

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
* not open. With no bridge, or a bridge without that capability, the web menu
1010
* opens exactly as before. The dropdown is the real one, opened by a real
1111
* click, so "the web menu opens" is observed, not assumed.
12+
*
13+
* In the shell the button renders at ONE membership too: the sheet lists the
14+
* shell's connections across servers, which this server's membership count
15+
* cannot see. On a walled deployment `CurrentOrganizationIndicator` then steps
16+
* aside, so the header carries the organization once, with the name shown at
17+
* the same widths the indicator showed it.
1218
*/
1319

1420
import '@testing-library/jest-dom/vitest';
1521
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
16-
import { render, screen, cleanup } from '@testing-library/react';
22+
import { render, screen, cleanup, waitFor } from '@testing-library/react';
1723
import userEvent from '@testing-library/user-event';
1824

1925
vi.mock('@object-ui/i18n', async (importOriginal) => ({
@@ -36,6 +42,8 @@ vi.mock('@object-ui/auth', async (importOriginal) => ({
3642

3743
import { NATIVE_HOST_GLOBAL } from '@object-ui/mobile';
3844
import { WorkspaceSwitcher } from '../WorkspaceSwitcher';
45+
import { CurrentOrganizationIndicator } from '../CurrentOrganizationIndicator';
46+
import { useTenancyPosture } from '../../hooks/useTenancyPosture';
3947

4048
const ORGS = [
4149
{ id: 'org_a', name: 'Plant A', slug: 'plant-a' },
@@ -124,3 +132,77 @@ describe('WorkspaceSwitcher in the mobile shell', () => {
124132
expect(showConnectionSwitcher).not.toHaveBeenCalled();
125133
});
126134
});
135+
136+
describe('WorkspaceSwitcher in the mobile shell at one membership (walled deployment)', () => {
137+
/** AppHeader's pair, in its order, plus the resolved posture to wait on. */
138+
function PostureProbe() {
139+
return <span data-testid="posture">{String(useTenancyPosture() ?? 'unresolved')}</span>;
140+
}
141+
function mountHeaderPair() {
142+
return render(
143+
<>
144+
<WorkspaceSwitcher />
145+
<CurrentOrganizationIndicator />
146+
<PostureProbe />
147+
</>,
148+
);
149+
}
150+
151+
beforeEach(() => {
152+
authState = {
153+
organizations: [ORGS[0]],
154+
activeOrganization: ORGS[0],
155+
switchOrganization: vi.fn(),
156+
getAuthConfig: vi.fn().mockResolvedValue({ features: { multiOrgEnabled: true, tenancyPosture: 'isolated' } }),
157+
};
158+
});
159+
160+
/** The name span of whichever surface carries the organization. */
161+
function nameSpan(container: HTMLElement): HTMLElement {
162+
const spans = Array.from(container.querySelectorAll('span')).filter((el) => el.textContent === ORGS[0].name);
163+
expect(spans).toHaveLength(1);
164+
return spans[0] as HTMLElement;
165+
}
166+
167+
it('with the capability, the native button renders, opens the sheet, and the read-only indicator steps aside', async () => {
168+
const showConnectionSwitcher = installBridge([...BASE_METHODS, 'showConnectionSwitcher']);
169+
const { container } = mountHeaderPair();
170+
await waitFor(() => expect(screen.getByTestId('posture')).toHaveTextContent('isolated'));
171+
172+
const button = screen.getByTestId('workspace-switcher');
173+
expect(button).toHaveAttribute('data-native-switcher');
174+
expect(screen.queryByTestId('current-organization-indicator')).not.toBeInTheDocument();
175+
// The organization is drawn once.
176+
expect(nameSpan(container).closest('[data-testid="workspace-switcher"]')).toBe(button);
177+
178+
await userEvent.click(button);
179+
expect(showConnectionSwitcher).toHaveBeenCalledTimes(1);
180+
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
181+
});
182+
183+
it('the native button shows the name at the same widths the indicator did', async () => {
184+
const plain = mountHeaderPair();
185+
await waitFor(() => expect(screen.getByTestId('current-organization-indicator')).toBeInTheDocument());
186+
const indicatorNameClass = nameSpan(plain.container).className;
187+
plain.unmount();
188+
189+
installBridge([...BASE_METHODS, 'showConnectionSwitcher']);
190+
const shell = mountHeaderPair();
191+
await waitFor(() => expect(screen.getByTestId('posture')).toHaveTextContent('isolated'));
192+
expect(nameSpan(shell.container).className).toBe(indicatorNameClass);
193+
});
194+
195+
it('control: with no bridge, one membership draws no switcher and the read-only indicator, as today', async () => {
196+
mountHeaderPair();
197+
await waitFor(() => expect(screen.getByTestId('current-organization-indicator')).toBeInTheDocument());
198+
expect(screen.queryByTestId('workspace-switcher')).not.toBeInTheDocument();
199+
});
200+
201+
it('control: a shell without the capability keeps today\'s single-membership rendering', async () => {
202+
const showConnectionSwitcher = installBridge(BASE_METHODS);
203+
mountHeaderPair();
204+
await waitFor(() => expect(screen.getByTestId('current-organization-indicator')).toBeInTheDocument());
205+
expect(screen.queryByTestId('workspace-switcher')).not.toBeInTheDocument();
206+
expect(showConnectionSwitcher).not.toHaveBeenCalled();
207+
});
208+
});

0 commit comments

Comments
 (0)