1
- import { type ComlinkStatus , useWindowConnection } from '@sanity/sdk-react'
1
+ import { useWindowConnection } from '@sanity/sdk-react'
2
2
import { Box , Button , Card , Container , Label , Stack , Text , TextInput } from '@sanity/ui'
3
- import { type ReactElement , useEffect , useRef , useState } from 'react'
3
+ import { type ReactElement , Suspense , useEffect , useRef , useState } from 'react'
4
4
5
5
import { FromIFrameMessage , ToIFrameMessage , UserData } from './types'
6
6
7
- const Framed = ( ) : ReactElement => {
8
- const [ status , setStatus ] = useState < ComlinkStatus > ( 'idle' )
7
+ // Extracted connection-dependent content
8
+ function FramedContent ( ) {
9
9
const [ receivedMessages , setReceivedMessages ] = useState < string [ ] > ( [ ] )
10
10
const [ users , setUsers ] = useState < UserData [ ] > ( [ ] )
11
11
const [ error , setError ] = useState < string | null > ( null )
@@ -15,7 +15,6 @@ const Framed = (): ReactElement => {
15
15
const { sendMessage, fetch} = useWindowConnection < FromIFrameMessage , ToIFrameMessage > ( {
16
16
name : 'frame' ,
17
17
connectTo : 'main-app' ,
18
- onStatus : setStatus ,
19
18
onMessage : {
20
19
TO_IFRAME : ( data : { message : string } ) => {
21
20
setReceivedMessages ( ( prev ) => [ ...prev , data . message ] )
@@ -25,7 +24,7 @@ const Framed = (): ReactElement => {
25
24
26
25
// Fetch all users when connected
27
26
useEffect ( ( ) => {
28
- if ( ! fetch || status !== 'connected' ) return
27
+ if ( ! fetch ) return
29
28
30
29
async function fetchUsers ( signal : AbortSignal ) {
31
30
try {
@@ -45,7 +44,7 @@ const Framed = (): ReactElement => {
45
44
return ( ) => {
46
45
controller . abort ( )
47
46
}
48
- } , [ fetch , status ] )
47
+ } , [ fetch ] )
49
48
50
49
const sendMessageToParent = ( ) => {
51
50
const message = messageInputRef . current ?. value || ''
@@ -57,76 +56,76 @@ const Framed = (): ReactElement => {
57
56
}
58
57
}
59
58
60
- const isConnected = status === 'connected'
59
+ return (
60
+ < >
61
+ < Stack space = { 3 } >
62
+ < Label > Send message to parent</ Label >
63
+ < Box display = "flex" >
64
+ < Box flex = { 1 } >
65
+ < TextInput
66
+ ref = { messageInputRef }
67
+ onKeyDown = { ( e ) => e . key === 'Enter' && sendMessageToParent ( ) }
68
+ />
69
+ </ Box >
70
+ < Button text = "Send" tone = "primary" onClick = { sendMessageToParent } />
71
+ </ Box >
72
+ </ Stack >
73
+
74
+ { /* Users section */ }
75
+ < Card padding = { 3 } border radius = { 2 } >
76
+ < Stack space = { 3 } >
77
+ < Label > Users</ Label >
78
+ { users . length > 0 ? (
79
+ < Stack space = { 2 } >
80
+ { users . map ( ( user ) => (
81
+ < Card key = { user . id } padding = { 3 } tone = "positive" radius = { 2 } >
82
+ < Stack space = { 2 } >
83
+ < Text size = { 1 } weight = "semibold" >
84
+ { user . name }
85
+ </ Text >
86
+ < Text size = { 1 } > { user . email } </ Text >
87
+ </ Stack >
88
+ </ Card >
89
+ ) ) }
90
+ </ Stack >
91
+ ) : error ? (
92
+ < Card padding = { 3 } tone = "critical" radius = { 2 } >
93
+ < Text size = { 1 } > { error } </ Text >
94
+ </ Card >
95
+ ) : (
96
+ < Card padding = { 3 } tone = "default" radius = { 2 } >
97
+ < Text size = { 1 } > Loading users...</ Text >
98
+ </ Card >
99
+ ) }
100
+ </ Stack >
101
+ </ Card >
102
+
103
+ { /* Received messages */ }
104
+ < Box flex = { 1 } style = { { height : '500px' } } >
105
+ < Stack space = { 3 } >
106
+ < Text weight = "semibold" > Received Messages</ Text >
107
+ { receivedMessages . map ( ( msg , idx ) => (
108
+ < Card key = { idx } padding = { 3 } radius = { 2 } >
109
+ < Text > { msg } </ Text >
110
+ </ Card >
111
+ ) ) }
112
+ </ Stack >
113
+ </ Box >
114
+ </ >
115
+ )
116
+ }
61
117
118
+ const Framed = ( ) : ReactElement => {
62
119
return (
63
120
< Container height = "fill" >
64
121
< Card tone = "transparent" >
65
122
< Stack padding = { 4 } space = { 4 } >
66
123
< Text weight = "semibold" size = { 2 } >
67
124
Frame Content
68
125
</ Text >
69
-
70
- { /* Message input */ }
71
- < Stack space = { 3 } >
72
- < Label > Send message to parent</ Label >
73
- < Box display = "flex" >
74
- < Box flex = { 1 } >
75
- < TextInput
76
- ref = { messageInputRef }
77
- onKeyDown = { ( e ) => e . key === 'Enter' && sendMessageToParent ( ) }
78
- disabled = { ! isConnected }
79
- />
80
- </ Box >
81
- < Button
82
- text = "Send"
83
- tone = "primary"
84
- onClick = { sendMessageToParent }
85
- disabled = { ! isConnected }
86
- />
87
- </ Box >
88
- </ Stack >
89
-
90
- { /* Users section */ }
91
- < Card padding = { 3 } border radius = { 2 } >
92
- < Stack space = { 3 } >
93
- < Label > Users</ Label >
94
- { users . length > 0 ? (
95
- < Stack space = { 2 } >
96
- { users . map ( ( user ) => (
97
- < Card key = { user . id } padding = { 3 } tone = "positive" radius = { 2 } >
98
- < Stack space = { 2 } >
99
- < Text size = { 1 } weight = "semibold" >
100
- { user . name }
101
- </ Text >
102
- < Text size = { 1 } > { user . email } </ Text >
103
- </ Stack >
104
- </ Card >
105
- ) ) }
106
- </ Stack >
107
- ) : error ? (
108
- < Card padding = { 3 } tone = "critical" radius = { 2 } >
109
- < Text size = { 1 } > { error } </ Text >
110
- </ Card >
111
- ) : (
112
- < Card padding = { 3 } tone = "default" radius = { 2 } >
113
- < Text size = { 1 } > Loading users...</ Text >
114
- </ Card >
115
- ) }
116
- </ Stack >
117
- </ Card >
118
-
119
- { /* Received messages */ }
120
- < Box flex = { 1 } style = { { height : '500px' } } >
121
- < Stack space = { 3 } >
122
- < Text weight = "semibold" > Received Messages</ Text >
123
- { receivedMessages . map ( ( msg , idx ) => (
124
- < Card key = { idx } padding = { 3 } radius = { 2 } >
125
- < Text > { msg } </ Text >
126
- </ Card >
127
- ) ) }
128
- </ Stack >
129
- </ Box >
126
+ < Suspense fallback = { < div > Connecting to ParentApp...</ div > } >
127
+ < FramedContent />
128
+ </ Suspense >
130
129
</ Stack >
131
130
</ Card >
132
131
</ Container >
0 commit comments