Remove payments API routing initialization
[ProtonMail-WebClient.git] / packages / components / containers / desktop / useInboxDesktopVersion.ts
blob2483d8f7e7bf728ae81554b0eb33d7e6b15375b0
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,
14     Version: '',
15     ReleaseDate: '',
16     File: [
17         {
18             Identifier: '.deb (Ubuntu/Debian)',
19             Url: getDownloadUrl('/mail/linux/ProtonMail-desktop-beta.deb'),
20             Sha512CheckSum: '',
21         },
22         {
23             Identifier: '.rpm (Fedora/RHEL)',
24             Url: getDownloadUrl('/mail/linux/ProtonMail-desktop-beta.rpm'),
25             Sha512CheckSum: '',
26         },
27     ],
28     ReleaseNotes: [],
29     RolloutProportion: 1,
30     ManualUpdate: [],
33 const initialWindowsClient: DesktopVersion = {
34     CategoryName: RELEASE_CATEGORIES.STABLE,
35     Version: '',
36     ReleaseDate: '',
37     File: [
38         {
39             Url: getDownloadUrl('/mail/windows/ProtonMail-desktop.exe'),
40             Sha512CheckSum: '',
41         },
42     ],
43     ReleaseNotes: [],
44     RolloutProportion: 1,
45     ManualUpdate: [],
48 const initialMacosClient: DesktopVersion = {
49     CategoryName: RELEASE_CATEGORIES.STABLE,
50     Version: '',
51     ReleaseDate: '',
52     File: [
53         {
54             Url: getDownloadUrl('/mail/macos/ProtonMail-desktop.dmg'),
55             Sha512CheckSum: '',
56         },
57     ],
58     ReleaseNotes: [],
59     RolloutProportion: 1,
60     ManualUpdate: [],
63 const fetchDesktopClient = async (platform: DESKTOP_PLATFORMS) => {
64     try {
65         const response = await fetch(getDownloadUrl(`/mail/${platform}/version.json`));
66         if (!response.ok) {
67             throw new Error(response.statusText);
68         }
70         const json = await response.json();
71         const res = VersionFileSchema.parse(json);
72         return res.Releases;
73     } catch (e: any) {
74         return undefined;
75     }
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);
88     useEffect(() => {
89         const fetchDesktopVersion = async () => {
90             const promises = [fetchDesktopClient(WINDOWS), fetchDesktopClient(MACOS), fetchDesktopClient(LINUX)];
91             const [windowsClient, macosClient, linuxClient] = await Promise.all(promises);
93             if (windowsClient) {
94                 setWindowsApp(
95                     (previousWindowsApp) => getLatestRelease(currentEnvironment, windowsClient) || previousWindowsApp
96                 );
97             }
99             if (macosClient) {
100                 setMacosApp(
101                     (previousMacosApp) => getLatestRelease(currentEnvironment, macosClient) || previousMacosApp
102                 );
103             }
105             if (linuxClient) {
106                 setLinuxApp(
107                     (previousLinuxApp) => getLatestRelease(currentEnvironment, linuxClient) || previousLinuxApp
108                 );
109             }
110         };
112         const fetchDesktopVersionFromElectron = async () => {
113             const latestDesktopVersion = getInboxDesktopInfo('latestVersion');
115             if (!latestDesktopVersion) {
116                 return;
117             }
119             if (isElectronOnWindows) {
120                 setWindowsApp(latestDesktopVersion);
121             } else if (isElectronOnMac) {
122                 setMacosApp(latestDesktopVersion);
123             } else if (isElectronOnLinux) {
124                 setLinuxApp(latestDesktopVersion);
125             }
126         };
128         if (hasInboxDesktopFeature('LatestVersionCheck')) {
129             void withLoading(fetchDesktopVersionFromElectron());
130         } else {
131             void withLoading(fetchDesktopVersion());
132         }
133     }, [currentEnvironment]);
135     return { windowsApp, macosApp, linuxApp, loading };
138 export default useInboxDesktopVersion;