Repository navigation
Expand file tree
/
Copy pathmain.tsx
More file actions
132 lines (128 loc) · 4.38 KB
/
Copy pathmain.tsx
File metadata and controls
132 lines (128 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useMutation } from "@tanstack/react-query";
import {
type PluginClientContext,
type PluginButtonRegistration,
type PluginWorkspacePanelProps,
useRpc,
useWorkspace,
} from "@getpaseo/plugin/client";
import { useCallback, useMemo } from "react";
import { Pressable, Text, View } from "react-native";
import { ExternalLink } from "@getpaseo/plugin/client/ui";
import { incrementRpc } from "../shared/increment";
export function contributeClient(client: PluginClientContext) {
const pills = new Map<string, PluginButtonRegistration>();
let stopped = false;
const lifetime = new AbortController();
const register = (agent: { id: string; workspaceId?: string | null }) => {
if (stopped || !agent.workspaceId) return;
pills.get(agent.id)?.remove();
const workspaceId = agent.workspaceId;
const remove = client.addComposerPill({
id: "open-counter",
workspaceId,
agentId: agent.id,
button: {
title: "Open plugin counter",
icon: "Blocks",
label: "Counter",
behavior: {
kind: "action",
onPress() {
client.openPanel("counter", { workspaceId });
},
},
},
});
pills.set(agent.id, remove);
};
const remove = (agentId: string) => {
pills.get(agentId)?.remove();
pills.delete(agentId);
};
void client.paseo.agents
.list({ subscribe: {}, signal: lifetime.signal })
.then(({ subscription }) => {
subscription.subscribe({
snapshot: ({ entries }) => {
for (const pill of pills.values()) pill.remove();
pills.clear();
for (const { agent } of entries) register(agent);
},
update: (message) => {
if (message.type !== "agent_update") return;
const update = message.payload;
if (update.kind === "remove") remove(update.agentId);
else register(update.agent);
},
});
return undefined;
})
.catch((error) => {
if (!stopped) console.error("Agent observation failed", error);
});
return () => {
stopped = true;
lifetime.abort();
for (const pill of pills.values()) pill.remove();
pills.clear();
};
}
export function ExamplePanel({
theme,
layout,
workspaceId,
navigation,
}: PluginWorkspacePanelProps) {
const workspace = useWorkspace(workspaceId, ({ name }) => ({ name }));
const callIncrement = useRpc(incrementRpc);
const { data, error, isPending, mutate } = useMutation({ mutationFn: callIncrement });
const value = data?.value ?? 0;
const styles = useMemo(
() => ({
screen: {
flex: 1,
padding: layout.compact ? 16 : 24,
gap: 16,
backgroundColor: theme.colors.surface0,
},
title: { color: theme.colors.foreground, fontSize: layout.compact ? 20 : 24 },
detail: { color: theme.colors.foregroundMuted },
button: { padding: 14, borderRadius: 10, backgroundColor: theme.colors.accent },
buttonText: { color: theme.colors.accentForeground, textAlign: "center" as const },
error: { color: theme.colors.statusDanger },
}),
[theme, layout.compact],
);
const handleIncrement = useCallback(() => {
mutate({ value });
}, [mutate, value]);
const openBrowser = navigation?.openBrowser;
const handleOpenBrowser = useCallback(() => {
openBrowser?.({ url: "https://paseo.sh/docs/plugins", workspaceId });
}, [openBrowser, workspaceId]);
return (
<View style={styles.screen}>
<Text style={styles.title}>Workspace plugin panel</Text>
<ExternalLink href="https://paseo.sh/docs/plugins">Plugin documentation</ExternalLink>
{openBrowser ? (
<Pressable accessibilityRole="button" onPress={handleOpenBrowser} style={styles.button}>
<Text style={styles.buttonText}>Read in workspace browser</Text>
</Pressable>
) : null}
<Text style={styles.detail}>{workspace?.name}</Text>
<Text style={styles.detail}>{data?.handledBy ?? "The RPC has not run yet."}</Text>
<Pressable
accessibilityRole="button"
accessibilityLabel={`Increment plugin counter, currently ${value}`}
onPress={handleIncrement}
style={styles.button}
>
<Text style={styles.buttonText}>
{isPending ? "Calling daemon…" : `RPC counter: ${value}`}
</Text>
</Pressable>
{error ? <Text style={styles.error}>{error.message}</Text> : null}
</View>
);
}