@startsimpli/integrations 0.2.76 → 0.2.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/package.json +3 -3
- package/src/__tests__/api-fetch-factories.test.ts +81 -0
- package/src/api/_fetch.ts +93 -61
- package/src/api/connections.ts +162 -152
- package/src/api/mcp-tokens.ts +25 -15
- package/src/components/pages/ConnectClaudePage.tsx +1 -14
- package/src/components/pages/MemberConnectionRoster.tsx +2 -5
- package/src/components/pages/PlanningPage.tsx +3 -15
- package/src/components/pages/connect-claude-format.ts +24 -0
- package/src/components/pages/connection-format.ts +20 -0
- package/src/components/pages/work-items.tsx +4 -51
- package/src/corpus/planning.ts +21 -0
- package/src/corpus/work-item-logic.ts +59 -0
- package/src/native/corpus/api.ts +2 -3
- package/src/native/index.ts +16 -0
- package/src/native/screens/connect-claude/ConnectClaudeScreen.tsx +102 -0
- package/src/native/screens/connect-claude/__tests__/controller.test.ts +116 -0
- package/src/native/screens/connect-claude/controller.ts +108 -0
- package/src/native/screens/connect-claude/fake.ts +57 -0
- package/src/native/screens/connect-claude/tab.tsx +19 -0
- package/src/native/screens/connections/ConnectionsScreens.tsx +108 -0
- package/src/native/screens/connections/__tests__/controller.test.ts +66 -0
- package/src/native/screens/connections/controller.ts +93 -0
- package/src/native/screens/connections/fake.ts +69 -0
- package/src/native/screens/connections/tab.tsx +15 -0
- package/src/native/screens/planning/PlanningScreens.tsx +178 -0
- package/src/native/screens/planning/__tests__/controller.test.ts +145 -0
- package/src/native/screens/planning/__tests__/format.test.ts +29 -0
- package/src/native/screens/planning/controller.ts +173 -0
- package/src/native/screens/planning/fake.ts +16 -0
- package/src/native/screens/planning/format.ts +49 -0
- package/src/native/screens/planning/tab.tsx +26 -0
- package/src/native/screens/shared/PressableCard.tsx +14 -0
- package/src/native/screens/shared/format.ts +10 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connect Claude as a framework-free controller over `McpTokensApi` (the same
|
|
3
|
+
* client contract the web `ConnectClaudePage` uses).
|
|
4
|
+
*
|
|
5
|
+
* MINTING IS NEVER AUTOMATIC. `list()` runs on open; `mint()` runs only from
|
|
6
|
+
* `mint()` below, which a screen calls from a button. Every mint issues another
|
|
7
|
+
* bearer that cannot be withdrawn before it expires (bd startsim-ja8mt), so no
|
|
8
|
+
* refresh, retry or effect may reach it. The token lives in memory only, is shown
|
|
9
|
+
* once, and `done()` forgets it - nothing is stored on the phone.
|
|
10
|
+
*/
|
|
11
|
+
import type { McpFoundry, McpMintedToken, McpTokensApi } from '../../../api/mcp-tokens';
|
|
12
|
+
import { expiryLine } from '../../../components/pages/connect-claude-format';
|
|
13
|
+
import { createStore, errorMessage } from '../store';
|
|
14
|
+
|
|
15
|
+
export type ShareText = (text: string) => Promise<boolean>;
|
|
16
|
+
|
|
17
|
+
export interface ConnectClaudeState {
|
|
18
|
+
phase: 'loading' | 'ready' | 'error';
|
|
19
|
+
foundries: McpFoundry[];
|
|
20
|
+
maxTtl: number;
|
|
21
|
+
defaultTtl: number;
|
|
22
|
+
/** Slug of the chosen workspace; empty until chosen when there are several. */
|
|
23
|
+
selected: string;
|
|
24
|
+
/** Chosen lifetime in days; null = the server default. */
|
|
25
|
+
ttlDays: number | null;
|
|
26
|
+
minting: boolean;
|
|
27
|
+
minted: McpMintedToken | null;
|
|
28
|
+
/** "Expires November 5, 2026 - about 30 days" for the minted token. */
|
|
29
|
+
expiry: string;
|
|
30
|
+
error: string | null;
|
|
31
|
+
/** What was just handed to the share sheet, so the button can say so. */
|
|
32
|
+
shared: '' | 'token' | 'command';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ConnectClaudeController {
|
|
36
|
+
getState(): ConnectClaudeState;
|
|
37
|
+
subscribe(listener: () => void): () => void;
|
|
38
|
+
refresh(): Promise<void>;
|
|
39
|
+
select(slug: string): void;
|
|
40
|
+
setTtlDays(days: number | null): void;
|
|
41
|
+
/** The ONLY call site of `api.mint`. */
|
|
42
|
+
mint(): Promise<void>;
|
|
43
|
+
share(which: 'token' | 'command'): Promise<void>;
|
|
44
|
+
/** Forget the token and return to the form. */
|
|
45
|
+
done(): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function createConnectClaudeController(
|
|
49
|
+
{ api, share, now = Date.now, foundrySlug }: { api: McpTokensApi; share: ShareText; now?: () => number; foundrySlug?: string },
|
|
50
|
+
): ConnectClaudeController {
|
|
51
|
+
const store = createStore<ConnectClaudeState>({
|
|
52
|
+
phase: 'loading', foundries: [], maxTtl: 90, defaultTtl: 30, selected: foundrySlug ?? '', ttlDays: null,
|
|
53
|
+
minting: false, minted: null, expiry: '', error: null, shared: '',
|
|
54
|
+
});
|
|
55
|
+
let inFlight = false;
|
|
56
|
+
|
|
57
|
+
async function refresh() {
|
|
58
|
+
try {
|
|
59
|
+
const r = await api.list();
|
|
60
|
+
const only = r.foundries.length === 1 ? r.foundries[0].slug : '';
|
|
61
|
+
store.set({
|
|
62
|
+
phase: 'ready', foundries: r.foundries, maxTtl: r.max_ttl_days, defaultTtl: r.default_ttl_days,
|
|
63
|
+
selected: store.getState().selected || only, error: null,
|
|
64
|
+
});
|
|
65
|
+
} catch (e) {
|
|
66
|
+
store.set({ phase: 'error', error: errorMessage(e, 'Could not load your workspaces.') });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
void refresh();
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
getState: store.getState,
|
|
73
|
+
subscribe: store.subscribe,
|
|
74
|
+
refresh,
|
|
75
|
+
select: (slug) => store.set({ selected: slug }),
|
|
76
|
+
setTtlDays: (days) => store.set({ ttlDays: days }),
|
|
77
|
+
async mint() {
|
|
78
|
+
const { selected, ttlDays } = store.getState();
|
|
79
|
+
if (!selected || inFlight) return;
|
|
80
|
+
inFlight = true;
|
|
81
|
+
store.set({ minting: true, error: null });
|
|
82
|
+
try {
|
|
83
|
+
// The ONLY call site. See the module docstring.
|
|
84
|
+
const minted = await api.mint({ foundry: selected, ...(ttlDays === null ? {} : { ttl_days: ttlDays }) });
|
|
85
|
+
store.set({ minted, expiry: expiryLine(minted.expires_at, now()), shared: '' });
|
|
86
|
+
} catch (e) {
|
|
87
|
+
// A 403 is the ordinary case, not an outage: the list is advisory and central decides.
|
|
88
|
+
store.set({ error: errorMessage(e, 'The token could not be created.') });
|
|
89
|
+
} finally {
|
|
90
|
+
inFlight = false;
|
|
91
|
+
store.set({ minting: false });
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
async share(which) {
|
|
95
|
+
const { minted } = store.getState();
|
|
96
|
+
const text = which === 'command' ? minted?.claude_code_command : minted?.token;
|
|
97
|
+
if (!text) return;
|
|
98
|
+
try {
|
|
99
|
+
const done = await share(text);
|
|
100
|
+
store.set({ shared: done ? which : '' });
|
|
101
|
+
} catch {
|
|
102
|
+
// A sheet that failed to open must not read as a failed mint: the token is on screen.
|
|
103
|
+
store.set({ shared: '' });
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
done: () => store.set({ minted: null, expiry: '', shared: '', error: null }),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An in-memory `McpTokensApi` for simulators and tests. Never touches a tenant or
|
|
3
|
+
* central: nothing here can mint, list or revoke a real credential.
|
|
4
|
+
*/
|
|
5
|
+
import type { McpFoundry, McpFoundryList, McpMintedToken, McpTokensApi } from '../../../api/mcp-tokens';
|
|
6
|
+
|
|
7
|
+
export interface FakeMcpTokensOptions {
|
|
8
|
+
foundries?: McpFoundry[];
|
|
9
|
+
listError?: Error;
|
|
10
|
+
mintError?: Error;
|
|
11
|
+
mintDelayMs?: number;
|
|
12
|
+
/** Clock for `issued_at` / `expires_at`. */
|
|
13
|
+
now?: () => number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type FakeMcpTokensApi = McpTokensApi & { calls: { list: number; mint: Array<{ foundry: string; ttl_days?: number }> } };
|
|
17
|
+
|
|
18
|
+
const DEFAULT_FOUNDRIES: McpFoundry[] = [
|
|
19
|
+
{ slug: 'tandem', name: 'Tandem', audience: 'tandem', mcp_url: 'https://tandem.example/mcp', state: 'active' },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
export function createFakeMcpTokensApi(options: FakeMcpTokensOptions = {}): FakeMcpTokensApi {
|
|
23
|
+
const foundries = options.foundries ?? DEFAULT_FOUNDRIES;
|
|
24
|
+
const now = options.now ?? Date.now;
|
|
25
|
+
const calls: FakeMcpTokensApi['calls'] = { list: 0, mint: [] };
|
|
26
|
+
let minted = 0;
|
|
27
|
+
return {
|
|
28
|
+
calls,
|
|
29
|
+
async list(): Promise<McpFoundryList> {
|
|
30
|
+
calls.list += 1;
|
|
31
|
+
if (options.listError) throw options.listError;
|
|
32
|
+
return { foundries, default_ttl_days: 30, max_ttl_days: 90 };
|
|
33
|
+
},
|
|
34
|
+
async mint(input): Promise<McpMintedToken> {
|
|
35
|
+
calls.mint.push(input);
|
|
36
|
+
if (options.mintDelayMs) await new Promise((r) => setTimeout(r, options.mintDelayMs));
|
|
37
|
+
if (options.mintError) throw options.mintError;
|
|
38
|
+
minted += 1;
|
|
39
|
+
const foundry = foundries.find((f) => f.slug === input.foundry) ?? foundries[0];
|
|
40
|
+
const ttl = input.ttl_days ?? 30;
|
|
41
|
+
const issued = now();
|
|
42
|
+
return {
|
|
43
|
+
token: `fake-mcp-token-${minted}`,
|
|
44
|
+
token_type: 'Bearer',
|
|
45
|
+
foundry: foundry.slug,
|
|
46
|
+
audience: foundry.audience,
|
|
47
|
+
acting_as: { org_id: 'tandem', role: 'member', email: 'quinn@example.com' },
|
|
48
|
+
jti: `fake-jti-${minted}`,
|
|
49
|
+
issued_at: new Date(issued).toISOString(),
|
|
50
|
+
expires_at: new Date(issued + ttl * 86_400_000).toISOString(),
|
|
51
|
+
mcp_url: foundry.mcp_url,
|
|
52
|
+
claude_code_command: `claude mcp add --transport http ${foundry.slug} ${foundry.mcp_url} --header "Authorization: Bearer fake-mcp-token-${minted}"`,
|
|
53
|
+
notice: 'This token is shown once. It reads this workspace as you and cannot be cancelled before it expires.',
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import type { FoundryTab } from '../../components/FoundryTabsShell';
|
|
3
|
+
import type { McpTokensApi } from '../../../api/mcp-tokens';
|
|
4
|
+
import { ConnectClaudeScreen, shareWithSystem, useConnectClaudeController } from './ConnectClaudeScreen';
|
|
5
|
+
import type { ShareText } from './controller';
|
|
6
|
+
|
|
7
|
+
function ConnectClaude({ api, share, active }: { api: McpTokensApi; share: ShareText; active: boolean }) {
|
|
8
|
+
const controller = useConnectClaudeController(api, share);
|
|
9
|
+
// The shell keeps a visited tab mounted (hidden), so leaving it must forget the token: it is shown once.
|
|
10
|
+
useEffect(() => { if (!active) controller.done(); }, [active, controller]);
|
|
11
|
+
return <ConnectClaudeScreen controller={controller} />;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The Connect Claude tab: create an MCP token, shown once, and hand it to the system share sheet. */
|
|
15
|
+
export function createConnectClaudeTab({ api, share = shareWithSystem, key = 'connect-claude', label = 'Claude' }: {
|
|
16
|
+
api: McpTokensApi; share?: ShareText; key?: string; label?: string;
|
|
17
|
+
}): FoundryTab {
|
|
18
|
+
return { key, label, render: ({ active }) => <ConnectClaude api={api} share={share} active={active} /> };
|
|
19
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { useMemo, useState } from 'react';
|
|
2
|
+
import { ScrollView, View } from 'react-native';
|
|
3
|
+
import { Alert, Button, DataList, Header, Sheet, StatusChip, Text, useTheme } from '@startsimpli/ui-native';
|
|
4
|
+
import { connectionStatusLabel, connectionStatusLine } from '../../../components/pages/connection-format';
|
|
5
|
+
import type { Connection, ConnectionStatus, MemberConnectionsApi } from '../../../components/pages/types';
|
|
6
|
+
import { PressableCard } from '../shared/PressableCard';
|
|
7
|
+
import { useControllerState } from '../../components/useControllerState';
|
|
8
|
+
import { createConnectionsController, type ConnectionsController } from './controller';
|
|
9
|
+
|
|
10
|
+
/** The StatusChip status word that gives a connection status its colour. */
|
|
11
|
+
const chipStatus = (s: ConnectionStatus): string =>
|
|
12
|
+
({ connected: 'connected', needs_reauth: 'warning', error: 'error', disabled: 'paused' } as const)[s] ?? s;
|
|
13
|
+
|
|
14
|
+
const subtitle = (c: Connection) => [c.connector_name !== c.name ? c.connector_name : null, c.account_label].filter(Boolean).join(' · ');
|
|
15
|
+
|
|
16
|
+
export interface ConnectionsListScreenProps { controller: ConnectionsController; onOpen: (c: Connection) => void }
|
|
17
|
+
|
|
18
|
+
/** My connected accounts and whether each one works. New accounts are connected on the web. */
|
|
19
|
+
export function ConnectionsListScreen({ controller, onOpen }: ConnectionsListScreenProps) {
|
|
20
|
+
const s = useControllerState(controller);
|
|
21
|
+
const theme = useTheme();
|
|
22
|
+
return (
|
|
23
|
+
<View style={{ flex: 1 }}>
|
|
24
|
+
<Header title="Connections" subtitle={s.loading ? undefined : `${s.rows.length} connected account${s.rows.length === 1 ? '' : 's'}`} />
|
|
25
|
+
<DataList
|
|
26
|
+
testID="connections-list"
|
|
27
|
+
data={s.rows}
|
|
28
|
+
keyExtractor={(c: Connection) => c.id}
|
|
29
|
+
loading={s.loading}
|
|
30
|
+
refreshing={s.refreshing}
|
|
31
|
+
onRefresh={() => { void controller.refresh(); }}
|
|
32
|
+
error={s.error}
|
|
33
|
+
onRetry={() => { void controller.refresh(); }}
|
|
34
|
+
emptyTitle="Nothing connected"
|
|
35
|
+
emptyDescription="Connect an account (calendar, files, meeting notes) on the web and it shows up here."
|
|
36
|
+
ItemSeparatorComponent={() => <View style={{ height: theme.spacing.sm }} />}
|
|
37
|
+
ListHeaderComponent={s.catalog.length ? (
|
|
38
|
+
<Text variant="muted" style={{ paddingBottom: theme.spacing.sm }}>
|
|
39
|
+
Open to connect: {s.catalog.map((c) => c.name).join(', ')}. Add new accounts on the web.
|
|
40
|
+
</Text>
|
|
41
|
+
) : undefined}
|
|
42
|
+
renderItem={({ item }: { item: Connection }) => (
|
|
43
|
+
<PressableCard testID={`connection-${item.id}`} onPress={() => onOpen(item)}>
|
|
44
|
+
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', gap: theme.spacing.sm }}>
|
|
45
|
+
<Text variant="heading" style={{ flex: 1, fontSize: theme.fontSizes.base }} numberOfLines={1}>{item.name || item.connector_name}</Text>
|
|
46
|
+
<StatusChip status={chipStatus(item.status)} label={connectionStatusLabel(item.status)} />
|
|
47
|
+
</View>
|
|
48
|
+
{subtitle(item) ? <Text variant="caption">{subtitle(item)}</Text> : null}
|
|
49
|
+
<Text variant="muted">{connectionStatusLine(item)}</Text>
|
|
50
|
+
</PressableCard>
|
|
51
|
+
)}
|
|
52
|
+
/>
|
|
53
|
+
</View>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function ConnectionDetailScreen({ controller, id, onBack }: { controller: ConnectionsController; id: string; onBack: () => void }) {
|
|
58
|
+
const s = useControllerState(controller);
|
|
59
|
+
const theme = useTheme();
|
|
60
|
+
const [confirming, setConfirming] = useState(false);
|
|
61
|
+
const row = s.rows.find((r) => r.id === id);
|
|
62
|
+
const check = s.checks[id];
|
|
63
|
+
|
|
64
|
+
if (!row) {
|
|
65
|
+
return (
|
|
66
|
+
<View style={{ flex: 1 }}>
|
|
67
|
+
<Header title="Connection" onBack={onBack} />
|
|
68
|
+
<Text variant="muted" style={{ padding: theme.spacing.lg }}>{s.loading ? 'Loading...' : 'This connection is no longer available.'}</Text>
|
|
69
|
+
</View>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
const live = row.status !== 'disabled';
|
|
73
|
+
return (
|
|
74
|
+
<View style={{ flex: 1 }} testID="connection-detail">
|
|
75
|
+
<Header title={row.name || row.connector_name} subtitle={subtitle(row) || undefined} onBack={onBack} />
|
|
76
|
+
<ScrollView contentContainerStyle={{ padding: theme.spacing.lg, gap: theme.spacing.md }}>
|
|
77
|
+
<View style={{ flexDirection: 'row', alignItems: 'center', gap: theme.spacing.sm }}>
|
|
78
|
+
<StatusChip status={chipStatus(row.status)} label={connectionStatusLabel(row.status)} />
|
|
79
|
+
<Text variant="muted" style={{ flex: 1 }}>{connectionStatusLine(row)}</Text>
|
|
80
|
+
</View>
|
|
81
|
+
{row.check_error ? <Alert variant="destructive">{row.check_error}</Alert> : null}
|
|
82
|
+
{row.status === 'needs_reauth' ? <Text variant="muted">The vendor stopped accepting this grant. Reconnect it on the web.</Text> : null}
|
|
83
|
+
{row.capabilities_enabled.length ? <Text variant="muted">Allowed: {row.capabilities_enabled.join(', ')}</Text> : null}
|
|
84
|
+
{s.actionError ? <Alert variant="destructive">{s.actionError}</Alert> : null}
|
|
85
|
+
{check ? <Alert variant={check.ok ? 'default' : 'destructive'}>{check.detail || (check.ok ? 'Connection is healthy.' : 'The check failed.')}</Alert> : null}
|
|
86
|
+
{live ? (
|
|
87
|
+
<View style={{ gap: theme.spacing.sm }}>
|
|
88
|
+
<Button testID="connection-check" title={s.checking === id ? 'Testing...' : 'Test now'} disabled={s.checking !== null} onPress={() => { void controller.check(id); }} />
|
|
89
|
+
<Button testID="connection-disconnect" title="Disconnect" variant="destructive" onPress={() => setConfirming(true)} />
|
|
90
|
+
</View>
|
|
91
|
+
) : <Text variant="muted">Disconnected. Its key was deleted and its scheduled fetches stopped. Reconnect it on the web.</Text>}
|
|
92
|
+
</ScrollView>
|
|
93
|
+
<Sheet visible={confirming} onClose={() => setConfirming(false)} title={`Disconnect ${row.connector_name}?`} testID="connection-confirm">
|
|
94
|
+
<View style={{ gap: theme.spacing.md, padding: theme.spacing.lg }}>
|
|
95
|
+
<Text variant="muted">Its stored key is deleted and its scheduled fetches stop. This cannot be undone: you would connect again from the vendor.</Text>
|
|
96
|
+
<Button testID="connection-confirm-yes" title={s.disconnecting === id ? 'Disconnecting...' : 'Yes, disconnect'} variant="destructive" disabled={s.disconnecting !== null}
|
|
97
|
+
onPress={() => { void controller.disconnect(id).then(() => setConfirming(false)); }} />
|
|
98
|
+
<Button title="Cancel" variant="ghost" onPress={() => setConfirming(false)} />
|
|
99
|
+
</View>
|
|
100
|
+
</Sheet>
|
|
101
|
+
</View>
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function useConnectionsController(api: MemberConnectionsApi): ConnectionsController {
|
|
106
|
+
// The controller reads once when it is built; Connections re-reads on pull-to-refresh and after a change.
|
|
107
|
+
return useMemo(() => createConnectionsController({ api }), [api]);
|
|
108
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createFakeConnectionsApi } from '../fake';
|
|
3
|
+
import { createConnectionsController } from '../controller';
|
|
4
|
+
import { connectionStatusLine } from '../../../../components/pages/connection-format';
|
|
5
|
+
|
|
6
|
+
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
7
|
+
async function settle(check: () => boolean) { for (let i = 0; i < 50 && !check(); i += 1) await tick(); }
|
|
8
|
+
|
|
9
|
+
describe('connections: what is connected and whether it works', () => {
|
|
10
|
+
it('loads my connections and the connectors open to me, through the shared member api', async () => {
|
|
11
|
+
const api = createFakeConnectionsApi();
|
|
12
|
+
const c = createConnectionsController({ api });
|
|
13
|
+
await settle(() => !c.getState().loading);
|
|
14
|
+
const s = c.getState();
|
|
15
|
+
expect(s.rows.map((r) => r.connector_name)).toEqual(['Google Calendar', 'Dropbox', 'Granola']);
|
|
16
|
+
expect(s.catalog.length).toBeGreaterThan(0);
|
|
17
|
+
expect(s.error).toBeNull();
|
|
18
|
+
expect(api.calls.connections).toBe(1);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('reports a failed read instead of an empty list', async () => {
|
|
22
|
+
const api = createFakeConnectionsApi({ readError: Object.assign(new Error('x'), { status: 401 }) });
|
|
23
|
+
const c = createConnectionsController({ api });
|
|
24
|
+
await settle(() => !c.getState().loading);
|
|
25
|
+
expect(c.getState().error).toBe('Your session has expired. Sign in again.');
|
|
26
|
+
expect(c.getState().rows).toEqual([]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('tests a connection now and keeps the answer for that row only', async () => {
|
|
30
|
+
const api = createFakeConnectionsApi();
|
|
31
|
+
const c = createConnectionsController({ api });
|
|
32
|
+
await settle(() => !c.getState().loading);
|
|
33
|
+
await c.check('cn2');
|
|
34
|
+
expect(api.calls.check).toEqual(['cn2']);
|
|
35
|
+
expect(c.getState().checks.cn2).toMatchObject({ ok: false });
|
|
36
|
+
expect(c.getState().checks.cn1).toBeUndefined();
|
|
37
|
+
expect(c.getState().checking).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('disconnect removes nothing locally: it reads again and shows what the server says', async () => {
|
|
41
|
+
const api = createFakeConnectionsApi();
|
|
42
|
+
const c = createConnectionsController({ api });
|
|
43
|
+
await settle(() => !c.getState().loading);
|
|
44
|
+
await c.disconnect('cn1');
|
|
45
|
+
expect(api.calls.disconnect).toEqual(['cn1']);
|
|
46
|
+
expect(api.calls.connections).toBe(2);
|
|
47
|
+
expect(c.getState().rows.find((r) => r.id === 'cn1')?.status).toBe('disabled');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('a failed disconnect keeps the row and says why', async () => {
|
|
51
|
+
const api = createFakeConnectionsApi({ disconnectError: new Error('Not allowed') });
|
|
52
|
+
const c = createConnectionsController({ api });
|
|
53
|
+
await settle(() => !c.getState().loading);
|
|
54
|
+
await c.disconnect('cn1');
|
|
55
|
+
expect(c.getState().actionError).toBe('Not allowed');
|
|
56
|
+
expect(c.getState().rows.find((r) => r.id === 'cn1')?.status).toBe('connected');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('connection status line (shared with the web roster)', () => {
|
|
61
|
+
it('reads status, health and when it was last checked, or never', () => {
|
|
62
|
+
expect(connectionStatusLine({ status: 'connected', health: 'ok', last_checked_at: null })).toBe('connected · ok · never checked');
|
|
63
|
+
expect(connectionStatusLine({ status: 'needs_reauth', health: '', last_checked_at: '2026-11-01T10:00:00Z' }, (iso) => `T(${iso})`))
|
|
64
|
+
.toBe('needs_reauth · last checked T(2026-11-01T10:00:00Z)');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connections (my own connected accounts) as a framework-free controller over the
|
|
3
|
+
* shared `MemberConnectionsApi` - the same contract the web `ConnectionsPage`
|
|
4
|
+
* renders in personal mode. The server already scopes the list to the signed-in
|
|
5
|
+
* member and sends it whole (a member holds a handful of cards), so there is no
|
|
6
|
+
* client narrowing, sorting or paging to do; every change re-reads.
|
|
7
|
+
*
|
|
8
|
+
* Connecting a NEW account (credentials, OAuth consent) stays on the web: it is a
|
|
9
|
+
* form and a browser round trip, not a native list screen.
|
|
10
|
+
*/
|
|
11
|
+
import { failureMessage } from '../../../corpus/work-item-logic';
|
|
12
|
+
import type { Connection, ConnectionPermissions, ConnectorCatalogEntry, MemberConnectionsApi } from '../../../components/pages/types';
|
|
13
|
+
import { createStore, errorMessage } from '../store';
|
|
14
|
+
|
|
15
|
+
export interface ConnectionCheck { ok: boolean; detail: string }
|
|
16
|
+
|
|
17
|
+
export interface ConnectionsState {
|
|
18
|
+
loading: boolean;
|
|
19
|
+
refreshing: boolean;
|
|
20
|
+
rows: Connection[];
|
|
21
|
+
permissions: ConnectionPermissions | null;
|
|
22
|
+
/** Connectors the foundry has opened to members (names only are shown). */
|
|
23
|
+
catalog: ConnectorCatalogEntry[];
|
|
24
|
+
error: string | null;
|
|
25
|
+
/** Result of "Test now", by connection id. */
|
|
26
|
+
checks: Record<string, ConnectionCheck>;
|
|
27
|
+
/** The id currently being tested or disconnected. */
|
|
28
|
+
checking: string | null;
|
|
29
|
+
disconnecting: string | null;
|
|
30
|
+
actionError: string | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ConnectionsController {
|
|
34
|
+
getState(): ConnectionsState;
|
|
35
|
+
subscribe(listener: () => void): () => void;
|
|
36
|
+
refresh(): Promise<void>;
|
|
37
|
+
check(id: string): Promise<void>;
|
|
38
|
+
disconnect(id: string): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createConnectionsController({ api }: { api: MemberConnectionsApi }): ConnectionsController {
|
|
42
|
+
const store = createStore<ConnectionsState>({
|
|
43
|
+
loading: true, refreshing: false, rows: [], permissions: null, catalog: [], error: null,
|
|
44
|
+
checks: {}, checking: null, disconnecting: null, actionError: null,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
async function read(initial: boolean) {
|
|
48
|
+
store.set(initial ? { error: null } : { refreshing: true, error: null });
|
|
49
|
+
try {
|
|
50
|
+
const [mine, catalog] = await Promise.all([
|
|
51
|
+
api.connections(),
|
|
52
|
+
// The catalog is context, not the point of the screen: its failure must not hide my cards.
|
|
53
|
+
api.connectorCatalog().then((c) => c.results, () => [] as ConnectorCatalogEntry[]),
|
|
54
|
+
]);
|
|
55
|
+
store.set({ rows: mine.results ?? [], permissions: mine.permissions ?? null, catalog });
|
|
56
|
+
} catch (e) {
|
|
57
|
+
store.set({ rows: [], error: failureMessage(e) });
|
|
58
|
+
} finally {
|
|
59
|
+
store.set({ loading: false, refreshing: false });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
void read(true);
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
getState: store.getState,
|
|
66
|
+
subscribe: store.subscribe,
|
|
67
|
+
refresh: () => read(false),
|
|
68
|
+
async check(id) {
|
|
69
|
+
if (store.getState().checking) return;
|
|
70
|
+
store.set({ checking: id, actionError: null });
|
|
71
|
+
try {
|
|
72
|
+
const r = await api.checkConnection(id);
|
|
73
|
+
store.set({ checks: { ...store.getState().checks, [id]: { ok: r.ok, detail: r.detail } } });
|
|
74
|
+
} catch (e) {
|
|
75
|
+
store.set({ actionError: errorMessage(e, 'The test could not run.') });
|
|
76
|
+
} finally {
|
|
77
|
+
store.set({ checking: null });
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
async disconnect(id) {
|
|
81
|
+
if (store.getState().disconnecting) return;
|
|
82
|
+
store.set({ disconnecting: id, actionError: null });
|
|
83
|
+
try {
|
|
84
|
+
await api.disconnect(id);
|
|
85
|
+
await read(false);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
store.set({ actionError: errorMessage(e, 'Could not disconnect.') });
|
|
88
|
+
} finally {
|
|
89
|
+
store.set({ disconnecting: null });
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** An in-memory `MemberConnectionsApi` for simulators and tests. Reads and writes nothing real. */
|
|
2
|
+
import type { Connection, ConnectorCatalogEntry, MemberConnectionsApi } from '../../../components/pages/types';
|
|
3
|
+
|
|
4
|
+
export interface FakeConnectionsOptions {
|
|
5
|
+
readError?: Error;
|
|
6
|
+
disconnectError?: Error;
|
|
7
|
+
/** Replace the sample rows. */
|
|
8
|
+
connections?: Connection[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type FakeConnectionsApi = MemberConnectionsApi & {
|
|
12
|
+
calls: { connections: number; check: string[]; disconnect: string[] };
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const conn = (id: string, key: string, name: string, over: Partial<Connection>): Connection => ({
|
|
16
|
+
id, public_id: `pub-${id}`, name, connector_key: key, connector_name: name, external_account_id: `ext-${id}`,
|
|
17
|
+
account_label: '', capabilities_enabled: ['read'], config: {}, status: 'connected', health: 'ok', auth_mode: 'oauth',
|
|
18
|
+
last_checked_at: '2026-11-03T08:00:00Z', ...over,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export function sampleConnections(): Connection[] {
|
|
22
|
+
return [
|
|
23
|
+
conn('cn1', 'google_calendar', 'Google Calendar', { account_label: 'quinn@example.com', capabilities_enabled: ['read', 'events'] }),
|
|
24
|
+
conn('cn2', 'dropbox', 'Dropbox', { account_label: 'Quinn O.', status: 'needs_reauth', health: 'token expired', check_error: 'Dropbox no longer accepts this grant. Reconnect it on the web.', last_checked_at: '2026-11-02T20:30:00Z' }),
|
|
25
|
+
conn('cn3', 'granola', 'Granola', { account_label: 'Quinn', auth_mode: 'api_key', last_checked_at: null, health: '' }),
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const catalog = (key: string, name: string, description: string): ConnectorCatalogEntry => ({
|
|
30
|
+
key, name, description, logo_url: '', docs_url: '', capabilities: ['read'], auth_mode: 'oauth',
|
|
31
|
+
credential_schema: {} as never, config_schema: {} as never, record_types: [], supports_webhooks: false,
|
|
32
|
+
availability: { available: true, reason: '' },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export function createFakeConnectionsApi(options: FakeConnectionsOptions = {}): FakeConnectionsApi {
|
|
36
|
+
let rows = options.connections ?? sampleConnections();
|
|
37
|
+
const calls: FakeConnectionsApi['calls'] = { connections: 0, check: [], disconnect: [] };
|
|
38
|
+
const unsupported = () => { throw new Error('Not available in the fake connections api.'); };
|
|
39
|
+
return {
|
|
40
|
+
calls,
|
|
41
|
+
async connectorCatalog() {
|
|
42
|
+
if (options.readError) throw options.readError;
|
|
43
|
+
return { results: [catalog('google_calendar', 'Google Calendar', 'Events and availability.'), catalog('dropbox', 'Dropbox', 'Files from a folder.'), catalog('granola', 'Granola', 'Meeting notes.'), catalog('gmail', 'Gmail', 'Threads and attachments.')] };
|
|
44
|
+
},
|
|
45
|
+
async connections() {
|
|
46
|
+
calls.connections += 1;
|
|
47
|
+
if (options.readError) throw options.readError;
|
|
48
|
+
return { results: rows, permissions: { view: true, manage: true, issue_tokens: false } };
|
|
49
|
+
},
|
|
50
|
+
async checkConnection(id) {
|
|
51
|
+
calls.check.push(id);
|
|
52
|
+
const row = rows.find((r) => r.id === id);
|
|
53
|
+
const ok = row?.status === 'connected';
|
|
54
|
+
return { ok, account_label: row?.account_label ?? '', detail: ok ? 'Connection is healthy.' : (row?.check_error ?? 'Check failed.') };
|
|
55
|
+
},
|
|
56
|
+
async disconnect(id) {
|
|
57
|
+
calls.disconnect.push(id);
|
|
58
|
+
if (options.disconnectError) throw options.disconnectError;
|
|
59
|
+
rows = rows.map((r) => (r.id === id ? { ...r, status: 'disabled', health: '' } : r));
|
|
60
|
+
},
|
|
61
|
+
connect: unsupported as never,
|
|
62
|
+
rotateCredential: unsupported as never,
|
|
63
|
+
connectionRuns: async () => ({ results: [] }),
|
|
64
|
+
updateConnectionConfig: unsupported as never,
|
|
65
|
+
startOAuth: unsupported as never,
|
|
66
|
+
oauthInitiate: unsupported as never,
|
|
67
|
+
connectionInstructions: unsupported as never,
|
|
68
|
+
} as FakeConnectionsApi;
|
|
69
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FoundryTab } from '../../components/FoundryTabsShell';
|
|
2
|
+
import type { MemberConnectionsApi } from '../../../components/pages/types';
|
|
3
|
+
import { ConnectionDetailScreen, ConnectionsListScreen, useConnectionsController } from './ConnectionsScreens';
|
|
4
|
+
|
|
5
|
+
function Connections({ api, route, push, back }: { api: MemberConnectionsApi; route: { id: string } | null; push: (id: string) => void; back: () => void }) {
|
|
6
|
+
const controller = useConnectionsController(api);
|
|
7
|
+
return route
|
|
8
|
+
? <ConnectionDetailScreen controller={controller} id={route.id} onBack={back} />
|
|
9
|
+
: <ConnectionsListScreen controller={controller} onOpen={(c) => push(c.id)} />;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The Connections tab: my connected accounts, their health, test and disconnect. */
|
|
13
|
+
export function createConnectionsTab({ api, key = 'connections', label = 'Connections' }: { api: MemberConnectionsApi; key?: string; label?: string }): FoundryTab {
|
|
14
|
+
return { key, label, render: ({ route, push, back }) => <Connections api={api} route={route} push={push} back={back} /> };
|
|
15
|
+
}
|