| 
1 | 1 | <svelte:options customElement="create-invoice-form" />  | 
2 | 2 | 
 
  | 
3 | 3 | <script lang="ts">  | 
4 |  | -  import { getAccount } from "@wagmi/core";  | 
 | 4 | +  import { getAccount, watchAccount, WatchAccountReturnType } from "@wagmi/core";  | 
5 | 5 |   import { Config as WagmiConfig } from "wagmi";  | 
6 | 6 |   // Types  | 
7 | 7 |   import { type GetAccountReturnType } from "@wagmi/core";  | 
 | 
24 | 24 |   import Status from "@requestnetwork/shared-components/status.svelte";  | 
25 | 25 |   import Modal from "@requestnetwork/shared-components/modal.svelte";  | 
26 | 26 |   import { EncryptionTypes, CipherProviderTypes } from "@requestnetwork/types";  | 
 | 27 | +    import { onDestroy, onMount, tick } from "svelte";  | 
27 | 28 | 
  | 
28 | 29 |   interface CipherProvider extends CipherProviderTypes.ICipherProvider {  | 
29 | 30 |     disconnectWallet: () => void;  | 
 | 
35 | 36 |   export let currencies: CurrencyTypes.CurrencyInput[] = [];  | 
36 | 37 |   let cipherProvider: CipherProvider | undefined;  | 
37 | 38 | 
  | 
38 |  | -  let account: GetAccountReturnType;  | 
 | 39 | +  let account: GetAccountReturnType | undefined = wagmiConfig && getAccount(wagmiConfig);  | 
39 | 40 |   let isTimeout = false;  | 
40 | 41 |   let activeConfig = config ? config : defaultConfig;  | 
41 | 42 |   let mainColor = activeConfig.colors.main;  | 
 | 
121 | 122 | 
  | 
122 | 123 |   $: cipherProvider = requestNetwork?.getCipherProvider() as CipherProvider;  | 
123 | 124 | 
  | 
124 |  | -  $: {  | 
125 |  | -    if (wagmiConfig) {  | 
126 |  | -      account = getAccount(wagmiConfig);  | 
127 |  | -      cipherProvider?.disconnectWallet();  | 
 | 125 | +  const handleWalletConnection = async () => {  | 
 | 126 | +    account = getAccount(wagmiConfig);  | 
 | 127 | +  };  | 
 | 128 | +
  | 
 | 129 | +  const handleWalletDisconnection = () => {  | 
 | 130 | +    cipherProvider?.disconnectWallet();  | 
 | 131 | +  };  | 
 | 132 | +
  | 
 | 133 | +  const handleWalletChange = (data: any) => {  | 
 | 134 | +    if (data?.address) {  | 
 | 135 | +      handleWalletConnection();  | 
 | 136 | +    } else {  | 
 | 137 | +      handleWalletDisconnection();  | 
128 | 138 |     }  | 
129 |  | -  }  | 
 | 139 | +  };  | 
 | 140 | +
  | 
 | 141 | +  onMount(() => {  | 
 | 142 | +    unwatchAccount = watchAccount(wagmiConfig, {  | 
 | 143 | +      onChange(data) {  | 
 | 144 | +        tick().then(() => {  | 
 | 145 | +          console.log("Wallet changed");  | 
 | 146 | +          handleWalletChange(data);  | 
 | 147 | +        });  | 
 | 148 | +      },  | 
 | 149 | +    });  | 
 | 150 | +  });  | 
 | 151 | +
  | 
 | 152 | +  let unwatchAccount: WatchAccountReturnType | undefined;  | 
 | 153 | +
  | 
 | 154 | +  onDestroy(() => {  | 
 | 155 | +    if (typeof unwatchAccount === "function") unwatchAccount();  | 
 | 156 | +  });  | 
130 | 157 | 
  | 
131 | 158 |   $: {  | 
132 | 159 |     formData.creatorId = (account?.address ?? "") as string;  | 
 | 
0 commit comments