|
| 1 | +import cx from 'classnames' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import React, { useCallback, useState, useEffect } from 'react' |
| 4 | + |
| 5 | +import Button from 'cozy-ui/transpiled/react/Buttons' |
| 6 | +import Icon from 'cozy-ui/transpiled/react/Icon' |
| 7 | +import IconButton from 'cozy-ui/transpiled/react/IconButton' |
| 8 | +import AssistantIcon from 'cozy-ui/transpiled/react/Icons/Assistant' |
| 9 | +import CopyIcon from 'cozy-ui/transpiled/react/Icons/Copy' |
| 10 | +import CrossMediumIcon from 'cozy-ui/transpiled/react/Icons/CrossMedium' |
| 11 | +import RefreshIcon from 'cozy-ui/transpiled/react/Icons/Refresh' |
| 12 | +import Paper from 'cozy-ui/transpiled/react/Paper' |
| 13 | +import Stack from 'cozy-ui/transpiled/react/Stack' |
| 14 | +import Typography from 'cozy-ui/transpiled/react/Typography' |
| 15 | +import Alerter from 'cozy-ui/transpiled/react/deprecated/Alerter' |
| 16 | +import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n' |
| 17 | + |
| 18 | +import { summarizeFile } from '../../helpers/aiApi' |
| 19 | +import { useViewer } from '../../providers/ViewerProvider' |
| 20 | + |
| 21 | +const loaderStyles = { |
| 22 | + container: { |
| 23 | + position: 'relative', |
| 24 | + overflow: 'hidden', |
| 25 | + height: '3px', |
| 26 | + width: '100%', |
| 27 | + backgroundColor: 'var(--dividerColor)', |
| 28 | + marginTop: '0px' |
| 29 | + }, |
| 30 | + bar: { |
| 31 | + position: 'absolute', |
| 32 | + top: 0, |
| 33 | + left: 0, |
| 34 | + height: '100%', |
| 35 | + width: '100%', |
| 36 | + background: |
| 37 | + 'linear-gradient(90deg, #00B8D4 0%, #667EEA 25%, #F093FB 50%, #F5576C 75%, #FDB813 100%)', |
| 38 | + animation: 'aiLoaderSlide 2s linear infinite' |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +const AIAssistantPanel = () => { |
| 43 | + const { t } = useI18n() |
| 44 | + const { file, setIsOpenAiAssistant } = useViewer() |
| 45 | + |
| 46 | + const [isLoading, setIsLoading] = useState(true) |
| 47 | + const [summary, setSummary] = useState('') |
| 48 | + const [error, setError] = useState(null) |
| 49 | + |
| 50 | + const handleClose = () => { |
| 51 | + setIsOpenAiAssistant(false) |
| 52 | + } |
| 53 | + |
| 54 | + const fetchSummary = useCallback(async () => { |
| 55 | + setIsLoading(true) |
| 56 | + setError(null) |
| 57 | + try { |
| 58 | + const response = await summarizeFile({ file, stream: false }) |
| 59 | + if (response && response.content) { |
| 60 | + setSummary(response.content) |
| 61 | + } else if (response && response.choices && response.choices[0]) { |
| 62 | + setSummary(response.choices[0].message.content) |
| 63 | + } |
| 64 | + } catch (err) { |
| 65 | + setError(err.message || 'Failed to generate summary') |
| 66 | + Alerter.error(t('Viewer.ai.error.summary')) |
| 67 | + } finally { |
| 68 | + setIsLoading(false) |
| 69 | + } |
| 70 | + }, [file, t]) |
| 71 | + |
| 72 | + const handleRefresh = () => { |
| 73 | + fetchSummary() |
| 74 | + } |
| 75 | + |
| 76 | + const handleCopy = () => { |
| 77 | + if (summary) { |
| 78 | + navigator.clipboard.writeText(summary) |
| 79 | + Alerter.success(t('Viewer.ai.copied')) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + fetchSummary() |
| 85 | + }, [fetchSummary]) |
| 86 | + |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <style> |
| 90 | + {` |
| 91 | + @keyframes aiLoaderSlide { |
| 92 | + 0% { |
| 93 | + transform: translateX(-100%); |
| 94 | + } |
| 95 | + 100% { |
| 96 | + transform: translateX(100%); |
| 97 | + } |
| 98 | + } |
| 99 | + `} |
| 100 | + </style> |
| 101 | + <Stack spacing="s" className={cx('u-flex u-flex-column u-h-100')}> |
| 102 | + <Paper |
| 103 | + className={cx({ |
| 104 | + 'u-flex-grow-1': !isLoading |
| 105 | + })} |
| 106 | + elevation={2} |
| 107 | + square |
| 108 | + > |
| 109 | + <div className="u-flex u-flex-items-center u-flex-justify-between u-h-3 u-ph-1 u-flex-shrink-0"> |
| 110 | + <Typography variant="h4"> |
| 111 | + <Icon icon={AssistantIcon} /> Ai assistant |
| 112 | + </Typography> |
| 113 | + <IconButton aria-label="Close AI Assistant" onClick={handleClose}> |
| 114 | + <Icon icon={CrossMediumIcon} /> |
| 115 | + </IconButton> |
| 116 | + </div> |
| 117 | + {!isLoading && ( |
| 118 | + <Stack spacing="s" className="u-ph-1"> |
| 119 | + <div> |
| 120 | + <div className="u-flex u-flex-items-center u-flex-justify-between u-mb-1"> |
| 121 | + <Typography variant="subtitle1">Summary</Typography> |
| 122 | + <div className="u-flex"> |
| 123 | + <IconButton size="small" onClick={handleRefresh}> |
| 124 | + <Icon icon={RefreshIcon} /> |
| 125 | + </IconButton> |
| 126 | + <IconButton size="small" onClick={handleCopy}> |
| 127 | + <Icon icon={CopyIcon} /> |
| 128 | + </IconButton> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + <Typography className="u-mb-1"> |
| 132 | + {error ? ( |
| 133 | + <span style={{ color: 'var(--errorColor)' }}>{error}</span> |
| 134 | + ) : ( |
| 135 | + summary |
| 136 | + )} |
| 137 | + </Typography> |
| 138 | + <Typography variant="caption" color="textSecondary"> |
| 139 | + This content is generated by AI and may contain errors. |
| 140 | + </Typography> |
| 141 | + </div> |
| 142 | + </Stack> |
| 143 | + )} |
| 144 | + </Paper> |
| 145 | + {isLoading ? ( |
| 146 | + <> |
| 147 | + <div style={loaderStyles.container}> |
| 148 | + <div style={loaderStyles.bar} /> |
| 149 | + </div> |
| 150 | + <div className="u-flex u-flex-items-center u-flex-justify-between u-ph-1"> |
| 151 | + <Typography |
| 152 | + variant="body1" |
| 153 | + className="u-flex u-flex-items-center" |
| 154 | + > |
| 155 | + <Icon |
| 156 | + icon={AssistantIcon} |
| 157 | + color="var(--primaryColor)" |
| 158 | + className="u-mr-1" |
| 159 | + /> |
| 160 | + Summarizing content |
| 161 | + </Typography> |
| 162 | + <Button |
| 163 | + size="small" |
| 164 | + variant="text" |
| 165 | + color="default" |
| 166 | + label="Stop" |
| 167 | + onClick={handleClose} |
| 168 | + /> |
| 169 | + </div> |
| 170 | + </> |
| 171 | + ) : null} |
| 172 | + </Stack> |
| 173 | + </> |
| 174 | + ) |
| 175 | +} |
| 176 | + |
| 177 | +AIAssistantPanel.propTypes = { |
| 178 | + isLoading: PropTypes.bool, |
| 179 | + summary: PropTypes.string, |
| 180 | + onStop: PropTypes.func, |
| 181 | + onSend: PropTypes.func |
| 182 | +} |
| 183 | + |
| 184 | +AIAssistantPanel.defaultProps = { |
| 185 | + isLoading: false, |
| 186 | + summary: '' |
| 187 | +} |
| 188 | + |
| 189 | +export default AIAssistantPanel |
0 commit comments