11import { computed , ref , markRaw , type Ref } from 'vue' ;
2- // import { sha256, stringify } from '@dm3-org/dm3-lib-shared';
32import { Dm3 , Dm3Sdk , type Dm3SdkConfig } from '@dm3-org/dm3-js-sdk' ;
4- import { JsonRpcProvider } from '@ethersproject/providers' ;
53import { ethers } from 'ethers' ;
6- // import type { Dm3 } from '@dm3-org/dm3-js-sdk/lib/esm/Dm3';
7- // import { DM3 } from '@dm3-org/dm3-messenger-widget';
84
95const sepoliaProvider = new ethers . providers . JsonRpcProvider ( "https://eth-sepolia.g.alchemy.com/v2/cBTHRhVcZ3Vt4BOFpA_Hi5DcTB1KQQV1" , {
106 name : 'sepolia' ,
@@ -20,23 +16,11 @@ const configLukso: Dm3SdkConfig = {
2016 userEnsSubdomain : ".testing-user.dm3.eth" ,
2117 resolverBackendUrl : "https://testing.dm3.network/resolver-handler" ,
2218 backendUrl : "https://testing.dm3.network/api" ,
23- // storageApi: {
24- // getConversations: () => Promise.resolve([]),
25- // getMessages: () => Promise.resolve([]),
26- // getHaltedMessages: () => Promise.resolve([]),
27- // clearHaltedMessages: () => Promise.resolve(),
28- // addMessageBatch: () => Promise.resolve(''),
29- // addConversation: () => Promise.resolve(),
30- // getNumberOfMessages: () => Promise.resolve(0),
31- // getNumberOfConverations: () => Promise.resolve(0),
32- // editMessageBatch: () => Promise.resolve(),
33- // addMessage: () => Promise.resolve(''),
34- // toggleHideConversation: () => Promise.resolve(),
35- // },
3619} ;
3720
3821const sdk = new Dm3Sdk ( configLukso ) ;
3922
23+ // TODO: check for installed extension
4024// https://docs.lukso.tech/install-up-browser-extension/
4125
4226type UseDm3ChatReturnType = {
@@ -47,42 +31,42 @@ type UseDm3ChatReturnType = {
4731 isReady : Ref < boolean > ;
4832} ;
4933
34+ const requestProvider = ( ) : Promise < ethers . providers . ExternalProvider > => {
35+ return new Promise ( ( resolve ) => {
36+ window . addEventListener ( "eip6963:announceProvider" , ( event ) => {
37+ const provider = ( event as any ) . detail . provider ;
38+ console . log ( 'Provider:' , provider ) ;
39+ resolve ( provider ) ;
40+ } ) ;
41+
42+ // Request installed providers
43+ window . dispatchEvent ( new Event ( "eip6963:requestProvider" ) ) ;
44+ } ) ;
45+ } ;
46+
5047export function useDm3Chat ( ) : UseDm3ChatReturnType {
5148 const dm3Instance = ref < Dm3 | null > ( null ) ;
5249 const isReady = ref ( false ) ;
5350 const init = async ( ) => {
54- console . log ( 'dm3Instance' , dm3Instance . value ) ;
55-
56- // Listen for provider announcements
57- window . addEventListener ( "eip6963:announceProvider" , async ( event ) => {
58- const dm3 = await sdk . universalProfileLogin ( ( event as any ) . detail . provider ) ;
59- // mark as raw to avoid reactivity issues with vue
60- // see: https://github.com/vuejs/core/issues/3024
61- dm3Instance . value = markRaw ( dm3 ) ;
62- console . log ( 'dm3Instance' , dm3Instance . value ) ;
63- isReady . value = true ;
64- } ) ;
65-
66- // Request installed providers
67- window . dispatchEvent ( new Event ( "eip6963:requestProvider" ) ) ;
51+ const dm3 = await sdk . universalProfileLoginWithCache ( requestProvider ) ;
52+ dm3Instance . value = markRaw ( dm3 ) ;
53+ isReady . value = true ;
6854 } ;
69- // init();
7055
7156 const rooms = computed ( ( ) => {
7257 console . log ( 'dm3Instance.value?.conversations list' , dm3Instance . value ?. conversations . list ) ;
73- return dm3Instance . value ?. conversations ?. list ;
58+ return dm3Instance . value ?. conversations ?. list
7459 } ) ;
7560 const messages = computed ( ( ) => rooms . value ?. at ( 0 ) ) ;
7661
7762 const startTestConversation = async ( ) => {
78- console . log ( 'dm3Instance' , dm3Instance . value ) ;
79- console . log ( 'dm3Instance.value?.conversations' , dm3Instance . value ?. conversations ) ;
63+ if ( ! dm3Instance ) {
64+ console . error ( 'dm3Instance is not initialized' ) ;
65+ return ;
66+ }
67+
8068 const conv = await dm3Instance . value ?. conversations ?. addConversation ( 'alice.eth' ) ;
81- console . log ( 'conv' , conv ) ;
82- console . log ( 'conv?.messages' , conv ?. messages . meta ) ;
83- console . log ( 'conv?.contact' , conv ?. contact ) ;
8469 await conv ?. messages . sendMessage ( 'Hello, world!' ) ;
85- console . log ( 'messages' , messages . value ) ;
8670 }
8771
8872 return { rooms, messages, isReady, init, startTestConversation } ;
0 commit comments