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);
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' });
31 waitingNewUserInvites: Access!.WaitingNewUserInvites,
32 monitor: Access!.Monitor ?? null,
34 defaultShareId: Access!.UserData.DefaultShareID ?? null,
35 aliasSyncEnabled: Access!.UserData.AliasSyncEnabled,
36 pendingAliasToSync: Access!.UserData.PendingAliasToSync,
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>;
55 features: FeatureFlagState;
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([
64 getUserLatestEventID(),
66 getAllAddresses(api).then((addresses) => toMap(addresses, 'ID')),
68 getFeatureFlags().catch(() => DEFAULT_PASS_FEATURES),
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,