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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 | ;; ported-from: src/app-boot.ts @ v1.2.0
;;
;; One-call social attachment for suite APPS (the hub has its own richer UI).
;;
;; An app that already runs a libp2p node hands over its pubsub and gets:
;; presence beacons to its user's friends ("in lampion"), live invite toasts,
;; a small fixed "friends can join" toggle that — only while switched on —
;; shares the current room link with friends (a capability grant, off by
;; default, per-session), and an "invite a friend" chip that opens a picker
;; and seals an invite for the current room straight to a friend (live when
;; they're around, parked in the pair drop otherwise). Friends come from the
;; identity bridge's soc blob; apps never manage the list, the hub does.
;;
;; Theming: every color reads a --soc-* var with the historical value as the
;; fallback, so unopted apps render exactly as before. An app opts in by
;; defining --soc-bg/--soc-fg/--soc-dim/--soc-line/--soc-accent on :root.
;; Body-class contract: `soc-play` hides the kit's chips during gameplay;
;; `soc-menu` shows the homeChip (join.cljs) on menu screens.
(ns ardegazu.social.app-boot
(:require [ardegazu.social.friends :as friends]
[ardegazu.social.notify :as notify]
[ardegazu.social.presence :as presence]
[ardegazu.social.text :as text])
(:require-macros [ardegazu.social.macros :refer [awaits obj oget truthy?]]))
(def ^:private CHIP-CSS "
.soc-chip {
position: fixed; left: calc(env(safe-area-inset-left, 0px) + 12px); z-index: 9998;
display: flex; gap: 7px; align-items: center;
font: 600 12px/1 -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif;
color: var(--soc-dim, #a49fb5); background: var(--soc-bg, rgba(20, 18, 28, .82));
border: 1px solid var(--soc-line, #2b2838);
border-radius: 999px; padding: 7px 12px; cursor: pointer; user-select: none;
backdrop-filter: blur(6px);
}
.soc-chip.join { bottom: calc(env(safe-area-inset-bottom, 0px) + 12px); }
.soc-chip.inv { bottom: calc(env(safe-area-inset-bottom, 0px) + 52px); }
.soc-chip.on { color: var(--soc-accent, #93c1a5); border-color: var(--soc-accent, #93c1a5); }
.soc-chip i { width: 8px; height: 8px; border-radius: 50%; background: var(--soc-line, #2b2838); font-style: normal; }
.soc-chip.on i { background: var(--soc-accent, #93c1a5); }
body.soc-play .soc-chip, body.soc-play .soc-joinchip { display: none; }
.soc-sheet {
position: fixed; inset: 0; z-index: 9999; display: flex;
align-items: flex-end; justify-content: flex-start;
background: rgba(0,0,0,.45);
}
.soc-sheet .box {
margin: 12px; min-width: 260px; max-width: 340px; max-height: 60vh; overflow-y: auto;
background: var(--soc-bg, #1e1c2a); color: var(--soc-fg, #eae6f2);
border: 1px solid var(--soc-line, #2b2838); border-radius: 14px;
padding: 14px 16px;
font: 14px/1.5 -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif;
}
.soc-sheet h3 { font-size: 12px; letter-spacing: .14em; text-transform: uppercase; color: var(--soc-dim, #a49fb5); margin: 0 0 8px; }
.soc-sheet .row {
display: flex; gap: 10px; align-items: center; padding: 8px 0;
border-top: 1px solid var(--soc-line, #2b2838);
}
.soc-sheet .row:first-of-type { border-top: 0; }
.soc-sheet .dot { flex: none; width: 9px; height: 9px; border-radius: 50%; background: var(--soc-line, #2b2838); }
.soc-sheet .dot.on { background: var(--soc-accent, #93c1a5); }
.soc-sheet .who { flex: 1; min-width: 0; font-weight: 650; }
.soc-sheet .who small { display: block; font-weight: 400; color: var(--soc-dim, #a49fb5); font-size: 12px; }
.soc-sheet button {
font: 600 12px/1 inherit; color: var(--soc-fg, #eae6f2); cursor: pointer;
background: var(--soc-bg, #272334); border: 1px solid var(--soc-accent, #93c1a5); border-radius: 8px; padding: 7px 11px;
}
.soc-sheet button[disabled] { border-color: var(--soc-line, #2b2838); color: var(--soc-dim, #a49fb5); cursor: default; }
.soc-sheet button.share { display: block; width: 100%; margin: 0 0 6px; padding: 9px 11px; }
.soc-sheet .hint { color: var(--soc-dim, #a49fb5); font-size: 13px; }
.soc-sheet a.hub { text-decoration: none; color: var(--soc-accent, #93c1a5); font-weight: 650; }
@media (max-width: 640px) {
.soc-sheet .box {
margin: 0; width: 100%; min-width: 0; max-width: none;
border-radius: 14px 14px 0 0;
padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
}
}
")
(def ^:private css-injected (volatile! false))
(defn- ensure-css []
(when-not @css-injected
(vreset! css-injected true)
(let [style (js/document.createElement "style")]
(set! (.-textContent style) CHIP-CSS)
(.appendChild js/document.head style))))
;; ---- DOM -------------------------------------------------------------------
;;
;; Built element by element, never through innerHTML. The markup here is all
;; static today, so the old `(set! (.-innerHTML …) "<span class=\"dot\"></span>")`
;; was safe — but this sheet renders friend-supplied names one line further
;; down, so keeping a parsing sink in the file is the kind of thing that is
;; only ever safe until someone interpolates into it. textContent is the only
;; way text enters the DOM in this namespace.
(defn- el
"A fresh element with a class, and optionally its text."
([tag class] (el tag class nil))
([tag class txt]
(let [e (js/document.createElement tag)]
(set! (.-className e) class)
(when (some? txt) (set! (.-textContent e) txt))
e)))
(defn- button
"A real `type=\"button\"` — inside a form, a default-type button submits it."
[class label]
(let [b (el "button" class label)]
(set! (.-type b) "button")
b))
(defn- share-button
"Share-first: the room link IS the invite, friends or not. On phones the
share sheet is the natural carrier; elsewhere the clipboard. A DISMISSED
share sheet is an answer, so there is no clipboard fallback behind it."
[tt room-link]
(let [b (button "share" (tt "social.sheet.share" js/undefined))]
(.addEventListener b "click"
(fn []
(let [url (room-link)]
(if (identical? (js* "typeof ~{}" (.-share js/navigator)) "function")
(-> (.share js/navigator (obj "url" url))
(.catch (fn [_] nil)))
(when-let [cb (.-clipboard js/navigator)]
(.then (.writeText ^js cb url)
(fn [_] (set! (.-textContent b)
(tt "social.toast.copied" js/undefined)))
(fn [_] nil)))))))
b))
(defn- friend-row
"One friend: presence dot, label + where-they-are, and the invite button.
The button ladder is disabled -> \"…\" -> sent/parked, because sealing an
envelope and reaching a mailbox both take a moment and a second click would
send a second invite."
[agent store opts tt f]
(let [online (.presenceOf ^js agent (oget f "pub"))
row (el "div" "row")
who (el "span" "who" (.labelOf ^js store f))
small (js/document.createElement "small")
btn (button "" (tt "social.sheet.invite" js/undefined))]
(.appendChild row (el "span" (if (some? online) "dot on" "dot")))
(set! (.-textContent small)
(if (some? online)
(tt "social.sheet.here-now"
(obj "app" (aget (.split ^js (oget online "app") ".") 0)))
(tt "social.sheet.drop" js/undefined)))
(.appendChild who small)
(.appendChild row who)
(.addEventListener btn "click"
(fn []
(set! (.-disabled btn) true)
(set! (.-textContent btn) "…")
(let [app-short (aget (.split ^js (oget opts "app") ".") 0)]
;; the label travels as EN prose for old receivers; new
;; receivers render the "join" code in their own language
(.then (.sendInvite ^js agent
#js [(oget f "pub")]
(oget opts "app")
((oget opts "roomLink"))
(text/en-text "social.invite.join" (obj "app" app-short))
(obj "k" "join"))
(fn [n]
(set! (.-textContent btn)
(if (pos? n)
(tt "social.sheet.sent" js/undefined)
(tt "social.sheet.parked" js/undefined))))))))
(.appendChild row btn)
row))
(defn- online-first
"Online first; ties break on the label so the order holds across opens."
[agent store]
(.sort (.friends ^js store)
(fn [a b]
(let [pa (if (some? (.presenceOf ^js agent (oget a "pub"))) 1 0)
pb (if (some? (.presenceOf ^js agent (oget b "pub"))) 1 0)
d (- pb pa)]
(if (zero? d)
(.localeCompare ^js (.labelOf ^js store a) (.labelOf ^js store b))
d)))))
(defn- append-cold-start!
"No friends yet: say the honest thing and point at the hub's friend flow."
[box tt]
(.appendChild box (el "div" "row hint" (tt "social.sheet.nofriends" js/undefined)))
(let [hub (el "a" "row hub" (tt "social.sheet.hub" js/undefined))]
(set! (.-href hub) "https://ardegazu.ro/#/you")
(.appendChild box hub)))
(defn- open-invite-sheet [agent store opts]
(ensure-css)
(let [tt (text/mk-t (oget opts "t"))]
;; one sheet at a time — re-opening replaces rather than stacks
(when-let [prev (js/document.querySelector ".soc-sheet")]
(.remove prev))
(let [overlay (el "div" "soc-sheet")
box (el "div" "box")
friends-arr (online-first agent store)
n (.-length friends-arr)]
(.appendChild box (el "h3" "" (tt "social.sheet.title" js/undefined)))
(.appendChild overlay box)
;; only the backdrop closes; a click inside the box must not
(.addEventListener overlay "click"
(fn [e]
(when (identical? (.-target e) overlay)
(.remove overlay))))
(.appendChild box (share-button tt (oget opts "roomLink")))
(loop [i 0]
(when (< i n)
(.appendChild box (friend-row agent store opts tt (aget friends-arr i)))
(recur (inc i))))
(when (zero? n)
(append-cold-start! box tt))
(.appendChild js/document.body overlay)
js/undefined)))
;; ---- chips -------------------------------------------------------------------
(defn- chip
"One fixed chip. `classes` carries the position (`join` / `inv`); the label
goes in a span so the CSS can put a status dot beside it."
[classes label title]
(let [c (el "div" (str "soc-chip " classes))]
(.appendChild c (el "span" "" label))
(set! (.-title c) title)
c))
(defn- add-join-toggle!
"The join toggle grants friends a capability while it is on — off by
default, per session, and pointless with no friends yet, which is why it
only renders once there are some."
[tt share-on]
(let [c (el "div" "soc-chip join")]
(.appendChild c (el "i" ""))
(.appendChild c (el "span" "" (tt "social.chip.join" js/undefined)))
(set! (.-title c) (tt "social.chip.join.title" js/undefined))
(.addEventListener c "click"
(fn []
(vswap! share-on not)
(.toggle (.-classList c) "on" @share-on)))
(.appendChild js/document.body c)))
(defn- add-invite-chip!
"Renders even friendless — the sheet's share-first row and hub pointer are
the cold start."
[agent store opts tt]
(let [c (chip "inv"
(tt "social.chip.invite" js/undefined)
(tt "social.chip.invite.title" js/undefined))]
(.addEventListener c "click" (fn [] (open-invite-sheet agent store opts)))
(.appendChild js/document.body c)))
;; ---- attach ------------------------------------------------------------------
(defn- agent-options
"The SocialAgent options this kit builds on an app's behalf. `joinLink` is a
thunk read at every beacon, so flipping the toggle takes effect on the next
round with nothing to re-register."
[opts store share-on room-link]
(obj "ns" (oget opts "ns")
"app" (oget opts "app")
"identity" (oget opts "identity")
"seed" (oget opts "seed")
"myName" (oget opts "myName")
"store" store
"pubsub" (oget opts "pubsub")
"joinLink" (fn []
(when (and @share-on (truthy? room-link))
(room-link)))
;; optional: brain/model versions this peer is playing, read at every
;; beacon (bots fill it)
"brains" (oget opts "brains")
"profileSync" (oget opts "profileSync")
"appState" (oget opts "appState")
;; the identity record's per-app map, so an app can carry its own
;; section across devices without a kit change
"appsSync" (oget opts "appsSync")
"events" (obj "invite"
(fn [inv]
(notify/invite-toast
inv
(obj "label" (.labelOf ^js store (oget inv "from"))
"t" (oget opts "t")))))))
(defn attach-social [opts]
(-> (awaits [_ (js/Promise.resolve nil)]
(let [store (friends/FriendStore. (oget opts "ns"))
share-on (volatile! false)
room-link (oget opts "roomLink")]
(when (truthy? (oget opts "soc"))
(.merge ^js store (oget opts "soc")))
(let [agent (presence/SocialAgent. (agent-options opts store share-on room-link))]
(awaits [_ (.start ^js agent)]
(when (and (not (truthy? (oget opts "noJoinToggle")))
(truthy? room-link))
(ensure-css)
(let [tt (text/mk-t (oget opts "t"))]
(when (pos? (.-length ^js (.friends ^js store)))
(add-join-toggle! tt share-on))
(add-invite-chip! agent store opts tt)))
agent))))
;; the social layer must never break an app
(.catch (fn [_] nil))))
|