InboxSetup.svelte (11200B)
1 <script lang="ts"> 2 /* global setInterval, clearInterval */ 3 import { getContext, onMount } from 'svelte'; 4 import Button from './primitives/Button.svelte'; 5 import { 6 IDENTITY_VIEW_CONTEXT, 7 subscribeIdentityView, 8 type IdentityViewContext 9 } from '../runtime/view-context.ts'; 10 import { 11 PUBLIC_RUNTIME_CONTEXT, 12 publicRuntimeReady, 13 type PublicRuntimeContext, 14 type PublicRuntime 15 } from '../runtime/public-runtime.ts'; 16 import { 17 createRuntimeInboxSetupView, 18 inboxSetupViewSnapshot, 19 subscribeInboxSetupView, 20 checkInboxSetupView, 21 reviewInboxSetupView, 22 enableInboxSetupView, 23 unlockInboxSetupView, 24 readbackInboxSetupView, 25 stopInboxSetupView, 26 closeInboxSetupView, 27 observeInboxSetupView, 28 type InboxSetupView 29 } from '../messaging/inbox-setup-view.ts'; 30 let { 31 controller, 32 inline = false 33 }: { controller?: InboxSetupView; inline?: boolean } = $props(); 34 const identityContext = getContext<IdentityViewContext>( 35 IDENTITY_VIEW_CONTEXT 36 ); 37 const publicContext = getContext<PublicRuntimeContext>( 38 PUBLIC_RUNTIME_CONTEXT 39 ); 40 let owned = $state.raw<InboxSetupView | undefined>(), 41 panel = $state(inboxSetupViewSnapshot(undefined)), 42 selected = $state<string[]>([]), 43 removeTags = $state<number[]>([]), 44 removeFields = $state<string[]>([]); 45 let runtime: PublicRuntime | undefined; 46 let off = () => {}; 47 onMount(() => { 48 let disposed = false; 49 function acquire() { 50 if (disposed) return; 51 if (owned) { 52 // An injected genuine owner belongs to its caller. The production 53 // context must retire its own old generation before reconnecting. 54 if (controller || inboxSetupViewSnapshot(owned).owner) return; 55 off(); 56 closeInboxSetupView(owned); 57 owned = undefined; 58 panel = inboxSetupViewSnapshot(undefined); 59 selected = []; 60 removeTags = []; 61 removeFields = []; 62 } 63 owned = 64 controller ?? 65 (runtime && createRuntimeInboxSetupView(identityContext, runtime)); 66 if (owned) 67 off = subscribeInboxSetupView(owned, (next) => { 68 panel = next; 69 }); 70 } 71 const identityOff = identityContext 72 ? subscribeIdentityView(identityContext, acquire) 73 : () => {}; 74 acquire(); 75 const timer = inline 76 ? setInterval(() => observeInboxSetupView(owned), 250) 77 : undefined; 78 if (!controller && publicContext) 79 void publicRuntimeReady(publicContext) 80 .then((value) => { 81 if (disposed) return; 82 runtime = value; 83 acquire(); 84 }) 85 .catch(() => { 86 /* Keep unavailable panel without acquiring any effects. */ 87 }); 88 return () => { 89 disposed = true; 90 if (timer !== undefined) clearInterval(timer); 91 identityOff(); 92 off(); 93 closeInboxSetupView(owned); 94 }; 95 }); 96 function originalFields(wire: string | null): { 97 tags: unknown[]; 98 extras: string[]; 99 } { 100 if (!wire) return { tags: [], extras: [] }; 101 try { 102 const value: unknown = JSON.parse(wire); 103 if (!value || typeof value !== 'object' || Array.isArray(value)) 104 return { tags: [], extras: [] }; 105 const row = value as Record<string, unknown>; 106 const extras: string[] = []; 107 function appendExtra(key: string) { 108 extras.push(key); 109 } 110 for (const key in row) { 111 if ( 112 ![ 113 'id', 114 'pubkey', 115 'sig', 116 'kind', 117 'created_at', 118 'tags', 119 'content' 120 ].includes(key) 121 ) 122 appendExtra(key); 123 } 124 return { 125 tags: Array.isArray(row.tags) ? row.tags : [], 126 extras 127 }; 128 } catch { 129 return { tags: [], extras: [] }; 130 } 131 } 132 const original = $derived(originalFields(panel.currentWire)); 133 function advertisedRelays(wire: string): string[] { 134 const relays: string[] = []; 135 function append(origin: string) { 136 relays.push(origin); 137 } 138 for (const tag of originalFields(wire).tags) { 139 if ( 140 Array.isArray(tag) && 141 tag.length === 2 && 142 tag[0] === 'relay' && 143 typeof tag[1] === 'string' 144 ) 145 append(tag[1]); 146 } 147 return relays; 148 } 149 const proposedRelays = $derived( 150 panel.preview ? advertisedRelays(panel.preview.wire) : [] 151 ); 152 const messages: Record<string, string> = { 153 not_checked: 154 'Check your observed public inbox preference before making changes.', 155 checking: 'Checking your public inbox preference.', 156 lookup_incomplete: 157 'The lookup is incomplete. Your current inbox preference is unknown.', 158 not_observed: 159 'No preference was observed in this completed bounded lookup. This does not prove global absence.', 160 needs_review: 'Review your observed configuration before changing it.', 161 compatible: 162 'Your observed preference is compatible with the configured destinations. Unlock to check inbox access.', 163 review: 'Review the exact public replacement before enabling your inbox.', 164 approval_wait: 165 'Preparing your inbox setup. Extension approvals or named relay acceptance may be pending. Another approval may be needed.', 166 awaiting_readback: 167 'Relay acceptance and exact readback are separate facts.', 168 checking_readback: 169 'Checking the exact signed preference on the discovery path.', 170 access_unavailable: 'Inbox access has not been qualified and exercised.', 171 compatible_existing: 172 'Your existing inbox configuration and exercised access are confirmed for this session.', 173 verified: 174 'Preference acceptance, exact readback and exercised inbox access are confirmed for this session.', 175 paused: 'Inbox setup is paused. Your unsent text stays in this page.', 176 unavailable: 177 'Inbox setup is unavailable. Connect a messaging-capable extension and use qualified inbox sources.', 178 storage_unavailable: 179 'Browser storage is unavailable. The preference change has not been confirmed.', 180 conflict: 181 'A conflicting inbox preference was observed. Check the current preference and review again.', 182 stopped: 183 'Setup stopped. Review the original operation before any further action.', 184 needs_action: 185 'The preference operation needs attention. Setup is not complete.' 186 }; 187 </script> 188 189 <section class="stack" aria-label="Inbox setup"> 190 <h2>Reply inbox</h2> 191 <p role="status" aria-live="polite"> 192 {messages[panel.status] ?? 193 'Setup could not continue. Check your current preference before trying again.'} 194 </p> 195 <p> 196 A relay stores encrypted messages for retrieval. Enabling an inbox changes 197 your public preference; it does not send your enquiry or publish a listing. 198 </p> 199 {#if panel.owner}<p>Public key: <code>{panel.owner}</code></p>{/if} 200 {#if !inline || ['not_checked', 'lookup_incomplete', 'unavailable', 'conflict', 'stopped', 'needs_action'].includes(panel.status)}<Button 201 label="Check current preference" 202 variant={inline ? 'primary' : 'secondary'} 203 disabled={!owned || panel.busy} 204 onclick={() => { 205 void checkInboxSetupView(owned); 206 }} 207 />{/if} 208 {#if panel.currentWire} 209 <h3>Current observed destinations</h3> 210 <ul aria-label="Current observed inbox destinations"> 211 {#each panel.existingInboxes as origin, index (index)}<li> 212 {origin} 213 </li>{/each} 214 </ul> 215 <details> 216 <summary>Exact current public preference</summary> 217 <pre>{panel.currentWire}</pre> 218 </details> 219 {/if} 220 {#if panel.status === 'compatible' || panel.status === 'access_unavailable' || panel.status === 'awaiting_readback'} 221 <Button 222 label="Unlock inbox" 223 variant={inline ? 'primary' : 'secondary'} 224 disabled={!owned || panel.busy} 225 onclick={() => { 226 void unlockInboxSetupView(owned); 227 }} 228 /> 229 {/if} 230 {#if (!inline || ['needs_review', 'not_observed'].includes(panel.status)) && panel.status !== 'compatible' && panel.availableInboxes.length > 0 && panel.currentWire !== undefined && !panel.busy && panel.status !== 'review' && panel.status !== 'awaiting_readback'} 231 <fieldset> 232 <legend>Select 1–3 configured inboxes</legend> 233 {#each panel.availableInboxes as origin (origin)} 234 <label 235 ><input 236 type="checkbox" 237 bind:group={selected} 238 value={origin} 239 />{origin}</label 240 > 241 {/each} 242 </fieldset> 243 {#if original.tags.length > 0 || original.extras.length > 0} 244 <details> 245 <summary>Explicit removals from the replacement</summary> 246 <p> 247 Existing entries remain unless you select their removal. Unsupported 248 extra fields require an explicit decision before replacement. 249 </p> 250 {#each original.tags as tag, index (index)}<label 251 ><input 252 type="checkbox" 253 bind:group={removeTags} 254 value={index} 255 />Remove entry {index + 1}: 256 <code>{JSON.stringify(tag)}</code></label 257 >{/each} 258 {#each original.extras as field (field)}<label 259 ><input 260 type="checkbox" 261 bind:group={removeFields} 262 value={field} 263 />Remove extra field {field}</label 264 >{/each} 265 </details> 266 {/if} 267 <Button 268 label="Review inbox change" 269 variant={inline ? 'primary' : 'secondary'} 270 disabled={!owned || 271 panel.busy || 272 selected.length < 1 || 273 selected.length > 3} 274 onclick={() => { 275 void reviewInboxSetupView(owned, selected, removeTags, removeFields); 276 }} 277 /> 278 {/if} 279 {#if panel.preview} 280 <h3>Selected configured reply inboxes</h3> 281 <ul> 282 {#each panel.preview.selectedInboxes as origin (origin)}<li> 283 {origin} 284 </li>{/each} 285 </ul> 286 <h3>Proposed public inbox destinations</h3> 287 <ul aria-label="Proposed public inbox destinations"> 288 {#each proposedRelays as origin, index (index)}<li>{origin}</li>{/each} 289 </ul> 290 <p>{panel.preview.globalEffect}</p> 291 <h3>Public preference publication destinations</h3> 292 <ul> 293 {#each panel.preview.destinations as origin (origin)}<li> 294 {origin} 295 </li>{/each} 296 </ul> 297 <details open> 298 <summary>Exact proposed public preference</summary> 299 <pre>{panel.preview.wire}</pre> 300 </details> 301 <Button 302 label="Enable inbox" 303 variant="primary" 304 disabled={panel.busy} 305 onclick={() => { 306 void enableInboxSetupView(owned); 307 }} 308 /> 309 {/if} 310 {#each panel.accepted as origin, index (index)}<p> 311 Accepted by {origin} 312 </p>{/each} 313 {#each panel.readback as origin (origin)}<p> 314 Exact signed preference read back from {origin} 315 </p>{/each} 316 {#if panel.accepted.length > 0 && (!inline || ['awaiting_readback', 'needs_action'].includes(panel.status))}<Button 317 label="Check acceptance and readback" 318 disabled={panel.busy} 319 onclick={() => { 320 void readbackInboxSetupView(owned); 321 }} 322 />{/if} 323 <Button 324 label="Not now" 325 onclick={() => { 326 stopInboxSetupView(owned); 327 }} 328 /> 329 </section>