|
| 1 | +import { PrototypeType, PrototypeVersion } from '@idea2app/data-server'; |
| 2 | +import { Box, Button, CircularProgress, Link, Typography } from '@mui/material'; |
| 3 | +import { observable } from 'mobx'; |
| 4 | +import { observer } from 'mobx-react'; |
| 5 | +import { ObservedComponent } from 'mobx-react-helper'; |
| 6 | +import { createRef } from 'react'; |
| 7 | +import { inViewport, sleep } from 'web-utility'; |
| 8 | + |
| 9 | +import { PrototypeVersionModel } from '../../models/PrototypeVersion'; |
| 10 | +import { i18n, I18nContext } from '../../models/Translation'; |
| 11 | + |
| 12 | +export interface PrototypeGeneratorProps { |
| 13 | + projectId: number; |
| 14 | + messageId: number; |
| 15 | + type: PrototypeType; |
| 16 | + prototype?: PrototypeVersion; |
| 17 | +} |
| 18 | + |
| 19 | +@observer |
| 20 | +export class PrototypeGenerator extends ObservedComponent<PrototypeGeneratorProps, typeof i18n> { |
| 21 | + static contextType = I18nContext; |
| 22 | + |
| 23 | + versionStore = new PrototypeVersionModel(this.props.projectId, this.props.type); |
| 24 | + |
| 25 | + @observable |
| 26 | + accessor version = this.props.prototype; |
| 27 | + |
| 28 | + private root = createRef<HTMLElement>(); |
| 29 | + |
| 30 | + componentDidMount() { |
| 31 | + super.componentDidMount(); |
| 32 | + |
| 33 | + this.pollStatusCheck(); |
| 34 | + } |
| 35 | + |
| 36 | + async pollStatusCheck() { |
| 37 | + const { props, version } = this, |
| 38 | + rootElement = this.root.current; |
| 39 | + |
| 40 | + while (version?.status === 'pending' || version?.status === 'processing') { |
| 41 | + if (!rootElement?.isConnected) break; |
| 42 | + |
| 43 | + if (inViewport(rootElement)) |
| 44 | + this.version = await this.versionStore.getOne(props.prototype!.id); |
| 45 | + |
| 46 | + await sleep(3); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + handleGenerateClick = async () => { |
| 51 | + this.version = await this.versionStore.updateOne({ |
| 52 | + evaluationMessage: this.props.messageId, |
| 53 | + }); |
| 54 | + |
| 55 | + return this.pollStatusCheck(); |
| 56 | + }; |
| 57 | + |
| 58 | + renderPending() { |
| 59 | + const { t } = this.observedContext; |
| 60 | + const loading = this.versionStore.uploading > 0; |
| 61 | + |
| 62 | + return ( |
| 63 | + <Button |
| 64 | + variant="contained" |
| 65 | + color="primary" |
| 66 | + size="small" |
| 67 | + disabled={loading} |
| 68 | + sx={{ textTransform: 'none' }} |
| 69 | + onClick={this.handleGenerateClick} |
| 70 | + > |
| 71 | + {loading ? t('generating') : t('generate_prototype')} |
| 72 | + </Button> |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + renderGenerating() { |
| 77 | + const { t } = this.observedContext; |
| 78 | + |
| 79 | + return ( |
| 80 | + <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}> |
| 81 | + <CircularProgress size={16} /> |
| 82 | + <Typography variant="body2">{t('prototype_generating')}</Typography> |
| 83 | + </Box> |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + renderCompleted() { |
| 88 | + const { t } = this.observedContext; |
| 89 | + const { previewLink, gitLogsLink } = this.version || {}; |
| 90 | + |
| 91 | + return ( |
| 92 | + <Box sx={{ display: 'flex', gap: 1, flexWrap: 'wrap' }}> |
| 93 | + {previewLink && ( |
| 94 | + <Link |
| 95 | + href={previewLink} |
| 96 | + target="_blank" |
| 97 | + rel="noopener noreferrer" |
| 98 | + sx={{ |
| 99 | + textDecoration: 'none', |
| 100 | + fontSize: '0.875rem', |
| 101 | + fontWeight: 500, |
| 102 | + color: 'primary.main', |
| 103 | + }} |
| 104 | + > |
| 105 | + {t('view_preview')} |
| 106 | + </Link> |
| 107 | + )} |
| 108 | + {gitLogsLink && ( |
| 109 | + <Link |
| 110 | + href={gitLogsLink} |
| 111 | + target="_blank" |
| 112 | + rel="noopener noreferrer" |
| 113 | + sx={{ |
| 114 | + textDecoration: 'none', |
| 115 | + fontSize: '0.875rem', |
| 116 | + fontWeight: 500, |
| 117 | + color: 'text.secondary', |
| 118 | + }} |
| 119 | + > |
| 120 | + {t('view_ai_log')} |
| 121 | + </Link> |
| 122 | + )} |
| 123 | + </Box> |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + renderFailed() { |
| 128 | + const { t } = this.observedContext; |
| 129 | + const { errorMessage, gitLogsLink } = this.version || {}; |
| 130 | + |
| 131 | + return ( |
| 132 | + <Box sx={{ display: 'flex', flexDirection: 'column', gap: 0.5 }}> |
| 133 | + <Typography variant="body2" color="error" sx={{ fontSize: '0.875rem' }}> |
| 134 | + {errorMessage || t('prototype_generation_failed')} |
| 135 | + </Typography> |
| 136 | + {gitLogsLink && ( |
| 137 | + <Link |
| 138 | + href={gitLogsLink} |
| 139 | + target="_blank" |
| 140 | + rel="noopener noreferrer" |
| 141 | + sx={{ |
| 142 | + textDecoration: 'none', |
| 143 | + fontSize: '0.875rem', |
| 144 | + fontWeight: 500, |
| 145 | + color: 'text.secondary', |
| 146 | + }} |
| 147 | + > |
| 148 | + {t('view_ai_log')} |
| 149 | + </Link> |
| 150 | + )} |
| 151 | + </Box> |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + render() { |
| 156 | + const { version } = this; |
| 157 | + |
| 158 | + return ( |
| 159 | + <Box ref={this.root} sx={{ borderTop: '1px solid', borderColor: 'divider' }}> |
| 160 | + {!version || version.status === 'pending' |
| 161 | + ? this.renderPending() |
| 162 | + : version.status === 'processing' |
| 163 | + ? this.renderGenerating() |
| 164 | + : version.status === 'completed' |
| 165 | + ? this.renderCompleted() |
| 166 | + : this.renderFailed()} |
| 167 | + </Box> |
| 168 | + ); |
| 169 | + } |
| 170 | +} |
0 commit comments