CapabilityGate.svelte (4812B)
1 <script lang="ts"> 2 import { onMount, tick } from 'svelte'; 3 import type { IdentityViewActionResult } from '../runtime/view-context.ts'; 4 import type { IdentitySnapshot } from '../runtime/identity-session.ts'; 5 import Button from './primitives/Button.svelte'; 6 import { internalHref } from '../navigation-url.ts'; 7 let { 8 identity, 9 required = 'connection', 10 compact = false, 11 primary = false, 12 focusScope = '', 13 focusTarget = 'inline', 14 ondisconnect, 15 onconnect, 16 onprobe 17 }: { 18 identity: IdentitySnapshot; 19 required?: 'connection' | 'messaging'; 20 compact?: boolean; 21 primary?: boolean; 22 focusScope?: string; 23 focusTarget?: 'navbar' | 'protected' | 'inline'; 24 ondisconnect?: () => void; 25 onconnect?: () => Promise<IdentityViewActionResult> | void; 26 onprobe?: () => Promise<IdentityViewActionResult> | void; 27 } = $props(); 28 let alive = false; 29 onMount(() => { 30 alive = true; 31 return () => { 32 alive = false; 33 }; 34 }); 35 const connected = $derived( 36 identity.state !== 'guest' && identity.state !== 'pending' 37 ); 38 const text = $derived( 39 identity.admission === 'busy' 40 ? 'An extension request is still pending. Wait for it to finish before trying again.' 41 : identity.state === 'pending' 42 ? 'Waiting for extension approval.' 43 : identity.state === 'guest' 44 ? identity.reason === 'refused' 45 ? 'Connection was not approved.' 46 : identity.reason === 'missing' || identity.reason === 'unavailable' 47 ? 'A compatible Nostr extension is not available.' 48 : identity.reason === 'changed_key' 49 ? 'The extension identity changed. Connect again to continue.' 50 : identity.reason === 'invalid_key' 51 ? 'The extension did not return a supported identity.' 52 : 'Use your Nostr extension to choose your identity.' 53 : required === 'connection' 54 ? 'Connected.' 55 : identity.messaging === 'capable' 56 ? 'Messaging support checked.' 57 : identity.messaging === 'unsupported' 58 ? 'This extension does not support the required message encryption.' 59 : identity.messaging === 'refused' 60 ? 'The messaging support check was not approved.' 61 : 'Check support for message encryption before continuing.' 62 ); 63 async function run( 64 command: (() => Promise<IdentityViewActionResult> | void) | undefined, 65 messaging: boolean 66 ) { 67 const originalScope = focusScope; 68 let result: IdentityViewActionResult | void; 69 try { 70 result = await command?.(); 71 } catch { 72 return; 73 } 74 if ( 75 !result || 76 !result.current() || 77 result.identity.admission === 'busy' || 78 result.identity.state === 'guest' || 79 result.identity.state === 'pending' || 80 (messaging && result.identity.messaging !== 'capable') 81 ) 82 return; 83 await tick(); 84 if (!alive || focusScope !== originalScope || !result.current()) return; 85 if (focusTarget === 'navbar') 86 document.getElementById('identity-navbar-status')?.focus(); 87 else if (focusTarget === 'protected') 88 document.getElementById('identity-protected-status')?.focus(); 89 else document.getElementById('identity-inline-status')?.focus(); 90 } 91 function connect() { 92 void run(onconnect, false); 93 } 94 function probe() { 95 void run(onprobe, true); 96 } 97 </script> 98 99 {#if !compact}<h2> 100 {connected ? 'Connection capabilities' : 'Connect to continue'} 101 </h2>{/if} 102 <p 103 id={focusTarget === 'navbar' 104 ? 'identity-navbar-status' 105 : focusTarget === 'protected' 106 ? 'identity-protected-status' 107 : 'identity-inline-status'} 108 role="status" 109 tabindex="-1" 110 > 111 {text} 112 </p> 113 {#if !connected} 114 {#if !compact}<p>Your private key stays in the extension.</p>{/if} 115 <Button 116 label="Connect extension" 117 variant={primary ? 'primary' : 'secondary'} 118 disabled={identity.state === 'pending' || !onconnect} 119 onclick={connect} 120 /> 121 {#if identity.state === 'pending' && ondisconnect}<Button 122 label="Disconnect" 123 onclick={ondisconnect} 124 />{/if} 125 {#if !compact}<div class="cluster"> 126 <a href={internalHref('/search')}>Keep browsing</a><a 127 href={internalHref('/about#help')}>Connection help</a 128 > 129 </div>{/if} 130 {:else if identity.state !== 'guest' && identity.state !== 'pending' && required === 'messaging' && identity.messaging !== 'capable'} 131 <p> 132 This check asks your extension to encrypt and decrypt a disposable 133 self-copy. It does not unlock messages or change public inbox preferences. 134 </p> 135 <Button 136 label="Check messaging support" 137 variant={primary ? 'primary' : 'secondary'} 138 disabled={!onprobe} 139 onclick={probe} 140 /> 141 {/if}