Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / utils / appPlatforms.ts
blobe3f82764afc6b7727be629a305112c65901fcdad
1 import { type IconName } from '@proton/components/components/icon/Icon';
2 import { fetchDesktopVersion } from '@proton/shared/lib/apps/desktopVersions';
3 import { DESKTOP_APP_NAMES, DESKTOP_PLATFORMS, RELEASE_CATEGORIES } from '@proton/shared/lib/constants';
4 import { isMac, isWindows } from '@proton/shared/lib/helpers/browser';
6 export type PlatformInfo = {
7     platform: DESKTOP_PLATFORMS;
8     icon: IconName;
9     name: string;
10     isPreferred: () => boolean;
11     hideIfUnavailable?: boolean;
13     // These are useful in case we ever want to lock downloads to
14     // a specific version or category in the future
15     version?: string;
16     releaseCategory?: RELEASE_CATEGORIES;
19 /**
20  * A sorted list of app platforms, in order of preference.
21  *
22  * For example, when browsing on macOS, the corresponding platform will be first in the list.
23  * If there is no preferred platform, the default order is used.
24  */
25 export const appPlatforms = (
26     [
27         {
28             platform: DESKTOP_PLATFORMS.WINDOWS,
29             icon: 'brand-windows',
30             name: 'Windows',
31             isPreferred: isWindows,
32         },
33         {
34             platform: DESKTOP_PLATFORMS.MACOS,
35             icon: 'brand-apple',
36             name: 'macOS',
37             isPreferred: isMac,
38             hideIfUnavailable: true,
39         },
40     ] satisfies PlatformInfo[] as PlatformInfo[]
41 ).sort((a, b) => Number(b.isPreferred()) - Number(a.isPreferred()));
43 /**
44  * Fetches download URLs for supported platforms and returns them in a map.
45  */
46 export const fetchDesktopDownloads = async () => {
47     let versions: Partial<Record<DESKTOP_PLATFORMS, string>> = {};
49     await Promise.all(
50         appPlatforms.map(({ platform, version, releaseCategory }) =>
51             fetchDesktopVersion({
52                 appName: DESKTOP_APP_NAMES.DRIVE,
53                 platform,
54                 version: version || 'latest',
55                 category: releaseCategory || RELEASE_CATEGORIES.STABLE,
56             })
57                 .then((meta) => {
58                     if (!meta) {
59                         throw new Error('Undefined response from API');
60                     }
62                     versions = { ...versions, [platform]: meta.url };
63                 })
64                 .catch((reason) => console.warn(`Download link for ${platform} cannot be fetched`, reason))
65         )
66     );
68     return versions;