1- import { PrototypeVersion , PrototypeVersionStatus } from '@idea2app/data-server' ;
1+ import { PrototypeType , PrototypeVersion } from '@idea2app/data-server' ;
22import { Box , Button , CircularProgress , Link , Typography } from '@mui/material' ;
33import { observable } from 'mobx' ;
44import { observer } from 'mobx-react' ;
55import { ObservedComponent } from 'mobx-react-helper' ;
6- import { sleep } from 'web-utility' ;
6+ import { createRef } from 'react' ;
7+ import { inViewport , sleep } from 'web-utility' ;
78
89import { PrototypeVersionModel } from '../../models/PrototypeVersion' ;
910import { i18n , I18nContext } from '../../models/Translation' ;
1011
1112export interface PrototypeGeneratorToolbarProps {
1213 projectId : number ;
1314 messageId : number ;
15+ type : PrototypeType ;
16+ prototype ?: PrototypeVersion ;
1417}
1518
1619@observer
@@ -20,86 +23,55 @@ export class PrototypeGeneratorToolbar extends ObservedComponent<
2023> {
2124 static contextType = I18nContext ;
2225
23- versionStore = new PrototypeVersionModel ( this . props . projectId ) ;
26+ versionStore = new PrototypeVersionModel ( this . props . projectId , this . props . type ) ;
2427
2528 @observable
26- accessor version : PrototypeVersion | undefined ;
29+ accessor version = this . props . prototype ;
2730
28- @observable
29- accessor isPolling = false ;
31+ private root = createRef < HTMLElement > ( ) ;
3032
3133 componentDidMount ( ) {
32- void this . loadVersion ( ) ;
33- }
34-
35- componentDidUpdate ( prevProps : PrototypeGeneratorToolbarProps ) {
36- if ( prevProps . messageId !== this . props . messageId ) {
37- void this . loadVersion ( ) ;
38- }
39-
40- if ( this . version ?. status === 'processing' ) {
41- this . startPolling ( ) ;
42- }
43- }
44-
45- componentWillUnmount ( ) {
46- this . isPolling = false ;
47- }
34+ super . componentDidMount ( ) ;
4835
49- async loadVersion ( ) {
50- const existingVersion = await this . versionStore . getVersionByMessageId ( this . props . messageId ) ;
51- this . version = existingVersion ;
36+ this . pollStatusCheck ( ) ;
5237 }
5338
54- async startPolling ( ) {
55- if ( this . isPolling ) return ;
39+ async pollStatusCheck ( ) {
40+ const { props, version } = this ,
41+ rootElement = this . root . current ;
5642
57- this . isPolling = true ;
43+ while ( version ?. status === 'pending' || version ?. status === 'processing' ) {
44+ if ( ! rootElement ?. isConnected ) break ;
5845
59- for ( let i = 0 ; i < 100 ; i ++ ) {
60- if ( ! this . isPolling ) break ;
46+ if ( inViewport ( rootElement ) )
47+ this . version = await this . versionStore . getOne ( props . prototype ! . id ) ;
6148
6249 await sleep ( 3 ) ;
63-
64- const updatedVersion = await this . versionStore . getVersionByMessageId ( this . props . messageId ) ;
65- this . version = updatedVersion ;
66-
67- if ( updatedVersion ?. status === 'completed' || updatedVersion ?. status === 'failed' ) {
68- this . isPolling = false ;
69- break ;
70- }
7150 }
72-
73- this . isPolling = false ;
7451 }
7552
7653 handleGenerateClick = async ( ) => {
77- try {
78- const newVersion = await this . versionStore . updateOne ( {
79- evaluationMessage : this . props . messageId ,
80- } ) ;
81- if ( newVersion ) {
82- this . version = newVersion ;
83- }
84- } catch ( error ) {
85- console . error ( 'Failed to create prototype version:' , error ) ;
86- }
54+ this . version = await this . versionStore . updateOne ( {
55+ evaluationMessage : this . props . messageId ,
56+ } ) ;
57+
58+ return this . pollStatusCheck ( ) ;
8759 } ;
8860
8961 renderPending ( ) {
9062 const { t } = this . observedContext ;
91- const { versionStore } = this ;
63+ const loading = this . versionStore . uploading > 0 ;
9264
9365 return (
9466 < Button
9567 variant = "contained"
9668 color = "primary"
9769 size = "small"
98- disabled = { versionStore . uploading > 0 }
70+ disabled = { loading }
9971 sx = { { textTransform : 'none' } }
10072 onClick = { this . handleGenerateClick }
10173 >
102- { versionStore . uploading > 0 ? t ( 'generating' ) : t ( 'generate_prototype' ) }
74+ { loading ? t ( 'generating' ) : t ( 'generate_prototype' ) }
10375 </ Button >
10476 ) ;
10577 }
@@ -117,13 +89,13 @@ export class PrototypeGeneratorToolbar extends ObservedComponent<
11789
11890 renderCompleted ( ) {
11991 const { t } = this . observedContext ;
120- const { version } = this ;
92+ const { previewLink , gitLogsLink } = this . version || { } ;
12193
12294 return (
12395 < Box sx = { { display : 'flex' , gap : 1 , flexWrap : 'wrap' } } >
124- { version ! . previewLink && (
96+ { previewLink && (
12597 < Link
126- href = { version ! . previewLink }
98+ href = { previewLink }
12799 target = "_blank"
128100 rel = "noopener noreferrer"
129101 sx = { {
@@ -136,9 +108,9 @@ export class PrototypeGeneratorToolbar extends ObservedComponent<
136108 { t ( 'view_preview' ) }
137109 </ Link >
138110 ) }
139- { version ! . gitLogsLink && (
111+ { gitLogsLink && (
140112 < Link
141- href = { version ! . gitLogsLink }
113+ href = { gitLogsLink }
142114 target = "_blank"
143115 rel = "noopener noreferrer"
144116 sx = { {
@@ -157,16 +129,16 @@ export class PrototypeGeneratorToolbar extends ObservedComponent<
157129
158130 renderFailed ( ) {
159131 const { t } = this . observedContext ;
160- const { version } = this ;
132+ const { errorMessage , gitLogsLink } = this . version || { } ;
161133
162134 return (
163135 < Box sx = { { display : 'flex' , flexDirection : 'column' , gap : 0.5 } } >
164136 < Typography variant = "body2" color = "error" sx = { { fontSize : '0.875rem' } } >
165- { version ! . errorMessage || t ( 'prototype_generation_failed' ) }
137+ { errorMessage || t ( 'prototype_generation_failed' ) }
166138 </ Typography >
167- { version ! . gitLogsLink && (
139+ { gitLogsLink && (
168140 < Link
169- href = { version ! . gitLogsLink }
141+ href = { gitLogsLink }
170142 target = "_blank"
171143 rel = "noopener noreferrer"
172144 sx = { {
@@ -187,14 +159,7 @@ export class PrototypeGeneratorToolbar extends ObservedComponent<
187159 const { version } = this ;
188160
189161 return (
190- < Box
191- sx = { {
192- mt : 1.5 ,
193- pt : 1.5 ,
194- borderTop : '1px solid' ,
195- borderColor : 'divider' ,
196- } }
197- >
162+ < Box ref = { this . root } sx = { { borderTop : '1px solid' , borderColor : 'divider' } } >
198163 { ! version || version . status === 'pending'
199164 ? this . renderPending ( )
200165 : version . status === 'processing'
0 commit comments