walletDomains.ts
1 import { StateCreator } from 'zustand'; 2 3 import { RootStore } from './root'; 4 import { domainFetchers } from './utils/domain-fetchers'; 5 6 export enum DomainType { 7 DEFAULT, 8 ENS, 9 } 10 11 export type WalletDomain = { 12 name?: string; 13 avatar?: string; 14 type: DomainType; 15 }; 16 17 export type WalletDomainsSlice = { 18 userDomains: WalletDomain[]; 19 defaultDomain: WalletDomain | null; 20 fetchConnectedWalletDomains: () => Promise<void>; 21 clearWalletDomains: () => void; 22 domainsLoading: boolean; 23 }; 24 25 export const createWalletDomainsSlice: StateCreator< 26 RootStore, 27 [['zustand/subscribeWithSelector', never], ['zustand/devtools', never]], 28 [], 29 WalletDomainsSlice 30 > = (set, get) => ({ 31 defaultDomain: null, 32 domainsLoading: false, 33 userDomains: [], 34 fetchConnectedWalletDomains: async () => { 35 set({ domainsLoading: true }); 36 const address = get().account; 37 const result = await Promise.all(domainFetchers.map((fetcher) => fetcher(address))); 38 const notNullDomains = result.filter((elem): elem is WalletDomain => elem !== null); 39 set({ 40 userDomains: notNullDomains, 41 defaultDomain: notNullDomains[0] ?? null, 42 domainsLoading: false, 43 }); 44 }, 45 clearWalletDomains: () => { 46 set({ userDomains: [], defaultDomain: null }); 47 }, 48 });