Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / lib / user / user.requests.ts
blobacb2d828b237370b1e0d39b6a9a3a242502ac500
1 import { DEFAULT_PASS_FEATURES } from '@proton/pass/constants';
2 import { api } from '@proton/pass/lib/api/api';
3 import type { FeatureFlagState, HydratedAccessState, HydratedUserState } from '@proton/pass/store/reducers';
4 import type { FeatureFlagsResponse } from '@proton/pass/types/api/features';
5 import { PassFeaturesValues } from '@proton/pass/types/api/features';
6 import { prop } from '@proton/pass/utils/fp/lens';
7 import { logger } from '@proton/pass/utils/logger';
8 import { getAllAddresses } from '@proton/shared/lib/api/addresses';
9 import { getLatestID } from '@proton/shared/lib/api/events';
10 import { getSettings } from '@proton/shared/lib/api/settings';
11 import { getUser } from '@proton/shared/lib/api/user';
12 import { toMap } from '@proton/shared/lib/helpers/object';
13 import type { Address, User, UserSettings } from '@proton/shared/lib/interfaces';
15 export const getFeatureFlags = async (): Promise<FeatureFlagState> => {
16     logger.info(`[User] syncing feature flags`);
17     const { toggles } = await api<FeatureFlagsResponse>({ url: `feature/v2/frontend`, method: 'get' });
19     return PassFeaturesValues.reduce<FeatureFlagState>((features, feat) => {
20         features[feat] = toggles.some((toggle) => toggle.name === feat);
21         return features;
22     }, {});
25 export const getUserAccess = async (): Promise<HydratedAccessState> => {
26     logger.info(`[User] Syncing access & plan`);
27     const { Access } = await api({ url: 'pass/v1/user/access', method: 'get' });
29     return {
30         plan: Access!.Plan,
31         waitingNewUserInvites: Access!.WaitingNewUserInvites,
32         monitor: Access!.Monitor ?? null,
33         userData: {
34             defaultShareId: Access!.UserData.DefaultShareID ?? null,
35             aliasSyncEnabled: Access!.UserData.AliasSyncEnabled,
36             pendingAliasToSync: Access!.UserData.PendingAliasToSync,
37         },
38     };
41 export const getUserSettings = async (): Promise<UserSettings> => {
42     logger.info(`[User] syncing settings`);
43     return (await api<{ UserSettings: UserSettings }>(getSettings())).UserSettings;
46 export const getUserModel = async (): Promise<User> => api<{ User: User }>(getUser()).then(prop('User'));
48 export const getUserLatestEventID = async (): Promise<string> =>
49     api<{ EventID: string }>(getLatestID()).then(prop('EventID'));
51 export type UserData = {
52     access: HydratedAccessState;
53     addresses: Record<string, Address>;
54     eventId: string;
55     features: FeatureFlagState;
56     user: User;
57     userSettings: UserSettings;
60 /** Resolves all necessary user data to build up the user state */
61 export const getUserData = async (): Promise<HydratedUserState> => {
62     const [user, eventId, userSettings, addresses, access, features] = await Promise.all([
63         getUserModel(),
64         getUserLatestEventID(),
65         getUserSettings(),
66         getAllAddresses(api).then((addresses) => toMap(addresses, 'ID')),
67         getUserAccess(),
68         getFeatureFlags().catch(() => DEFAULT_PASS_FEATURES),
69     ]);
71     return {
72         ...access,
73         addresses,
74         devices: [],
75         eventId,
76         features,
77         user,
78         userSettings: {
79             Email: { Status: userSettings.Email.Status },
80             HighSecurity: userSettings.HighSecurity,
81             Locale: userSettings.Locale,
82             News: userSettings.News,
83             Password: { Mode: userSettings.Password.Mode },
84             Telemetry: userSettings.Telemetry,
85         },
86     };