@serfab/cadre-client 0.0.0-stage → 2.0.0
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/ADOPTING.md +220 -0
- package/README.md +129 -3
- package/dist/address.d.ts +45 -0
- package/dist/address.js +109 -0
- package/dist/address.js.map +1 -0
- package/dist/app-link/index.d.ts +16 -0
- package/dist/app-link/index.js +17 -0
- package/dist/app-link/index.js.map +1 -0
- package/dist/app-link/join-response.d.ts +31 -0
- package/dist/app-link/join-response.js +87 -0
- package/dist/app-link/join-response.js.map +1 -0
- package/dist/app-link/known-apps.d.ts +32 -0
- package/dist/app-link/known-apps.js +51 -0
- package/dist/app-link/known-apps.js.map +1 -0
- package/dist/app-link/link-request-store.d.ts +50 -0
- package/dist/app-link/link-request-store.js +101 -0
- package/dist/app-link/link-request-store.js.map +1 -0
- package/dist/app-link/links.d.ts +47 -0
- package/dist/app-link/links.js +76 -0
- package/dist/app-link/links.js.map +1 -0
- package/dist/app-link/recommend-links.d.ts +30 -0
- package/dist/app-link/recommend-links.js +33 -0
- package/dist/app-link/recommend-links.js.map +1 -0
- package/dist/app-link/recommendation-store.d.ts +54 -0
- package/dist/app-link/recommendation-store.js +99 -0
- package/dist/app-link/recommendation-store.js.map +1 -0
- package/dist/bootstrap.d.ts +106 -0
- package/dist/bootstrap.js +106 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/inbox.d.ts +10 -0
- package/dist/inbox.js +28 -0
- package/dist/inbox.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/invitation-text.d.ts +81 -0
- package/dist/invitation-text.js +80 -0
- package/dist/invitation-text.js.map +1 -0
- package/dist/lifecycle/background-runner.d.ts +94 -0
- package/dist/lifecycle/background-runner.js +278 -0
- package/dist/lifecycle/background-runner.js.map +1 -0
- package/dist/lifecycle/index.d.ts +9 -0
- package/dist/lifecycle/index.js +10 -0
- package/dist/lifecycle/index.js.map +1 -0
- package/dist/loader.d.ts +47 -0
- package/dist/loader.js +93 -0
- package/dist/loader.js.map +1 -0
- package/dist/model.d.ts +284 -0
- package/dist/model.js +22 -0
- package/dist/model.js.map +1 -0
- package/dist/node-code-text.d.ts +10 -0
- package/dist/node-code-text.js +30 -0
- package/dist/node-code-text.js.map +1 -0
- package/dist/result.d.ts +79 -0
- package/dist/result.js +84 -0
- package/dist/result.js.map +1 -0
- package/dist/sereus/client.d.ts +49 -0
- package/dist/sereus/client.js +420 -0
- package/dist/sereus/client.js.map +1 -0
- package/dist/sereus/edge-host.d.ts +22 -0
- package/dist/sereus/edge-host.js +61 -0
- package/dist/sereus/edge-host.js.map +1 -0
- package/dist/sereus/host.d.ts +57 -0
- package/dist/sereus/host.js +2 -0
- package/dist/sereus/host.js.map +1 -0
- package/dist/sereus/index.d.ts +8 -0
- package/dist/sereus/index.js +9 -0
- package/dist/sereus/index.js.map +1 -0
- package/dist/sereus/invitations.d.ts +16 -0
- package/dist/sereus/invitations.js +196 -0
- package/dist/sereus/invitations.js.map +1 -0
- package/dist/sereus/node-code.d.ts +52 -0
- package/dist/sereus/node-code.js +51 -0
- package/dist/sereus/node-code.js.map +1 -0
- package/dist/store.d.ts +27 -0
- package/dist/store.js +51 -0
- package/dist/store.js.map +1 -0
- package/dist/stores.d.ts +37 -0
- package/dist/stores.js +45 -0
- package/dist/stores.js.map +1 -0
- package/examples/README.md +20 -0
- package/examples/nativescript/cadrons-page.xml +24 -0
- package/examples/nativescript/cadrons-view-model.ts +68 -0
- package/examples/react-native/CadronList.tsx +204 -0
- package/examples/react-native/InvitationPanel.tsx +162 -0
- package/examples/react-native/useStore.ts +11 -0
- package/examples/svelte/CadronList.svelte +86 -0
- package/examples/svelte/InvitationPanel.svelte +79 -0
- package/package.json +65 -3
- package/src/address.ts +125 -0
- package/src/app-link/index.ts +45 -0
- package/src/app-link/join-response.ts +109 -0
- package/src/app-link/known-apps.ts +70 -0
- package/src/app-link/link-request-store.ts +129 -0
- package/src/app-link/links.ts +90 -0
- package/src/app-link/recommend-links.ts +49 -0
- package/src/app-link/recommendation-store.ts +132 -0
- package/src/bootstrap.ts +184 -0
- package/src/globals.d.ts +26 -0
- package/src/inbox.ts +36 -0
- package/src/index.ts +15 -0
- package/src/invitation-text.ts +156 -0
- package/src/lifecycle/background-runner.ts +363 -0
- package/src/lifecycle/index.ts +17 -0
- package/src/loader.ts +103 -0
- package/src/model.ts +299 -0
- package/src/node-code-text.ts +25 -0
- package/src/result.ts +131 -0
- package/src/sereus/client.ts +454 -0
- package/src/sereus/edge-host.ts +81 -0
- package/src/sereus/host.ts +56 -0
- package/src/sereus/index.ts +8 -0
- package/src/sereus/invitations.ts +206 -0
- package/src/sereus/node-code.ts +80 -0
- package/src/store.ts +61 -0
- package/src/stores.ts +59 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example (React Native): a person's cadrons. Copy and adapt; it is not built or published.
|
|
3
|
+
*
|
|
4
|
+
* - Each cadron with a status dot (`cadronPresence`), the person's name for it (else its host name,
|
|
5
|
+
* `suggestedName`), the host and short id it can be matched by, a star to name it first in
|
|
6
|
+
* invitations, renaming, and removal.
|
|
7
|
+
* - Adding a cadron by its node code: read it, show which machine it is and which cadre it joins,
|
|
8
|
+
* claim only after the person approves (a code can arrive from anyone), and name it after its
|
|
9
|
+
* host.
|
|
10
|
+
* - How many addresses an invitation names.
|
|
11
|
+
*
|
|
12
|
+
* `client` is `createCadreClient(...)` from `@serfab/cadre-client/sereus`. Strings are inline
|
|
13
|
+
* English; an app maps each error `kind` to its own catalog instead of showing `message`.
|
|
14
|
+
*/
|
|
15
|
+
import React, { useEffect, useMemo, useState } from 'react';
|
|
16
|
+
import { Alert, Pressable, StyleSheet, Text, TextInput, View } from 'react-native';
|
|
17
|
+
import {
|
|
18
|
+
cadreStore,
|
|
19
|
+
cadronPresence,
|
|
20
|
+
MAX_BOOTSTRAP_ADDRS,
|
|
21
|
+
shortPeerId,
|
|
22
|
+
suggestedName,
|
|
23
|
+
type CadreMachine,
|
|
24
|
+
type CadronPresence,
|
|
25
|
+
type CadronPreferences,
|
|
26
|
+
} from '@serfab/cadre-client';
|
|
27
|
+
import type { CadreClient } from '@serfab/cadre-client/sereus';
|
|
28
|
+
import { useStore } from './useStore';
|
|
29
|
+
|
|
30
|
+
const DOT: Record<CadronPresence, string> = { connected: '#2c8a3f', recent: '#c98a00', quiet: '#888888', unknown: '#dddddd' };
|
|
31
|
+
// "Seen recently", not "running": a cadron's record stays recent for up to 15 minutes after it stops.
|
|
32
|
+
const PRESENCE: Record<CadronPresence, string> = {
|
|
33
|
+
connected: 'Connected',
|
|
34
|
+
recent: 'Seen recently',
|
|
35
|
+
quiet: 'Not seen lately',
|
|
36
|
+
unknown: 'Not seen yet',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function CadronList({ client }: { client: CadreClient }) {
|
|
40
|
+
// One store per client: create it once, not on every render.
|
|
41
|
+
const store = useMemo(() => cadreStore(client), [client]);
|
|
42
|
+
const load = useStore(store);
|
|
43
|
+
const prefs = usePreferences(client);
|
|
44
|
+
|
|
45
|
+
if (load.state === 'loading') return <Text>Loading…</Text>;
|
|
46
|
+
if (load.state === 'failed') return <Text onPress={() => store.reload()}>{load.error.message} — tap to retry</Text>;
|
|
47
|
+
const status = load.value;
|
|
48
|
+
|
|
49
|
+
const toggleStar = (m: CadreMachine) => {
|
|
50
|
+
if (!prefs) return;
|
|
51
|
+
const preferred = prefs.preferred.includes(m.peerId)
|
|
52
|
+
? prefs.preferred.filter(id => id !== m.peerId)
|
|
53
|
+
: [...prefs.preferred, m.peerId];
|
|
54
|
+
void client.invitations.setPreferences({ preferred });
|
|
55
|
+
};
|
|
56
|
+
// Confirm with what the removal will do, then remove completely (record, invitations, closed-strand keys).
|
|
57
|
+
const remove = async (m: CadreMachine) => {
|
|
58
|
+
const plan = await client.cadre.planRemoval(m.peerId);
|
|
59
|
+
if (!plan.ok) { Alert.alert('Cannot check this removal', plan.error.message); return; }
|
|
60
|
+
if (plan.value.refusal) { Alert.alert('This machine cannot be removed here', plan.value.refusal === 'self' ? 'It is this device.' : 'This device holds no owner key.'); return; }
|
|
61
|
+
const also = [
|
|
62
|
+
plan.value.invitations ? `${plan.value.invitations} invitation(s) it could rejoin with are closed` : null,
|
|
63
|
+
plan.value.closedStrands.length ? `${plan.value.closedStrands.length} private conversation(s) get a new key it cannot read` : null,
|
|
64
|
+
].filter(Boolean).join('; ');
|
|
65
|
+
Alert.alert('Remove this machine?', `It will no longer act for you.${also ? `\n\nAlso: ${also}.` : ''}`, [
|
|
66
|
+
{ text: 'Cancel', style: 'cancel' },
|
|
67
|
+
{
|
|
68
|
+
text: 'Remove',
|
|
69
|
+
style: 'destructive',
|
|
70
|
+
onPress: async () => {
|
|
71
|
+
const done = await client.cadre.removeMachine(m.peerId);
|
|
72
|
+
if (!done.ok) Alert.alert('Could not remove the machine', done.error.message);
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
]);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<View style={styles.list}>
|
|
80
|
+
<Text style={styles.header}>{status.running ? 'Running' : 'Not running'} · {status.machines.length} machine(s)</Text>
|
|
81
|
+
{status.machines.map(m => {
|
|
82
|
+
const presence = cadronPresence(m);
|
|
83
|
+
const host = suggestedName(m);
|
|
84
|
+
return (
|
|
85
|
+
<View key={m.peerId} style={styles.row}>
|
|
86
|
+
<View style={[styles.dot, { backgroundColor: DOT[presence] }]} />
|
|
87
|
+
<View style={styles.grow}>
|
|
88
|
+
<Text style={styles.title}>{m.thisDevice ? 'This device' : m.name || host || `Machine ${shortPeerId(m.peerId)}`}</Text>
|
|
89
|
+
{/* What it can be matched by on its own host: its host name and short id. */}
|
|
90
|
+
<Text style={styles.subtitle}>{[host, shortPeerId(m.peerId)].filter(Boolean).join(' · ')}</Text>
|
|
91
|
+
{!m.thisDevice && (
|
|
92
|
+
<Text style={styles.subtitle}>
|
|
93
|
+
{[m.trusted === false ? 'Not verified' : PRESENCE[presence], m.alwaysOn ? 'Always on' : null, m.preferred ? 'Preferred' : null].filter(Boolean).join(' · ')}
|
|
94
|
+
</Text>
|
|
95
|
+
)}
|
|
96
|
+
</View>
|
|
97
|
+
{!m.thisDevice && <Rename client={client} machine={m} prefs={prefs} />}
|
|
98
|
+
{!m.thisDevice && <Pressable onPress={() => toggleStar(m)}><Text>{m.preferred ? '★' : '☆'}</Text></Pressable>}
|
|
99
|
+
{!m.thisDevice && <Pressable onPress={() => { void remove(m); }}><Text style={styles.danger}>Remove</Text></Pressable>}
|
|
100
|
+
</View>
|
|
101
|
+
);
|
|
102
|
+
})}
|
|
103
|
+
<AddCadron client={client} disabled={!status.ownerEnrolled} />
|
|
104
|
+
{prefs && <AddressCount client={client} prefs={prefs} />}
|
|
105
|
+
</View>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The cadron preferences, kept fresh: names and stars show in the list, the count below it. */
|
|
110
|
+
function usePreferences(client: CadreClient): CadronPreferences | null {
|
|
111
|
+
const [prefs, setPrefs] = useState<CadronPreferences | null>(null);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
const read = () => { void client.invitations.preferences().then(setPrefs); };
|
|
114
|
+
read();
|
|
115
|
+
return client.changes.invitations(read);
|
|
116
|
+
}, [client]);
|
|
117
|
+
return prefs;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Paste a node code, see what it says, approve, claim. A scanner would hand its text to `review` the same way. */
|
|
121
|
+
function AddCadron({ client, disabled }: { client: CadreClient; disabled: boolean }) {
|
|
122
|
+
const [code, setCode] = useState('');
|
|
123
|
+
const [busy, setBusy] = useState(false);
|
|
124
|
+
const review = async () => {
|
|
125
|
+
// Already in the network, or this device: the error says so (kind invalid-node-code).
|
|
126
|
+
const preview = await client.cadre.inspectNodeCode(code);
|
|
127
|
+
if (!preview.ok) { Alert.alert('That code cannot be used', preview.error.message); return; }
|
|
128
|
+
const p = preview.value;
|
|
129
|
+
const host = suggestedName(p);
|
|
130
|
+
const where = p.reach === 'home-network'
|
|
131
|
+
? 'It can only be reached on its own network: be on the same Wi-Fi as it.'
|
|
132
|
+
: 'It can be reached from anywhere.';
|
|
133
|
+
// Which machine, and which cadre it joins: the network id and owner key match what the
|
|
134
|
+
// person's other devices (and the node's own host, once it joins) show.
|
|
135
|
+
const what = `${host ? `${host} (${shortPeerId(p.peerId)})` : `Machine ${shortPeerId(p.peerId)}`} will join your network`
|
|
136
|
+
+ (p.partyId ? ` ${p.partyId}` : '') + (p.ownerFingerprint ? `, owned by your key ${p.ownerFingerprint}` : '') + '.';
|
|
137
|
+
Alert.alert('Add this machine?', `${what}\n\n${where}`, [
|
|
138
|
+
{ text: 'Cancel', style: 'cancel' },
|
|
139
|
+
{
|
|
140
|
+
text: 'Add',
|
|
141
|
+
onPress: async () => {
|
|
142
|
+
setBusy(true);
|
|
143
|
+
const claimed = await client.cadre.claimNode(code); // can take up to two minutes
|
|
144
|
+
setBusy(false);
|
|
145
|
+
if (!claimed.ok) { Alert.alert('Could not add the machine', claimed.error.message); return; } // node-unreachable, claim-refused, claim-failed
|
|
146
|
+
setCode('');
|
|
147
|
+
// Named after its host straight away; the person can rename it in the list.
|
|
148
|
+
if (host) {
|
|
149
|
+
const prefs = await client.invitations.preferences();
|
|
150
|
+
void client.invitations.setPreferences({ names: { ...prefs.names, [p.peerId]: host } });
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
]);
|
|
155
|
+
};
|
|
156
|
+
return (
|
|
157
|
+
<View style={styles.row}>
|
|
158
|
+
<TextInput style={[styles.input, styles.grow]} placeholder="sereus-join:…" value={code} onChangeText={setCode}
|
|
159
|
+
autoCapitalize="none" autoCorrect={false} editable={!busy && !disabled} />
|
|
160
|
+
<Pressable onPress={review} disabled={busy || disabled || !code.trim()}><Text>{busy ? 'Adding…' : 'Add'}</Text></Pressable>
|
|
161
|
+
</View>
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Rename a cadron in place; an empty name clears it. Names are kept on this device. */
|
|
166
|
+
function Rename({ client, machine, prefs }: { client: CadreClient; machine: CadreMachine; prefs: CadronPreferences | null }) {
|
|
167
|
+
const [editing, setEditing] = useState(false);
|
|
168
|
+
const [text, setText] = useState('');
|
|
169
|
+
if (!prefs) return null;
|
|
170
|
+
if (!editing) {
|
|
171
|
+
return <Pressable onPress={() => { setText(machine.name ?? suggestedName(machine) ?? ''); setEditing(true); }}><Text>Rename</Text></Pressable>;
|
|
172
|
+
}
|
|
173
|
+
const save = () => {
|
|
174
|
+
const names = { ...prefs.names };
|
|
175
|
+
if (text.trim()) names[machine.peerId] = text.trim(); else delete names[machine.peerId];
|
|
176
|
+
void client.invitations.setPreferences({ names });
|
|
177
|
+
setEditing(false);
|
|
178
|
+
};
|
|
179
|
+
return <TextInput style={styles.input} value={text} onChangeText={setText} onSubmitEditing={save} onBlur={save} autoFocus />;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function AddressCount({ client, prefs }: { client: CadreClient; prefs: CadronPreferences }) {
|
|
183
|
+
const set = (maxAddrs: number) => { void client.invitations.setPreferences({ maxAddrs }); };
|
|
184
|
+
return (
|
|
185
|
+
<View style={styles.row}>
|
|
186
|
+
<Text style={styles.grow}>Addresses per invitation (fewer: a shorter link, a simpler QR code)</Text>
|
|
187
|
+
<Pressable onPress={() => set(prefs.maxAddrs - 1)} disabled={prefs.maxAddrs <= 1}><Text>−</Text></Pressable>
|
|
188
|
+
<Text>{prefs.maxAddrs}</Text>
|
|
189
|
+
<Pressable onPress={() => set(prefs.maxAddrs + 1)} disabled={prefs.maxAddrs >= MAX_BOOTSTRAP_ADDRS}><Text>+</Text></Pressable>
|
|
190
|
+
</View>
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const styles = StyleSheet.create({
|
|
195
|
+
list: { gap: 8, padding: 16 },
|
|
196
|
+
header: { fontWeight: '600' },
|
|
197
|
+
row: { flexDirection: 'row', alignItems: 'center', gap: 10, paddingVertical: 6 },
|
|
198
|
+
dot: { width: 8, height: 8, borderRadius: 4 },
|
|
199
|
+
grow: { flex: 1 },
|
|
200
|
+
title: { fontSize: 15 },
|
|
201
|
+
subtitle: { fontSize: 12, color: '#666666' },
|
|
202
|
+
danger: { color: '#c4392f' },
|
|
203
|
+
input: { borderWidth: 1, borderColor: '#dddddd', borderRadius: 6, padding: 8 },
|
|
204
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example (React Native): beside an invitation the app has just minted. Copy and adapt; it is not
|
|
3
|
+
* built or published.
|
|
4
|
+
*
|
|
5
|
+
* - Whether it is ready to hand out: a cadron holds the strand, so it can answer a join while
|
|
6
|
+
* this phone sleeps (`invitationReadinessStore`).
|
|
7
|
+
* - "What's in this invitation": the machines it names, as the cadre screen shows them, each
|
|
8
|
+
* address a checkbox. The other addresses it could name are unchecked; checking or unchecking
|
|
9
|
+
* one re-encodes this invitation (`invitations.rebootstrap`) and hands the app the new token.
|
|
10
|
+
* - The QR code, which a tap opens full screen (dark on white, the largest the screen allows).
|
|
11
|
+
*
|
|
12
|
+
* Props: `token` is what `node.encodeInvitation(...)` returned (after `invitations.withBootstrap`),
|
|
13
|
+
* `link` the text actually shared (the URL the QR code carries), `strandId` the invitation's strand,
|
|
14
|
+
* `newStrand` true when the invitation founded it just now. `onRebootstrap`: the person changed its
|
|
15
|
+
* addresses; the app rebuilds its link and QR code from the new token, and keeps it with the
|
|
16
|
+
* invitation. A link already handed out still works, with the addresses it had.
|
|
17
|
+
*/
|
|
18
|
+
import React, { useEffect, useMemo, useState } from 'react';
|
|
19
|
+
import { Modal, Pressable, StyleSheet, Text, useWindowDimensions, View } from 'react-native';
|
|
20
|
+
import QRCode from 'react-native-qrcode-svg';
|
|
21
|
+
import { invitationReadinessStore, type CadronPresence, type InvitationText } from '@serfab/cadre-client';
|
|
22
|
+
import type { CadreClient } from '@serfab/cadre-client/sereus';
|
|
23
|
+
import { useStore } from './useStore';
|
|
24
|
+
|
|
25
|
+
interface Props {
|
|
26
|
+
client: CadreClient;
|
|
27
|
+
token: string;
|
|
28
|
+
link: string;
|
|
29
|
+
strandId: string;
|
|
30
|
+
newStrand: boolean;
|
|
31
|
+
onRebootstrap(token: string): void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const READINESS = {
|
|
35
|
+
ready: 'Ready to hand out: your machine can answer even while this phone sleeps.',
|
|
36
|
+
waiting: 'Getting ready to hand out… your machine is taking a copy of the conversation.',
|
|
37
|
+
'no-cadron': 'Only this phone can answer it: keep the app open until they join, or add a machine that stays on.',
|
|
38
|
+
} as const;
|
|
39
|
+
|
|
40
|
+
const KIND = {
|
|
41
|
+
public: 'public address', name: 'public name', relay: 'relayed', webrtc: 'relayed, then direct if possible',
|
|
42
|
+
private: 'home network only', loopback: 'this machine only',
|
|
43
|
+
} as const;
|
|
44
|
+
// As the cadre screen shows each cadron.
|
|
45
|
+
const DOT: Record<CadronPresence, string> = { connected: '#2c8a3f', recent: '#c98a00', quiet: '#888888', unknown: '#dddddd' };
|
|
46
|
+
const PRESENCE: Record<CadronPresence, string> = {
|
|
47
|
+
connected: 'connected now', recent: 'seen recently', quiet: 'not seen lately', unknown: 'not seen yet',
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** "in 7 days", "in 5 hours". */
|
|
51
|
+
function fromNow(iso: string): string {
|
|
52
|
+
const ms = new Date(iso).getTime() - Date.now();
|
|
53
|
+
if (ms <= 0) return 'expired';
|
|
54
|
+
const hours = Math.round(ms / 3_600_000);
|
|
55
|
+
return hours >= 48 ? `in ${Math.round(hours / 24)} days` : hours >= 1 ? `in ${hours} hours` : 'within the hour';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function InvitationPanel({ client, token, link, strandId, newStrand, onRebootstrap }: Props) {
|
|
59
|
+
const store = useMemo(() => invitationReadinessStore(client, strandId, { newStrand }), [client, strandId, newStrand]);
|
|
60
|
+
const readiness = useStore(store);
|
|
61
|
+
return (
|
|
62
|
+
<View style={styles.panel}>
|
|
63
|
+
<InvitationQr value={link} />
|
|
64
|
+
<Text style={readiness === 'ready' ? styles.ready : styles.muted}>{READINESS[readiness]}</Text>
|
|
65
|
+
<InvitationDetails client={client} token={token} link={link} onRebootstrap={onRebootstrap} />
|
|
66
|
+
</View>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function InvitationDetails({ client, token, link, onRebootstrap }: Pick<Props, 'client' | 'token' | 'link' | 'onRebootstrap'>) {
|
|
71
|
+
const [open, setOpen] = useState(false);
|
|
72
|
+
const [text, setText] = useState<InvitationText | null>(null);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (!open) return;
|
|
75
|
+
let live = true;
|
|
76
|
+
void client.invitations.describe(token, link).then(r => { if (live && r.ok) setText(r.value); });
|
|
77
|
+
return () => { live = false; };
|
|
78
|
+
}, [open, client, token, link]);
|
|
79
|
+
|
|
80
|
+
// Check or uncheck one address: the same invitation, re-encoded with the new list.
|
|
81
|
+
const toggle = async (addr: string, include: boolean) => {
|
|
82
|
+
if (!text) return;
|
|
83
|
+
const now = text.addresses.map(a => a.addr);
|
|
84
|
+
const next = include ? [...now, addr] : now.filter(a => a !== addr);
|
|
85
|
+
if (next.length === 0) return; // it must name at least one
|
|
86
|
+
const r = await client.invitations.rebootstrap(token, next);
|
|
87
|
+
if (r.ok) onRebootstrap(r.value);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<View>
|
|
92
|
+
<Pressable onPress={() => setOpen(o => !o)}><Text style={styles.link}>{open ? 'Hide the details' : "What's in this invitation"}</Text></Pressable>
|
|
93
|
+
{open && text && (
|
|
94
|
+
<View style={styles.details}>
|
|
95
|
+
{text.cadrons.map(c => (
|
|
96
|
+
<View key={c.peerId}>
|
|
97
|
+
<View style={styles.row}>
|
|
98
|
+
<View style={[styles.dot, { backgroundColor: c.presence ? DOT[c.presence] : 'transparent' }]} />
|
|
99
|
+
<Text style={styles.title}>{c.thisDevice ? 'This phone' : c.label}</Text>
|
|
100
|
+
</View>
|
|
101
|
+
<Text style={styles.muted}>
|
|
102
|
+
{[c.thisDevice ? null : c.yours === false ? 'Not one of your machines' : 'Your machine',
|
|
103
|
+
c.alwaysOn ? 'always on' : null, c.presence ? PRESENCE[c.presence] : null].filter(Boolean).join(' · ')}
|
|
104
|
+
</Text>
|
|
105
|
+
{c.addresses.map(a => (
|
|
106
|
+
<Pressable key={a.addr} style={styles.row} onPress={() => { void toggle(a.addr, !a.included); }}
|
|
107
|
+
accessibilityRole="checkbox" accessibilityState={{ checked: a.included }}>
|
|
108
|
+
<Text>{a.included ? '☑' : '☐'}</Text>
|
|
109
|
+
<Text style={a.reachesAnywhere ? undefined : styles.muted}>
|
|
110
|
+
{(a.kind ? KIND[a.kind] : 'not usable by a phone') + (a.via === 'wss' ? ', secure' : '') + ' · '
|
|
111
|
+
+ (a.via === 'relay' ? `through relay ${a.host}` : `${a.host}, port ${a.port ?? '?'}`)}
|
|
112
|
+
</Text>
|
|
113
|
+
</Pressable>
|
|
114
|
+
))}
|
|
115
|
+
</View>
|
|
116
|
+
))}
|
|
117
|
+
<Text style={styles.muted}>
|
|
118
|
+
{`${text.addresses.length} address(es)` + (text.maxAddrs ? `; your setting names up to ${text.maxAddrs}` : '')
|
|
119
|
+
+ (text.maxAddrs && text.addresses.length < text.maxAddrs && text.cadronAddrsLeftOut === 0
|
|
120
|
+
? '. Your machines have no other address a phone can use.' : '.')}
|
|
121
|
+
</Text>
|
|
122
|
+
<Text style={styles.muted}>Expires {fromNow(text.expiresAt)} ({new Date(text.expiresAt).toLocaleString()})</Text>
|
|
123
|
+
<Text style={styles.muted}>
|
|
124
|
+
Link {text.linkLength} characters · {text.qrVersion ? `QR code size ${text.qrVersion}` : 'too long for one QR code'}
|
|
125
|
+
</Text>
|
|
126
|
+
</View>
|
|
127
|
+
)}
|
|
128
|
+
</View>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function InvitationQr({ value }: { value: string }) {
|
|
133
|
+
const { width, height } = useWindowDimensions();
|
|
134
|
+
const [full, setFull] = useState(false);
|
|
135
|
+
return (
|
|
136
|
+
<>
|
|
137
|
+
<Pressable onPress={() => setFull(true)} style={styles.center}>
|
|
138
|
+
<QRCode value={value} size={180} />
|
|
139
|
+
<Text style={styles.muted}>Tap the code to enlarge it</Text>
|
|
140
|
+
</Pressable>
|
|
141
|
+
<Modal visible={full} animationType="fade" onRequestClose={() => setFull(false)} supportedOrientations={['portrait', 'landscape']}>
|
|
142
|
+
<Pressable style={styles.full} onPress={() => setFull(false)}>
|
|
143
|
+
<QRCode value={value} size={Math.floor(Math.min(width, height) * 0.9)} backgroundColor="#ffffff" color="#000000" />
|
|
144
|
+
<Text style={styles.muted}>Close</Text>
|
|
145
|
+
</Pressable>
|
|
146
|
+
</Modal>
|
|
147
|
+
</>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const styles = StyleSheet.create({
|
|
152
|
+
panel: { gap: 10, padding: 16 },
|
|
153
|
+
center: { alignItems: 'center', gap: 6 },
|
|
154
|
+
full: { flex: 1, backgroundColor: '#ffffff', alignItems: 'center', justifyContent: 'center', gap: 16 },
|
|
155
|
+
details: { gap: 4 },
|
|
156
|
+
row: { flexDirection: 'row', alignItems: 'center', gap: 8 },
|
|
157
|
+
dot: { width: 8, height: 8, borderRadius: 4 },
|
|
158
|
+
title: { fontWeight: '600' },
|
|
159
|
+
ready: { color: '#2c8a3f' },
|
|
160
|
+
muted: { color: '#777777' },
|
|
161
|
+
link: { color: '#0066cc' },
|
|
162
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binding a cadre-client store in React (React Native or web): `useSyncExternalStore` over the
|
|
3
|
+
* store's `subscribe` and `get`. A store returns the same object until its value changes, so this
|
|
4
|
+
* re-renders only on a change.
|
|
5
|
+
*/
|
|
6
|
+
import { useSyncExternalStore } from 'react';
|
|
7
|
+
import type { Readable } from '@serfab/cadre-client';
|
|
8
|
+
|
|
9
|
+
export function useStore<T>(store: Readable<T>): T {
|
|
10
|
+
return useSyncExternalStore(store.subscribe as (onChange: () => void) => () => void, store.get, store.get);
|
|
11
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Example (Svelte 5): a person's cadrons, with a status dot each, a star to name it first in
|
|
3
|
+
invitations, and how many addresses an invitation names. Copy and adapt; it is not built or
|
|
4
|
+
published. A cadre-client store follows the Svelte store contract, so `$store` reads it.
|
|
5
|
+
`client` is `createCadreClient(...)` from `@serfab/cadre-client/sereus`.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import {
|
|
9
|
+
cadreStore,
|
|
10
|
+
cadronPresence,
|
|
11
|
+
MAX_BOOTSTRAP_ADDRS,
|
|
12
|
+
shortPeerId,
|
|
13
|
+
suggestedName,
|
|
14
|
+
type CadreMachine,
|
|
15
|
+
type CadronPresence,
|
|
16
|
+
type CadronPreferences,
|
|
17
|
+
} from '@serfab/cadre-client';
|
|
18
|
+
import type { CadreClient } from '@serfab/cadre-client/sereus';
|
|
19
|
+
|
|
20
|
+
const { client }: { client: CadreClient } = $props();
|
|
21
|
+
|
|
22
|
+
const machines = $derived(cadreStore(client));
|
|
23
|
+
let prefs = $state<CadronPreferences | null>(null);
|
|
24
|
+
|
|
25
|
+
$effect(() => {
|
|
26
|
+
const read = () => { void client.invitations.preferences().then(p => { prefs = p; }); };
|
|
27
|
+
read();
|
|
28
|
+
return client.changes.invitations(read);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const DOT: Record<CadronPresence, string> = { connected: '#2c8a3f', recent: '#c98a00', quiet: '#888888', unknown: '#dddddd' };
|
|
32
|
+
const PRESENCE: Record<CadronPresence, string> = {
|
|
33
|
+
connected: 'Connected', recent: 'Seen recently', quiet: 'Not seen lately', unknown: 'Not seen yet',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function toggleStar(m: CadreMachine) {
|
|
37
|
+
if (!prefs) return;
|
|
38
|
+
const preferred = prefs.preferred.includes(m.peerId)
|
|
39
|
+
? prefs.preferred.filter(id => id !== m.peerId)
|
|
40
|
+
: [...prefs.preferred, m.peerId];
|
|
41
|
+
void client.invitations.setPreferences({ preferred });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function setCount(maxAddrs: number) {
|
|
45
|
+
void client.invitations.setPreferences({ maxAddrs });
|
|
46
|
+
}
|
|
47
|
+
</script>
|
|
48
|
+
|
|
49
|
+
{#if $machines.state === 'loading'}
|
|
50
|
+
<p>Loading…</p>
|
|
51
|
+
{:else if $machines.state === 'failed'}
|
|
52
|
+
<button onclick={() => machines.reload()}>{$machines.error.message} — retry</button>
|
|
53
|
+
{:else}
|
|
54
|
+
<ul>
|
|
55
|
+
{#each $machines.value.machines as m (m.peerId)}
|
|
56
|
+
{@const presence = cadronPresence(m)}
|
|
57
|
+
<li>
|
|
58
|
+
<span class="dot" style:background={DOT[presence]}></span>
|
|
59
|
+
<span class="grow">
|
|
60
|
+
{m.thisDevice ? 'This device' : m.name || suggestedName(m) || `Machine ${shortPeerId(m.peerId)}`}
|
|
61
|
+
<small>{[suggestedName(m), shortPeerId(m.peerId)].filter(Boolean).join(' · ')}</small>
|
|
62
|
+
{#if !m.thisDevice}<small>{m.trusted === false ? 'Not verified' : PRESENCE[presence]}{m.alwaysOn ? ' · Always on' : ''}</small>{/if}
|
|
63
|
+
</span>
|
|
64
|
+
{#if !m.thisDevice}
|
|
65
|
+
<button onclick={() => toggleStar(m)} aria-label="Preferred for invitations">{m.preferred ? '★' : '☆'}</button>
|
|
66
|
+
{/if}
|
|
67
|
+
</li>
|
|
68
|
+
{/each}
|
|
69
|
+
</ul>
|
|
70
|
+
{#if prefs}
|
|
71
|
+
<p>
|
|
72
|
+
Addresses per invitation:
|
|
73
|
+
<button onclick={() => setCount(prefs!.maxAddrs - 1)} disabled={prefs.maxAddrs <= 1}>−</button>
|
|
74
|
+
{prefs.maxAddrs}
|
|
75
|
+
<button onclick={() => setCount(prefs!.maxAddrs + 1)} disabled={prefs.maxAddrs >= MAX_BOOTSTRAP_ADDRS}>+</button>
|
|
76
|
+
</p>
|
|
77
|
+
{/if}
|
|
78
|
+
{/if}
|
|
79
|
+
|
|
80
|
+
<style>
|
|
81
|
+
ul { list-style: none; padding: 0; }
|
|
82
|
+
li { display: flex; align-items: center; gap: 0.6rem; padding: 0.3rem 0; }
|
|
83
|
+
.dot { width: 8px; height: 8px; border-radius: 50%; }
|
|
84
|
+
.grow { flex: 1; display: flex; flex-direction: column; }
|
|
85
|
+
small { color: #666; }
|
|
86
|
+
</style>
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Example (Svelte 5): beside an invitation the app has just minted: whether it is ready to hand
|
|
3
|
+
out, the machines it names (each address a checkbox; the unchecked ones it could name too, a
|
|
4
|
+
change re-encoding it through `invitations.rebootstrap`), and its QR code full screen on a click. Copy and adapt; it is not
|
|
5
|
+
built or published. The QR code here comes from the `qrcode` package (`toDataURL`).
|
|
6
|
+
Props as in the React Native example: `token` (encoded, after `invitations.withBootstrap`),
|
|
7
|
+
`link` (the text shared), `strandId`, `newStrand` (the invitation founded the strand just now), `onRebootstrap(token)` (the
|
|
8
|
+
person changed its addresses: the app rebuilds the link from the new token and keeps it).
|
|
9
|
+
-->
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
import QRCode from 'qrcode';
|
|
12
|
+
import { invitationReadinessStore, type InvitationText } from '@serfab/cadre-client';
|
|
13
|
+
import type { CadreClient } from '@serfab/cadre-client/sereus';
|
|
14
|
+
|
|
15
|
+
const { client, token, link, strandId, newStrand, onRebootstrap }:
|
|
16
|
+
{ client: CadreClient; token: string; link: string; strandId: string; newStrand: boolean; onRebootstrap(token: string): void } = $props();
|
|
17
|
+
|
|
18
|
+
const readiness = $derived(invitationReadinessStore(client, strandId, { newStrand }));
|
|
19
|
+
let details = $state<InvitationText | null>(null);
|
|
20
|
+
let open = $state(false);
|
|
21
|
+
let full = $state(false);
|
|
22
|
+
let qr = $state('');
|
|
23
|
+
|
|
24
|
+
$effect(() => { void QRCode.toDataURL(link, { margin: 2, width: 720 }).then(url => { qr = url; }); });
|
|
25
|
+
$effect(() => {
|
|
26
|
+
if (!open) return;
|
|
27
|
+
void client.invitations.describe(token, link).then(r => { if (r.ok) details = r.value; });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
async function toggle(addr: string, include: boolean) {
|
|
31
|
+
if (!details) return;
|
|
32
|
+
const now = details.addresses.map(a => a.addr);
|
|
33
|
+
const next = include ? [...now, addr] : now.filter(a => a !== addr);
|
|
34
|
+
if (next.length === 0) return;
|
|
35
|
+
const r = await client.invitations.rebootstrap(token, next);
|
|
36
|
+
if (r.ok) onRebootstrap(r.value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const READINESS = {
|
|
40
|
+
ready: 'Ready to hand out: your machine can answer even while this device sleeps.',
|
|
41
|
+
waiting: 'Getting ready to hand out… your machine is taking a copy of the conversation.',
|
|
42
|
+
'no-cadron': 'Only this device can answer it: keep the app open until they join, or add a machine that stays on.',
|
|
43
|
+
} as const;
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
{#if qr}
|
|
47
|
+
<button class="qr" onclick={() => { full = true; }} aria-label="Enlarge the QR code"><img src={qr} alt="Invitation QR code" width="180" /></button>
|
|
48
|
+
{/if}
|
|
49
|
+
<p class:ready={$readiness === 'ready'}>{READINESS[$readiness]}</p>
|
|
50
|
+
|
|
51
|
+
<button class="link" onclick={() => { open = !open; }}>{open ? 'Hide the details' : "What's in this invitation"}</button>
|
|
52
|
+
{#if open && details}
|
|
53
|
+
{#each details.cadrons as c (c.peerId)}
|
|
54
|
+
<p><strong>{c.thisDevice ? 'This device' : c.label}</strong>
|
|
55
|
+
<span class="muted">{c.thisDevice ? '' : c.yours === false ? 'Not one of your machines' : 'Your machine'}{c.presence ? ` · ${c.presence}` : ''}</span></p>
|
|
56
|
+
{#each c.addresses as a (a.addr)}
|
|
57
|
+
<label class:muted={!a.reachesAnywhere}>
|
|
58
|
+
<input type="checkbox" checked={a.included} onchange={() => toggle(a.addr, !a.included)} />
|
|
59
|
+
{a.kind ?? 'not usable by a phone'}{a.via === 'wss' ? ', secure' : ''} ·
|
|
60
|
+
{a.via === 'relay' ? `through relay ${a.host}` : `${a.host}, port ${a.port ?? '?'}`}
|
|
61
|
+
</label>
|
|
62
|
+
{/each}
|
|
63
|
+
{/each}
|
|
64
|
+
<p class="muted">Expires {new Date(details.expiresAt).toLocaleString()} · link {details.linkLength} characters</p>
|
|
65
|
+
{/if}
|
|
66
|
+
|
|
67
|
+
{#if full}
|
|
68
|
+
<button class="full" onclick={() => { full = false; }} aria-label="Close"><img src={qr} alt="Invitation QR code" /></button>
|
|
69
|
+
{/if}
|
|
70
|
+
|
|
71
|
+
<style>
|
|
72
|
+
.qr, .link, .full { background: none; border: 0; padding: 0; cursor: pointer; }
|
|
73
|
+
.link { color: #0066cc; }
|
|
74
|
+
.ready { color: #2c8a3f; }
|
|
75
|
+
.muted { color: #777; }
|
|
76
|
+
label { display: block; }
|
|
77
|
+
.full { position: fixed; inset: 0; background: #fff; display: grid; place-items: center; }
|
|
78
|
+
.full img { width: min(90vw, 90vh); height: auto; }
|
|
79
|
+
</style>
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serfab/cadre-client",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Cadre management for Sereus apps, without UI: cadrons (the cadre's machines), claiming a node by its code, owner key backup, relays, reachability, and which cadrons an invitation names",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"dist",
|
|
11
|
+
"examples",
|
|
12
|
+
"ADOPTING.md",
|
|
13
|
+
"!**/*.tsbuildinfo"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./sereus": {
|
|
21
|
+
"types": "./dist/sereus/index.d.ts",
|
|
22
|
+
"import": "./dist/sereus/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./app-link": {
|
|
25
|
+
"types": "./dist/app-link/index.d.ts",
|
|
26
|
+
"import": "./dist/app-link/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./lifecycle": {
|
|
29
|
+
"types": "./dist/lifecycle/index.d.ts",
|
|
30
|
+
"import": "./dist/lifecycle/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/gotchoices/sereus.git",
|
|
37
|
+
"directory": "packages/cadre-client"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"sereus",
|
|
41
|
+
"cadre",
|
|
42
|
+
"cadron",
|
|
43
|
+
"invitation"
|
|
44
|
+
],
|
|
45
|
+
"scripts": {
|
|
46
|
+
"clean": "rimraf dist",
|
|
47
|
+
"build": "tsc -p tsconfig.build.json",
|
|
48
|
+
"typecheck": "tsc -p tsconfig.typecheck.json --noEmit",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"dev:test": "vitest"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@multiformats/multiaddr": "^13.0.3",
|
|
54
|
+
"uint8arrays": "^6.1.1"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"@serfab/cadre-core": "^2.0.0"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@serfab/cadre-core": "^2.0.0",
|
|
61
|
+
"@types/node": "^25.0.10",
|
|
62
|
+
"rimraf": "^6.1.2",
|
|
63
|
+
"typescript": "^5.9.3",
|
|
64
|
+
"vitest": "^4.0.17"
|
|
65
|
+
},
|
|
66
|
+
"author": "Got Choices Foundation",
|
|
67
|
+
"license": "MIT"
|
|
6
68
|
}
|