7
7
useState ,
8
8
} from "react" ;
9
9
import type { FrameState } from "../types" ;
10
- import type { UseFrameReturnValue } from "../unstable-types" ;
10
+ import type { PartialFrameV2 , UseFrameReturnValue } from "../unstable-types" ;
11
11
import { useFreshRef } from "../hooks/use-fresh-ref" ;
12
12
import type {
13
13
FrameMessage ,
@@ -17,7 +17,6 @@ import type {
17
17
RootContainerDimensions ,
18
18
RootContainerElement ,
19
19
FrameButtonProps ,
20
- PartialFrameV2 ,
21
20
} from "./types" ;
22
21
import {
23
22
getErrorMessageFromFramesStackItem ,
@@ -30,7 +29,7 @@ export type FrameUIComponents<TStylingProps extends Record<string, unknown>> =
30
29
export type FrameUITheme < TStylingProps extends Record < string , unknown > > =
31
30
Partial < FrameUIComponentStylingProps < TStylingProps > > ;
32
31
33
- export type AppLaunchButtonPressEvent =
32
+ export type FrameUILaunchFrameButtonPressEvent =
34
33
| {
35
34
status : "complete" ;
36
35
frame : FrameV2 ;
@@ -42,6 +41,10 @@ export type AppLaunchButtonPressEvent =
42
41
frameUIState : FrameUIState ;
43
42
} ;
44
43
44
+ export type FrameUILaunchFrameButtonPressHandler = (
45
+ event : FrameUILaunchFrameButtonPressEvent
46
+ ) => void ;
47
+
45
48
export type BaseFrameUIProps < TStylingProps extends Record < string , unknown > > = {
46
49
frameState :
47
50
| FrameState < any , any >
@@ -81,13 +84,13 @@ export type BaseFrameUIProps<TStylingProps extends Record<string, unknown>> = {
81
84
*
82
85
* Only Frames v2 support this feature.
83
86
*/
84
- onAppLaunchButtonPress ?: ( event : AppLaunchButtonPressEvent ) => void ;
87
+ onLaunchFrameButtonPress ?: FrameUILaunchFrameButtonPressHandler ;
85
88
/**
86
89
* Called when an error occurs in onAppLaunchButtonPress
87
90
*
88
91
* @defaultValue console.error()
89
92
*/
90
- onAppLaunchButtonPressError ?: ( error : Error ) => void ;
93
+ onLaunchFrameButtonPressError ?: ( error : Error ) => void ;
91
94
} ;
92
95
93
96
// eslint-disable-next-line @typescript-eslint/no-empty-function -- this is noop
@@ -113,19 +116,20 @@ export function BaseFrameUI<TStylingProps extends Record<string, unknown>>({
113
116
allowPartialFrame = false ,
114
117
enableImageDebugging = false ,
115
118
onError = defaultErrorLogger ,
116
- onAppLaunchButtonPressError = defaultErrorLogger ,
119
+ onLaunchFrameButtonPressError = defaultErrorLogger ,
117
120
onMessage = defaultMessageHandler ,
118
121
createElement = reactCreateElement ,
119
- onAppLaunchButtonPress = defaultOnAppLaunchButtonPress ,
122
+ onLaunchFrameButtonPress = defaultOnAppLaunchButtonPress ,
120
123
} : BaseFrameUIProps < TStylingProps > ) : JSX . Element | null {
121
124
const [ isImageLoading , setIsImageLoading ] = useState ( true ) ;
122
125
const { currentFrameStackItem } = frameState ;
123
126
const rootRef = useRef < RootContainerElement > ( null ) ;
124
127
const rootDimensionsRef = useRef < RootContainerDimensions | undefined > ( ) ;
125
128
const onErrorRef = useFreshRef ( onError ) ;
126
- const onAppLaunchButtonPressErrorRef = useFreshRef (
127
- onAppLaunchButtonPressError
129
+ const onLaunchFrameButtonPressErrorRef = useFreshRef (
130
+ onLaunchFrameButtonPressError
128
131
) ;
132
+ const onLaunchFrameButtonPressRef = useFreshRef ( onLaunchFrameButtonPress ) ;
129
133
130
134
const onImageLoadEnd = useCallback ( ( ) => {
131
135
setIsImageLoading ( false ) ;
@@ -340,7 +344,27 @@ export function BaseFrameUI<TStylingProps extends Record<string, unknown>>({
340
344
onPress ( ) {
341
345
// we don't need to track dimensions here because this button does nothing to frame stack
342
346
try {
343
- onAppLaunchButtonPress (
347
+ if ( ! ( "onLaunchFrameButtonPress" in frameState ) ) {
348
+ throw new Error (
349
+ "onLaunchFrameButtonPress is not implemented, you are porbably using old useFrame hook"
350
+ ) ;
351
+ }
352
+
353
+ // call onLaunchFrameButtonPress on useFrame() hook
354
+ // because that's where the core of the frame v2 message handling is implemented
355
+ frameState . onLaunchFrameButtonPress (
356
+ frameUiState . status === "complete"
357
+ ? {
358
+ status : "complete" ,
359
+ frame : frameUiState . frame ,
360
+ }
361
+ : {
362
+ status : "partial" ,
363
+ frame : frameUiState . frame ,
364
+ }
365
+ ) ;
366
+
367
+ onLaunchFrameButtonPressRef . current (
344
368
frameUiState . status === "complete"
345
369
? {
346
370
status : "complete" ,
@@ -354,7 +378,7 @@ export function BaseFrameUI<TStylingProps extends Record<string, unknown>>({
354
378
}
355
379
) ;
356
380
} catch ( e ) {
357
- onAppLaunchButtonPressErrorRef . current (
381
+ onLaunchFrameButtonPressErrorRef . current (
358
382
e instanceof Error ? e : new Error ( String ( e ) )
359
383
) ;
360
384
}
0 commit comments