1 import type { ReactNode } from 'react';
2 import { createContext, useCallback, useContext, useMemo, useState } from 'react';
4 import { useApi } from '@proton/components';
5 import { queryUpdateUserSettings } from '@proton/shared/lib/api/drive/user';
6 import { DEFAULT_USER_SETTINGS } from '@proton/shared/lib/drive/constants';
7 import type { UserModel } from '@proton/shared/lib/interfaces';
10 RevisionRetentionDaysSetting,
13 } from '@proton/shared/lib/interfaces/drive/userSettings';
14 import useFlag from '@proton/unleash/useFlag';
16 import type { UserSortParams } from './sorting';
17 import { getSetting, parseSetting } from './sorting';
19 const UserSettingsContext = createContext<{
21 layout: LayoutSetting;
22 revisionRetentionDays: RevisionRetentionDaysSetting;
23 b2bPhotosEnabled: boolean;
24 changeSort: (sortParams: UserSortParams) => Promise<void>;
25 changeLayout: (Layout: LayoutSetting) => Promise<void>;
26 changeB2BPhotosEnabled: (B2BPhotosEnabled: boolean) => Promise<void>;
29 export function UserSettingsProvider({
31 initialDriveUserSettings,
35 initialUser: UserModel;
36 initialDriveUserSettings: UserSettingsResponse;
39 const driveB2BPhotosUpload = useFlag('DriveB2BPhotosUpload');
41 const [userSettings, setUserSettings] = useState<UserSettings>(() => {
42 const { UserSettings, Defaults } = initialDriveUserSettings;
43 const { hasPaidDrive } = initialUser;
44 return Object.entries(UserSettings).reduce((settings, [key, value]) => {
45 // In case of user downgrade from paid to free, we want to set the default free user value
46 if (key === 'RevisionRetentionDays' && !hasPaidDrive) {
49 RevisionRetentionDays: Defaults.RevisionRetentionDays,
56 (Defaults[key as keyof UserSettingsResponse['Defaults']] ||
57 DEFAULT_USER_SETTINGS[key as keyof UserSettingsResponse['UserSettings']]),
59 }, {} as UserSettings);
62 const sort = useMemo(() => parseSetting(userSettings.Sort), [userSettings.Sort]);
64 const changeSort = useCallback(async (sortParams: UserSortParams) => {
65 const sortSetting = getSetting(sortParams);
69 setUserSettings((settings) => ({ ...settings, Sort: sortSetting }));
71 queryUpdateUserSettings({
77 const changeLayout = useCallback(async (Layout: LayoutSetting) => {
78 setUserSettings((settings) => ({ ...settings, Layout }));
80 queryUpdateUserSettings({
86 const changeB2BPhotosEnabled = useCallback(async (B2BPhotosEnabled: boolean) => {
87 setUserSettings((settings) => ({ ...settings, B2BPhotosEnabled }));
89 queryUpdateUserSettings({
97 layout: userSettings.Layout,
98 revisionRetentionDays: userSettings.RevisionRetentionDays,
99 b2bPhotosEnabled: !driveB2BPhotosUpload || (driveB2BPhotosUpload && userSettings.B2BPhotosEnabled),
102 changeB2BPhotosEnabled,
105 return <UserSettingsContext.Provider value={value}>{children}</UserSettingsContext.Provider>;
108 export default function useUserSettings() {
109 const state = useContext(UserSettingsContext);
111 throw new Error('Trying to use uninitialized UserSettingsProvider');