1 import { useEffect, useState } from 'react';
3 import useEarlyAccess from '@proton/components/hooks/useEarlyAccess';
4 import useLoading from '@proton/hooks/useLoading';
5 import { DESKTOP_PLATFORMS, RELEASE_CATEGORIES } from '@proton/shared/lib/constants';
6 import { type DesktopVersion, VersionFileSchema } from '@proton/shared/lib/desktop/DesktopVersion';
7 import { getLatestRelease } from '@proton/shared/lib/desktop/getLatestRelease';
8 import { getInboxDesktopInfo, hasInboxDesktopFeature } from '@proton/shared/lib/desktop/ipcHelpers';
9 import { isElectronOnLinux, isElectronOnMac, isElectronOnWindows } from '@proton/shared/lib/helpers/desktop';
10 import { getDownloadUrl } from '@proton/shared/lib/helpers/url';
12 const initialLinuxClients: DesktopVersion = {
13 CategoryName: RELEASE_CATEGORIES.EARLY_ACCESS,
18 Identifier: '.deb (Ubuntu/Debian)',
19 Url: getDownloadUrl('/mail/linux/ProtonMail-desktop-beta.deb'),
23 Identifier: '.rpm (Fedora/RHEL)',
24 Url: getDownloadUrl('/mail/linux/ProtonMail-desktop-beta.rpm'),
33 const initialWindowsClient: DesktopVersion = {
34 CategoryName: RELEASE_CATEGORIES.STABLE,
39 Url: getDownloadUrl('/mail/windows/ProtonMail-desktop.exe'),
48 const initialMacosClient: DesktopVersion = {
49 CategoryName: RELEASE_CATEGORIES.STABLE,
54 Url: getDownloadUrl('/mail/macos/ProtonMail-desktop.dmg'),
63 const fetchDesktopClient = async (platform: DESKTOP_PLATFORMS) => {
65 const response = await fetch(getDownloadUrl(`/mail/${platform}/version.json`));
67 throw new Error(response.statusText);
70 const json = await response.json();
71 const res = VersionFileSchema.parse(json);
78 const { WINDOWS, MACOS, LINUX } = DESKTOP_PLATFORMS;
80 const useInboxDesktopVersion = () => {
81 const { currentEnvironment } = useEarlyAccess();
82 const [loading, withLoading] = useLoading(true);
84 const [windowsApp, setWindowsApp] = useState<DesktopVersion>(initialWindowsClient);
85 const [macosApp, setMacosApp] = useState<DesktopVersion>(initialMacosClient);
86 const [linuxApp, setLinuxApp] = useState<DesktopVersion>(initialLinuxClients);
89 const fetchDesktopVersion = async () => {
90 const promises = [fetchDesktopClient(WINDOWS), fetchDesktopClient(MACOS), fetchDesktopClient(LINUX)];
91 const [windowsClient, macosClient, linuxClient] = await Promise.all(promises);
95 (previousWindowsApp) => getLatestRelease(currentEnvironment, windowsClient) || previousWindowsApp
101 (previousMacosApp) => getLatestRelease(currentEnvironment, macosClient) || previousMacosApp
107 (previousLinuxApp) => getLatestRelease(currentEnvironment, linuxClient) || previousLinuxApp
112 const fetchDesktopVersionFromElectron = async () => {
113 const latestDesktopVersion = getInboxDesktopInfo('latestVersion');
115 if (!latestDesktopVersion) {
119 if (isElectronOnWindows) {
120 setWindowsApp(latestDesktopVersion);
121 } else if (isElectronOnMac) {
122 setMacosApp(latestDesktopVersion);
123 } else if (isElectronOnLinux) {
124 setLinuxApp(latestDesktopVersion);
128 if (hasInboxDesktopFeature('LatestVersionCheck')) {
129 void withLoading(fetchDesktopVersionFromElectron());
131 void withLoading(fetchDesktopVersion());
133 }, [currentEnvironment]);
135 return { windowsApp, macosApp, linuxApp, loading };
138 export default useInboxDesktopVersion;