11import { useEffect , useState } from "react" ;
2- import { connect } from "@permaweb/aoconnect"
2+ import { connect } from "@permaweb/aoconnect" ;
33
44async function getResults ( process : string , cursor = "" ) {
5- const ao = connect ( ) ;
5+ const ao = connect ( ) ;
66
7- const r = await ao . results ( {
8- process,
9- from : cursor ,
10- sort : "ASC" ,
11- limit : 999 ,
12- } )
7+ const r = await ao . results ( {
8+ process,
9+ from : cursor ,
10+ sort : "ASC" ,
11+ limit : 999 ,
12+ } ) ;
1313
14- if ( r . edges . length > 0 ) {
15- const newCursor = r . edges [ r . edges . length - 1 ] . cursor ;
16- const results = r . edges . map ( ( e ) => e . node ) ;
17- return { cursor : newCursor , results } ;
18- } else {
19- return { cursor, results : [ ] } ;
20- }
14+ if ( r . edges . length > 0 ) {
15+ const newCursor = r . edges [ r . edges . length - 1 ] . cursor ;
16+ const results = r . edges . map ( ( e ) => e . node ) ;
17+ return { cursor : newCursor , results } ;
18+ } else {
19+ return { cursor, results : [ ] } ;
20+ }
2121}
2222
23+ export default function CodeCell ( { cellId, appName, code = "print('Hello AO!')" , devMode = false , nowallet = false , width = "100%" , height = "300px" , className = "" , style = { } , onAOProcess = ( pid : string ) => { } , onNewMessage = ( msgs : any [ ] ) => { } , onInbox = ( inbox : any ) => { } } : { cellId : string ; appName : string ; code ?: string ; devMode ?: boolean ; nowallet ?: boolean ; width ?: string ; height ?: string ; className ?: string ; style ?: React . CSSProperties ; onAOProcess ?: ( pid : string ) => void ; onNewMessage ?: ( msgs : any [ ] ) => void ; onInbox ?: ( inbox : any ) => void } ) {
24+ // const [myAoId, setMyAoId] = useState<string>("")
25+ // const [intrvlId, setIntrvlId] = useState<number>(0)
26+ const url = new URL ( devMode ? "http://localhost:3000/codecell" : "https://ide.betteridea.dev/codecell" ) ;
2327
24- export default function CodeCell ( { cellId,
25- appName,
26- code = "print('Hello AO!')" ,
27- devMode = false ,
28- width = "100%" ,
29- height = "300px" ,
30- className = "" ,
31- style = { } ,
32- onAOProcess = ( pid : string ) => { } ,
33- onNewMessage = ( msgs : any [ ] ) => { } ,
34- onInbox = ( inbox : any ) => { }
35- } : {
36- cellId : string ;
37- appName : string ;
38- code ?: string ;
39- devMode ?: boolean ;
40- width ?: string ;
41- height ?: string ;
42- className ?: string ;
43- style ?: React . CSSProperties ;
44- onAOProcess ?: ( pid : string ) => void ;
45- onNewMessage ?: ( msgs : any [ ] ) => void ;
46- onInbox ?: ( inbox : any ) => void ;
47- } ) {
48- // const [myAoId, setMyAoId] = useState<string>("")
49- // const [intrvlId, setIntrvlId] = useState<number>(0)
50- const url = new URL ( devMode ? "http://localhost:3000/codecell" : "https://ide.betteridea.dev/codecell" ) ;
51-
52- url . searchParams . append ( "app-name" , appName ) ;
53- url . searchParams . append ( "code" , code ) ;
28+ url . searchParams . append ( "app-name" , appName ) ;
29+ url . searchParams . append ( "code" , code ) ;
30+ if ( nowallet ) url . searchParams . append ( "nowallet" , "true" ) ;
5431
55- useEffect ( ( ) => {
56- const callback = async ( e : any ) => {
57- if ( e . data . action == "set_process" ) {
58- if ( ! e . data . process ) return
59- onAOProcess ( e . data . process ) ;
60- // setMyAoId(e.data.process)
61- sessionStorage . setItem ( "cell-ao-id" , e . data . process )
62- } else if ( e . data . action == "inbox" ) {
63- if ( ! e . data . data ) return
64- onInbox ( e . data . data )
65- }
66- } ;
32+ useEffect ( ( ) => {
33+ const callback = async ( e : any ) => {
34+ if ( e . data . action == "set_process" && e . data . appname == appName ) {
35+ if ( ! e . data . process ) return ;
36+ onAOProcess ( e . data . process ) ;
37+ // setMyAoId(e.data.process)
38+ const ids = JSON . parse ( sessionStorage . getItem ( "cell-ids" ) || "{}" ) ;
39+ ids [ appName ] = e . data . process ;
40+ sessionStorage . setItem ( "cell-ids" , JSON . stringify ( ids ) ) ;
41+ } else if ( e . data . action == "inbox" ) {
42+ if ( ! e . data . data ) return ;
43+ onInbox ( e . data . data ) ;
44+ }
45+ } ;
6746
68- window . removeEventListener ( "message" , callback ) ;
69- window . addEventListener ( "message" , callback ) ;
70- return ( ) => window . removeEventListener ( "message" , callback ) ;
71- } , [ ] )
47+ window . removeEventListener ( "message" , callback ) ;
48+ window . addEventListener ( "message" , callback ) ;
49+ return ( ) => window . removeEventListener ( "message" , callback ) ;
50+ } , [ ] ) ;
7251
73- useEffect ( ( ) => {
74- if ( ! sessionStorage . getItem ( "cell-ao-id" ) ) return
75- // clearInterval(intrvlId)
76- clearInterval ( parseInt ( sessionStorage . getItem ( "interval" ) ! as string ) )
77- async function fetchNewInboxMsg ( ) {
78- const localCursor = sessionStorage . getItem ( "cursor" ) || ""
79- const r = await getResults ( sessionStorage . getItem ( "cell-ao-id" ) as string , localCursor )
80- if ( ! localCursor ) // if ran for first time, set cursor and return
81- return sessionStorage . setItem ( "cursor" , r . cursor )
52+ useEffect ( ( ) => {
53+ clearInterval ( parseInt ( sessionStorage . getItem ( "interval" ) ! as string ) ) ;
8254
83- if ( r . cursor != sessionStorage . getItem ( "cursor" ) ) {
84- console . log ( r . cursor )
85- sessionStorage . setItem ( "cursor" , r . cursor )
86- if ( r . results . length > 0 ) {
87- // console.log(r.results)
88- onNewMessage ( r . results )
89- }
90- }
55+ async function fetchNewInboxMsg ( ) {
56+ const ids = JSON . parse ( sessionStorage . getItem ( "cell-ids" ) || "{}" ) ;
57+ const cursors = JSON . parse ( sessionStorage . getItem ( "cursors" ) || "{}" ) ;
58+ Object . keys ( ids ) . forEach ( async ( name ) => {
59+ const localCursor = cursors [ name ] ;
9160
61+ const r = await getResults ( ids [ name ] , localCursor || "" ) ;
62+ if ( ! localCursor ) {
63+ cursors [ name ] = r . cursor ;
64+ return sessionStorage . setItem ( "cursors" , JSON . stringify ( cursors ) ) ;
9265 }
9366
94- sessionStorage . setItem ( "interval" , setInterval ( fetchNewInboxMsg , 2500 ) . toString ( ) )
95-
96- return ( ) => clearInterval ( parseInt ( sessionStorage . getItem ( "interval" ) ! as string ) )
97- } , [ ] )
67+ if ( r . cursor != cursors [ name ] ) {
68+ cursors [ name ] = r . cursor ;
69+ sessionStorage . setItem ( "cursors" , JSON . stringify ( cursors ) ) ;
70+ if ( r . results . length > 0 ) {
71+ onNewMessage ( r . results ) ;
72+ }
73+ }
74+ } ) ;
75+ }
76+ sessionStorage . setItem ( "interval" , setInterval ( fetchNewInboxMsg , 3000 ) . toString ( ) ) ;
9877
78+ return ( ) => clearInterval ( parseInt ( sessionStorage . getItem ( "interval" ) ! as string ) ) ;
79+ } , [ appName ] ) ;
9980
100- return < iframe
101- id = { cellId }
102- src = { url . toString ( ) }
103- width = { width }
104- height = { height }
105- className = { className }
106- style = { { ...style , backgroundColor : "black !important" , borderRadius : "7px" , border : "1px solid #222" } }
107- // referrerPolicy="no-referrer"
81+ return (
82+ < iframe
83+ id = { cellId }
84+ src = { url . toString ( ) }
85+ width = { width }
86+ height = { height }
87+ className = { className }
88+ style = { { ...style , backgroundColor : "black !important" , borderRadius : "7px" , border : "1px solid #222" } }
89+ // referrerPolicy="no-referrer"
10890 />
109- }
91+ ) ;
92+ }
0 commit comments