1
- import type {
2
- FrameHost ,
3
- FrameLocationContext ,
4
- FrameLocationContextLauncher ,
5
- } from "@farcaster/frame-sdk" ;
6
1
import type { ParseFramesV2ResultWithFrameworkDetails } from "frames.js/frame-parsers" ;
7
- import type { HostEndpoint } from "@farcaster/frame-host" ;
2
+ import type { FrameHost , HostEndpoint , Context } from "@farcaster/frame-host" ;
3
+ import { AddFrame } from "@farcaster/frame-host" ;
8
4
import { useMemo } from "react" ;
9
5
import { useFreshRef } from "./hooks/use-fresh-ref" ;
10
6
import type { FarcasterSignerState } from "./farcaster" ;
@@ -14,10 +10,12 @@ import type {
14
10
FrameClientConfig ,
15
11
HostEndpointEmitter ,
16
12
OnAddFrameRequestedFunction ,
13
+ OnEIP6963RequestProviderRequestedFunction ,
17
14
OnPrimaryButtonSetFunction ,
18
15
OnSendTransactionRequestFunction ,
19
16
OnSignMessageRequestFunction ,
20
17
OnSignTypedDataRequestFunction ,
18
+ OnViewProfileFunction ,
21
19
ResolveClientFunction ,
22
20
} from "./frame-app/types" ;
23
21
import { assertNever } from "./assert-never" ;
@@ -69,6 +67,25 @@ const defaultOnSignTypedDataRequest: OnSignTypedDataRequestFunction = () => {
69
67
return Promise . resolve ( true ) ;
70
68
} ;
71
69
70
+ const defaultViewProfile : OnViewProfileFunction = ( ) => {
71
+ // eslint-disable-next-line no-console -- provide feedback to the developer
72
+ console . warn (
73
+ "@frames.js/render/use-frame-app" ,
74
+ "onViewProfile not implemented"
75
+ ) ;
76
+
77
+ return Promise . reject ( new Error ( "onViewProfile not implemented" ) ) ;
78
+ } ;
79
+
80
+ const defaultEIP6963RequestProviderRequested : OnEIP6963RequestProviderRequestedFunction =
81
+ ( ) => {
82
+ // eslint-disable-next-line no-console -- provide feedback to the developer
83
+ console . warn (
84
+ "@frames.js/render/use-frame-app" ,
85
+ "onEIP6963RequestProviderRequested not implemented"
86
+ ) ;
87
+ } ;
88
+
72
89
export type UseFrameAppOptions = {
73
90
/**
74
91
* @example
@@ -100,7 +117,7 @@ export type UseFrameAppOptions = {
100
117
*
101
118
* @defaultValue launcher context
102
119
*/
103
- location ?: FrameLocationContext ;
120
+ location ?: Context . LocationContext ;
104
121
/**
105
122
* Either:
106
123
*
@@ -164,6 +181,16 @@ export type UseFrameAppOptions = {
164
181
* If the method has been called during the session more than once it immediatelly rejects
165
182
*/
166
183
onAddFrameRequested ?: OnAddFrameRequestedFunction ;
184
+ /**
185
+ * Called when app calls `viewProfile` method.
186
+ */
187
+ onViewProfile ?: OnViewProfileFunction ;
188
+ /**
189
+ * Called when app calls `eip6963RequestProvider` method.
190
+ *
191
+ * It will announce the provider to the frame app once this function returns the info
192
+ */
193
+ onEIP6963RequestProviderRequested ?: OnEIP6963RequestProviderRequestedFunction ;
167
194
/**
168
195
* Enabled debugging
169
196
*
@@ -201,7 +228,7 @@ export type UseFrameAppReturn =
201
228
status : "error" ;
202
229
} ;
203
230
204
- const defaultLocation : FrameLocationContextLauncher = {
231
+ const defaultLocation : Context . LauncherLocationContext = {
205
232
type : "launcher" ,
206
233
} ;
207
234
@@ -226,6 +253,8 @@ export function useFrameApp({
226
253
onSendTransactionRequest = defaultOnSendTransactionRequest ,
227
254
onSignMessageRequest = defaultOnSignMessageRequest ,
228
255
onSignTypedDataRequest = defaultOnSignTypedDataRequest ,
256
+ onViewProfile = defaultViewProfile ,
257
+ onEIP6963RequestProviderRequested = defaultEIP6963RequestProviderRequested ,
229
258
} : UseFrameAppOptions ) : UseFrameAppReturn {
230
259
const providerRef = useFreshRef ( provider ) ;
231
260
const debugRef = useFreshRef ( debug ) ;
@@ -234,6 +263,10 @@ export function useFrameApp({
234
263
const closeRef = useFreshRef ( onClose ) ;
235
264
const onOpenUrlRef = useFreshRef ( onOpenUrl ) ;
236
265
const onPrimaryButtonSetRef = useFreshRef ( onPrimaryButtonSet ) ;
266
+ const onViewProfileRef = useFreshRef ( onViewProfile ) ;
267
+ const onEIP6963RequestProviderRequestedRef = useFreshRef (
268
+ onEIP6963RequestProviderRequested
269
+ ) ;
237
270
const farcasterSignerRef = useFreshRef ( farcasterSigner ) ;
238
271
const onAddFrameRequestedRef = useFreshRef ( onAddFrameRequested ) ;
239
272
const addFrameRequestsCacheRef = useFreshRef ( addFrameRequestsCache ) ;
@@ -307,10 +340,7 @@ export function useFrameApp({
307
340
reason : "invalid_domain_manifest" ,
308
341
} ) ;
309
342
310
- return {
311
- added : false ,
312
- reason : "invalid_domain_manifest" ,
313
- } ;
343
+ throw new AddFrame . InvalidDomainManifest ( ) ;
314
344
}
315
345
316
346
if (
@@ -325,40 +355,34 @@ export function useFrameApp({
325
355
reason : "rejected_by_user" ,
326
356
} ) ;
327
357
328
- return {
329
- added : false ,
330
- reason : "rejected_by_user" ,
331
- } ;
358
+ throw new AddFrame . RejectedByUser ( ) ;
332
359
}
333
360
334
- const added = await onAddFrameRequestedRef . current ( frame ) ;
361
+ const result = await onAddFrameRequestedRef . current ( frame ) ;
335
362
336
- logDebug ( "onAddFrameRequested() called" , added ) ;
363
+ logDebug ( "onAddFrameRequested() called" , result ) ;
337
364
338
365
addFrameRequestsCacheRef . current . add (
339
366
frame . frame . button . action . url
340
367
) ;
341
368
342
- if ( ! added ) {
369
+ if ( ! result ) {
343
370
logDebug ( "Frame add request rejected by user" ) ;
344
371
345
372
endpoint . emit ( {
346
373
event : "frame_add_rejected" ,
347
374
reason : "rejected_by_user" ,
348
375
} ) ;
349
376
350
- return {
351
- added : false ,
352
- reason : "rejected_by_user" ,
353
- } ;
377
+ throw new AddFrame . RejectedByUser ( ) ;
354
378
}
355
379
356
380
endpoint . emit ( {
357
381
event : "frame_added" ,
358
- notificationDetails : added . notificationDetails ,
382
+ notificationDetails : result . notificationDetails ,
359
383
} ) ;
360
384
361
- return added ;
385
+ return result ;
362
386
} ,
363
387
close ( ) {
364
388
logDebug ( "sdk.close() called" ) ;
@@ -373,6 +397,9 @@ export function useFrameApp({
373
397
// @ts -expect-error -- type mismatch
374
398
return providerRef . current . request ( parameters ) ;
375
399
} ,
400
+ eip6963RequestProvider ( ) {
401
+ onEIP6963RequestProviderRequestedRef . current ( { endpoint } ) ;
402
+ } ,
376
403
openUrl ( url ) {
377
404
logDebug ( "sdk.openUrl() called" , url ) ;
378
405
@@ -398,6 +425,9 @@ export function useFrameApp({
398
425
// @todo implement
399
426
throw new Error ( "not implemented" ) ;
400
427
} ,
428
+ viewProfile ( options ) {
429
+ return onViewProfileRef . current ( options ) ;
430
+ } ,
401
431
} ) ,
402
432
status : "success" ,
403
433
frame : frameResolutionState . frame ,
@@ -431,5 +461,7 @@ export function useFrameApp({
431
461
onOpenUrlRef ,
432
462
readyRef ,
433
463
onPrimaryButtonSetRef ,
464
+ onViewProfileRef ,
465
+ onEIP6963RequestProviderRequestedRef ,
434
466
] ) ;
435
467
}
0 commit comments