|
| 1 | +import { nls } from '@theia/core/lib/common/nls'; |
| 2 | +import React from '@theia/core/shared/react'; |
| 3 | +// @ts-expect-error see https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183 |
| 4 | +import type { Options } from 'react-markdown'; |
| 5 | +import { ProgressInfo, UpdateInfo } from '../../../common/protocol/ide-updater'; |
| 6 | +import ProgressBar from '../../components/ProgressBar'; |
| 7 | + |
| 8 | +const ReactMarkdown = React.lazy<React.ComponentType<Options>>( |
| 9 | + // @ts-expect-error see above |
| 10 | + () => import('react-markdown') |
| 11 | +); |
| 12 | + |
| 13 | +export interface UpdateProgress { |
| 14 | + progressInfo?: ProgressInfo | undefined; |
| 15 | + downloadFinished?: boolean; |
| 16 | + downloadStarted?: boolean; |
| 17 | + error?: Error; |
| 18 | +} |
| 19 | + |
| 20 | +export interface IDEUpdaterComponentProps { |
| 21 | + updateInfo: UpdateInfo; |
| 22 | + updateProgress: UpdateProgress; |
| 23 | + openExternal: (url: string) => undefined; |
| 24 | +} |
| 25 | + |
| 26 | +export const IDEUpdaterComponent = ({ |
| 27 | + updateInfo, |
| 28 | + updateProgress: { |
| 29 | + downloadStarted = false, |
| 30 | + downloadFinished = false, |
| 31 | + progressInfo, |
| 32 | + error, |
| 33 | + }, |
| 34 | + openExternal, |
| 35 | +}: IDEUpdaterComponentProps): React.ReactElement => { |
| 36 | + const { version, releaseNotes } = updateInfo; |
| 37 | + const [changelog, setChangelog] = React.useState<string>(''); |
| 38 | + React.useEffect(() => { |
| 39 | + if (releaseNotes) { |
| 40 | + setChangelog( |
| 41 | + typeof releaseNotes === 'string' |
| 42 | + ? releaseNotes |
| 43 | + : releaseNotes.reduce( |
| 44 | + (acc, item) => (item.note ? (acc += `${item.note}\n\n`) : acc), |
| 45 | + '' |
| 46 | + ) |
| 47 | + ); |
| 48 | + } |
| 49 | + }, [releaseNotes, changelog]); |
| 50 | + |
| 51 | + const DownloadCompleted: () => React.ReactElement = () => ( |
| 52 | + <div className="ide-updater-dialog--downloaded"> |
| 53 | + <div> |
| 54 | + {nls.localize( |
| 55 | + 'arduino/ide-updater/versionDownloaded', |
| 56 | + 'Arduino IDE {0} has been downloaded.', |
| 57 | + version |
| 58 | + )} |
| 59 | + </div> |
| 60 | + <div> |
| 61 | + {nls.localize( |
| 62 | + 'arduino/ide-updater/closeToInstallNotice', |
| 63 | + 'Close the software and install the update on your machine.' |
| 64 | + )} |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + ); |
| 68 | + |
| 69 | + const DownloadStarted: () => React.ReactElement = () => ( |
| 70 | + <div className="ide-updater-dialog--downloading"> |
| 71 | + <div> |
| 72 | + {nls.localize( |
| 73 | + 'arduino/ide-updater/downloadingNotice', |
| 74 | + 'Downloading the latest version of the Arduino IDE.' |
| 75 | + )} |
| 76 | + </div> |
| 77 | + <ProgressBar percent={progressInfo?.percent} showPercentage /> |
| 78 | + </div> |
| 79 | + ); |
| 80 | + |
| 81 | + const PreDownload: () => React.ReactElement = () => ( |
| 82 | + <div className="ide-updater-dialog--pre-download"> |
| 83 | + <div className="ide-updater-dialog--logo-container"> |
| 84 | + <div className="ide-updater-dialog--logo"></div> |
| 85 | + </div> |
| 86 | + <div className="ide-updater-dialog--new-version-text dialogSection"> |
| 87 | + <div className="dialogRow"> |
| 88 | + <div className="bold"> |
| 89 | + {nls.localize( |
| 90 | + 'arduino/ide-updater/updateAvailable', |
| 91 | + 'Update Available' |
| 92 | + )} |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + <div className="dialogRow"> |
| 96 | + {nls.localize( |
| 97 | + 'arduino/ide-updater/newVersionAvailable', |
| 98 | + 'A new version of Arduino IDE ({0}) is available for download.', |
| 99 | + version |
| 100 | + )} |
| 101 | + </div> |
| 102 | + {changelog && ( |
| 103 | + <div className="dialogRow changelog-container"> |
| 104 | + <div className="changelog"> |
| 105 | + <React.Suspense |
| 106 | + fallback={ |
| 107 | + <div className="fallback"> |
| 108 | + <div className="spinner" /> |
| 109 | + </div> |
| 110 | + } |
| 111 | + > |
| 112 | + <ReactMarkdown |
| 113 | + components={{ |
| 114 | + a: ({ href, children, ...props }) => ( |
| 115 | + <a onClick={() => href && openExternal(href)} {...props}> |
| 116 | + {children} |
| 117 | + </a> |
| 118 | + ), |
| 119 | + }} |
| 120 | + > |
| 121 | + {changelog} |
| 122 | + </ReactMarkdown> |
| 123 | + </React.Suspense> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + )} |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + ); |
| 130 | + |
| 131 | + const GoToDownloadPage: () => React.ReactElement = () => ( |
| 132 | + <div className="ide-updater-dialog--go-to-download-page"> |
| 133 | + <div> |
| 134 | + {nls.localize( |
| 135 | + 'arduino/ide-updater/goToDownloadPage', |
| 136 | + "An update for the Arduino IDE is available, but we're not able to download and install it automatically. Please go to the download page and download the latest version from there." |
| 137 | + )} |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + ); |
| 141 | + |
| 142 | + return ( |
| 143 | + <div className="ide-updater-dialog--content"> |
| 144 | + {!!error ? ( |
| 145 | + <GoToDownloadPage /> |
| 146 | + ) : downloadFinished ? ( |
| 147 | + <DownloadCompleted /> |
| 148 | + ) : downloadStarted ? ( |
| 149 | + <DownloadStarted /> |
| 150 | + ) : ( |
| 151 | + <PreDownload /> |
| 152 | + )} |
| 153 | + </div> |
| 154 | + ); |
| 155 | +}; |
0 commit comments