@@ -20,24 +20,104 @@ import {onMount} from 'svelte';
2020onMount (async () => {
2121 console .log (" onMount() starting dweb server..." );
2222 await startServer ();
23+ await refreshWallet ();
2324});
2425
2526let dwebHost = " http://127.0.0.1" ;
2627let dwebPort = 5537 ;
2728let dwebServer = dwebHost + " :" + dwebPort;
2829let dwebWebsite = " awesome" ;
2930
31+ // Wallet info state
32+ let walletAddress = " " ;
33+ let antBalance = " " ;
34+ let ethBalance = " " ;
35+ let walletLoading = false ;
36+ let walletError = " " ;
37+
38+ // Open by address state
39+ let openAddress = " " ;
40+
41+ function formatAmount (amount ) {
42+ if (! amount) return " " ;
43+ const s = String (amount);
44+ const parts = s .split (" ." );
45+ if (parts .length === 1 ) return parts[0 ];
46+ const intPart = parts[0 ];
47+ const fracPart = parts[1 ] || " " ;
48+ const shown = fracPart .slice (0 , 5 );
49+ const needsDots = fracPart .length > 5 ;
50+ return ` ${ intPart} .${ shown}${ needsDots ? " …" : " " } ` ;
51+ }
52+
3053async function startServer () {
31- await invoke (" start_server" , {port: dwebPort});
54+ try {
55+ await invoke (" start_server" , {port: dwebPort});
56+ } catch (e) {
57+ console .error (" Failed to start server" , e);
58+ }
3259}
3360
3461async function browseAutonomi () {
3562 console .log (" browseAutonomi()" );
3663 invoke (" dweb_open" , {addressNameOrLink: dwebWebsite});
3764}
65+
66+ async function openByAddress () {
67+ const addr = (openAddress || " " ).trim ();
68+ if (! addr) return ;
69+ invoke (" dweb_open" , {addressNameOrLink: addr});
70+ }
71+
72+ /** @param {KeyboardEvent} ev */
73+ function handleEnter (ev ) {
74+ if (ev .key === ' Enter' ) {
75+ openByAddress ();
76+ }
77+ }
78+
79+ async function refreshWallet () {
80+ walletLoading = true ;
81+ walletError = " " ;
82+ try {
83+ // Retry a few times in case the server isn't ready yet
84+ const url = ` ${ dwebServer} /dweb-0/wallet-balance` ;
85+ for (let i = 0 ; i < 5 ; i++ ) {
86+ try {
87+ const res = await fetch (url);
88+ if (res .ok ) {
89+ const data = await res .json ();
90+ walletAddress = data .wallet_address || " " ;
91+ antBalance = data .ant_balance || " " ;
92+ ethBalance = data .eth_balance || " " ;
93+ walletLoading = false ;
94+ return ;
95+ }
96+ } catch (_) {}
97+ await new Promise (r => setTimeout (r, 500 ));
98+ }
99+ walletError = " Unable to fetch wallet info" ;
100+ } finally {
101+ walletLoading = false ;
102+ }
103+ }
38104 </script >
39105
40106<main class =" container" >
107+ <div class =" topbar" >
108+ <div class =" wallet center" title =" Current wallet balances" >
109+ {#if walletLoading }
110+ <span >Loading wallet…</span >
111+ {:else if walletError }
112+ <span >{walletError }</span >
113+ {:else }
114+ <span class ="wallet-item" ><strong >Wallet</strong >: {walletAddress }</span >
115+ <span class ="wallet-item" title ={antBalance }><strong >ANT</strong >: {formatAmount (antBalance )}</span >
116+ <span class ="wallet-item" title ={ethBalance }><strong >ETH</strong >: {formatAmount (ethBalance )}</span >
117+ {/if }
118+ </div >
119+ </div >
120+
41121 <h1 >Autonomi dweb <a href =" https://codeberg.org/happybeing/dweb#dweb" target =" _blank" >
42122 <img src =" /dweb-logo.svg" class =" logo dweb" alt =" dweb Logo" />
43123 </a >
@@ -48,6 +128,17 @@ async function browseAutonomi() {
48128
49129 <p ><button onclick ={browseAutonomi }>Browse Autonomi dweb</button ></p >
50130 <p >Explore the secure peer-to-peer web on the Autonomi network</p >
131+
132+ <div class =" open-input" >
133+ <input
134+ type =" text"
135+ bind:value ={openAddress }
136+ placeholder =" Open dweb app via address or name"
137+ onkeydown ={handleEnter }
138+ aria-label =" Open dweb app via address or name"
139+ />
140+ <button onclick ={openByAddress }>Open</button >
141+ </div >
51142</main >
52143
53144<style >
@@ -76,6 +167,28 @@ async function browseAutonomi() {
76167 text-align : center ;
77168}
78169
170+ .topbar {
171+ position : fixed ;
172+ top : 0.75rem ;
173+ left : 0 ;
174+ right : 0 ;
175+ }
176+
177+ .wallet {
178+ display : flex ;
179+ gap : 0.75rem ;
180+ font-size : 0.85rem ;
181+ align-items : center ;
182+ }
183+
184+ .wallet.center {
185+ justify-content : center ;
186+ }
187+
188+ .wallet-item {
189+ white-space : nowrap ;
190+ }
191+
79192.logo {
80193 height : 40px ;
81194 padding-left : .5em ;
@@ -92,6 +205,21 @@ async function browseAutonomi() {
92205 justify-content : center ;
93206}
94207
208+ .open-input {
209+ margin-top : 1rem ;
210+ display : inline-flex ;
211+ gap : 0.5rem ;
212+ align-items : center ;
213+ justify-content : center ;
214+ }
215+
216+ .open-input input [type = " text" ] {
217+ padding : 0.55em 0.8em ;
218+ border : 1px solid #ccc ;
219+ border-radius : 8px ;
220+ min-width : 22rem ;
221+ }
222+
95223a {
96224 font-weight : 500 ;
97225 color : #646cff ;
0 commit comments