terminalchat-cli 0.0.0-stage → 0.1.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/README.md +553 -2
- package/bin/terminalchat.js +81 -0
- package/package.json +38 -4
- package/src/engine.js +407 -0
- package/src/identity.js +80 -0
- package/src/network.js +224 -0
- package/src/outbox.js +159 -0
- package/src/protocol.js +175 -0
- package/src/store.js +168 -0
- package/src/ui/App.js +68 -0
- package/src/ui/ChatPane.js +23 -0
- package/src/ui/Overlays.js +69 -0
- package/src/ui/Sidebar.js +18 -0
- package/src/ui/useEngine.js +71 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
export function useEngine (engine) {
|
|
4
|
+
const [snapshot, setSnapshot] = useState({
|
|
5
|
+
friends: [],
|
|
6
|
+
selectedKey: null,
|
|
7
|
+
messages: {},
|
|
8
|
+
unread: {},
|
|
9
|
+
online: {},
|
|
10
|
+
ready: false,
|
|
11
|
+
error: null,
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
let active = true
|
|
16
|
+
const refresh = () => {
|
|
17
|
+
if (!active) return
|
|
18
|
+
setSnapshot(previous => ({
|
|
19
|
+
...previous,
|
|
20
|
+
friends: engine.getFriends(),
|
|
21
|
+
online: Object.fromEntries(engine.getFriends().map(friend => [friend.keyHex, friend.online])),
|
|
22
|
+
}))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const onReady = async () => {
|
|
26
|
+
const friends = engine.getFriends()
|
|
27
|
+
const messages = {}
|
|
28
|
+
for (const friend of friends) messages[friend.keyHex] = await engine.getHistory(friend.keyHex)
|
|
29
|
+
if (active) setSnapshot(previous => ({ ...previous, friends, messages, ready: true }))
|
|
30
|
+
}
|
|
31
|
+
const onMessage = async (key, entry) => {
|
|
32
|
+
const history = await engine.getHistory(key)
|
|
33
|
+
if (!active) return
|
|
34
|
+
setSnapshot(previous => ({
|
|
35
|
+
...previous,
|
|
36
|
+
messages: { ...previous.messages, [key]: history },
|
|
37
|
+
unread: entry.direction === 'in' && previous.selectedKey !== key
|
|
38
|
+
? { ...previous.unread, [key]: (previous.unread[key] ?? 0) + 1 }
|
|
39
|
+
: previous.unread,
|
|
40
|
+
}))
|
|
41
|
+
}
|
|
42
|
+
const onFriend = () => refresh()
|
|
43
|
+
const onError = error => setSnapshot(previous => ({ ...previous, error }))
|
|
44
|
+
|
|
45
|
+
engine.on('ready', onReady)
|
|
46
|
+
engine.on('message', onMessage)
|
|
47
|
+
engine.on('friend-added', onFriend)
|
|
48
|
+
engine.on('friend-online', onFriend)
|
|
49
|
+
engine.on('friend-offline', onFriend)
|
|
50
|
+
engine.on('ack', onMessage)
|
|
51
|
+
engine.on('error', onError)
|
|
52
|
+
engine.start().catch(onError)
|
|
53
|
+
|
|
54
|
+
return () => {
|
|
55
|
+
active = false
|
|
56
|
+
for (const [event, listener] of [
|
|
57
|
+
['ready', onReady], ['message', onMessage], ['friend-added', onFriend],
|
|
58
|
+
['friend-online', onFriend], ['friend-offline', onFriend],
|
|
59
|
+
['ack', onMessage], ['error', onError],
|
|
60
|
+
]) engine.off(event, listener)
|
|
61
|
+
}
|
|
62
|
+
}, [engine])
|
|
63
|
+
|
|
64
|
+
const selectFriend = key => setSnapshot(previous => ({
|
|
65
|
+
...previous,
|
|
66
|
+
selectedKey: key,
|
|
67
|
+
unread: { ...previous.unread, [key]: 0 },
|
|
68
|
+
}))
|
|
69
|
+
|
|
70
|
+
return { ...snapshot, selectFriend }
|
|
71
|
+
}
|