1
1
import { computed , ref , markRaw , type Ref } from 'vue' ;
2
- // import { sha256, stringify } from '@dm3-org/dm3-lib-shared';
3
2
import { Dm3 , Dm3Sdk , type Dm3SdkConfig } from '@dm3-org/dm3-js-sdk' ;
4
- import { JsonRpcProvider } from '@ethersproject/providers' ;
5
3
import { 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';
8
4
9
5
const sepoliaProvider = new ethers . providers . JsonRpcProvider ( "https://eth-sepolia.g.alchemy.com/v2/cBTHRhVcZ3Vt4BOFpA_Hi5DcTB1KQQV1" , {
10
6
name : 'sepolia' ,
@@ -20,23 +16,11 @@ const configLukso: Dm3SdkConfig = {
20
16
userEnsSubdomain : ".testing-user.dm3.eth" ,
21
17
resolverBackendUrl : "https://testing.dm3.network/resolver-handler" ,
22
18
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
- // },
36
19
} ;
37
20
38
21
const sdk = new Dm3Sdk ( configLukso ) ;
39
22
23
+ // TODO: check for installed extension
40
24
// https://docs.lukso.tech/install-up-browser-extension/
41
25
42
26
type UseDm3ChatReturnType = {
@@ -47,42 +31,42 @@ type UseDm3ChatReturnType = {
47
31
isReady : Ref < boolean > ;
48
32
} ;
49
33
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
+
50
47
export function useDm3Chat ( ) : UseDm3ChatReturnType {
51
48
const dm3Instance = ref < Dm3 | null > ( null ) ;
52
49
const isReady = ref ( false ) ;
53
50
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 ;
68
54
} ;
69
- // init();
70
55
71
56
const rooms = computed ( ( ) => {
72
57
console . log ( 'dm3Instance.value?.conversations list' , dm3Instance . value ?. conversations . list ) ;
73
- return dm3Instance . value ?. conversations ?. list ;
58
+ return dm3Instance . value ?. conversations ?. list
74
59
} ) ;
75
60
const messages = computed ( ( ) => rooms . value ?. at ( 0 ) ) ;
76
61
77
62
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
+
80
68
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 ) ;
84
69
await conv ?. messages . sendMessage ( 'Hello, world!' ) ;
85
- console . log ( 'messages' , messages . value ) ;
86
70
}
87
71
88
72
return { rooms, messages, isReady, init, startTestConversation } ;
0 commit comments